收集 LaTeX 环境的内容

收集 LaTeX 环境的内容

我有以下内容用于收集环境的内容并在稍后打印它们,它可以工作,但对于我的实际任务,我想使用环境包,因为我需要能够访问\BODY。如何使用\NewEnviron而不是\newenvironment使用来产生相同的输出?

\documentclass{article}
\usepackage{collect}
\usepackage{environ}

\definecollection{notes}


%works but not what i realy want i need access to \BODY and for that i cant  use \newenvironment command
\makeatletter
\newenvironment{note}
  {\@nameuse{collect}{notes}{\par\noindent}{\par}{}{}}
   {\@nameuse{endcollect}}
\makeatother

%what i realy want
%\NewEnviron{note}{
%\@nameuse{collect}{notes}{\par\noindent}{\par}{}{}
  %\BODY
%\@nameuse{endcollect}
%}


\begin{document}

\begin{note}
 First note
 \end{note}

\begin{note}
 Second note
 \end{note}

 \begin{note}
Third note
\end{note}

\section{collected}
\includecollection{notes}
\end{document}

答案1

如果您收集的注释仅在文档末尾需要,则可以避免写出文件:只需使用令牌寄存器。

\documentclass{article}
\usepackage{environ}

\newtoks\mainnotetoks
\newtoks\tempnotetoks
\newtoks\prenotetoks
\newtoks\postnotetoks

\NewEnviron{note}{%
  \tempnotetoks=\expandafter{\BODY}%
  \edef\notetemp{%
    \the\mainnotetoks % what was already stored
    \the\prenotetoks % text before the new note
    \the\tempnotetoks % the current note
    \the\postnotetoks % text after the new note
  }%
  % update \mainnotetoks
  \global\mainnotetoks=\expandafter{\notetemp}%
}
\newcommand\includenotes{\the\mainnotetoks}

% set the pre and post note
\prenotetoks={\par\noindent}
\postnotetoks={\par}

\begin{document}

Here we have a first note.
\begin{note}
First note
\end{note}

Here we have a second note.
\begin{note}
Second note
\end{note}

Here we have a third note.
\begin{note}
Third note
\end{note}

\section{collected}
\includenotes
\end{document}

在此处输入图片描述

答案2

如果您不坚持使用该collect软件包,那么只有一种方法是直接将\BODY命令写入文件并在那里展开,然后重新读取该文件:

\documentclass{article}
\usepackage{collect}
\usepackage{xcolor}
\usepackage{amsmath}
\usepackage{environ}

\definecollection{notes}

\newwrite\collecthandle%

\AtBeginDocument{%
\immediate\openout\collecthandle=\jobname.mynotes%
}

\newcommand{\CollectNotes}[1]{%
\immediate\closeout\collecthandle%
\IfFileExists{#1}{\input{#1}}{Nope}%
}%

\AtEndDocument{%
\immediate\closeout\collecthandle% Close it anyway
}%

%works but not what i realy want i need access to \BODY and for that i cant  use \newenvironment command
\makeatletter
\newenvironment{note}
  {\@nameuse{collect}{notes}{\par\noindent}{\par}{}{}}
   {\@nameuse{endcollect}}
\makeatother

%what i realy want
\NewEnviron{Note}{%
\immediate\write\collecthandle{%
\unexpanded\expandafter{\expandafter{\BODY}}%
}%
\immediate\write\collecthandle{%
\string\newline
}%
}%


\begin{document}

\begin{Note}
 First Note
 \end{Note}

\begin{Note}
 Second Note
\end{Note}

\begin{Note}
Third Note
\end{Note}

\begin{Note}
fourth Note with coloured mathematical markup \textcolor{red}{\[\int\limits \left({x^2}\right)dx = \dfrac{1}{3}x^3 +C \]}
and even with \textbf{bold font}
\end{Note}


\section{collected}
\CollectNotes{\jobname.mynotes}
\end{document}

在此处输入图片描述

答案3

我把这个答案留在这里,因为它们对创建这个scontents包很有启发。所有内容都存储在内存中,包括逐字内容。如果您愿意,还可以使用键将内容保存在外部文件中write-env=file.tex。感谢@egreg 的 MWE 和精彩的回答。

\documentclass{article}
\usepackage[store-env=notes]{scontents}
\pagestyle{empty}
\begin{document}

Here we have a first note.
\begin{scontents}
First note
\end{scontents}

Here we have a second note.
\begin{scontents}
Second note
\end{scontents}

Here we have a third note.
\begin{scontents}
Third note
\end{scontents}

\section{collected}
\foreachsc[sep={\\[10pt]}]{notes}
\end{document}

相关内容