# Have \foreach restart or loop

I understand that trying to have a "loop function" like \foreach loop sounds like a paradox but I'll try to explain what I mean. Recently, I've asked a question about automatising the creation of hours in a timeline, in order to have them increase, i.e. 00:00, 01:00, 02:00 and so on.

I forgot to mention something there and the problem came out when I was playing with the options. Now for reference this is the part of the code that controlled the hours:

\newcommand*{\MaxNumber}{17}%
\begin{document}
\begin{tikzpicture}
%draw horizontal line
\draw[|->, -latex] (0,0) -- (\MaxNumber,0);
\draw[-, dashed] (-1,0) -- (0,0);

%draw hours
\foreach \x  in {0,...,\MaxNumber} {%
\draw (\x,0) node[below=7pt,anchor=east,xshift=0,rotate=45] {$\x\colon 00$};
\draw[] (\x,-0.1) -- (\x,0.1);
}


If I set the \MaxNumber to 23, the hours would draw each hour using the 24-Hour clock.

But if I set it to, say, 50, the displayed hours would not restart from 00:00, i.e. midnight, instead they'd keep increasing up to 50:00. But since this is a timeline, I'd like the \foreach to restart each time it reaches 23:00, since 24:00 and 00:00 are the same hour.

I've tried tweaking it and searching both this site and internet, but I'm not that expert yet to understand what I could work on. I expect there to be many ways to do this so I'd like to know, what would be the most appropriate way to do it?

-

One way to do that is to use use mod function and add 1 to it.

Below I have set it to display the values mod 10 for illustrative purposes. And you see that it goes up to 10 and then starts back again at 1. For your example you should set

\newcommand*{\ModValue}{24}


## Notes:

• As egreg pointed out, perhaps you want the final hour to be displayed as 0:00 instead of 24:00. If so, you can remove the +1 in:

 \pgfmathtruncatemacro{\ModValue}{mod(\x,\ModValue)+1}

• David Carlisle said he prefers to have two digit for the hours so I have adapted the solution from How to convert a one digit number to a two digit number to do that. If this is not desired, one can simply remove the call to \TwoDigits, or replace the definition of \TwoDigits with:

\newcommand{\TwoDigits}[1]{#1}


## Code:

\documentclass{article}
\usepackage{tikz}

\newcommand{\TwoDigits}[1]{\ifnum#1<10 0#1\else #1\fi}

\newcommand*{\MaxNumber}{17}%
\newcommand*{\ModValue}{10}%   Change this to 24 for your actual use case.
\begin{document}
\begin{tikzpicture}
%draw horizontal line
\draw[|->, -latex] (0,0) -- (\MaxNumber,0);
\draw[-, dashed] (-1,0) -- (0,0);

%draw hours
\foreach \x  in {0,...,\MaxNumber} {%
\pgfmathtruncatemacro{\ModValue}{mod(\x,\ModValue)+1}
\draw (\x,0) node[below=7pt,anchor=east,xshift=0,rotate=45] {\TwoDigits{\ModValue}:00};
\draw[] (\x,-0.1) -- (\x,0.1);
}
\end{tikzpicture}
\end{document}

-
Just two small remarks. (1) \colon is wrong; it should simply be {\ModValue:00}. (2) More importantly, +1 should be removed. – egreg Oct 8 '12 at 23:50
@egreg: But that seems to leave too much space between the hh and mm? The +1 depends on if you want to see 24:00, or 0:00 as the end of the day? – Peter Grill Oct 8 '12 at 23:52
To the contrary: \colon leaves a space that : doesn't. – egreg Oct 8 '12 at 23:53
@egreg: Ok, I had the text in math mode was the problem -- fixing it now... – Peter Grill Oct 8 '12 at 23:55
Peter Grill and @egreg: I agree, it should be 00:00. I think we never actually use 24:00 (and I think nobody does) since the hour after is 01:00 and it would be inconsistent to have 24:00 - 01:00. :D Thanks for the great effort! – Alenanno Oct 9 '12 at 9:40

You just want to change the text, not where it is set? Something like

  \foreach \x  in {0,...,\MaxNumber} {%
\count0=\ifnum\x<24 \x\else\numexpr\x-24\fi\relax\relax
\draw (\x,0) node[below=7pt,anchor=east,xshift=0,rotate=45] {$\the\count0\colon 00$};
\draw[] (\x,-0.1) -- (\x,0.1);
}

-