绘制节点下方有一条线

绘制节点下方有一条线

我正在尝试在节点下方画一条线。该宏用于标记句子的各个部分。以下是代码;

\documentclass{article}
\usepackage{tikz}


\newcommand{\LabelText}[3]{%
  \begin{tikzpicture}[baseline=(LabelText.base)]
    \node(a) [
      text width=width("{#1}"),
      text centered,
      draw=none,
      thick,
      rectangle,
      inner sep=0pt,
      outer sep=0pt,
      fill={#3},
    ] (LabelText) {#2};
    \path[draw=black, very thick] (a.south west) -- (a.south east);
  \end{tikzpicture}%
}



\newcommand{\padv}[1]{\LabelText{#1}{\strut Adv}{blue!30}}%

\begin{document}
  This is an \padv{adverb}
\end{document}

\path 语句应该在节点下方绘制一条线,但我收到错误“没有已知的名为 a 的形状”。即使没有 \path 语句,代码也能完美编译,但不幸的是没有绘制任何线。

任何帮助均感激不尽。

答案1

使用当前边界框而不是命名节点。为此,你可以将\path命令替换为\draw[very thick](current bounding box.south west) -- (current bounding box.south east);

\documentclass{article}
\usepackage{tikz}

\newcommand{\LabelText}[3]{%
  \begin{tikzpicture}[baseline=(LabelText.base)]
    \node[
      text width=width("{#1}"),
      text centered,
      draw=none,
      thick,
      rectangle,
      inner sep=0pt,
      outer sep=0pt,
      fill={#3},
    ] (LabelText) {#2};
    \draw[very thick](current bounding box.south west) -- (current bounding box.south east);
  \end{tikzpicture}%
}

\newcommand{\padv}[1]{\LabelText{#1}{\strut Adv}{blue!30}}%

\begin{document}
  This is an \padv{adverb}
\end{document}

enter image description here

相关内容