# How to output a counter with leading zeros?

I can output the value of a counter in a LaTeX document via:

\setcounter{mycounter}{1}
\arabic{mycounter}


Which inserts a 1. But how do I format the output by, for example, adding a leading zero if mycounter is less than 10 in a way similar to what printf lets you do by specifying %02d as your format string?

-

The principle is

\ifnum\value{mycounter}<10 0\fi\arabic{mycounter}


How to implement it into your macros depends on how and where you want to use this representation.

-
I want to use it as an argument to \Opensolutionfile{ans}[prefixCOUNTER] which is part of the answers package. And your approach works fine in that context. –  maxschlepzig Oct 8 '11 at 14:46

The fmtcount package provide a variety of ways to format counter (even changing the base to binary or octal, rather than decimal, say). To prepend a bunch of zeroes to a counter, use \padzeroes[<n>]{\decimal{<cntr>}}. This will add zeroes 0 in front of the counter <cntr> such that the eventual length is <n>. \decimal{<cntr>} is similar to \arabic{<cntr>} but is required to work with \padzeroes:

\documentclass{article}
\usepackage{fmtcount}% http://ctan.org/pkg/fmtcount
\begin{document}
\newcounter{mycounter}%
\newcommand{\printcntr}{%
\stepcounter{mycounter}%
}

\begin{tabular}{ccc}
\textbf{Decimal} & \textbf{Binary} & \textbf{Octal} \\ \hline
\printcntr \\ \printcntr \\ \printcntr \\ \printcntr \\ \printcntr \\
\printcntr \\ \printcntr \\ \printcntr \\ \printcntr \\ \printcntr
\end{tabular}
\end{document}


-
Just a note: \padzeroes seems to be a \protected macro, so you cannot do stuff like \edef\mypad{\padzeroes[2]{5}} - Latex will complain about ! Undefined control sequence. –  sdaau May 25 '12 at 10:27

LaTeX knows a macro \two@digits

\documentclass{article}
\newcounter{mycounter}
\makeatletter
\renewcommand\themycounter{\two@digits{\value{mycounter}}}
\makeatother

\newcommand\Test{\stepcounter{mycounter}\themycounter\ }
\begin{document}
\Test
\Test
\Test

\end{document}

-
Just a note: \two@digits seems not to be \protected - and thus expandable; so you can do stuff like \makeatletter\edef\mypad{\two@digits{5}}\makeatother (to store its output in a command). –  sdaau May 25 '12 at 10:26

If you use LuaTeX, you can do this:

\documentclass{article}
\begin{document}
\newcounter{mycounter}
\setcounter{mycounter}{10}
\newcommand\print[1]{%
\directlua{
tex.sprint(string.format("\%02d",\arabic{#1}))
}
}
\print{mycounter}
\end{document}

-

You could use pgf for this. It has a \pgfmathsetbasenumberlength setting used for the base conversion macros. Setting it to 2 and do a dummy conversion the counter value to base 10 will give you 01, 02, ..., 10, etc. It also scales well, i.e. you can just use 3 and get 001, etc. However, for smaller things I would use egreg's solution instead.

\documentclass{article}

\usepackage{pgf}

\newcounter{mycounter}
\newcommand\test{{%
\pgfmathsetbasenumberlength{2}% try 3, 4, ...
\pgfmathbasetodec\testvalue{\the\value{mycounter}}{10}%
\testvalue
\stepcounter{mycounter}%
}}

\begin{document}

\test

\test

\test

\test

\test

\test

\test

\test

\test

\test

\test

\end{document}


Please also see the PGF manual section 66 Number Printing which should also be interesting for you.

-