Tikz 多线节点基线

Tikz 多线节点基线

锚点base确实很不错……但是当我们使用多行文本时,我希望能够引用,例如base 1第一行的基点、base 2第二行的基点、base last最后一行的基点……在 tikz 中可以有这个吗?这很实用,可以轻松对齐节点,而不管其内容的高度/深度如何。\strut有时可以通过强制线条具有给定的深度来提供帮助,但它不那么灵活。

在此处输入图片描述

平均能量损失

\documentclass[]{article}

\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}

\NewDocumentCommand{\mySpecialNode}{mm}{
  \node[draw,align=center](foo){#1};
  \node[draw,below=1.5mm of foo]{#2};
}

If I do not specify nodes positioning relative to baseline, adding a letter like ``p'' breakes the vertical alignement of the boxes

\begin{minipage}[t][3cm][t]{2cm}\vspace{0pt}%
  \begin{tikzpicture}
    \mySpecialNode{Abc}{def}
  \end{tikzpicture}
\end{minipage}%
\begin{minipage}[t][3cm][t]{2cm}\vspace{0pt}%
  \begin{tikzpicture}
    \mySpecialNode{Abc}{Correct}
  \end{tikzpicture}
\end{minipage}%
\begin{minipage}[t][3cm][t]{2cm}\vspace{0pt}%
  \begin{tikzpicture}
    \mySpecialNode{Abcp}{Incorrect}
  \end{tikzpicture}
\end{minipage}%

If I use base instead, it works for single line content like:

\RenewDocumentCommand{\mySpecialNode}{mm}{
  \node[draw,align=center,text width=1cm](foo){\small #1};
  \node[draw, below=3mm of foo.base]{#2};
}
\begin{minipage}[t][3cm][t]{2cm}\vspace{0pt}%
  \begin{tikzpicture}
    \mySpecialNode{Abc}{def}
  \end{tikzpicture}
\end{minipage}%
\begin{minipage}[t][3cm][t]{2cm}\vspace{0pt}%
  \begin{tikzpicture}
    \mySpecialNode{Abc}{Correct}
  \end{tikzpicture}
\end{minipage}%
\begin{minipage}[t][3cm][t]{2cm}\vspace{0pt}%
  \begin{tikzpicture}
    \mySpecialNode{Abcp}{Correct!}
  \end{tikzpicture}
\end{minipage}%

But fails on multiline content like:

\begin{minipage}[t][3cm][t]{2cm}\vspace{0pt}%
  \begin{tikzpicture}
    \mySpecialNode{Abc\\DEF}{def}
  \end{tikzpicture}
\end{minipage}%
\begin{minipage}[t][3cm][t]{2cm}\vspace{0pt}%
  \begin{tikzpicture}
    \mySpecialNode{Abc\\GHI}{Correct}
  \end{tikzpicture}
\end{minipage}%
\begin{minipage}[t][3cm][t]{2cm}\vspace{0pt}%
  \begin{tikzpicture}
    \mySpecialNode{Abc\\HTT}{Incorrect}
  \end{tikzpicture}
\end{minipage}%

\newpage
Workaround is to use strut, but not as flexible (e.g. the node does not fit tightly without dirty workarounds like using another node to fit, we can't easily align the last baseline of two nodes that are next to each others with different inner sep with something as simple as \texttt{right=of mynode.last base, anchor=last base}):

\RenewDocumentCommand{\mySpecialNode}{mm}{
  \node[draw,align=center,text width=1cm](foo){\small #1\strut};
  \node[draw, below=3mm of foo]{#2};
}
\begin{minipage}[t][3cm][t]{2cm}\vspace{0pt}%
  \begin{tikzpicture}
    \mySpecialNode{Abc}{def}
  \end{tikzpicture}
\end{minipage}%
\begin{minipage}[t][3cm][t]{2cm}\vspace{0pt}%
  \begin{tikzpicture}
    \mySpecialNode{Abc}{Correct}
  \end{tikzpicture}
\end{minipage}%
\begin{minipage}[t][3cm][t]{2cm}\vspace{0pt}%
  \begin{tikzpicture}
    \mySpecialNode{Abcp}{Correct!}
  \end{tikzpicture}
\end{minipage}%

\begin{minipage}[t][3cm][t]{2cm}\vspace{0pt}%
  \begin{tikzpicture}
    \mySpecialNode{Abc\\DEF}{def}
  \end{tikzpicture}
\end{minipage}%
\begin{minipage}[t][3cm][t]{2cm}\vspace{0pt}%
  \begin{tikzpicture}
    \mySpecialNode{Abc\\GHI}{Correct}
  \end{tikzpicture}
\end{minipage}%
\begin{minipage}[t][3cm][t]{2cm}\vspace{0pt}%
  \begin{tikzpicture}
    \mySpecialNode{Abc\\HTT}{Correct}
  \end{tikzpicture}
\end{minipage}%


\end{document}

相关内容