# Align tikzpictures at internal bounding box with text below it

I want to align the bottom of the red bounding boxes, but can't seem to find the right combination of baseline and anchor options to get this to work.

The failing test case of the MWE below is the case where the number of lines in the text differs from the two tikzpictures:

## Notes:

• Ideally I would prefer to add code only to the \DrawBoundingBox if at all possible.

## Code:

\documentclass{article}
\usepackage{tikz}
\usepackage{xstring}

\newcommand*{\DrawBoundingBox}[1][]{%
\draw [
%anchor=base,
red,
]
([shift={(-5pt,-5pt)}]current bounding box.south west)
rectangle
([shift={(5pt,+5pt)}]current bounding box.north east);

%\useasboundingbox (current bounding box.south west) rectangle (current bounding box.north east);

\IfStrEq{#1}{}{}{% 5.0em selected to get failing test case
\node [below, anchor=north, text width=5.0em, align=center,
%text depth=0pt,% 1. Bottom almost aligns (but slightly off), overlaps following text.
%yshift=-\baselineskip,% 2. This along with applying a \smash, results in a single line text.
baseline=0pt,
]
at (current bounding box.south)
%{\smash{#1\strut}};%% Used with yshift=-\baselineskip option.
{#1\strut};
}%
}

\newcommand*{\MyRectangle}[1][]{%
\begin{tikzpicture}
\draw [fill=gray!20, draw=black] (0,0) rectangle (2,3);
\DrawBoundingBox[#1]
\end{tikzpicture}%
}

\newcommand*{\MyCircle}[1][]{%
\begin{tikzpicture}
\draw [fill=yellow!20, draw=black] (0,0) circle (1.0cm);
\DrawBoundingBox[#1]
\end{tikzpicture}%
}

\begin{document}
\section{No text}
\MyCircle~%
\MyRectangle%

\section{Single line text}
\MyCircle[Circle]~%
\MyRectangle[Not Square]%

\section{With multi line text}
\MyCircle[Circle]~%
\MyRectangle[Not quite a square]%

\end{document}


The baseline option is usually a tikzpicture option but you can use it directly into your tikzpicture via \tikzset. Here, I define the X coordinate as the alignment point into your \DrawBoundingBox macro.

\documentclass{article}
\usepackage{tikz}
\usepackage{xstring}
\pagestyle{empty}
\newcommand*{\DrawBoundingBox}[1][]{%
\draw [red]
([shift={(-5pt,-5pt)}]current bounding box.south west)
rectangle
([shift={(5pt,+5pt)}]current bounding box.north east);

\coordinate (X) at (current bounding box.south);
\tikzset{baseline={(X)}} % X is the alignment point

\IfStrEq{#1}{}{}{% 5.0em selected to get failing test case
\node [below, anchor=north, text width=5.0em, align=center,
]
at (current bounding box.south)
{#1\strut};
}%
}

\newcommand*{\MyRectangle}[1][]{%
\begin{tikzpicture}
\draw [fill=gray!20, draw=black] (0,0) rectangle (2,3);
\DrawBoundingBox[#1]
\end{tikzpicture}%
}

\newcommand*{\MyCircle}[1][]{%
\begin{tikzpicture}
\draw [fill=yellow!20, draw=black] (0,0) circle (1.0cm);
\DrawBoundingBox[#1]
\end{tikzpicture}%
}

\begin{document}
\section{No text}
\MyCircle~%
\MyRectangle%

\section{Single line text}
\MyCircle[Circle]~%
\MyRectangle[Not Square]%

\section{With multi line text}
\MyCircle[Circle]~%
\MyRectangle[Not quite a square]%


• Looks good. I though that baseline was a \node option. – Peter Grill Mar 5 '18 at 6:44
• @PeterGrill There's a base anchor for nodes. – cfr Mar 6 '18 at 0:55
• @cfr: Yeah, I think I got baseline and base mixed up. – Peter Grill Mar 6 '18 at 1:57
• Is below argument necessary ? – Tarass Mar 6 '18 at 8:38
• @Tarass The below option without argument is redundant with anchor=north. – Paul Gaborit Mar 6 '18 at 8:54