Text FormattingPrint Leading Zeros on a Counter

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
elahrairah
Posts: 1
Joined: Tue Jul 12, 2011 7:27 pm

Print Leading Zeros on a Counter

Post by elahrairah »

I would like to use a counter to automatically include image files (for example, I have images im001.png, im002.ping, ... im999.png). How can I use the counter to correctly get the fact that there are leading zeros in the file names? Is there a way to "print out" a counter (or other variable) with leading zeros in this way?

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Print Leading Zeros on a Counter

Post by localghost »

Define a counter which tests several conditions like shown below.

Code: Select all

\documentclass[11pt,a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\newcounter{image}
\renewcommand*{\theimage}{%
  \ifnum\value{image}<100 0\fi%
  \ifnum\value{image}<10 0\fi%
  \arabic{image}%
}

\begin{document}
  \theimage

  \setcounter{image}{10}
  \theimage

  \setcounter{image}{100}
  \theimage
\end{document}
To read a single graphics file you then need to insert the counter accordingly.

Code: Select all

\includegraphics{im\theimage}

Best regards and welcome to the board
Thorsten
Post Reply