如何收集文本并将它们放在一起?

如何收集文本并将它们放在一起?

我正在尝试创建两个命令:

  1. \collect存储字符串/文本的命令。
  2. \callcollection调用放置在命令之前的所有这些存储文本的命令(无需任何配置,只需打印文本内容)。

我试图模仿parnote 包装策略,但我很难适应它。(而且我也不理解“收集笔记”并随后调用它们的机制。)

基本上,我所有的尝试都失败了,我不知道发生了什么......

这里有一个虚拟的 MWE:

\documentclass{article}
    
    \newcommand{\collect}[1]{#1}
    \newcommand{\callcollection}{-- Call all previous texts in "collect" --}
    \begin{document}
    
    
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer aliquet
    quam\collect{Vitae mauris tempus!} consequat. In nec neque vitae magna
    pulvinar pretium.\collect{Nunc purus augue, egestas vitae lobortis at,
    eleifend quis erat.  Vestibulum ante ipsum primis in faucibus orci luctus
    et ultrices posuere cubilia Curae.} In aliquet tortor sed tellus
    \collect{Porta} et elementum diam sagittis.
    
    \callcollection
    % Here I want the \callcollection command place all the texts in previous paragraph

    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer aliquet
    quam\collect{Vitae mauris tempus!} consequat. In nec neque vitae magna
    pulvinar pretium.\collect{Nunc purus augue, egestas vitae lobortis at,
    eleifend quis erat.  Vestibulum ante ipsum primis in faucibus orci luctus
    et ultrices posuere cubilia Curae.} In aliquet tortor sed tellus
    \collect{Porta} et elementum diam sagittis.
    
    \callcollection
    % Here I want the \callcollection command place all the texts in previous paragraph

    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer aliquet
    quam\collect{Vitae mauris tempus!} consequat. In nec neque vitae magna
    pulvinar pretium.\collect{Nunc purus augue, egestas vitae lobortis at,
    eleifend quis erat.  Vestibulum ante ipsum primis in faucibus orci luctus
    et ultrices posuere cubilia Curae.} In aliquet tortor sed tellus
    \collect{Porta} et elementum diam sagittis.
    
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer aliquet
    quam\collect{Vitae mauris tempus!} consequat. In nec neque vitae magna
    pulvinar pretium.\collect{Nunc purus augue, egestas vitae lobortis at,
    eleifend quis erat.  Vestibulum ante ipsum primis in faucibus orci luctus
    et ultrices posuere cubilia Curae.} In aliquet tortor sed tellus
    \collect{Porta} et elementum diam sagittis.
    
    \callcollection
% Here I want the \callcollection command place all the texts in the 2 previous paragraphs    

    \end{document}

答案1

这是一个基于Lua的方法:

\documentclass{article}
\directlua{buffer = {}}
\newcommand{\collect}[1]{#1\directlua{table.insert(buffer, [[#1]])}}
\newcommand{\callcollection}{\directlua{
    tex.sprint(table.concat(buffer, " "))
    buffer = {}
}}
\begin{document}

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer aliquet
quam\collect{Vitae mauris tempus!} consequat. In nec neque vitae magna
pulvinar pretium.\collect{Nunc purus augue, egestas vitae lobortis at,
eleifend quis erat.  Vestibulum ante ipsum primis in faucibus orci luctus
et ultrices posuere cubilia Curae.} In aliquet tortor sed tellus
\collect{Porta} et elementum diam sagittis.

\callcollection
% Here I want the \callcollection command place all the texts in previous paragraph

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer aliquet
quam\collect{Vitae mauris tempus!} consequat. In nec neque vitae magna
pulvinar pretium.\collect{Nunc purus augue, egestas vitae lobortis at,
eleifend quis erat.  Vestibulum ante ipsum primis in faucibus orci luctus
et ultrices posuere cubilia Curae.} In aliquet tortor sed tellus
\collect{Porta} et elementum diam sagittis.

\callcollection
% Here I want the \callcollection command place all the texts in previous paragraph

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer aliquet
quam\collect{Vitae mauris tempus!} consequat. In nec neque vitae magna
pulvinar pretium.\collect{Nunc purus augue, egestas vitae lobortis at,
eleifend quis erat.  Vestibulum ante ipsum primis in faucibus orci luctus
et ultrices posuere cubilia Curae.} In aliquet tortor sed tellus
\collect{Porta} et elementum diam sagittis.

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer aliquet
quam\collect{Vitae mauris tempus!} consequat. In nec neque vitae magna
pulvinar pretium.\collect{Nunc purus augue, egestas vitae lobortis at,
eleifend quis erat.  Vestibulum ante ipsum primis in faucibus orci luctus
et ultrices posuere cubilia Curae.} In aliquet tortor sed tellus
\collect{Porta} et elementum diam sagittis.

\callcollection
% Here I want the \callcollection command place all the texts in the 2 previous paragraphs    

\end{document}

在此处输入图片描述

编辑: \collect现在还打印其参数(错误修复)

答案2

scontents包裹提供了这样的存储/调用机制。将您想要存储的部分封闭在环境中scontents,然后使用 调用它\getstored

\begin{scontents}[store-env=a_name]
text that you want to store
\end{scontents}

\getstored{a_name}

a_name是您选择的名称。

相关内容