for ループの先頭にゼロを含む includegraphics

for ループの先頭にゼロを含む includegraphics

先頭にゼロが付いたファイル名を持つグラフィックを含めたいと思います。ファイル名は次のとおりです。

画像_001_test.png
画像_002_test.png
画像を拡大

先頭のゼロを出力することはできます\forloopが、コマンドでは使用できません\includegraphics

\documentclass{article}
\usepackage{forloop}
\usepackage{graphicx}
\usepackage{fmtcount}

\begin{document}

\newcommand{\filename}{}

\newcounter{i}
\forloop{i}{1}{\value{i} < 4}{
    \begin{figure}
        \renewcommand{\filename}{pic\_\padzeroes[3]{\decimal{i}}\_test.pdf}
        \includegraphics[scale=0.6]{pic_00\arabic{i}_test.png}
        \caption{Screenshot\_\arabic{i}}
        \filename
    \end{figure}
}

\end{document}

次のコードは、「! undefined control sequence」のため機能しません。

\newcommand{\filename}{}
\newcounter{i}
\forloop{i}{1}{\value{i} < 4}{
    \begin{figure}
        \renewcommand{\filename}{pic\_\padzeroes[3]{\decimal{i}}\_test.pdf}
        \includegraphics[scale=0.6]{\filename}
        \caption{Screenshot\_\arabic{i}}
    \end{figure}
}

答え1

LaTeXは(拡張可能な)\two@digitsこれはあなたの用途には十分と思われます(ただし、必要に応じて拡張することもできます)。

\documentclass{article}
\usepackage{forloop}
\usepackage[draft]{graphicx}

\newcounter{filename}
\makeatletter
\newcommand{\twodigits}{\two@digits}
\makeatother
\begin{document}

\forloop{filename}{1}{\value{filename} < 4}{
  \begin{figure}
    \includegraphics[scale=0.6]{pic_0\twodigits{\value{filename}}_test.png}
    \caption{Screenshot\_\thefilename: pic\_0\twodigits{\value{filename}}\_test.png}
  \end{figure}
}

\end{document}

ファイル名にアンダースコアを使用しているため、印刷や使用上の問題を回避するために、いくつかの複製が行われているようです。

答え2

ファイル名は拡張可能である必要があります:

\renewcommand*{\filename}{%
  pic_%
  \ifnum\value{i}<100 0\fi
  \ifnum\value{i}<10 0\fi
  \number\value{i}%
  _.pdf%
}%
\includegraphics[scale=0.6]{\filename}

拡張子を非表示にするファイル名を持つマクロは、 のファイル名引数の先頭から開始する必要があることに注意してください\includegraphics

答え3

これも動作するはずです:

% in preamble:
\makeatletter
% notice this will use the value of i at the time of macro use
% not at the time of definition here
\newcommand*{\filename}{%
  pic_%
  \expandafter\@gobble\the\numexpr 1000+\value{i}%
  _.pdf%
}%
\makeatother
% in body:
\includegraphics[scale=0.6]{\filename}

iLaTeX カウンターの値が 999 を超えないことを前提としています。

関連情報