4

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?

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.

0

A simple solution, when number is less than 10:

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

Do you need larger numbers?

0

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}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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