问题描述
我是 Latex 新手,当我使用 pdflatex 读取文件时,我发现某些行(带有“%”)无法读取,我该如何绕过? 这是我的 .tex 文件
\documentclass{article}
\begin{document}
\newread\file
\openin\file=test.txt
\newwrite\fileo
\immediate\openout\fileo=ss.txt
\loop\unless\ifeof\file
\read\file to\fileline
\immediate\write\fileo{\fileline}
\repeat
\cloSEOut\fileo
\closein\file
sometext(not important)
\end{document}
这是 test.txt
%abcdefg
aaa
sss
pdflatex 后的 .tex 文件,结果是
aaa
sss
我怎样才能得到第一行:"%abcdefg"
解决方法
你必须逃避角色。某些字符在 LaTeX 中具有特殊含义:
&
%
#
_
~
^
\
{
}
{{ 1}}
要避开其中的大部分,只需添加 $
,其他人有自己的命令:
\
如您所见,如果您不对 % 字符进行转义,它会注释掉源代码中的一行文本。我希望我有帮助
编辑:正如您所说,您无法更改 test.txt 文件,您可以将 % 作为特殊字符在 \documentclass{article}
\begin{document}
\& \% \# \_ \$ \{ \} \textasciitilde \textasciicircum \textbackslash
% A comment line
% In your code should be:
\%abcdefg \\
aaa \\
sss
\end{document}
之后添加 \catcode`\%=12
\begin{document}