6

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:

enter image description here

Notes:

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

References:

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}

Peter Grill
  • 223,288

1 Answers1

7

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%

    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}

enter image description here

Paul Gaborit
  • 70,770
  • 10
  • 176
  • 283