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 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
add comment

4 Answers 4

up vote 23 down vote accepted

You can define a macro as follows:

\newcommand\twodigits[1]{%
   \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:

\newcommand\twodigits[1]{%
   \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:
\newcommand\formatdate[1]{\formatdatei#1\relax}
\def\formatdatei#1 #2\relax{%
   \twodigits{#1} \twodigits{#2}%
}

\usepackage{collcell}

% later
\begin{tabular}{l>{\collectcell\formatdate}l<{\endcollectcell}}
   Bus date  & 12 4 \\
\end{tabular}

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
1  
@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
show 2 more comments

You can do that using the siunitx package

\documentclass[a4paper,twoside,10pt]{article}

\usepackage{siunitx}

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

prints

0123 0004

or alternatively you can declare the option as default,

\documentclass[a4paper,twoside,10pt]{article}
\usepackage{siunitx}

\begin{document}
\sisetup{minimum-integer-digits = 4}
\num{12}
\num{34}
\end{document}
share|improve this answer
3  
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
add comment

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

enter image description here

Code:

\documentclass{article}
\usepackage{xstring}
\newcommand*{\TwoDigit}[1]{%
    \IfStrEqCase{#1}{%
        {1}{0}%
        {2}{0}%
        {3}{0}%
        {4}{0}%
        {5}{0}%
        {6}{0}%
        {7}{0}%
        {8}{0}%
        {9}{0}%
    }#1%
}%
\begin{document}
123 $\to$ \TwoDigit{123}

12 $\to$ \TwoDigit{12}

2 $\to$  \TwoDigit{2}
\end{document}
share|improve this answer
add comment

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

enter image description here

\documentclass{article}
\makeatletter
\newcommand{\twodigit}[1]{\two@digits{#1}}
\makeatother
\begin{document}

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

\end{document}
share|improve this answer
add comment

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.