# Automatically setting one node after the other in Tikz

I've changed a timeline of mine to look like the one asked in this question: timeline and tikz

Everything went smoothly but I have a problem. I don't know how to automatically set the nodes' position.

Basically their positions should "fixed", starting from (3,0) and then one after the other with a fixed distance between them (regardless if they are one-line or not). After that I'd be making the lines. How can I tell Tikz to automatically get the coordinates of each text node without setting it manually? (This would mean removing the second argument for the command \evento.)

I expect the final result to look like the one in the question or like Tom Bombadil's answer but using my code:

\documentclass[]{article}
\usepackage[a4paper, margin=1cm]{geometry}
\usepackage[utf8]{inputenc}
\usepackage{rotating}
\usepackage{amsmath}
\usepackage{pgfplots}
\usepackage{tikz}
\usepackage{xparse,expl3}

\usetikzlibrary{fit, arrows,backgrounds,patterns,shapes,shapes.multipart,positioning,calc,decorations.markings}
\pgfplotsset{compat=1.7}
\pagestyle{empty}

\tikzset{
opera/.style={draw, rectangle, align=center, text width=6cm, fill=white, font=\scriptsize},
typnode/.style={anchor=north west, text width=12cm, inner sep=0mm},
data/.style={draw=gray, rectangle, fill=white, font=\scriptsize, inner sep=0.5mm},
datum/.style={font=\scriptsize, rotate=90, inner sep=1mm},
}

\newcommand*{\evento}[3]{
\coordinate (A) at (0,{(#1-1600)/10});
\coordinate (B) at (3,{#2});%
\coordinate[right=5mm of A] (Z);
\coordinate[left=1cm of B] (Y);
\draw (A) -- (Z);
\draw[-|] (Y) -- (B) node[data, pos=0.55] {#1};
\draw (Z) -- (Y);
\node[right=3mm of B, typnode] (D) {#3};
}

\begin{document}
\footnotesize
\begin{tikzpicture}[x=1cm,y=-7mm]
\centering
%draw horizontal line
\draw[|->, -latex, draw] (0,0) -- (0,39);
\draw[-, dashed] (0,-0.5) -- (0,0);
\draw[|->, -latex, draw] (34,0) -- (34,39);
\draw[-, dashed] (34,-0.5) -- (34,39);

%draw years
\foreach \y [evaluate=\y as \xear using int(1600+\y*10)] in {0,1,...,38}{
\draw (0,\y) node[left=2pt,anchor=east,xshift=0,font=\scriptsize] {$\xear$};
\draw (-0.1,\y) -- (0.1,\y);
}

\foreach \y [evaluate=\y as \xear using int(1600+\y*10)] in {0,1,...,37}{
\draw (0,\y) node[left=2pt,anchor=east,xshift=0,font=\scriptsize] {};
\draw (0,\y+.5) -- (0.1,\y+.5);
}

\evento{1603}{0}{King James VI of Scotland ascends to the English throne, becoming James I of England and uniting the crowns - but not the parliaments - of the two kingdoms};
\evento{1605}{2}{\textbf{5 November} - Gunpowder Plot: A plot in which Guy Fawkes and other Catholic associates conspired to blow up King James VI and I and the Parliament of England was uncovered.};
\evento{1618}{3}{Walter Raleigh was executed.};
\evento{1639}{5}{Bishops' Wars: A war with Scotland began which would last until 1640.};
\evento{1640}{6}{Long Parliament: The Parliament was convened.};
\evento{1642}{7}{English Civil War: The war began (see timeline of the English Civil War).};
\evento{1649}{8}{January Trial and execution of Charles I\\Interregnum began with the First Commonwealth};
\evento{1653}{9}{The Protectorate began under the Lord Protector Oliver Cromwell};
\evento{1659}{10}{Second Commonwealth a period of great political instability};
\evento{1660}{11}{Restoration of the monarchy};
\evento{1666}{12}{\textbf{2 September} - Great Fire of London: A fire began in London.\\\textbf{6 September} - Great Fire of London: The fire ended.};

\end{tikzpicture}
\end{document}

-

I would use chains here. The west below key helps to align the nodes correctly. Its definition is taken from my positioning-plus library.

I haven’t followed your \evento macro bit for bit but included the lines in the loop. I have removed the fill=white from the data style as it is not placed on top of a line anymore.

The vertical distances between the typnodes is specified via the node distance option and is taken between the south west of the previous node and the north west anchor (“west below”) of the new node.

The label (data) with the year is placed base left of the typnode so that they share the same baseline.

One could also make a function out of {(\Year-1600)/10} so that you can easily change the y axis. There could be very different implementation on how to get the coordinate on the line.

## Code

\documentclass[tikz]{standalone}
\usetikzlibrary{chains}
\makeatletter
\tikzset{west below/.code=\tikz@lib@place@handle@{#1}{north west}{0}{-1}{south west}{1}}
\makeatother
\tikzset{
typnode/.style={anchor=north west, text width=12cm, inner sep=0mm},
data/.style={draw=gray, rectangle, font=\scriptsize, inner sep=0.5mm},
}
\begin{document}\footnotesize
\begin{tikzpicture}[x=1cm, y=-7mm]
%draw horizontal line
\foreach \xValue in {0,34}
\path (\xValue,0) edge[-latex] ++(0,39)
edge[dashed] ++ (down:1);

%%draw years
\foreach \y [evaluate=\y as \xear using int(1600+\y*10)] in {0,1,...,38}{
\node[left=2pt,anchor=east,xshift=0,font=\scriptsize] at (0,\y) {$\xear$};
\draw (-0.1,\y) -- (0.1,\y); \draw (0,\y+.5) -- (0.1,\y+.5);
}

\begin{scope}[start chain=ch1 going west below, node distance=+1em]
\foreach \Year/\Text in {%
1603/{King James VI of Scotland ascends to the English throne, becoming James I of England and uniting the crowns - but not the parliaments - of the two kingdoms},
1605/{\textbf{5 November} - Gunpowder Plot: A plot in which Guy Fawkes and other Catholic associates conspired to blow up King James VI and I and the Parliament of England was uncovered.},
1618/{Walter Raleigh was executed.},
1639/{Bishops' Wars: A war with Scotland began which would last until 1640.},
1640/{Long Parliament: The Parliament was convened.},
1642/{English Civil War: The war began (see timeline of the English Civil War).},
1649/{January Trial and execution of Charles I\\Interregnum began with the First Commonwealth},
1653/{The Protectorate began under the Lord Protector Oliver Cromwell},
1659/{Second Commonwealth a period of great political instability},
1660/{Restoration of the monarchy},
1666/{\textbf{2 September} - Great Fire of London: A fire began in London.\\\textbf{6 September} - Great Fire of London: The fire ended.}}{
\node[typnode, at=(right:5cm), on chain=ch1, alias=Text] {\Text};
\node[data,    base left=+2em of Text, alias=Year] {\Year};
\draw[-|] (Year.east) -- ++(right:3mm);
\draw     (Year.west) -- ++(left:3mm)
-- ([shift=(right:3mm)] 0,{(\Year-1600)/10})
--                     (0,{(\Year-1600)/10});
}
\end{scope}
\end{tikzpicture}
\end{document}


## Output

-
Thank you, I thought I had accepted but I didn't. A side question (if it's easy): is it possible to assign names to the single nodes, either manually (custom names) or automatically (e.g. increasing labels)? –  Alenanno Oct 4 '13 at 13:54
The main nodes (with the \Text) or the ones with the year in it? The main nodes are on the chain and are automatically named ch1-1, ch1-2, and so on (ch1 is the name of the chain as defined with the start chain key). The first and the last node are also named ch1-begin and ch1-end respectively. –  Qrrbrbirlbel Oct 4 '13 at 21:41
Yes those, thanks! I didn't know they were named automatically. :) –  Alenanno Oct 4 '13 at 21:51
Hey, sorry to bother again, but I was wondering, mind to explain the syntax of \tikzset{west below/.code=\tikz@lib@place@handle@{#1}{north west}{0}{-1}{south west}{1}} What do those numbers stand for? Thanks! –  Alenanno Oct 9 '13 at 18:06
@Alenanno Take a look at the examples from the positioning library and my linked positioning-plus library for examples. The macro \tikz@lib@place@handle@ checks #1 for of and then executes certain macros and sets the new node’s north west below (-1 * <node distance>) of the referenced node’s south west anchor (unless one is given). The 0 is the shifting in x direction and the 1 is a factor for node distance does only contain one value. –  Qrrbrbirlbel Oct 9 '13 at 20:05