Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question

5 Answers 5

up vote 24 down vote accepted

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.

share|improve this answer
    
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. –  johntex Jul 29 at 8:14

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

share|improve this answer
    
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}
share|improve this answer
    
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}
share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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