
Este é o código (estou tentando criar um ambiente literal que salve tudo em um arquivo):
\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}
Estou esperando esse conteúdo em myfile.txt
:
First \LaTeX
% Second
Third
No entanto, estou recebendo:
^^MFirst \LaTeX ^^MThird^^M
Como corrigir o símbolo de comentário e ^^M
(espero que o EOL normal esteja lá)? Além disso, o espaço depois \LaTeX
não deve estar lá.
Responder1
É igual à sua última pergunta
\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}