将字符串附加到变量并将其显示在文档的前面?

将字符串附加到变量并将其显示在文档的前面?

对此不太确定 - 但有些想法会有所帮助,我想知道如何附加字符串,然后在最后显示开头的字符串?这可能吗?有什么例子吗?

例如, ...

\documentclass{article}

\def\mystring{} % temp string
\def\myappendstring#1{ \myappendstring#1 }

\begin{document}

\mystring % how to display the final string here last? e.g., 'once upon a time'

Text...

\myappendstring{Once}

Text ...

\myappendstring{upon}

\myappendstring{a}

\myappendstring{time}

\end{document}

答案1

系统宏\g@addto@macro将第二个参数附加到第一个参数引用的宏。

将其与写入辅助文件相结合,最终结果如下

\relax
\@writefile{xyz}{\gdef \thefinalstring { Once upon a time}}

写入\gdef \thefinalstring { Once upon a time}文件.xyz

以下是 MWE:

\documentclass{article}

\def\mystring{} % temp string
\def\thefinalstring{} % Saved in aux file
\makeatletter
\def\myappendstring#1{\g@addto@macro\mystring{ #1}}
\makeatother

\begin{document}
\makeatletter
\@starttoc{xyz}% executes stuff in .xyz file
\makeatother
\thefinalstring % display the final string here 

Text...

\myappendstring{Once}

Text ...

\myappendstring{upon}

\myappendstring{a}

\myappendstring{time}

\addtocontents{xyz}{\gdef\protect\thefinalstring{\mystring}}

\end{document}

在此处输入图片描述

答案2

无需在文档中附加任何宏(即\begin{document}...之间\end{document}):

\documentclass{article}

\def\mystring{} % temp string
\long\def\addto#1#2{\expandafter\def\expandafter#1\expandafter{#1#2}}
\def\myappendstring#1{\immediate\write\csname @auxout\endcsname
   {\global\noexpand\addto\noexpand\mystring{ #1}}}

\begin{document}

\mystring % how to display the final string here last? e.g., 'once upon a time'

Text...

\myappendstring{Once}

Text ...

\myappendstring{upon}

\myappendstring{a}

\myappendstring{time}

\end{document}

相关内容