# Tikz matrix vertical alignment

I'm having trouble with the vertical alignment of the nodes in a tikz matrix. I have two plots next to each other. I want them to be aligned with respect to the center of their bounding boxes, but they end up aligned with respect to the zero coordinate (the abscissa in this case). Based on other questions (e.g., Aligning matrix nodes in tikz), I've tried with various combinations of align=base, align=center, anchor=center, etc, without success.

Below is the code and corresponding output.

\documentclass{article}
\usepackage{tikz}

\begin{document}

\begin{figure}[h!]
\centering
\begin{tikzpicture}
\matrix [column sep=1cm, row sep=1cm, cells={xscale=0.7, yscale=2},
align=center, nodes={rectangle, anchor=center, align=center}]
{
\draw [->] (-4.2,0) -- (4.2,0);
\draw [->] (0,0) -- (0,1.3);
\draw [blue, ultra thick, domain=-4:4, samples=100] plot
(\x, {1/sqrt(1 + \x*\x)});
&
\draw [->] (-4.2,0) -- (4.2,0);
\draw [->] (0,-0.6) -- (0,0.6);
\draw [blue, ultra thick, domain=-4:4, samples=100] plot
(\x, {-0.4*pi*atan(\x)/180});
\\
};
\end{tikzpicture}
\caption{How to align?}
\end{figure}
\end{document} Any idea about how to vertically align the center of the cell's bounding boxes?

• A quick fix is to change both y-axis to (0,-0.6) -- (0,1.2); – Jesse Nov 19 '13 at 12:28
• @Jesse Thanks. I would also need to offset the plot. And then I would need to change the offset every time I change the plot. – Alejandro Nov 19 '13 at 12:32
• If a non-tikz solution may be proposed, I'd split the figures and deal with the subcaption package which allows you to align figures in the center, top or bottom. This is particulary easy as all your plots have the same dimensions (height x width) – s__C Nov 19 '13 at 12:35
• The problem is that y=0 is the same in both plots because they are part of the same picture. So one approach would be to separate them into two separate tikzpictures and simply put them inside a \makebox separated by a \qquad space. – Thruston Nov 19 '13 at 12:41

You don't need a matrix, just declare each graphic as a separate tikzpicture which will be treated as any other box in LaTeX. No need for \makebox.

\documentclass{article}
\usepackage{tikz}

\begin{document}

\begin{figure}[h!]
\centering
\begin{tikzpicture}[xscale=0.7,yscale=2]
\draw [->] (-4.2,0) -- (4.2,0);
\draw [->] (0,0) -- (0,1.3);
\draw [blue, ultra thick, domain=-4:4, samples=100] plot
(\x, {1/sqrt(1 + \x*\x)});
\end{tikzpicture}
\hfill
\begin{tikzpicture}[xscale=0.7,yscale=2]
\draw [->] (-4.2,0) -- (4.2,0);
\draw [->] (0,-0.6) -- (0,0.6);
\draw [blue, ultra thick, domain=-4:4, samples=100] plot
(\x, {-0.4*pi*atan(\x)/180});
\end{tikzpicture}
\caption{How to align?}
\end{figure}
\end{document} • Thanks! I just replaced the hfill by hspace{2em} to avoid a large empty space in between the plots. – Alejandro Nov 19 '13 at 16:51

You can achieve the alignment by using the tikzlibrary matrix, adding matrix of nodes to your matrix options, and drawing your plots as nested tikzpictures.

Here the full code

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}
\begin{figure}[h!]
\centering
\begin{tikzpicture}
\matrix [matrix of nodes,column sep=1cm, row sep=1cm, align=center,
nodes={rectangle, anchor=center, align=center}]
{
\begin{tikzpicture}[xscale=0.7, yscale=2]
\draw [->] (-4.2,0) -- (4.2,0);
\draw [->] (0,0) -- (0,1.3);
\draw [blue, ultra thick, domain=-4:4, samples=100] plot
(\x, {1/sqrt(1 + \x*\x)});
\end{tikzpicture}
&
\begin{tikzpicture}[xscale=0.7, yscale=2]
\draw [->] (-4.2,0) -- (4.2,0);
\draw [->] (0,-0.6) -- (0,0.6);
\draw [blue, ultra thick, domain=-4:4, samples=100] plot
(\x, {-0.4*pi*atan(\x)/180});
\end{tikzpicture}
\\
};
\end{tikzpicture}
\caption{How to align?}
\end{figure}
\end{document}


And the result • Nesting tikzpictures has its own casualties :-) – user11232 Nov 19 '13 at 12:48
• Yes, I agree, but here it just works fine (as far as I see). In addition you do not have to adjust the pictures manually and to compute the yshift by yourself, although this is easy in this example. – crixstox Nov 19 '13 at 13:02
• @HarishKumar: besides compilation time, what are the major casualities of nesting TikZ pictures? – Sergio Parreiras May 28 '14 at 13:46

You can put the first graph inside a scope and lower it by -.65cm (i.e., to the mid point of y-axis).

\documentclass{article}
\usepackage{tikz}

\begin{document}

\begin{figure}[h!]
\centering
\begin{tikzpicture}
\matrix [column sep=1cm, row sep=1cm, cells={xscale=0.7, yscale=2},
align=center, nodes={rectangle, anchor=center, align=center}]
{
\begin{scope}[yshift=-0.65cm]
\draw [->] (-4.2,0) -- (4.2,0);
\draw [->] (0,0) -- (0,1.3);
\draw [blue, ultra thick, domain=-4:4, samples=100] plot
(\x, {1/sqrt(1 + \x*\x)});
\end{scope}
&
\draw [->] (-4.2,0) -- (4.2,0);
\draw [->] (0,-0.6) -- (0,0.6);
\draw [blue, ultra thick, domain=-4:4, samples=100] plot
(\x, {-0.4*pi*atan(\x)/180});
\\
};
\end{tikzpicture}
\caption{How to align?}
\end{figure}
\end{document} 