Como manter todos os comentários no novo arquivo látex feitos pelo comando newwrite?

Como manter todos os comentários no novo arquivo látex feitos pelo comando newwrite?
\documentclass[12pt,a4paper,oneside]{book}

\begin{document}

\newwrite\copyfile
\immediate\openout\copyfile=Theorem.tex 
\immediate\write\copyfile{hello %this is comment 1
                                %this is comment 2
                            }
\immediate\closeout\copyfile                        
\end{document}

O código acima cria Theorem.texo arquivo. Mas todos os comentários (este é o comentário 1, este é o comentário 2) desaparecem. Como manter todos os comentários %this is comment 1, %this is comment 2?

Responder1

\documentclass[12pt,a4paper,oneside]{book}

\begin{document}

\newwrite\copyfile
\immediate\openout\copyfile=Theorem.tex 
{\catcode`\%=12
\immediate\write\copyfile{hello %this is comment 1
                                %this is comment 2
                            }
}
\immediate\closeout\copyfile                        
\end{document}

fazer com %que o catcode 12 perca seu status de comentário (você também pode querer preservar as novas linhas?)

O acima faz

hello %this is comment 1 %this is comment 2 
\documentclass[12pt,a4paper,oneside]{book}

\begin{document}

\newwrite\copyfile
\immediate\openout\copyfile=Theorem.tex 
{\catcode`\%=12
\endlinechar=`\^^J%
\immediate\write\copyfile{hello %this is comment 1
                                %this is comment 2
                            }%
}%
\immediate\closeout\copyfile                        
\end{document}

produz

hello %this is comment 1
%this is comment 2

Responder2

Um exemplo mais complicado que permite todos os caracteres especiais. Isso funciona com Plain LuaTeX e LuaLaTeX, com algumas alterações também funcionará com outros motores. A implementação é semelhante ao filecontentsambiente do kernel LaTeX.

\catcode`@=11

\newwrite\file
\immediate\openout\file=xxx.zzz\relax

\newif\if@firstline

\def\@makeother#1{\catcode`#112\relax}

\begingroup
\obeylines%
\gdef\verbatimwrite#1#2{%
    \begingroup%
    \let\do\@makeother \dospecials%
    \expandafter\edef\expandafter\E\expandafter{%
        \expandafter\csstring\expandafter\\\expandafter%
        \scantextokens\expandafter{\csstring#2}}%
    \edef\reserved@b{\def\noexpand\reserved@b####1\E####2\E####3\relax}%
    \reserved@b{%
        \ifx\relax##3\relax%
          \if@firstline%
            \wlog{Wrong input `##1' discarded.}%
            \@firstlinefalse%
          \else\immediate\write#1{##1}%
          \fi%
        \else%
          \let^^M\endgroup%
          \if@firstline%
          \errhelp{The verbatim writing command must not end on the same line.}%
          \errmessage{Writing nothing}%
          \else%
          \ifx\relax##1\relax%
          \else%
          \wlog{Writing text `##1' before%
              \E\space as last line.}%
          \immediate\write#1{##1}%
          \fi%
          \ifx\relax##2\relax%
          \else%
          \wlog{Ignoring text `##2' after \E.}%
          \fi%
          \fi%
        \fi%
        ^^M}%
    \@firstlinetrue%
    \obeylines%
    \edef^^M##1^^M{\noexpand\reserved@b##1\E\E\relax}%
    ^^M}%
\endgroup

Let's test:

\verbatimwrite\file\endverbatimwrite This is deleted!
%12

informação relacionada