LaTeXで連番画像の挿入をマクロ化する
レポートで大量の連番画像を挿入する必要があった。
しかし\includegraphics{1.png}
みたいなのを何個も書くのは嫌だったのでマクロ化した。
思ったより工夫が必要だったのでせっかくなので記事にした。
やり方
以下のようにファイルを配置する。
┣ figure/
┃ ┣ 1.png
┃ ┣ 2.png
┃ ┣ 3.png
┃ ┣ ・
┃ ┣ ・
┃ ┣ ・
┃ ┗ 10.png
┃
┗ main.tex
以下のコードをコピペする。
\documentclass[uplatex,dvipdfmx]{jsarticle}
\usepackage{graphicx}
\newcommand{\filepath}[1]{#1}
\newcount\cnt
\def\includeFigLoop{
\cnt=1 \loop\ifnum\cnt<11
\section{連番\number\cnt }
\begin{figure}[p]
\centering
\includegraphics[width=150mm]{figure/\filepath{\number\cnt}.png}
\caption{連番\number\cnt の画像}
\end{figure}
\advance\cnt by1\repeat
}
\begin{document}
\includeFigLoop
\end{document}
解説
Qiitaの記事1を参考にしてループを作成した。
このループ内で\number\cnt
を参照するとそのときのループのカウントが返る。
しかし\number\cnt
を
\includegraphics{figure/\number\cnt.png}
のように\includegraphics
内で参照するとエラーとなってしまう。
ここで\newcommand
で
\newcommand{\filepath}[1]{#1}
のように変数の値を返すコマンドを定義する。
このコマンドを以下のように\includegraphics
で使うことで連番画像の挿入をマクロ化することができる。
\includegraphics{figure/\filepath{\number\cnt}.png}
この手法は変数をそのまま挿入できない他の場面でも役立つかもしれない