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?
-
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 ScharrerMay 17, 2012 at 10:02
-
Very related is How to output a counter with leading zeros?– Werner ♦May 17, 2012 at 22:50
4 Answers
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.
-
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. May 17, 2012 at 10:46
-
@IonMorozan: In such cases always add a link to the other question in the new question. May 17, 2012 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! May 17, 2012 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. May 17, 2012 at 12:50
-
1@MartinScharrer: I found the following definition in latex.ltx ;-)
\def\two@digits#1{\ifnum#1<10 0\fi\number#1}
May 17, 2012 at 13:18
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}
-
3Can you set
minimum-integer-digits
separately, as a default? May 17, 2012 at 11:56 -
@percusse thanks for the edit, wasn't in front of a computer all day long.. May 17, 2012 at 22:35
You can also use the xstring
package to add the leading zero for single digits:
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}
You can use LaTeX's internal \two@digit
for formatting:
\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}