includegraphics с ведущим нулем в цикле for

includegraphics с ведущим нулем в цикле for

Я хотел бы включить графику, имя файла которой начинается с нулей. Имена файлов:

pic_001_test.png
pic_002_test.png
.
.
.
pic_028_test.png

Я могу вывести начальные нули в a \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}

при условии, что iзначение счетчика LaTeX не превышает 999.

Связанный контент