如何收集包含逐字文本和带有参数的定义的环境以便稍后进行排版?

如何收集包含逐字文本和带有参数的定义的环境以便稍后进行排版?

我一直在尝试收集 tex 标记(去标记化)并在稍后用 输出它们\scantokens。问题是不能删除重复的参数(#)理想情况下,将所有内容作为字符串读取直到某个标记会很棒,但这可能需要逐个读取标记并将其 catcode 更改为字符串并在某些位置添加空格。

例如我想收集thebibliography并输出到文档末尾。当我取消注释时会出现问题\def\path...。它会给出一个错误! Parameters must be numbered consecutively.

例子:

\documentclass{article}
\usepackage{environ}

\makeatletter
\def\printOrigBibliography{%
    \let\thebibliography\copythebibliography
    \let\endthebibliography\copyendthebibliography   
    %% fix of space after \verb token
    \let\origverb\verb
    \def\verb##1{\origverb##1}%
    \expandafter\scantokens\expandafter{\my@envthebibliography\noexpand}\relax
    }

\AtBeginDocument{%
    \environbodyname\envthebibliography
    \let\copythebibliography\thebibliography
    \let\copyendthebibliography\endthebibliography    
    \RenewEnviron{thebibliography}
      {\xdef\my@envthebibliography{%
        \noexpand\begin{thebibliography}%
        \expandafter\detokenize\expandafter{\envthebibliography}%
        \noexpand\end{thebibliography}}%
      }%
    }
    
\AtEndDocument{\printOrigBibliography}
\makeatother

\begin{document}

\begin{thebibliography}{1}
%\def\path#1{#1}

\bibitem{R}
aa \verb|a_a| bb

\end{thebibliography}

Final words 
\end{document}    \end{document}

我可以将一些重复的参数降低一级吗?我尝试使用以下命令将其删除:

\expandafter\scantokens\expandafter{%
   \expandafter\long\expandafter\def\expandafter\fooo
       \expandafter{\my@envthebibliography}\noexpand}\relax
\fooo

但后来我\verb又遇到问题了。

答案1

将参考书目存储在盒子里不是更简单吗?

\documentclass{article}

\let\origthebibliography\thebibliography
\let\origendthebibliography\endthebibliography
\newsavebox{\bibliographybox}
\renewenvironment{thebibliography}
 {\global\setbox\bibliographybox=\vtop\bgroup\origthebibliography}
 {\origendthebibliography\egroup}
\AtEndDocument{\unvbox\bibliographybox}

\begin{document}

\begin{thebibliography}{1}
\def\path#1{#1}

\bibitem{R}
aa \verb|a_a| bb

\end{thebibliography}

Final words \cite{R}

\end{document}

在此处输入图片描述

相关内容