![将字符串附加到变量并将其显示在文档的前面?](https://linux22.com/image/365817/%E5%B0%86%E5%AD%97%E7%AC%A6%E4%B8%B2%E9%99%84%E5%8A%A0%E5%88%B0%E5%8F%98%E9%87%8F%E5%B9%B6%E5%B0%86%E5%85%B6%E6%98%BE%E7%A4%BA%E5%9C%A8%E6%96%87%E6%A1%A3%E7%9A%84%E5%89%8D%E9%9D%A2%EF%BC%9F.png)
对此不太确定 - 但有些想法会有所帮助,我想知道如何附加字符串,然后在最后显示开头的字符串?这可能吗?有什么例子吗?
例如, ...
\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}