TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a table that represents a timetable for bus, so each cell contains time of departure as hour followed by minute, both as two digits, but sometimes I don't have two digits,just one so I have to switch eg. from 12 4 -> 12 04. How is it possible?

share|improve this question
If you add a small usage example which shows the exact use-case people will be able to help you better. How do you get/import the data? – Martin Scharrer May 17 '12 at 10:02
Very related is How to output a counter with leading zeros? – Werner May 17 '12 at 22:50
up vote 23 down vote accepted

You can define a macro as follows:

   \ifnum#1<10 0#1\else #1\fi

\twodigits{12}  % 12
\twodigits{4}   % 04
\twodigits{123} % 123

This macro is fully expandable.

If you also want to cut trailing zeros you can use:

   \ifnum#1<10 0\number#1 \else #1\fi

\twodigits{004} % 04

If you want to change a tabular cell from 12 4 to 12 04 without adding explicit macros you can use the collcell package to collect the cell content and feed it to a macro which splits the numbers by the space:

% preamble:
\def\formatdatei#1 #2\relax{%
   \twodigits{#1} \twodigits{#2}%


% later
   Bus date  & 12 4 \\

If you post a real usage example I can help me with more specific macros.

share|improve this answer
Thank you for response. Can you please take a look here? I've added the question before with a case usage, but someone suggested to post a different question regarding this problem. – Ion Morozan May 17 '12 at 10:46
@IonMorozan: In such cases always add a link to the other question in the new question. – Martin Scharrer May 17 '12 at 10:48
Yes you are right! It's is my third post on a forum, so I have to learn. Thank you for the feedback and by the way your solution works great for me. Thank you again! – Ion Morozan May 17 '12 at 10:51
@MarcoDaniel: Thanks for pointing this out. I wrote the answer under my Windows installation were I don't have LaTeX installed, so I couldn't test it right away. I added a version which handles this kind of input without an assignment. – Martin Scharrer May 17 '12 at 12:50
@MartinScharrer: I found the following definition in latex.ltx ;-) \def\two@digits#1{\ifnum#1<10 0\fi\number#1} – Marco Daniel May 17 '12 at 13:18

You can do that using the siunitx package



\num[minimum-integer-digits = 4]{123}
\num[minimum-integer-digits = 4]{4}


0123 0004

or alternatively you can declare the option as default,


\sisetup{minimum-integer-digits = 4}
share|improve this answer
Can you set minimum-integer-digits separately, as a default? – Brent.Longborough May 17 '12 at 11:56
@percusse thanks for the edit, wasn't in front of a computer all day long.. – Andreas Wallner May 17 '12 at 22:35

You can also use the xstring package to add the leading zero for single digits:

enter image description here


123 $\to$ \TwoDigit{123}

12 $\to$ \TwoDigit{12}

2 $\to$  \TwoDigit{2}
share|improve this answer

You can use LaTeX's internal \two@digit for formatting:

enter image description here


\twodigit{12} \par % 12
\twodigit{4} \par  % 04
\twodigit{123}     % 123

share|improve this answer

Your Answer


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.