[Tex/LaTex] TikZ: How to center captions below figures in matrix

captionshorizontal alignmenttikz-pgftikz-trees

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.

Best Answer

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}

enter image description here

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}