涉及 TikZ 节点的宏不起作用

涉及 TikZ 节点的宏不起作用

我创建的宏遇到了困难;

\documentclass{article}

\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}

\usepackage{tikz}


\newcommand{\LabelText}[3]{%
\begin{tikzpicture}
    \node [text width=\widthof{#1}, text centered, draw=none, thick, rectangle, inner sep = 0pt, outer sep = 0pt, fill=#3]
        {#2};
\end{tikzpicture}%
}


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


\begin{document}

This is an \padv{adverb}


\end{document}

我在语言学课上用它来标注词性。错误是

./MWE for labeltext.tex:22: Undefined control sequence. [This is an 
\padv{adverb}]
./MWE for labeltext.tex:22: Package PGF Math Error: Unknown function `adverb' (in '{adverb}'). [This is an \padv{adverb}]
./MWE for labeltext.tex:22: Undefined control sequence. [This is an \padv{adverb}]
./MWE for labeltext.tex:22: Package PGF Math Error: Unknown function `adverb' (in '{adverb}'). [This is an \padv{adverb}]

多年来我一直成功地使用这个宏,但由于某种原因,它突然停止工作了。提前致谢。

答案1

\widthof不属于 TikZ,而是属于包。TikZ 提供了用于测量引号中的文本字符串的calc数学函数:width

\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};
  \end{tikzpicture}%
}
\newcommand{\padv}[1]{\LabelText{#1}{\strut Adv}{blue!30}}%

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

结果

相关内容