如何禁用%符號註解?

如何禁用%符號註解?

這是程式碼(我正在嘗試創建將所有內容保存到文件中的逐字環境):

\documentclass{article}
\NewDocumentEnvironment{foo}{b}{%
  \newwrite\myfile
  \immediate\openout\myfile=myfile.txt
  \immediate\write\myfile{\detokenize{#1}}
  \immediate\closeout\myfile
}{}
\AddToHook{env/foo/before}{\obeylines\obeyspaces}
\begin{document}
\begin{foo}
First \LaTeX
% Second
Third
\end{foo}
\end{document}

我期待以下內容myfile.txt

First \LaTeX
% Second
Third

但是,我得到:

^^MFirst \LaTeX ^^MThird^^M

如何修復註釋符號和^^M(我希望正常的 EOL 在那裡)?另外,後面\LaTeX一定不能有空格。

答案1

這與你上一個問題相同

\documentclass{article}
\newwrite\myfile
\NewDocumentEnvironment{foo}{b}{%
% no  \newwrite\myfile
  \immediate\openout\myfile=myfile.txt
  \immediate\write\myfile{\detokenize{#1}}%
  \immediate\closeout\myfile
}{}
\AddToHook{env/foo/before}{%
\newlinechar=\endlinechar% as before
\catcode`\%=12\relax
\obeylines\obeyspaces}
\begin{document}
\begin{foo}
First \LaTeX
% Second
Third
\end{foo}
\end{document}

相關內容