# TikZ: How to center captions below figures in matrix

I have three figures that I want to align in a matrix. Each figure is drawn in a different way and needs a caption that is centered below the corresponding figure.

Here is what I currently have:

As you can see, the centering didn't go too well ;-)

The code that produces the above result is:

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

\begin{document}
\begin{tikzpicture}[grow=right]
\matrix[column sep=1cm] {
\node (R1)  {X};
\node (P1) [right=45pt of R1] {P};
\draw (R1) to (P1);
&
\node {A} child { node {P} } ;
&
\node {X}
child { node {R} }
child { node {R}
child { node {A} }
child { node {A}
child { node {P} }
child { node {P} }
child { node {P} }
}
child { node {A} }
}
child { node {R} }
;
\\
\node {caption}; & \node {other caption}; & \node {more caption};
\\
};
\end{tikzpicture}
\end{document}


How can I center the captions in a non-hacky way using TikZ?
I managed to get the result I need by adding absolute positioning to the labels, but that is a non-sustainable solution.

-

A) With three tikzpicture environments and without matrix

This way seems to work : three figures are aligned with the option baseline and captions are placed inside the code with (current bounding box.base)

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

\begin{document}

\begin{tikzpicture}[baseline]
\node (R1)  {X};
\node (P1) [right=45pt of R1] {P};
\draw (R1) to (P1);
\node[below=2cm] at (current bounding box.base) {caption 1};
\end{tikzpicture}
\hfill
\begin{tikzpicture}[grow=right,baseline]
\node {A} child { node {P} } ;
\node[below=2cm] at (current bounding box.base) {caption 2};
\end{tikzpicture}
\hfill
\begin{tikzpicture}[grow=right,baseline]
\node {X}
child { node {R} }
child { node {R}
child { node {A} }
child { node {A}
child { node {P} }
child { node {P} }
child { node {P} }
}
child { node {A} }
}
child { node {R} } ;
\node[below=2cm] at (current bounding box.base) {caption 3};
\end{tikzpicture}

\end{document}


B) One tikzpicture environment with three scope environments and a matrix

We get the same result

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

\begin{document}
\begin{tikzpicture}
\matrix[column sep=2cm] {
\begin{scope}[baseline]
\node (R1)  {X};
\node (P1) [right=45pt of R1] {P};
\draw (R1) to (P1);
\node[below=2cm] at (current bounding box.base) {caption 1};
\end{scope}
&
\begin{scope}[grow=right,baseline]
\node {A} child { node {P} } ;
\node[below=2cm] at (current bounding box.base) {caption 2};
\end{scope}
&
\begin{scope}[grow=right,baseline]
\node {X}
child { node {R} }
child { node {R}
child { node {A} }
child { node {A}
child { node {P} }
child { node {P} }
child { node {P} }
}
child { node {A} }
}
child { node {R} } ;
\node[below=2cm] at (current bounding box.base) {caption 3};
\end{scope}
\\};
\end{tikzpicture}
\end{document}

-
The use of scope is interesting. I tried to use it myself with the matrix solution to get the subfigures centered, but I couldn't find out how. The (current bounding box.base) is an interesting trick. –  Ruben Verborgh Mar 27 '13 at 8:06
@RubenVerborgh Not exactly a trick it's a feature. baseline is also interesting to know. You need to know how to place objects with Tikz without the use of raw coordinates. Tikz gives you anchors for the nodes, the bounding box, the page etc. I actually work about notes on Tikz and it's a big part of the notes. –  Alain Matthes Mar 27 '13 at 8:24

One possibility would be to name some appropriate nodes in each cell of the first row, then use the fit library to build some framing nodes and finally place the captions with respect to these new nodes:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{fit,positioning}

\begin{document}
\begin{tikzpicture}[grow=right]
\matrix[column sep=1cm] {
\node (R1)  {X};
\node (P1) [right=45pt of R1] {P};
\draw (R1) to (P1);
\node[inner sep=0pt,fit= (R1) (P1)]  (box1) {}; &
\node (root) {A} child { node (child) {P} } ;
\node[inner sep=0pt,fit= (root) (child)]  (box2) {};
&
\node (lroot) {X}
child { node {R} }
child { node {R}
child { node {A} }
child { node {A}
child { node {P} }
child { node (lchild) {P} }
child { node {P} }
}
child { node {A} }
}
child { node {R} }
;
\node[inner sep=0pt,fit= (lroot) (lchild)]  (box3) {};
\\
\node[below=of box1.center] {caption}; & \node[below=of box2.center] {other caption}; & \node[below=of box3.center] {more caption};
\\
};
\end{tikzpicture}
\end{document}


-
I see, great suggestion. Would there be any way to do this without manually assigning center nodes, i.e., saying to TikZ "the center of this subpicture"? –  Ruben Verborgh Mar 26 '13 at 22:14

It seems to me that your tikzpictures can actually be separated since there's no co-dependence between them. As such, I would use the subcaption package's subfigure environment as follows.

The idea is one that I have used quite a few times before:

• measure the biggest picture
• then use its height to justify the smaller pictures vertically.

Here's the complete MWE:

% arara: pdflatex
% !arara: indent: {overwrite: true}
\documentclass{article}
\usepackage{geometry}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usepackage{subcaption}
\newsavebox{\tempbox}

\begin{document}

% store the biggest picture
% so that we can measure it
\sbox{\tempbox}{%
\begin{tikzpicture}[grow=right]
\node {X}
child { node {R} }
child { node {R}
child { node {A} }
child { node {A}
child { node {P} }
child { node {P} }
child { node {P} }
}
child { node {A} }
}
child { node {R} };
\end{tikzpicture}%
}

\begin{figure}[!htb]
\centering
\begin{subfigure}{.33\textwidth}
\centering
\vbox to\ht\tempbox{%
\vfill
\begin{tikzpicture}[grow=right]
\node (R1)  {X};
\node (P1) [right=45pt of R1] {P};
\draw (R1) to (P1);
\end{tikzpicture}
\vfill
}
\caption{caption}
\end{subfigure}%
\begin{subfigure}{.33\textwidth}
\centering
\vbox to\ht\tempbox{%
\vfill
\begin{tikzpicture}[grow=right]
\node {A} child { node {P} } ;
\end{tikzpicture}
\vfill
}
\caption{other caption}
\end{subfigure}%
\begin{subfigure}{.33\textwidth}
\centering
% use the save box
\usebox{\tempbox}
\caption{more caption}
\end{subfigure}
\end{figure}
\end{document}

-
This is a valid solution and easier from a LaTeX perspective. The only downside seems that you indeed have to measure the largest figure beforehand. However, for this question, I'm specifically interested in a TikZ solution. I will clarify that in the question. –  Ruben Verborgh Mar 27 '13 at 8:09
@RubenVerborgh fair enough :) In my mind, as soon as you say caption I automatically want to label and ref it though- this is easy with my solution, much harder with the others :) –  cmhughes Mar 27 '13 at 14:41