How to output a counter with leading zeros?

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.


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}%
  \padzeroes[2]{\decimal{mycounter}} &
  \padzeroes[4]{\binary{mycounter}} &
  \padzeroes[3]{\octal{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}

Counters in different bases, with prepended zeroes 0


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}