# Why is picture and matrix not drawn at the same height

I'm trying to put a adjacency matrix beneath a graph using subfloat. The graph is drawn with tikz. No matter what I do, the matrix is put below the graph as shown here:

The code is here:

\begin{figure}[h]
\centering
\subfloat{
\begin{tikzpicture}[-,>=stealth',shorten >=1pt,auto,node distance=2cm,
thick,main node/.style={circle,fill=black!20,draw}]

\node[main node] (1) {$v_2$};
\node[main node] (2) [below left of=1] {$v_1$};
\node[main node] (3) [below right of=1] {$v_3$};
\node[main node] (4) [below of=3] {$v_4$};
\node[main node] (5) [below of=2] {$v_5$};

\path[every node/.style={font=\sffamily\small}]
(2) edge node [left] {} (1)
(2) edge node [left] {} (3)
(1) edge node [left] {} (5)
(1) edge node [left] {} (3)
(3) edge node [left] {} (4)
(4) edge node [left] {} (5)
;
\end{tikzpicture}}
\subfloat{
\begin{math}
A_{G} = \left(
\begin{array}{ccccc}
0 & 1 & 1 & 0 & 0 \\
1 & 0 & 1 & 0 & 1 \\
1 & 1 & 0 & 1 & 0 \\
0 & 0 & 1 & 0 & 1 \\
0 & 1 & 0 & 1 & 0
\end{array}
\right)
\end{math}
}
\caption{\textbf{Sample graph and adjacency matrix.} Example graph with 5 vertices and 5 edges and its adjacency matrix $A_{G}$}
\label{fig: example graph for modularity calculation}
\end{figure}


Does anybody have an idea, why this happens and how I can prevent it?

• the math matrix is vertically centred on the row but the basleine of the tikz picture is at its bottom so they are at the same height but the alignment points are not as you would wish. If you vertically align the tikzpicture on its center things will line up. Tikz will have an option for that but I don't know it, sorry:-) – David Carlisle Nov 23 '12 at 14:58
• the option to be added to tikzpicture is baseline=(current bounding box.center). But can also be any other coordinate for positioning. – percusse Nov 23 '12 at 15:13
• @percusse thank you so much, your suggestion helped... can I give you any Kudos for the answer? – Michael Lihs Nov 23 '12 at 15:18

It is a problem of baseline's alignment. The two subfigures are aligned at their base lines, and while the tikzpicture has its base line at the bottom of its bounding box, the math environment has its baseline at its center (this always happens with math environments).

So you have two solutions:

1. Move the baseline of the tikzpicture to an upper position. This requires to add baseline=(some point coordinates) to your tikzpicture environment. You can use as coordinates any of the nodes in your graph. Remember that the center of the matrix will be aligned with the horizontal line which passs trough that node. For example:

\begin{tikzpicture}[-,>=stealth',shorten >=1pt,auto,node distance=2cm,
thick,main node/.style={circle,fill=black!20,draw}, baseline= (3)]


will align the center of the matrix with (the center of) your $v_3$ node:

You can also use anchors of the nodes:

\begin{tikzpicture}[-,>=stealth',shorten >=1pt,auto,node distance=2cm,
thick,main node/.style={circle,fill=black!20,draw}, baseline= (3.south)]


You can even use interpolated coordinates (\usetikzlibrary{calc} is required).

\begin{tikzpicture}[-,>=stealth',shorten >=1pt,auto,node distance=2cm,
thick,main node/.style={circle,fill=black!20,draw}, baseline= ($(3)!.5!(4)$)]


2. Put the baseline of the math environment at its bottom. This involves trickery with \vbox, since the baseline of a \vbox is always the baseline of its last inner \hbox, so we can create a \vbox containing two \hboxes, the first one being your matrix and the second one a null \hbox. In this case you don't need to give any baseline option to the tikz figure. The complete code for this case is:

    \begin{figure}[h]
\centering
\subfloat{
\begin{tikzpicture}[-,>=stealth',shorten >=1pt,auto,node distance=2cm,
thick,main node/.style={circle,fill=black!20,draw}]

\node[main node] (1) {$v_2$};
\node[main node] (2) [below left of=1] {$v_1$};
\node[main node] (3) [below right of=1] {$v_3$};
\node[main node] (4) [below of=3] {$v_4$};
\node[main node] (5) [below of=2] {$v_5$};

\path[every node/.style={font=\sffamily\small}]
(2) edge node [left] {} (1)
(2) edge node [left] {} (3)
(1) edge node [left] {} (5)
(1) edge node [left] {} (3)
(3) edge node [left] {} (4)
(4) edge node [left] {} (5)
;
\end{tikzpicture}}
\subfloat{
\vbox{\hbox{
\begin{math}
A_{G} = \left(
\begin{array}{ccccc}
0 & 1 & 1 & 0 & 0 \\
1 & 0 & 1 & 0 & 1 \\
1 & 1 & 0 & 1 & 0 \\
0 & 0 & 1 & 0 & 1 \\
0 & 1 & 0 & 1 & 0
\end{array}
\right)
\end{math}
}% end of first hbox
\null% last null hbox, which sets the baseline of the \vbox
} % end of vbox
} % end of subfloat
\caption{\textbf{Sample graph and adjacency matrix.} Example graph with 5 vertices and 5 edges and its adjacency matrix $A_{G}$}
\label{fig: example graph for modularity calculation}
\end{figure}


Resulting in:

• the baseline of the tex math expression is not at its precise vertical center. it is at the baseline of the "main line" of the expression. (in the example given, it is at the baseline of the $A_G$.) – barbara beeton Nov 23 '12 at 19:26

Here's another option using the subcaption package, and borrowing ideas from

The idea is to measure the height of the biggest subfloat, and then use \vfill to center the other subfloat vertically.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows}
\usepackage{subcaption}

\newsavebox{\tempbox}

\begin{document}

\begin{figure}
% store the bigger of the two pictures in a vbox
\sbox{\tempbox}{%
\begin{tikzpicture}[-,>=stealth',shorten >=1pt,auto,node distance=2cm,
thick,main node/.style={circle,fill=black!20,draw}]

\node[main node] (1) {$v_2$};
\node[main node] (2) [below left of=1] {$v_1$};
\node[main node] (3) [below right of=1] {$v_3$};
\node[main node] (4) [below of=3] {$v_4$};
\node[main node] (5) [below of=2] {$v_5$};

\path[every node/.style={font=\sffamily\small}]
(2) edge node [left] {} (1)
(2) edge node [left] {} (3)
(1) edge node [left] {} (5)
(1) edge node [left] {} (3)
(3) edge node [left] {} (4)
(4) edge node [left] {} (5)
;
\end{tikzpicture}
}
\begin{subfigure}{.5\textwidth}
\centering
\usebox{\tempbox}
\caption{First caption}
\end{subfigure}%
\begin{subfigure}{.5\textwidth}
\centering
\vbox to\ht\tempbox{
\vfill
\begin{math}
A_{G} = \left(
\begin{array}{ccccc}
0 & 1 & 1 & 0 & 0 \\
1 & 0 & 1 & 0 & 1 \\
1 & 1 & 0 & 1 & 0 \\
0 & 0 & 1 & 0 & 1 \\
0 & 1 & 0 & 1 & 0
\end{array}
\right)
\end{math}
\vfill
}
\caption{Second caption}
\end{subfigure}%
\caption{Global caption}
\end{figure}

\end{document}