[comp.text.tex] Verbatim environment in macro argument

mernst@theory.lcs.mit.edu (Michael Ernst) (07/10/90)

I would like to pass a macro an argument containing a verbatim environment,
but when I do so, the closing brace of the argument seems to be ignored.
Can someone help me out of my predicament?

A simple example follows; my \foo is actually a little more complex, but
this demonstrates the behavior.

The file:
 \documentstyle{article}
 \begin{document}
 
 \long\def\foo#1{#1}
 
 \foo{
 \begin{verbatim}
 bar
 \end{verbatim}
 }				%% <=== This brace is getting lost
 
 \end{document}

I hoped that this would give me ``bar'' in typewriter font.
 
The actual result:
 Runaway argument?
  bar \end {verbatim} ^^M^^M\end{document}^^M
 ! File ended while scanning use of \@xverbatim.
 <inserted text>
                 \par
 <*> &lplain test-verb.tex
 
 ?
					 Thanks,

					-Michael Ernst
					 mernst@theory.lcs.mit.edu 

mernst@theory.lcs.mit.edu (Michael Ernst) (07/11/90)

Bracy H. Elton pointed out to me that a verbatim environment as a macro
argument is an Official No-No; see bullet number one on page 66 of "LaTeX:
A Document Preparation System" by Leslie Lamport.

However, I'm stubborn.  On page 39, paragraph one, of the TeXbook, Knuth
says, ``(2) Once a category code has been attached to a character token,
the attachment is permanent.''  Is there any way to get around this?  For
instance, can I somehow re-scan a piece of text that has already been
tokenized to re-classify each of the tokens, perhaps giving them new
catcodes?
					 Thanks again,

					-Michael Ernst
					 mernst@theory.lcs.mit.edu 

t-rayc@microsoft.UUCP (Raymond CHEN) (07/11/90)

In article <MERNST.90Jul10132554@wren.lcs.mit.edu> mernst@theory.lcs.mit.edu (Michael Ernst) writes:
>Can I somehow re-scan a piece of text that has already been
>tokenized to re-classify each of the tokens, perhaps giving them new
>catcodes?

Taking inspiration from the top of page 382, with the same caveats:

\newcount\oldescapechar
\newwrite\tokout
\newread\tokin

\def\retokenize#1{%
	\immediate\openout\tokout=toktmp
	{\escapechar=\oldescapechar
	\immediate\write\tokout{\expandafter\strip\meaning#1}}%
	\immediate\closeout\tokout
	\openin\tokin=toktmp
	\read\tokin to#1%
	\closein\tokin
}
\def\strip#1>{}

% ! will be the escape character, and () will be the grouping characters.

\def\mytok{!message(Now isn't that special.)}

% Okay, now we change catcodes like a madman.

\oldescapechar=\escapechar
\escapechar=`\!
\catcode`\!=0
!catcode`!\=12

!catcode`!(=1
!catcode`!)=2
!catcode`!{=12
!catcode`!}=12

!retokenize!mytok

% Now I execute the new stuff: 
!mytok
!end

% Don Hosek can probably find a less kludgy way of doing it.
% (Alternate reply path:  raymond@math.berkeley.edu)

dougcc@csv.viccol.edu.au (Douglas Miller) (07/20/90)

In article <MERNST.90Jul10132554@wren.lcs.mit.edu>, mernst@theory.lcs.mit.edu (Michael Ernst) writes:
> However, I'm stubborn.  On page 39, paragraph one, of the TeXbook, Knuth
> says, ``(2) Once a category code has been attached to a character token,
> the attachment is permanent.''  Is there any way to get around this?  For
> instance, can I somehow re-scan a piece of text that has already been
> tokenized to re-classify each of the tokens, perhaps giving them new
> catcodes?

It can be done.  You have to write the text to an auxiliary file, and
read it in again.