![用于显示调查中的自由文本答案的命令,消除了 ASCII 控制字符](https://linux22.com/image/234454/%E7%94%A8%E4%BA%8E%E6%98%BE%E7%A4%BA%E8%B0%83%E6%9F%A5%E4%B8%AD%E7%9A%84%E8%87%AA%E7%94%B1%E6%96%87%E6%9C%AC%E7%AD%94%E6%A1%88%E7%9A%84%E5%91%BD%E4%BB%A4%EF%BC%8C%E6%B6%88%E9%99%A4%E4%BA%86%20ASCII%20%E6%8E%A7%E5%88%B6%E5%AD%97%E7%AC%A6.png)
我想根据一项调查创建一份报告,其中包含数据库中收集的自由文本答案。对于每个问题,通常有许多答案,我可以在 itemize 环境中呈现这些答案,每个项目一个答案。我遇到的问题是,单个答案(采用 Latin1 编码)可能包含双空格、回车符和其他控制字符,这些字符在呈现中不应该有影响。是否有合适的环境或命令(可能使用 catcode 技巧实现)可以平滑这些字符,从而呈现均匀的效果?
\answer{Code with double spaces, returns, linefeeds and other control character that should not matter here}
另一种方法是使用从数据库中提取数据并生成 LaTeX 代码的软件过滤掉所有这些。但是,我更喜欢 TeX/LaTeX 解决方案。我也对其他有创意的解决方案感兴趣。
答案1
我建议简单地将所有 ASCII 控制字符(0-31)设置为 catcode 9(忽略)等:
\def\mycatcodes{%
% Control characters ASCII #0-31:
% Catcode 9 = "ignore"
\catcode0=9
\catcode1=9
\catcode2=9
\catcode3=9
...
\catcode30=9
\catcode31=9
% and DEL:
\catcode127=9
%
% You might want the line-end character to be a space:
\catcode13=10
% Might be also required:
\endlinechar=-1
%
% Verbatimise all special TeX characters:
\let\do\@makeother
\dospecials
%
% Let Spaces be spaces again:
\catcode`\ =10
}
\newenvironment{answer}{%
\begingroup
\mycatcodes
\@answer
}{}%
% A macro which reads everything until a verbatim "\end{answer}":
\begingroup
\catcode`\|=0
\catcode`\(=1
\catcode`\)=2
\@makeother\{
\@makeother\}
\@makeother\\
|gdef|@answer#1\end{answer}(%
|endgroup % end catcode changes
|@@answer(#1)%
|end(answer)% real \end{answer}
)
|endgroup
% Now a third macro is used which doesn't has the hassle with catcode changes
% like \@answer has!:
\def\@@answer#1{%
% Does the typesetting and formatting of the answer, e.g.
\texttt{#1}%
% Note, that not all verbatim character (like `_`) are correctly displayed
% by all fonts
}
在文档中:
\begin{answer}
Test with possible control characters (ignored) and LaTeX special characters
incl. { and }.
\end{answer}
正如您已经说过的,过滤脚本(例如 Perl)也可能是一个很好的选择。
编辑:把它变成了一个环境
答案2
以下是如何在 ConTeXt 中实现此操作。
\def\startignore
{\dostartbuffer[ignore][startignore][stopignore]}
\def\stopignore
{\pushcatcodetable
\setcatcodetable\nilcatcodes
\getbuffer[ignore]
\popcatcodetable}
\setupbodyfont[cambria]
%Any unicode font that contains greek used in the example will do
\starttext
\startignore
Line
feeds
and multiple spaces are ignored. The text may contain \undefined \TeX
commands or ` or ' or _ or ^ or ωθατενερ
\stopignore
\stoptext
原则上,同样的想法在 LaTeX 中也可以起作用:逐字捕获环境的内容,设置 nil catcode(基本上只是将空格、行尾、换页符、制表符和文件末尾的 catcode 设置为它们的通常值;其他所有内容都设置为其他),并检索环境的内容。