# Tikz vertical align in grid

The following is one MWE to illustrate the problem. I don't even know how to describe it properly. As soon as I added the last curved line, the vertical position is destroyed. Could someone shed some light on this behavior?

\documentclass{beamer}

\mode<presentation>
{
\usecolortheme{default} % or try albatross, beaver, crane, ...
\usefonttheme{default}  % or try serif, structurebold, ...
\setbeamertemplate{caption}[numbered]
}
\usepackage{subfig}
\makeatletter
\usepackage{tikz}
\usetikzlibrary{positioning,calc,shapes,arrows,fit}
\begin{document}
\begin{frame}
\begin{figure}
\subfloat[]{\label{}
\begin{tikzpicture}[darkstyle/.style={circle,draw,fill=blue!20,minimum size=20}]
\foreach \y in {0,...,3}
\foreach \x in {0,...,3}
{\pgfmathtruncatemacro{\label}{4 * \y + \x}
\node [darkstyle]  (\y\x) at (1.5*\x,4.5-1.5*\y) {\label};}

\foreach \y [count=\yi]  in {0,...,2}
\foreach \x in {0,...,3}
\draw [->,green] (\x\y) -- (\x\yi) ;

% \foreach \i [count=\ii] in {0,...,2}
% \draw [->,green] (\i3) to [in=90, out=-90] (\ii0) ;

\foreach \i [count=\ii] in {0,...,2}
\draw [->,green] (\i3) to [in=45, out=-135] (\ii0) ;
\end{tikzpicture}
}
\subfloat[]{\label{}
\begin{tikzpicture}[darkstyle/.style={circle,draw,fill=blue!20,minimum size=20, outer sep=0pt}]
\foreach \y in {0,...,3}
\foreach \x in {0,...,3}
{\pgfmathtruncatemacro{\label}{4 * \y + \x}
\node [darkstyle]  (\y\x) at (1.5*\x,4.5-1.5*\y) {\label};}

\foreach \y [count=\yi]  in {0,...,2}
\foreach \x in {0,...,3}
\draw [->,green] (\x\y) -- (\x\yi) ;

\draw [->,green] (03) to [in=90, out=-90] (10) ;
\draw [->,green] (13) to [in=90, out=-90] (20) ;
% \draw [->,green] (23) to [in=90, out=-90] (30) ;
\end{tikzpicture}
}
\end{figure}
\end{frame}
\begin{frame}
\begin{figure}
\subfloat[]{\label{}
\begin{tikzpicture}[darkstyle/.style={circle,draw,fill=blue!20,minimum size=20}]
\foreach \y in {0,...,3}
\foreach \x in {0,...,3}
{\pgfmathtruncatemacro{\label}{4 * \y + \x}
\node [darkstyle]  (\y\x) at (1.5*\x,4.5-1.5*\y) {\label};}

\foreach \y [count=\yi]  in {0,...,2}
\foreach \x in {0,...,3}
\draw [->,green] (\x\y) -- (\x\yi) ;

% \foreach \i [count=\ii] in {0,...,2}
% \draw [->,green] (\i3) to [in=90, out=-90] (\ii0) ;

\foreach \i [count=\ii] in {0,...,2}
\draw [->,green] (\i3) to [in=45, out=-135] (\ii0) ;
\end{tikzpicture}
}
\subfloat[]{\label{}
\begin{tikzpicture}[darkstyle/.style={circle,draw,fill=blue!20,minimum size=20, outer sep=0pt}]
\foreach \y in {0,...,3}
\foreach \x in {0,...,3}
{\pgfmathtruncatemacro{\label}{4 * \y + \x}
\node [darkstyle]  (\y\x) at (1.5*\x,4.5-1.5*\y) {\label};}

\foreach \y [count=\yi]  in {0,...,2}
\foreach \x in {0,...,3}
\draw [->,green] (\x\y) -- (\x\yi) ;

\draw [->,green] (03) to [in=90, out=-90] (10) ;
\draw [->,green] (13) to [in=90, out=-90] (20) ;
\draw [->,green] (23) to [in=90, out=-90] (30) ;
\end{tikzpicture}
}
\end{figure}
\end{frame}
\end{document}


-
Add the overlay option to the curved paths, at least also for the most top one. The control points of the Bézier curve lies outside of your actual picture and extends the bounding box. – Qrrbrbirlbel Oct 10 '13 at 12:56
@Qrrbrbirlbel Could you specify where I should put overlay exactly? – Albert Netymk Oct 10 '13 at 13:38

Curved paths are constructed with Bézier curves with two control points. TikZ extends the picture’s bounding box automatically by making sure that all points of a path lie inside the bounding box. For straight lines, arcs/circles/ellipses and basic curve, this usually works very good. For

You can visualize the control points (the red crosses) for the paths

\draw (  0,0 ) to    [bend left] (2,2);
\draw ( .5,0 ) arc   [start angle=180, delta angle=-90, radius=1.5];


with TikZ itself, the dashed lines shows the bounding box.
Observe what happens when I rotate the circle about 45 degrees in the right picture.

TikZ doesn’t use the actual path to extend the bounding box but uses (besides the start and target point) the control points.

Now, let’s look at your picture (just to make clear: this also happens without the drawn points I added for this answer):

(Even though, TikZ can actually calculate the points along the path (for nodes) it doesn’t use this for computing the bounding box.)

In your example you don’t see the added space add the top because the picture’s baseline lies at the bottom and this is were TeX aligns the pictures vertically.

Easiest solution in your case would be to add the overlay option to the curved paths (which already lie inside the bounding box created by the nodes anyway):

I’d also advise you to use edges which oftentimes makes the code cleaner:

\path[overlay, ->, green, in=90, out=-90] (03) edge (10)
(13) edge (20)
(23) edge (30);


(I also would use other node names, say n-<row>-<column> so that the node n-12-3 uses another name as the node n-1-23.)

### Appendix

The code for creating the pictures in this answer is

\usetikzlibrary{backgrounds,decorations.pathreplacing,shapes.misc}
\tikzset{
show bezier control points/.style={
postaction={
decoration={name=show path construction,
curveto code={\draw [-, overlay, opacity=1, blue, densely dotted]
(\tikzinputsegmentfirst) -- (\tikzinputsegmentsupporta)
node [at end, cross out, draw, solid, red, inner sep=1pt]{};
\draw [-, overlay, opacity=1, blue, densely dotted]
(\tikzinputsegmentsupportb) -- (\tikzinputsegmentlast)
node [at start, cross out, draw, solid, red, inner sep=1pt]{};}},
decorate}},
every picture/.append style={
inner frame sep=0, framed, thick, opacity=.5,
background rectangle/.append style={thin, dashed}}}


The show bezier control points style can then be used to visualize the Bézier curves (uses the decorations.pathreplacing library and its show path construction decoration as well as the cross out shape from the shapes.misc library. The bounding box is added via backgroundsframed option that adds a rectangle at the end of the picture.

-

You can fake such curve by defining a path on the same way on the left figure using

\path [->,green] (23) to [in=90, out=-90] (30) ;


so you will have the same height. The \path command will move the pen along the curve, but won't draw anything. The curve will be "invisible'.

-
I could fix the problem using your solution. Thank you for this. Is it possible to provide some explanation? For me, it's not necessary to move it up, for the space is enough, as far as I am concerned. – Albert Netymk Oct 10 '13 at 13:40
@Henri Menke, thanks for helping with the explanation. – Sigur Oct 10 '13 at 14:59
@AlbertNetymk, does the Henri's comment above help you? – Sigur Oct 10 '13 at 14:59