48

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?

42

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
  • 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. – Ioannis Filippidis Jul 29 '14 at 8:14
38

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

  • 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 9 '17 at 3:30
  • @anderstood: How about these options...? – Werner Nov 9 '17 at 5: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 9 '17 at 5:41
29

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}
  • 2
    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
16

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}
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.

  • Where should I use \pgfmathsetbasenumberlength{2} if I want some counters used within tikzpicture to be with 2 digits? – Sigur Feb 27 '16 at 14:32
  • 1
    @Sigur: Try it at the beginning right after \begin{tikzpicture}. – Martin Scharrer Mar 9 '16 at 21:22
  • I tried \pgfmathsetbasenumberlength{3}. How can I get 001 with the firsr \test? – minhthien_2016 Oct 2 '16 at 8:33
  • I tried \pgfmathsetbasenumberlength{3}. How can I get 001 with the firsrt \test? Because I got 000 with the first \test. – minhthien_2016 Oct 2 '16 at 8:39
  • @toandhsp: Simply move the \stepcounter{mycounter}% to the front inside the macro. – Martin Scharrer Oct 10 '16 at 20:23
0

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}
  • 2
    This is the definition of \two@digits in LaTeX: \def\two@digits#1{\ifnum#1<10 0\fi\number#1} :) – clemens 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 – Steven Penny Mar 18 '16 at 17:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.