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
\DrawBoundingBoxif at all possible.
References:
- How to align a series of TikZ pictures at the baseline
- Anchoring TiKZ pics
- Fixed size bounding box around a tikzpicture
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%
Some follow on text.
\section{Single line text}
\MyCircle[Circle]~%
\MyRectangle[Not Square]%
Some follow on text.
\section{With multi line text}
\MyCircle[Circle]~%
\MyRectangle[Not quite a square]%
Some follow on text.
\end{document}


baselinewas a\nodeoption. – Peter Grill Mar 05 '18 at 06:44baseanchorfor nodes. – cfr Mar 06 '18 at 00:55baselineandbasemixed up. – Peter Grill Mar 06 '18 at 01:57belowargument necessary ? – Tarass Mar 06 '18 at 08:38belowoption without argument is redundant withanchor=north. – Paul Gaborit Mar 06 '18 at 08:54