2

I would like to include graphics which have a filename with leading zeros. The filenames are:

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

I'm able to output leading zeros in a \forloop but I can't use them in the \includegraphics command:

\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}

The following code does not work due to "! 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
  • \padzeroes isn't expandable
    – user31729
    Commented Mar 24, 2016 at 18:47

3 Answers 3

2

LaTeX knows the (expandable) \two@digits which seems sufficient for your usage (although it can also be extended, if needed):

\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}

Since you're using underscores in the filename(s), some replication seemed in order to avoid printing and usage problems.

3

The file name must be expandable:

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

Note, a macro with the file name, which hides the extension, must start from the begin of the file name argument of \includegraphics.

2

This should work too:

% 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}

assuming that i LaTeX counter does not exceed 999 as value.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .