Q:

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?

A:

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.

A:

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}

A:

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}

