与标准环境相比,使用 environ 和 newenviron 时宏扩展有什么变化

与标准环境相比,使用 environ 和 newenviron 时宏扩展有什么变化

这两个包environnewenviron提供了 LaTeX 附带的标准环境宏的替代方案。主要区别在于它们可以使用宏引用环境主体,但另一个区别是宏扩展会受到影响,如以下文档中通过(取消)注释相关行所见:

\documentclass{article}
\usepackage{environ}
\usepackage{newenviron}

\def\speakify#1: #2\par{{\bfseries#1}: {\itshape``#2\unskip''}\par}
\newenvironment{interview}{\everypar{\speakify}}{\par}
%\NewEnviron{interview}{\everypar{\speakify}\BODY\par}
%\newenviron{interview}{}{\everypar{\speakify}\envbody\par}

\begin{document}
\begin{interview}
I: I have a question for you that I would really like to know the answer to. Could you please explain it in your own words while the moon is still high in the sky? That would be lovely thank you very much!

L: Of course I will answer gladly all questions you might have no matter how silly I feel they may be provided payment is made in full before commencement of the answering phase.
\end{interview}
\end{document}

发生的宏扩展究竟发生了怎样的变化?

答案1

\everypar处理完第二段后,根据发布的文档,您基本上

\speakify L: Of course I will answer gladly all questions you might have no matter how
             silly I feel they may be provided payment is made in full before commencement
             of the answering phase.\end{interview} \end{document}

现在\speakify看到L为,#1但超出文档末尾,寻找\par分隔符#2。因此您得到:

! File ended while scanning use of \speakify.
<inserted text> 
                \par 
<*>

如果你编辑文档,环境之后会出现一个段落分隔符,因此文件结束

phase.
\end{interview}

\end{document}

然后\speakify将包括\end{interview}在内#2,这样可以避免上述错误,但分组逻辑混乱,你会得到:

! Missing } inserted.
<inserted text> 
                }
l.16 

? 

如果您编辑文档以添加段落分隔符环境的结束,因此文件结束:

 phase.

\end{interview}
\end{document}

然后文档就可以正常工作了。


扩展逻辑在使用的形式中是不同的,\BODY因为您添加了显式的,\par所以\BODY被扩展,并且当处理最后一段时,\par已经在输入流中,所以处理相当于

\speakify L: Of course I will answer gladly all questions you might have no matter how
             silly I feel they may be provided payment is made in full before commencement
             of the answering phase.\par

并按\speakify预期看到其参数分隔符。

相关内容