如何使用花括号作为命令输出的一部分?

如何使用花括号作为命令输出的一部分?

有一项练习我本来打算在纸上做,但出于某种原因我坚持用 LaTeX 做。不过这需要一段时间,因此我想制作一个或多个命令来缩写某些内容。总的来说,我认为这样做很有用。不过我
很快就陷入困境。我在 tikzpicture 环境中有以下内容: \node (square) {\textit{square}} ;,它产生了所需的结果。当我创建一个命令\newcommand{\testa}[1]{ \textit{#1} }并使用时\node (square) {\testa{square}} ;,它仍然有效。但是当我创建一个命令\newcommand{\testb}[1]{ { \textit{#1} } }并使用时\node (square) \testb{square} ;,我得到“Package tikz Error: A node must have a (possible empty) label text.”。
这是怎么回事,我可以修复它吗?如果可以,怎么修复?
谢谢。:) 如果我知道如何修复这个问题,我可以扩展命令以便\node \testc{square}产生所需的结果。

答案1

您可以使用一些扩展技巧来实现这一点,但这可能不是您想要的,因为它比明确写出来更为冗长。

\documentclass{article}
\usepackage{tikz}
\newcommand{\testb}[1]{{\textit{#1}}}
\begin{document}
\begin{tikzpicture}
  \expanded{\noexpand\node (square) \unexpanded\expandafter{\testb{square}};}
\end{tikzpicture}
\end{document}

您还可以尝试一下这个未经充分测试的内部 TikZ 代码补丁。它会在扩展链中引入大量开销,并产生难以理解且可能难以恢复的错误。

\documentclass{article}
\usepackage{tikz}

\makeatletter
\let\tikz@@scan@fig@normal\tikz@@scan@fig
\def\tikz@@scan@fig{%
  \pgfutil@ifnextchar\space
    {\errmessage{Can't happen!}}%
    {\expandafter\tikz@@scan@fig@normal\romannumeral-`0}}
\makeatother

\newcommand{\testb}[1]{ {\textit{#1}} }
\newcommand{\testc}[1]{ (#1) {\textit{#1}} }

\begin{document}
\begin{tikzpicture}
  \node (square) \testb{square};

  \node[draw] at (2,0) \testc{rectangle};
\end{tikzpicture}
\end{document}

相关内容