以下是代码:
\documentclass{article}
\usepackage{fancyvrb}
\NewDocumentCommand\foo{v}{\texttt{#1}}
\catcode`\|\active
\AtBeginDocument{\catcode`\|\active\def|{\foo|}}
\catcode`\| 12 %
\newenvironment{vvv}
{\VerbatimEnvironment\begin{VerbatimOut}{a.txt}}
{\end{VerbatimOut}}
\begin{document}
\begin{vvv}
hello, |world|!
\end{vvv}
\end{document}
它不能编译:
Runaway text?
hello, \foo \foo \foo \foo \foo \foo \foo \foo \foo \foo \foo \foo \foo \ETC.
! TeX capacity exceeded, sorry [main memory size=5000000].
|->\foo
|
l.20 hello, |world|!
怎么了?我希望我的vvv
环境保存hello, |world|!
到文件a.txt
。也许有比更好的方法\VerbatimEnvironment
?
答案1
\documentclass{article}
\usepackage{fancyvrb}
\NewDocumentCommand\foo{v}{\texttt{#1}}
\catcode`\|\active
\AtBeginDocument{\catcode`\|\active\def|{\foo|}}
\catcode`\| 12 %
\newenvironment{vvv}
{\catcode`\|=12 \VerbatimEnvironment\begin{VerbatimOut}{a.txt}}
{\end{VerbatimOut}}
\begin{document}
\begin{vvv}
hello, |world|!
\end{vvv}
\end{document}