9

Some time ago I've asked a question about Timelines, many solutions were given, but in the end I decided to go with TikZ and also presented my solution. But I couldn't have done it without Torbjørn T.'s comment, where he suggested to make the creation of dates automatic, and this helped since I didn't need to type all of that myself and above all, it saved space in my Tex document. The solution was1:

\documentclass[10pt]{article}

\usepackage[a4paper, landscape]{geometry}
\usepackage[utf8]{inputenc}
\usepackage{rotating}
\usepackage{pgfplots}
%\usepackage{changepage}
\usepackage{tikz}
\usetikzlibrary{arrows,backgrounds,snakes}

\begin{document}

    \begin{tikzpicture}

       %draw horizontal line   
       \draw[|->, -latex] (0,0) -- (17,0);
       \draw[-, dashed] (-1,0) -- (0,0);

       %draw years

       \foreach \x [evaluate=\x as \year using int(1790+\x*10)] in {0,1,...,17}{ 

            \draw (\x,0) node[below=7pt,anchor=east,xshift=0,rotate=45] {$\year$}; 
            }

       \draw[] (0,-0.1) -- (0,0.1);
       \draw[] (1,-0.1) -- (1,0.1);
       \draw[] (2,-0.1) -- (2,0.1);
       \draw[] (3,-0.1) -- (3,0.1);
       \draw[] (4,-0.1) -- (4,0.1);
       \draw[] (5,-0.1) -- (5,0.1);
       \draw[] (6,-0.1) -- (6,0.1);
       \draw[] (7,-0.1) -- (7,0.1);
       \draw[] (8,-0.1) -- (8,0.1);
       \draw[] (9,-0.1) -- (9,0.1);
       \draw[] (10,-0.1) -- (10,0.1);
       \draw[] (11,-0.1) -- (11,0.1);
       \draw[] (12,-0.1) -- (12,0.1);
       \draw[] (13,-0.1) -- (13,0.1);
       \draw[] (14,-0.1) -- (14,0.1);
       \draw[] (15,-0.1) -- (15,0.1);
       \draw[] (16,-0.1) -- (16,0.1);

   \end{tikzpicture}

\end{document}

The result was:

screenshot

... And it continues (too long to fit here). Now I want to recreate this but with hours and minutes, so that at the bottom of the line I can see 00:00 then 01:00 and so on.

This could be indefinite, as I have the intention of making a long horizontal document (not going to be printed, only digital use so no problem).

I have tried tweaking that code and searching this site but to no avail. How should I go about it? Is the code good for years but not for time as I want it? I really don't know where to start.


1: This is actually a minimal working example. I'm referring to the lines reported in the comment.

Alenanno
  • 37,338
  • 2
    Can you post what you tried so as to determine what went wrong with using HH:MM instead of years? Replacing the \foreach with \foreach \x in {0,...,17}{ \draw (\x,0) node[below=7pt,anchor=east,xshift=0,rotate=45] {$\x\colon 00$}; } seems to work just fine. You can also eliminate all the manual draw commands by using \draw[] (\x,-0.1) -- (\x,0.1); within the \foreach loop. – Peter Grill Oct 05 '12 at 18:43
  • @PeterGrill I didn't write {$\x\colon 00$};, just touching the using int(1790+\x*10)] part which gave errors. I'm not that expert. :D Now that you mention it, I did the automatized thing for the lines somewhere else, this one was a pretty old code. But thanks anyway. :) – Alenanno Oct 05 '12 at 19:01
  • 1

1 Answers1

9

By a slight change to the \foreach loop you get what I think is the desired result:

enter image description here

Notes:

You can greatly simplify your code:

  • I commented out the package not needed for this MWE.
  • By setting \MaxNumberyou can easily change one parameter and increase the width of the picture (page dimensions need to be adjust).
  • The individual \draw commands could be in the \foreach loop.

Code:

\documentclass[10pt]{article}

\usepackage[a4paper, landscape]{geometry} %\usepackage[utf8]{inputenc} %\usepackage{rotating} %\usepackage{pgfplots} %\usepackage{changepage} \usepackage{tikz} %\usetikzlibrary{arrows,backgrounds,snakes}

\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:00}; 
        \draw[] (\x,-0.1) -- (\x,0.1);
        }

\end{tikzpicture} \end{document}

Peter Grill
  • 223,288
  • Peter, I've deleted my previous comments (just letting you know so you could delete yours). But I have another side question. I kind of forgot to mention this. If I raise the \MaxNumber, hours continue indefinitely. But I want them to act like... hours. So when it reaches "23:00", I'd like it to restart at "00:00" again and not continue to 24, 25, 26... I'd appreciate even an hint. I've tried looking around but I have no idea. :| Thanks in advance. – Alenanno Oct 08 '12 at 16:41
  • As that is also unrelated to drawing things that would also be better asked as a separate question. That way others who have a similar question are more likely to find it rather than have it buried in this question. There are probably many ways to do that using mod arithmetic. I would probably use something like \pgfmathmod – Peter Grill Oct 08 '12 at 17:51
  • Uhm ok I'll ask a question later. :) – Alenanno Oct 08 '12 at 18:07
  • 1
    @Alenanno: I have corrected the way the HH:MM were being typeset by not placing the text in math mode, and removed the use of \colon. Please see updated answer. – Peter Grill Oct 09 '12 at 00:04