# Vertically align nodes in tikz graph

I have taken to drawing automata in TikZ, as it will position the nodes for me, and generally requires very little post-processing in terms of options.

I know I can force nodes to be in the same horizontal layer by using the [same layer] directive.

How can I do this vertically (i.e. align 1,3,5 and 2,4)?

MWE:

\documentclass[10pt,letterpaper]{article}
\usepackage{tikz}
\usetikzlibrary{graphdrawing,positioning,graphs}
\usegdlibrary{layered,trees}
\begin{document}
\begin{tikzpicture}[baseline]
\begin{graph}[layered layout]
{1->2->3->4,
2->4,1->4,
1->3,4->5,2->5,
{[same layer]1,2},
{[same layer]3,4}
};
\end{graph}
\end{tikzpicture}
\end{document}

• Not any more. TikZ v3.00 is officialy out on CTAN. – percusse Sep 11 '14 at 20:52
• If you want layers being vertical, use [grow left] or [grow right]. There is also [branch up] (or down/left/right) and [grid placement]. See more on TikZ manual §19.9.2 Placement on a Grid. Or, after all, why not consider tikz-cd? – Symbol 1 Sep 14 '14 at 7:52

Like this?

\documentclass{minimal}
\usepackage{tikz}
\usetikzlibrary{graphs}
\begin{document}
\begin{tikzpicture}
\begin{graph}[grid placement,wrap after=2]{
1 -> 2 -> 3 -> 4 -> 5, 1 -> 3, 1 -> 4, 2 -> 4, 2 -> 5};
\end{graph}
\end{tikzpicture}
\end{document}


## More Details

You can

• assign a shift to a node.
• use / to declare a hole. In fact, nema/txet declars a node named nema but shown as txet.

\begin{tikzpicture}
\begin{graph}[grid placement,wrap after=4]{
/, 2             , 3              , /,
5, 6[at={(.5,0)}], 7[at={(0,-.5)}], 8,
9,10[at={(0,.5)}],11[at={(-.5,0)}],12,
/,14             ,15};
\end{graph}
\end{tikzpicture}


## Even More Details

Roughly speaking, a TikZ-node consists of two part: its shape and its shown text. The text-part is usually shown, and the shape-part is drawn when you say [draw]. A simple way to stop it is to say [white], which makes the shape-part drawn in white. Or even stronger, say [opacity=0] in case your background is not white.

As to spread out nodes, you may adjust build-in parameters such as [branch down=2cm,grow right=2cm].

• Two questions (just cause I don't know much about the grid placement style). 1) is there a way to put nodes between grid points (say above the midpoint of 1 and 2) how does one insert a placeholder node (invis) for spacing? – soandos Sep 14 '14 at 20:28
• Is there a way to get the node given by / to not be drawn at all (if I use nodes=[draw,circle]) I get an empty circle – soandos Sep 15 '14 at 8:43
• Also, how can I make there be more space between nodes? – soandos Sep 15 '14 at 8:49

You can also use the following hack: use same layer to align horizontally, but also rotate=90 the whole graph, so that horizontal becomes vertical (this does not rotate the node contents, which is perfect for this case):

\documentclass[10pt,letterpaper]{article}
\usepackage{tikz}
\usetikzlibrary{graphdrawing,positioning,graphs}
\usegdlibrary{layered,trees}
\begin{document}
\begin{tikzpicture}[baseline,rotate=90]
\begin{graph}[layered layout]
{1->2->3->4,
2->4,1->4,
1->3,4->5,2->5,
{[same layer]1,3,5},
{[same layer]2,4}
};
\end{graph}
\end{tikzpicture}
\end{document}


Produces:

• The issue is that then I still can only use [same layer] for one direction. – soandos Sep 15 '14 at 8:44