[texhax] making LaTeX-environment from existing command

Morten Høgholm morten.hoegholm at gmail.com
Thu Aug 16 12:42:53 CEST 2007


On Thu, 16 Aug 2007 11:37:48 +0200, Alexander Grahn wrote:

>> Unfortunately, I cannot put unbalanced {'s and }'s into the begdef and
>> enddef parameters of \newenvironment.
>
> I must correct myself, the begdef and enddef parameters tolerate opening
> and closing braces. The following code seems to work as expected:
>
> \newenvironment{storetoks}{\global\mytoks{}{}}

This executes
  \global\mytoks{}{}
at the beginning of the environment. What happes afterwards depends on  
what follows in your definition, if its a blank line it merely inserts a  
\par token.

\setbox works differently as it keeps typesetting while looking for a  
token with meaning }, this is why \egroup is used and it works. toks  
registers on the other hand need balanced text from the input and so can't  
use a } buried in some macro definition.

Look for amsmath's way of grabbing the contents of an environment:

\documentclass{article}
\usepackage{amsmath}
\newtoks\mytoks
\newenvironment{storetoks}{\global\mytoks{}{}}{after}

\makeatletter
\newenvironment{testing}{\collect at body\testfunc}{}
\newcommand\testfunc[1]{Here is the contents: ``#1''}
\makeatother
\begin{document}

\begin{storetoks}%
   Hello!%
\end{storetoks}

``\the\mytoks'' %oops, it's empty.

\begin{testing}
   Hello World.
\end{testing}
\end{document}
-- 
Morten


More information about the texhax mailing list