texでforループ

latexで書いているときにfigureを差し込む際にforで回して入れたくなる時用のメモです。

例1

数字しか回せなさそうなので、うまいこと名前をつけておきます。 ここではディレクトリをうまく分けているとします。

\newcount\K % int K
\def\figfor#1{
    \K=0 \loop\ifnum\K<4 % for(K=0;K<4;K++)
    \begin{figure}[H]
        \begin{center}
            \includegraphics[scale=0.5]{fig\number\K/sample.pdf}
        \end{center}
    \end{figure}
    \advance\K by1\repeat % end for
}
%実行部分
\figfor{4}

これで4回繰り返されます。

例2

minipageとかでやる時も同じように。sample1.pdf, sample2.pdfとか名前がついてるとして、

\newcount\K % int K
\def\minipagefor#1{
    \K=1 \loop\ifnum\K<=9 % for(K=0;K<=9;K++)
    \begin{minipage}{0.33\hsize}
        \begin{center}
            \includegraphics[scale=0.35]{fig/sample\number\K.pdf}
        \end{center}
    \end{minipage}
    \advance\K by1\repeat % end for
}
%実行部分
\minipagefor{9}