[OS X TeX] Embedding environment contents in a macro

Ross Moore ross at ics.mq.edu.au
Thu Aug 29 00:29:07 CEST 2002



> Hi Ross and all,
> 
> Thanks for the explanation of why my old attempts were behaving as they 
> were; reading it was both satisfying and frustrating, the latter since 
> I'd really prefer it to work than understand why it doesn't. =) I've 
> tried comment.sty before and hit a dead end, but I'm giving it another 
> try, and I've hit another dead end.
> 
> The problem now is this: I've defined the environment
> -------------
> \newenvironment{env}{\begin{envcomment}\begin{innerenv}}
> {\end{innerenv}\end{envcomment}}
> -------------
> Beforehand, I've included the comment.sty file and said 
> \includecomment{envcomment}. When I write in file eca-00-endexer.def the 

No.
Environments that are included/excluded need to be outermost
in your document, since the pattern-matching needs to see the
\end{envcomment}  explicitly, without having to expand
some other macro to get it.

Alternatively, you could write:

\begin{env}
 ...
MUCH DATA GOES HERE
 ...
\end{innerenv}
\end{envcomment}

... but this looks weird, and would confuse LaTeX's book-keeping of which
environment you were in.


> following:
> -------------
> \begin{env}
> 	MUCH DATA GOES HERE
> \end{env}
> -------------
> I get
> -------------
> Runaway argument?
> ! File ended while scanning use of \next.
> <inserted text>
>                  \par
> l.602 ...ut{eca-00-endexer.def}


Yes. Because the \end{envcomment} is not being found.

> -------------
> Here, eca-00-endexer.def is being included in another file. When I write:
> -------------
> \begin{envcomment} \begin{innerenv}
> 	MUCH DATA GOES HERE
> \end{innerenv} \end{envcomment}
> -------------
> I have the same problem, but the perplexing thing is that when I write
> -------------
> \begin{envcomment}
> \begin{innerenv}
> 	MUCH DATA GOES HERE
> \end{innerenv}
> \end{envcomment}
> -------------
> it behaves as I want it to, respecting the \includecomment and 
> \excludecomment like it should. ...

Yes. This works, as the {envcomment} is explicitly in your LaTeX source.

> ...  Isn't white space supposed to be 
> unimportant in LaTeX in most places? Any ideas on resolving this?

It's nothing to do with white space.
TeX does *not* expand macros while it is matching a pattern
for the arguments of a macro, so TeX doesn't even *see* the
\end{envcomment} that you are wanting it to match. 


If you can guarantee that the contents of your {envcomment}
environments never crosses page-boundaries,
then you could use a \vbox:

\newbox\envcommentbox   %  declaration in preamble

\newenvironment{\envcomment}
 {\setbox\envcommentbox\vbox\bgroup}
 {\egroup\handlecomment}

where \handlecomment tests a condition to decide whether
to do nothing, or to \box\envcommentbox (to add the box
to the current page).

(Alternatively, you could try \unvbox\envcommentbox 
 which allows the glue to be reset, but I don't think that
allows crossing over pages --- not sure, try it!)



Hope this helps,

	Ross


> Michael
> 
> On Tuesday, August 27, 2002, at 07:09  PM, Ross Moore wrote:
> 
> >> Hi,
> >>
> >> I'm told this is a great place to write and ask questions. I'd like to
> >> define an environment, say env, that wraps its contents inside a macro.
> >> Something like:
> >> ---------
> >> \newenvironment{env}{\macroname \bgroup \begin{ anotherenv } }
> >> { \end{anotherenv} \egroup}
> >
> > This approach cannot work, since the \egroup to match the \bgroup
> > would never be seen -- it is wrapped-up inside the  \end{env}
> > and there is nothing to cause this to be expanded while trying to
> > match the argument to the \macroname .
> >
> > In fact what happens here is that the \bgroup token becomes
> > the argument to \macroname and  the {anotherenv}  environment
> > will *always* be processed ...
> >
> >> ---------
> >> so that \begin{env} MANY PARAGRAPHS OF INFO \end{env{}  is the same as
> >> \macroname{MANY PARAGRAPHS OF INFO}.
> >>
> >> When I do this, I get
> >> ---------
> >> ! Extra }, or forgotten \endgroup.
> >> \env ->\end {anotherenv}\egroup
> >>
> >> l.39 \end{env}
> >
> >  ... so that the closing  \egroup  is totally redundant, not matching
> > anything at all.
> >
> >
> >
> >> how I can do this? I have a similar set up where anotherenv is defined
> >
> > Have you tried  comment.sty  ?
> >
> >> by newtheorem instead of newenvironment, and that works, but I don't 
> >> see
> >> how that's helpful.
> >
> > Hmm; that may be a bit harder to achieve.
> >
> >> The end goal is to have the macro display its contents if a flag is 
> >> set,
> >> or to execute other code if the flag is not set. If there is a better
> >> way to do this, please let me know.
> >
> > Have a look at the  comment.sty  package.
> > Your *flag* can be implemented as  \includecomment and/or  
> > \excludecomment
> > calls, with the appropriate environment names.
> >
> >
> > Hope this helps,
> >
> > 	Ross Moore
> >
> >
> >> Thanks.
> >>
> >> Michael
> 
> 
> -----------------------------------------------------------------
> Threaded list archives can be found at:
> <http://www.masda.vxu.se/~pku/MacOSX_TeX/>
> -----------------------------------------------------------------
> To UNSUBSCRIBE, send email to <info at email.esm.psu.edu> with
> "unsubscribe macosx-tex" (no quotes) in the body.
> For additional HELP, send email to <info at email.esm.psu.edu> with
> "help" (no quotes) in the body.
> -----------------------------------------------------------------


-----------------------------------------------------------------
Threaded list archives can be found at:
<http://www.masda.vxu.se/~pku/MacOSX_TeX/>
-----------------------------------------------------------------
To UNSUBSCRIBE, send email to <info at email.esm.psu.edu> with
"unsubscribe macosx-tex" (no quotes) in the body.
For additional HELP, send email to <info at email.esm.psu.edu> with
"help" (no quotes) in the body.
-----------------------------------------------------------------




More information about the macostex-archives mailing list