我有:
\newcommand{\test}[1]{\renewcommand{\test}{#1}}
在類別文件中,以使文件本身中的命令設定更清晰。
如果文件包含以下內容,這一切都有效:
\test{some content}
\test
並在文件中使用時輸出「某些內容」 。
但是當我嘗試設定\test
一個新值時
\test{some other content}
後來,它似乎只是輸出 的值\test
,然後輸出「一些其他內容」。
如何停止 LaTeX 擴充功能\test
並實際呼叫 來\renewcommand
更新值?
答案1
的第一次用法\test
將重新定義\test
為不帶參數的命令,該命令僅輸出第一次調用的參數。所以你的建議不會起作用。你必須使用類似的東西:
\newcommand{\test}{}
\newcommand{\settest}[1]{\renewcommand{\test}{#1}}
然後你可以使用
\settest{some content}
\test, \test, \test% shows "some contents" three times
\settest{some other content}
\test, \test% shows "some other contents" two times
您可以使用可選參數,例如,使用xparse
來區分儲存參數和參數輸出:
\documentclass{article}
\usepackage{xparse}
\newcommand*{\testvalue}{}
\NewDocumentCommand{\test}{o}{%
\IfNoValueTF{#1}{\testvalue}{\def\testvalue{#1}}%
}
\begin{document}
Define: \test[some content]
Show: \test, \test
Define: \test[some other contents]
Show: \test, \test.
\end{document}
但這違背了可選參數只能修改命令的預設行為而不能將其更改為完整的其他命令的原則。所以我不建議這樣做。