5

I'm trying to include a lot of images to document and decided to use a counter to generate the filenames. A minimal (not working) example would look something like this:

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

\newcounter{imgnr}

\begin{document}    
    % image names are img000.png, img001.png, ..., img999.png
    \setcounter{imgnr}{1}
    \includegraphics[width=0.5\linewidth]{./images/img\padzeroes[3]\decimal{imgnr}.png}
    \setcounter{imgnr}{2}
    \includegraphics[width=0.5\linewidth]{./images/img\padzeroes[3]\decimal{imgnr}.png}
    \setcounter{imgnr}{10}
    \includegraphics[width=0.5\linewidth]{./images/img\padzeroes[3]\decimal{imgnr}.png}
\end{document}

This example gives the error TeX capacity exceeded, sorry [input stack size=5000]. Any idea on how to fix this? Or is this the wrong way to go?

In the real example, the counter is incremented and then a template with title, some text and tables and the image is included for each image. So thats the reason why im using a counter. I can nicely select the data for a given image.

Bonus: Display a box with Image 'imgxxx.png' is missing if a image is not found.

jrast
  • 586
  • Just found out that \decimal might not be expandable: https://groups.google.com/d/msg/comp.text.tex/86la9mMQVXI/kgW0vrArcTUJ – jrast Jun 15 '15 at 09:13

1 Answers1

5

You need an expandable version of the padding. The following example redefines \theimgnr to expand to a number padded with zeros:

\documentclass{article}
\usepackage{graphicx}

\newcounter{imgnr}
\renewcommand{\theimgnr}{%
  \ifnum\value{imgnr}<10 0\fi
  \ifnum\value{imgnr}<100 0\fi
  \the\value{imgnr}%
}

\begin{document}
    % image names are img000.png, img001.png, ..., img999.png
    \setcounter{imgnr}{1}

\includegraphics[width=0.5\linewidth]{./images/img\theimgnr.png}
    \setcounter{imgnr}{2}

\includegraphics[width=0.5\linewidth]{./images/img\theimgnr.png}
    \setcounter{imgnr}{10}

\includegraphics[width=0.5\linewidth]{./images/img\theimgnr.png}
\end{document}

Expandable macro \padnum for TeX numbers

The number range of TeX's numbers are limited, the largest number is 231-1. Therefore macro \padnum uses this fact to make its implementation simple. The first argument is the number of digits, the second is a number between 0 and 231-1.

\documentclass{article}
\usepackage{graphicx}

\newcounter{imgnr}

\makeatletter
\newcommand{\padnum}[2]{%
  \ifnum#1>1 \ifnum#2<10 0\fi
  \ifnum#1>2 \ifnum#2<100 0\fi
  \ifnum#1>3 \ifnum#2<1000 0\fi
  \ifnum#1>4 \ifnum#2<10000 0\fi
  \ifnum#1>5 \ifnum#2<100000 0\fi
  \ifnum#1>6 \ifnum#2<1000000 0\fi
  \ifnum#1>7 \ifnum#2<10000000 0\fi
  \ifnum#1>8 \ifnum#2<100000000 0\fi
  \ifnum#1>9 \ifnum#2<1000000000 0\fi
  \fi\fi\fi\fi\fi\fi\fi\fi\fi
  \expandafter\@firstofone\expandafter{\number#2}%
}

\makeatother

% image names are img000.png, img001.png, ..., img999.png
\setcounter{imgnr}{1}

\includegraphics[width=0.5\linewidth]{./images/img\padnum{3}{\value{imgnr}}.png
    \setcounter{imgnr}{2}

\includegraphics[width=0.5\linewidth]{./images/img\padnum{3}{\value{imgnr}}.png
    \setcounter{imgnr}{10}

\includegraphics[width=0.5\linewidth]{./images/img\padnum{3}{\value{imgnr}}.png
\end{document}

Remarks:

  • No, an optional argument for \padnum via \newcommand*{\padnum}[2][3]{...} is not possible, because then \padnum is no longer expandable and the usage in the file names will break.
Heiko Oberdiek
  • 271,626
  • This solution looks good, but is lacking the possibility of specifying the number of padding zeros. – jrast Jun 15 '15 at 10:03
  • Very nice! Thank you! Just for me to learn: a macro with optional arguments is never expandable or just in this case? – jrast Jun 15 '15 at 14:12
  • 1
    @jrast The detection of the square bracket is based on \futurelet, which is an assignment and therefore not expandable. – Heiko Oberdiek Jun 15 '15 at 14:16