I am trying to write verbatim text into a file in Plain TeX. In LaTeX I could use the verbatim
package. My goal is using Asymptote in Plain TeX. So I need something that works like \verbatim@start
from verbatim.sty
.
MWE:
% Plain LuaTeX
\catcode`@=11
% from latex.ltx
\long\def\@ifnextchar#1#2#3{%
\let\reserved@d=#1%
\def\reserved@a{#2}%
\def\reserved@b{#3}%
\futurelet\@let@token\@ifnch}
\def\@ifnch{%
\ifx\@let@token\@sptoken
\let\reserved@c\@xifnch
\else
\ifx\@let@token\reserved@d
\let\reserved@c\reserved@a
\else
\let\reserved@c\reserved@b
\fi
\fi
\reserved@c}
\def\:{\let\@sptoken= } \: % this makes \@sptoken a space token
\def\:{\@xifnch} \expandafter\def\: {\futurelet\@let@token\@ifnch}
\def\@makeother#1{\catcode`#1=12\relax}
Let's try to write.
\newwrite\file
\immediate\openout\file=temp.tmp\relax
\def\verbatimwrite{\begingroup\catcode`\^^M=12\relax
%\let\do\@makeother\dospecials%%%%%%% PROBLEM! (A)
\@verbatimwrite}
\begingroup
\catcode`\^^M=12 %
\gdef\@verbatimwrite#1^^M{% Every line must be written separately to save the line breaks.
\immediate\write\file{\unexpanded{#1}}%
\@ifnextchar\endverbatimwrite\endgroup\@verbatimwrite}% detect the end of verbatim (B)
\endgroup
\let\endverbatimwrite\relax
\verbatimwrite
123#;^{t}
äöüß
4567\undefined
\endverbatimwrite
\immediate\closeout\file
\bye
There are 2 problems (also as comments in the code):
- (A): Where do I have to change all the other catcodes? It should be possible for example to write unbalanced braces. If this is done correctly I think the
\unexpanded
command is not needed any more. - (B): Do you have an idea how to improve the detection of
\endverbatimwrite
? I think my method is really poor.
Thanks.