Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I have a lot of images numbered like so:

image_0001.png
image_0002.png
image_0003.png
...

I would like to set up a Latex command to include a specific numbered image, for example:

\myimageinclude{3}

...would include `image_0003.png'.

In a Latex/C hybrid language this might look something like this:

\newcommand{\myimageinclude}[1]{%
   \includegraphics{sprintf('image_%04d.png', #1)]}}

But I am unsure of the string formation syntax. Is this possible in Latex?

share|improve this question
    
See tex.stackexchange.com/a/86637/15925 for padding the numbers. –  Andrew Swann Apr 16 '13 at 13:11

3 Answers 3

You can use a command to convert your numbers to four-digits numbers adding leading zeroes:

\documentclass{article}
\usepackage{graphicx}

\def\fourdigits#1{%
  \ifnum#1<1000 0\fi
  \ifnum#1<100 0\fi
  \ifnum#1<10 0\fi
  \number#1}
\newcommand\myinclude[2][]{%
  \includegraphics[#1]{image_\fourdigits{#2}}}

\begin{document}

\myinclude[width=3cm]{18}

\end{document}

By the way, I'd rather noy use underscores in the names of the images files; perhaps a hyphen would be safer.

share|improve this answer

A simple solution, when number is less than 10:

\newcommand{\myimageinclude}[1]{%
\includegraphics{{image_000#1.png}}}

Do you need larger numbers?

share|improve this answer

This should work if you have files 'image_1' and 'image_2' with suitable extensions.

\documentclass{article}
\newcommand\myimageinclude[1]{\includegraphics{image_#1}}
\usepackage{graphicx}
\begin{document}
\myimageinclude{1}
\myimageinclude{2}
\end{document}

You can insert zeros before the image number as follows.

\documentclass{article}
\newcommand\myimageinclude[1]{%
\ifnum #1 < 10
  \def\zs{000}
\else
  \ifnum #1 < 100
    \def\zs{00}
  \else
     \def\zs{0}
  \fi
\fi
\includegraphics{image_\zs#1}}

\usepackage{graphicx}
\begin{document}
\myimageinclude{1}
\myimageinclude{20}
\end{document}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.