[texhax] dual-use input/output macro

Oleg Katsitadze geolsoft at mail.ru
Thu Nov 17 22:26:32 CET 2005


On Thu, Nov 17, 2005 at 01:40:38PM -0600, Karl Berry wrote:
> About all I can imagine is
> changing all the catcodes to other before reading the argument, printing
> the arg in tt, then writing the arg to an external file, and 
> reading it back with normal catcodes to get the normal output.

Wouldn't it be easier the other way around?  I mean, write
the argument to an external file through the \toks0 to avoid
expansion, read it by some kind of verbatim macro, and
typeset the result as `#1'.  As a side effect, there will be
spaces after macro names in the verbatim text, so this
approach is no good if this is critical.

This is what I mean (I use Eplain for the \listing macro):

-------------------- Cut --------------------
\input eplain

\newwrite\showfile

\def\showme#1{%
  \toks0={#1}%
  \immediate\openout\showfile=show
  \immediate\write\showfile{\the\toks0}%
  \immediate\closeout\showfile
  \listing{show}%
  #1%
}%

\parindent=0pt

\showme{hello, \relax here}
\smallskip

\showme{hello, {\it there}}
\smallskip

\showme{\line{hello,\hrulefill everywhere}}

\bye
---------------------------------------------

It's also possible to avoid writing/reading the file, if the
argument is not too complex to be processed by the
``inline'' \verbatim.  For example (\verbatim...\endverbatim
and \sanitize also come from Eplain):

\def\showme#1{%
  \def\temp{#1}%
  \edef\showtemp{\sanitize\temp}%
  \expandafter\verbatim\showtemp\endverbatim\par
  #1%
}

Unfortunately, this approach also suffers from the spurious
spaces after macro names.

Hope this helps.

-- 
Best regards,
Oleg Katsitadze



More information about the texhax mailing list