我正在编写一个包,需要将几个环境的内容以逐字模式保存到辅助文件中。然后由外部程序处理辅助文件。在下一次编译文档时,将外部程序的输出输入到文档中。
我正在使用fancyvrb
将环境的内容输出到辅助文件。
我已经有一些工作了,只是inputenc
在环境中使用非 ascii unicode 字符(即带重音符号的字母,如 á)时会出现错误。
为了演示这个问题,我编写了以下定义testenv
环境的简化包。
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{testenv}[2012/03/10 v0.1 JRM test environment]
\RequirePackage{fancyvrb}
% =========================================================
% fancyvrb new commands to append to a file
% =========================================================
\def\VerbatimOutAppend{\FV@Environment{}{VerbatimOutAppend}}
\def\FVB@VerbatimOutAppend#1{%
\@bsphack
\begingroup
\FV@UseKeyValues
\FV@DefineWhiteSpace
\def\FV@Space{\space}%
\FV@DefineTabOut
\def\FV@ProcessLine{\immediate\write#1}%
\let\FV@FontScanPrep\relax
\let\@noligs\relax
\FV@Scan
}
\def\FVE@VerbatimOutAppend{%
\endgroup
\@esphack
}
\DefineVerbatimEnvironment{VerbatimOutAppend}{VerbatimOutAppend}{}
% =========================================================
% my test environment
% =========================================================
\newwrite\testenv@outfile
\newenvironment{testenv}{%
\immediate\write\testenv@outfile{\noexpand\def\noexpand\testenvcommand\@charlb}%
\VerbatimEnvironment
\begin{VerbatimOutAppend}{\testenv@outfile}%
}{%
\end{VerbatimOutAppend}%
\immediate\write\testenv@outfile{\@charrb}%
\ifdefined\testenvcommand\testenvcommand\fi
}
% =========================================================
% final actions
% =========================================================
\AtEndOfPackage{%
\IfFileExists{\jobname.testenv}{%
\input{\jobname.testenv}%
}{%
\PackageWarning{testenv}{File `\jobname.testenv' not found.}%
}%
\immediate\openout\testenv@outfile\jobname.testenv%
}
\AtEndDocument{%
\closeout\testenv@outfile%
}
\endinput
我正在使用以下文档来测试它:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{testenv}
\begin{document}
\begin{testenv}
This is a test with this unicode char: á.
\end{testenv}
\end{document}
使用 pdflatex 编译时,我收到以下错误消息:
! Package inputenc Error: Unicode char \u8:á.\fi not set up for use with LaTeX.
See the inputenc package documentation for explanation.
Type H <return> for immediate help.
...
l.11 \end{testenv}
有人知道如何解决这个问题吗?
答案1
您可以尝试改变
\def\FV@ProcessLine{\immediate\write#1}%
进入
\def\FV@ProcessLine{\immediate\unexpandedwrite#1}%
添加定义
\long\def\unexpandedwrite#1#2{\write#1{\unexpanded{#2}}}
问题是,超出范围的字符"7F
被激活,并且被扩展了\write
。
顺便说一下,
\immediate\write\testenv@outfile{\noexpand\def\noexpand\testenvcommand\@charlb}%
可以简化为
\immediate\write\testenv@outfile{\def\string\testenvcommand\@charlb}%
因为\def
不可扩展。