# Aligning subcaptions with tikz pictures vertically

A simple follow-up question to this one How to place and scale TikZ pictures in subfigure? I shamelessly copied the same code that was presented in the answer there because it fits my needs exactly as well.

Maybe the code need not to be repeated here, as it can be seen from the link above. This is what it outputs anyway. (Would have inserted the image here, but seems like I couldn't as a new user, please see the link for that also).

Looks nice and all, but I would like to have only "(a)", "(b") and so forth as my subcaptions. I can do this for example by just modifying the subfigure-lines to "\subfigure []". When I do this, the letters below the pictures don't align vertically with the first node (the root of the "tree" in the picture). I tried adding a label to the tikzpicture below/above a node, but it didn't really work out and it felt very hackish anyway. What would be an easy to way to accomplish this?

-
One possible way would be to introduce extra "empty" nodes in each subfigure: \node[draw=none] (E) [below left of = B] {\phantom{$\boldsymbol{2}$}};. – Gonzalo Medina Jun 3 '11 at 19:54
Or explicitly setting the bounding box. – Caramdir Jun 3 '11 at 20:23

Two options:

The first (which I prefer) uses the subcaption package to generate the subcaption in a bax in a node inside the tikzpicture, which gives you a lot of flexibility (you can put the subcaption anywhere in the picture to make better use of empty areas, for example). This won't work if you need to use the subfigure package or depend on \resizebox to scale your whole picture, as this will scale the subcaption as well.

Creating the node to hold the subcaption can be wrapped in a command like

\newcommand{\alignedsublabel}[2]{%
\node at ($(current bounding box.south west)!(#1)!(current bounding box.south east)$) [anchor=base,text depth=0pt,yshift=-3ex] {\parbox{10em}{\subcaption[]{#2}}};%
}


Here's a simplified example of the setup in the question you linked to:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{caption,subcaption}

\usetikzlibrary{arrows,automata}

\newcommand{\alignedsublabel}[1]{%
\node at ($(current bounding box.south west)!(#1)!(current bounding box.south east)$) [anchor=base,text depth=0pt,yshift=-3ex] {\parbox{10em}{\subcaption[]{}}};%
}

\begin{document}

\begin{figure}[htbp]
\begin{tikzpicture}[node distance=2.0cm,semithick]
\node[state] (A) {1};
\node[state] (B) [below left  of = A] {2};
\node[state] (C) [below right of = A] {3};
\node[state] (D) [below right of = C] {4};
\draw [gray,ultra thick] (current bounding box.north west) rectangle (current bounding  box.south east);
\alignedsublabel{A.center}{}
\end{tikzpicture}
%
\hfill
\begin{tikzpicture}[node distance=2.0cm,semithick]
\node[state] (A) {1};
\node[state] (B) [below left  of = A] {2};
\node[state] (C) [below right of = A] {3};
\node[state] (D) [below right of = C] {4};
\draw [gray,ultra thick] (current bounding box.north west) rectangle (current bounding  box.south east);
\alignedsublabel{A.center}
\end{tikzpicture}
\caption{Several options}
\end{figure}

\end{document}


If you do depend on the \resizebox stuff, you need to keep the label outside the tikzpicture. In order to get the alignment right, the most accurate thing to do is to extend the bounding box of the tikzpicture so it is symmetric around a specified center. This can be done by specifying a path at the end of the tikzpicture which can be calculated with the calc library. The drawback is that this introduces additional empty space on the side of the picture:

\documentclass{article}

\usepackage{tikz}
\usepackage{subfigure}
\usepackage{array}
\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}

\usetikzlibrary{calc,automata}

\newcommand{\centeraround}[1]{
\path (#1) -- +($($(current bounding box.east)!(#1)!(current bounding box.west)$) - (current bounding box.east)$);
}

\begin{document}

\begin{figure}[htbp]

\begin{tabular}{C{.48\textwidth}C{.48\textwidth}}
\subfigure [] {
\resizebox{0.4\textwidth}{!}{%
\begin{tikzpicture}[node distance=2.0cm,semithick]
\node[state] (A) {1};
\node[state] (B) [below left  of = A] {2};
\node[state] (C) [below right of = A] {3};
\node[state] (D) [below right of = C] {4};
\draw [gray,ultra thick] (current bounding box.north west) rectangle (current bounding  box.south east);

\centeraround{A.center}
\end{tikzpicture}
}
} &
\subfigure [] {
\resizebox{0.4\textwidth}{!}{%
\begin{tikzpicture}[node distance=2.0cm,semithick]
\node[state] (A) {1};
\node[state] (B) [below left  of = A] {2};
\node[state] (C) [below right of = A] {3};
\node[state] (D) [below right of = C] {4};
\draw [gray,ultra thick] (current bounding box.north west) rectangle (current bounding  box.south east);

\centeraround{A.center}
\end{tikzpicture}
}
} \\
\end{tabular}
\caption{Several options}
\end{figure}

\end{document}

-
Thanks, I used the first one and it compiled and worked in a separate document. Using it in my "main document" however resulted in "Missing number, treated as zero". Any ideas? Apparently it is the result of using \alignedsublabel. – Rask Jun 5 '11 at 9:38
@Rask: That's really hard to answer without more details. There should be more info in the .log file. You might want to make a minimal example of it that still produces the error and post that as a new question. – Jake Jun 5 '11 at 11:27