为什么此代码不能存储/使用值?

为什么此代码不能存储/使用值?

我写了这段代码

\documentclass{article}

% make a command called \insertfoo{..} for the user to insert foo
\newcommand{\insertfoo}[1]{%  
\savebox{\boxthatcontainsfoo}{#1}%  
}

% make a command called \showfoo that shows the value for foo
\newcommand{\showfoo}{%
\usebox{\boxthatcontainsfoo}%
}

\insertfoo{somefoo}

\begin{document}
The foo you choose was \showfoo .
end{document}
\end{document}

为什么我会收到编译错误?

答案1

是一个有效的 MWE:

在此处输入图片描述

\documentclass{article}
% make a command called \insertfoo{..} for the user to insert foo
\newsavebox{\boxthatcontainsfoo}
\newcommand{\insertfoo}[1]{%  
  \savebox{\boxthatcontainsfoo}{#1}%  
}

% make a command called \showfoo that shows the value for foo
\newcommand{\showfoo}{%
  \usebox{\boxthatcontainsfoo}%
}
\begin{document}
\insertfoo{somefoo}
The foo you chose was \showfoo.

\insertfoo{some other foo}
The foo you chose was \showfoo.
\end{document}

您需要定义用于存储内容的框(\boxthatcontainsfoo),但\insertfoo{...}在向宏传递参数时也需要使用,而不是\insertfoo(...)

如果需要的话,可以修改\insertfoo为使用(圆)括号来运行。

相关内容