7

I am making a timeline (spanning hours, not days/years) labelled with digital times (e.g. 1:15, 5:45, 3:00). Everything is calculated based on minutes. The problem I have is the o'clock times, where the minutes shows as zero, instead of a double zero: the six o'cloxk looks silly

Is there an easy way to do this, or is best to hard-code an if-then?

Here is the MWE (based on a numberline code from somewhere on this site, can't remember where):

\documentclass[10pt,oneside]{article}
\usepackage[margin=0.5in,a4paper,landscape]{geometry}
\usepackage{tikz}
\newcommand{\drawtimeline}[5]
{
\pgfmathsetmacro\starttime{#1*60+#2}
\pgfmathsetmacro\endtime{#3*60+#4}
\pgfmathsetmacro\timediff{\endtime-\starttime}
\pgfmathsetmacro\numticks{\timediff} 
\pgfmathsetmacro\numlabels{\timediff/15}
\pgfmathsetmacro\intervallabel{#5/\numlabels}
\pgfmathsetmacro\intervaltick{#5/\numticks}
\pgfmathsetmacro\extraticks{\numticks+2}
\begin{tikzpicture}
    %draw line
    \draw [thick](-2*\intervaltick pt,0) -- (#5+2*\intervaltick pt,0);
    %draw small ticks
    \foreach \y  in {-2,-1,..., \extraticks } {
        \draw[thin, yshift=0 cm, xshift= \intervaltick  * \y ] (0pt,+4pt) -- (0pt,-3pt);}
    %draw big ticks and labels
    \foreach \y  
    [evaluate=\y as \hrs using ({div(\y * 15 + \starttime,60)}] 
    [evaluate=\y as \mins using ({mod(\y * 15 +\starttime,60)}]
    in {0,1,...,\numlabels}
    { 
    \draw[yshift=-0.5 cm, xshift = \intervallabel * \y ] node[ below]{$\pgfmathprintnumber{\hrs}$:$\pgfmathprintnumber{\mins}$};
    \draw[thick, yshift=0 cm, xshift= \intervallabel  * \y ] (0pt,+8pt) -- (0pt,-8pt);}
\end{tikzpicture}

}

\begin{document}
\drawtimeline{5}{45}{6}{30}{25cm}

\end{document}
Tim
  • 1,539

1 Answers1

3

After I determined that \mins outputs strings like 15.0 and 0.0, I replaced the \pgfmathprintnumber{\mins} output with \twodigit{\mins}, where \twodigit is defined as

\newcommand\twodigit[1]{\expandafter\twodigitaux#1\relax}
\def\twodigitaux#1.#2\relax{\ifnum10>#1\relax0\fi\pgfmathprintnumber{#1.#2}}

In this way, if the pre-dot \mins number is less than 10, an extra 0 is added to the output. So, for example, \twodigit{7.23} produces 07.23.

\documentclass[10pt,oneside]{article}
\usepackage[margin=0.5in,a4paper,landscape]{geometry}
\usepackage{tikz}
\newcommand{\drawtimeline}[5]
{
\pgfmathsetmacro\starttime{#1*60+#2}
\pgfmathsetmacro\endtime{#3*60+#4}
\pgfmathsetmacro\timediff{\endtime-\starttime}
\pgfmathsetmacro\numticks{\timediff} 
\pgfmathsetmacro\numlabels{\timediff/15}
\pgfmathsetmacro\intervallabel{#5/\numlabels}
\pgfmathsetmacro\intervaltick{#5/\numticks}
\pgfmathsetmacro\extraticks{\numticks+2}
\begin{tikzpicture}
    %draw line
    \draw [thick](-2*\intervaltick pt,0) -- (#5+2*\intervaltick pt,0);
    %draw small ticks
    \foreach \y  in {-2,-1,..., \extraticks } {
        \draw[thin, yshift=0 cm, xshift= \intervaltick  * \y ] (0pt,+4pt) -- (0pt,-3pt);}
    %draw big ticks and labels
    \foreach \y  
    [evaluate=\y as \hrs using ({div(\y * 15 + \starttime,60)}] 
    [evaluate=\y as \mins using ({mod(\y * 15 +\starttime,60)}]
    in {0,1,...,\numlabels}
    { 
    \draw[yshift=-0.5 cm, xshift = \intervallabel * \y ] node[ below]{%
      $\pgfmathprintnumber{\hrs}$:%
      $\twodigit{\mins}$};
    \draw[thick, yshift=0 cm, xshift= \intervallabel  * \y ] (0pt,+8pt) -- (0pt,-8pt);}
\end{tikzpicture}
}
\newcommand\twodigit[1]{\expandafter\twodigitaux#1\relax}
\def\twodigitaux#1.#2\relax{\ifnum10>#1\relax0\fi\pgfmathprintnumber{#1.#2}}
\begin{document}
\drawtimeline{5}{45}{6}{30}{25cm}
\end{document}

enter image description here

  • I tried, following one of the comments, to use LaTex's own \two@digits formula, but couldn't get it to work. How does your implementation differ? – Tim Apr 25 '17 at 18:01
  • 1
    @Tim I do not know how LaTeX's intrinsic function works, but what I did here with the helper macro \twodigitaux (called by \twodigit) is to examine what is passed (\mins) which takes the form of #1.#2. If #1 was below 10, I added a leading 0 manually; then I just let \pgfmathprintnumber print out #1.#2 the way it wants. – Steven B. Segletes Apr 25 '17 at 18:10