String 将文本字符串存储在latex中,然后向其中添加其他文本(连接)
我首先定义一个命令来存储字符串“Hello”: 我想附加字符串“world”,但不幸的是,此代码导致错误:String 将文本字符串存储在latex中,然后向其中添加其他文本(连接),string,latex,String,Latex,我首先定义一个命令来存储字符串“Hello”: 我想附加字符串“world”,但不幸的是,此代码导致错误: \renewcommand{\textstring}{\textstring world} 您可以使用\expandafter完成此操作。例如: % redefine \textstring by appending " world" to it \expandafter\def\expandafter\textstring\expandafter{\textstring { }worl
\renewcommand{\textstring}{\textstring world}
您可以使用
\expandafter
完成此操作。例如:
% redefine \textstring by appending " world" to it
\expandafter\def\expandafter\textstring\expandafter{\textstring { }world}
如果不使用\expandafter
,则最终会出现递归问题。您可以阅读更多信息。问题在于,这会覆盖\textstring
的定义,而不是引用旧的定义。为了附加,标准的方法是使用TeX命令\edef
,它在分配某些内容之前扩展定义。因此,如果你有
\def\textstring{Hello} % Or with \newcommand
\edef\textstring{\textstring{} world}
LaTeX会将
\edef
的右侧更改为Hello world
,然后将其重新分配给\textstring
,这正是您想要的。相反,在您当前的版本中,\newcommand
不会展开右侧,因此当您使用\textstring
时,它会展开到\textstring world
,它本身也会展开到\textstring world
,这本身就扩展到……你明白了。类似于大卫·安德希尔的答案如下
\newcommand{\textstring}{Hello}
\makeatletter
\g@addto@macro\textstring{ world}
\makeatother
g@addto@宏
macro实现了相同的效果,并且可能会生成可读性稍高的代码(特别是如果您的代码在包/样式中,或者您已经处于\makeatletter
&\makeatother
的情况下)使用此问题的输入生成
\edef\history{ }
\newcommand{\historyAdd}[1]{\edef\history{\history{}#1 }}
\newcommand{\historyAddEcho}[1]{#1\historyAdd{#1}}
The history was:
\historyAddEcho{Hi brian}
\historyAdd{you idiot}
\historyAddEcho{how are you?}
\lipsum[3]
The real history was: \history
(对不起,布莱恩,但这是我能想到的最能说明问题的例子)
scructure可以帮助您创建一个简单的待办事项列表,如下所示:
\lipsum[1]
\historyAdd{\\work more with: }
\section{\historyAddEcho{Introduction}}
\lipsum[1]
\historyAdd{\\work more with the text on page \thepage}
\lipsum[1]
\section{ToDo:}
\history
希望这能帮助有人尝试为这个目的连接字符串 当要添加的文本包含命令时,此方法似乎有问题,它生成了许多错误。此方法工作正常(即使追加的文本包含命令)。但现在我又遇到了另一个问题:当我在文本中使用\textstring时,它会显示“Hello world”,但我必须在生成空尾注的\endnotetext[\value{enumi}]{\textstring}中使用它。
\lipsum[1]
\historyAdd{\\work more with: }
\section{\historyAddEcho{Introduction}}
\lipsum[1]
\historyAdd{\\work more with the text on page \thepage}
\lipsum[1]
\section{ToDo:}
\history