[texhax] How to write a TeX code into file literally

Yue Wu vanopen at gmail.com
Sun Oct 9 14:43:17 CEST 2011


Hi, Heiko,

Thanks, it solves most part of the problem, but stil has a few left, plz see
them below the context.

On Sun, Oct 09, 2011 at 01:27:27PM +0200, Heiko Oberdiek wrote:
> On Sun, Oct 09, 2011 at 06:22:41PM +0800, Yue Wu wrote:
> 
> > Another question arises, I try to let TeX not expand macros at all for a
> > \write, so I tried with it:
> > 
> > \newwrite\myout
> > \immediate\openout\myout=myout.tex
> > \long\def\myoutone{%
> >   \begingroup
> >   \catcode`\|=0
> >   |catcode`|\=12
> >   |catcode`|#=12
> >   |catcode`|&=12
> >   |immediate|write|myout{}
> >   |myouttwo
> > }
> > \long\def\myouttwo#1#2{%
> >   |immediate|write|myout{#1|endgraf#2}
> >   |endgroup
> > }
> 
> \catcode`\|=0 is inside the definition, thus it is only
> called if \myoutone is executed. For the definition text
> the catcodes are used that are active at the time of the
> definition. Therefore you don't need the hack with |.
> 
> \def\myoutone{%
>   \begingroup
>   \catcode`\\=12 %
>   \catcode`\#=12 %
>   \catcode`\&=12 %
>   \immediate\write\myout{}%
>   \myouttwo
> }
> \long\def\myouttwo#1#2{%
>   \endgroup
>   \immediate\write\myout{#1\noexpand\endgraf#2}%
> }

\def\myoutone{%
  \begingroup
  \catcode`\\=12
  \catcode`\#=12
  \catcode`\&=12
  \immediate\write\testout{}
  \myouttwo
}
\long\def\myouttwo#1#2{%
  \endgroup
  \immediate\write\testout{{\bf#1}\endgraf#2}
}

\newwrite\testout
\immediate\openout\testout=testout.tex
\myoutone{section}{
\halign{#\hfil&#\hfil\cr
cell1&cell2\cr
}}
\immediate\closeout\testout

The output of testout.tex will expand \bf into primitive commands. I want it
output literally too.

> 
> If you are using plain TeX or LaTeX look at \dospecials and
> \@sanitize, e.g. for plain TeX:
> 
> \def\myoutone{%
>   \begingroup
>   \def\do#1{\catcode`#1=12 %}%
>   \dospecials
>   \catcode`\{=1 % are needed as argument delimiter for \myouttwo
>   \catcode`\}=2 %
>   \immediate\write\myout{}%
>   \myouttwo
> }

There will be an error when trying to complile the file as above:

! Illegal parameter number in definition of \myoutone.
<to be read again> 
                   1
l.35   \def\do#1
                {\catcode`#1=12 %}%

> 
> Yours sincerely
>   Heiko Oberdiek
> _______________________________________________
> TeX FAQ: http://www.tex.ac.uk/faq
> Mailing list archives: http://tug.org/pipermail/texhax/
> More links: http://tug.org/begin.html
> 
> Automated subscription management: http://tug.org/mailman/listinfo/texhax
> Human mailing list managers: postmaster at tug.org

-- 
Regards,
Yue Wu

Key Laboratory of Modern Chinese Medicines
Department of Traditional Chinese Medicine
China Pharmaceutical University(中國藥科大學)
No.24, Tongjia Xiang Street, Nanjing 210009, China


More information about the texhax mailing list