newwrite 명령으로 작성된 새 라텍스 파일에 모든 주석을 유지하는 방법은 무엇입니까?

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}

위의 코드는 파일을 만듭니다 Theorem.tex. 하지만 모든 댓글(이것은 댓글 1, 이것은 댓글 2)이 사라집니다. 모든 댓글을 보관하는 방법은 무엇입니까 %this is comment 1, %this is comment 2?

답변1

\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}

catcode 12를 사용하면 %주석 상태가 사라집니다. (개행 문자를 보존하고 싶을 수도 있습니다.)

위의 내용은

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}

생산하다

hello %this is comment 1
%this is comment 2

답변2

모든 특수 문자를 허용하는 더 복잡한 예입니다. 이는 Plain LuaTeX 및 LuaLaTeX에서 작동하며 일부 변경 사항을 적용하면 다른 엔진에서도 작동합니다. 구현은 filecontentsLaTeX 커널 환경 과 유사합니다 .

\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

관련 정보