57

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?

badp
  • 424
maxschlepzig
  • 12,082

6 Answers6

50

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.

egreg
  • 1,121,712
  • 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 08 '11 at 14:46
  • an equally robust (because it is based on the same principle) but much more general solution is provided by the animate package as the command \@anim@pad It can be used as \makeatletter\@anim@pad{\mynumber}{\arabic{mycounter}}\makeatother provided \usepackage{animate} is included in the preamble. – 0 _ Jul 29 '14 at 08:14
44

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

Moriambar
  • 11,466
Werner
  • 603,163
  • 3
    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
  • Any idea how to use \padzeroes such as in \immediate\write\file{\padzeroes[3]{\decimal{<countername>}}, i.e. to export it to a file? I get ! Use of \@item doesn't match its definition.... maybe it is related to the above comment... – anderstood Nov 09 '17 at 03:30
  • 1
    @anderstood: How about these options...? – Werner Nov 09 '17 at 05:19
  • @Werner Yes, that's basically egreg's suggestion, which I have used. I did not manage with \padzeroes nor pgf. Does not matter but I was curious why! – anderstood Nov 09 '17 at 05:41
35

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}
  • 4
    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
  • it should be \newcommand\themycounter{\two@digits{\value{reqCounter}}}, otherwise LaTeX returns LaTeX Error: Command \themycounter undefined. (at least for me) – Alessandro Cuttin Aug 16 '19 at 09:26
17

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}
topskip
  • 37,020
14

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.

Martin Scharrer
  • 262,582
1

Here is a command that does it:

\newcommand\z[1]{\ifnum #1 < 10 0\fi #1}

Example:

\the\year-\z{\the\month}-\z{\the\day}
Zombo
  • 1
  • 4
    This is the definition of \two@digits in LaTeX: \def\two@digits#1{\ifnum#1<10 0\fi\number#1} :) – cgnieder Mar 18 '16 at 17:16
  • Please advise on the way(s) that may distinguish the method proposed by you from the one proposed by @egreg in his (accepted) answer. E.g., is it material that you use \ifnum#1 whereas egreg used \ifnum\value{#1}? – Mico Mar 18 '16 at 17:24
  • @Mico that, and mine is wrapped in a command. I did find his useful, but I cant see people wanting to use it outside of the context I have provided – Zombo Mar 18 '16 at 17:27