将 tikzpictures 与内部边界框及其下方的文本对齐

将 tikzpictures 与内部边界框及其下方的文本对齐

我想对齐红色边界框的底部,但似乎找不到正确的组合baselineanchor选项来实现这个功能。

下面的 MWE 测试失败的案例是文本中的行数与两者不同的情况tikzpicture

在此处输入图片描述

笔记:

  • 理想情况下,我宁愿只在\DrawBoundingBox可能的情况下添加代码。

参考:

代码:

\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}

答案1

baseline选项通常是一个tikzpicture选项,但您可以直接将其用于您的tikzpicturevia中\tikzset。在这里,我将X坐标定义为宏中的对齐点\DrawBoundingBox

\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}

在此处输入图片描述

相关内容