定义代码与效果并存的环境

例如, 需要一个环境 Foo, 使得

\begin{Foo}
some source code
\end{Foo}

得到:

===========
some source code
===========
code result(image)
===========

关键是 verbatim 的问题

我曾经在 CTeX 上发贴求助, 最后还是在别人提示下找到方法.

ctex-faq.sty 里将代码写到一个文件里, 再 input 进来实现的.

仔细看了 eTeX 文档, 发现 \scantokens 可以不用文件操作得到类似的效果.

代码片段:

\newenvironment*{AsyExample}[1][type=SR]{%
\stepcounter{asy}%
\setkeys[pt]{AsyExample}{#1}%
\edef\AsyExampleSize{\AsyExampleSize}
\begingroup
\@bsphack
\let\do\@makeother\dospecials\catcode`\^^M\active
\AsyExample@toks={}
\def\verbatim@processline{% \expandafter\addto@hook\expandafter\AsyExample@toks\expandafter{\the\verbatim@line^^J}
}%
\verbatim@start}{%
\@esphack
\global\AsyExample@toks\AsyExample@toks
\endgroup
\AsyExample@Source
\AsyExample@Result
}

然后在 \AsyExample@Source 等里显示或扫描 \AsyExample@toks 即可.

No comments: