[comp.text.tex] LaTeX: How to include \end{document} in a macro?

guzman-juan@cs.yale.edu (Juan Carlos Guzman) (04/12/91)

Hi,

I have defined the environment `file', which should work essentially
like `document', except that I can have nested file environments, in
which case the nested ones behave as noop.

  \newenvironment{file}[1]
    {\ifnum\value{FileCounter}=0\begin{document}\fi
     \addtocounter{FileCounter}{1}}
    {\addtocounter{FileCounter}{-1}
     \ifnum\value{FileCounter}=0\end{document}\fi}

The environment works fine, but when it's time to close the
document, LaTeX complains:

   % latex myfile
   LaTeX error.  See LaTeX manual for explanation.
		 Type  H <return>  for immediate help.
   ! \begin{file} ended by \end{document}.
   ...
   l.123 \end{file}

   ?

After hitting <CR>, the file is closed with no problem, except that
\end{file} does not seem to have been closed properly (note
complaining about \ifnum not being complete):

   [3] (myfile.aux) )
   (\end occurred inside a group at level 1)
   (\end occurred when \ifnum on line 123 was incomplete)
   Output written on .dvi (3 pages, 7200 bytes).
   Transcript written on myfile.log.

From this I gather that when LaTeX processes \end{document}, it
immediately stops further processing of the input.  So my question is:
How can I do to `delay' LaTeX's processing of \end{document} until it
has completely expanded \end{file}?

On a related note, How should I change the macro so that when LaTeX
reaches a recursive \end{file} it ignores further input from this
file, and returns processing its parent?

Thanks a lot,

Juan Carlos Guzman
guzman-juan@cs.yale.edu
-- 
Juan in a million

piet@cs.ruu.nl (Piet van Oostrum) (04/17/91)

>>>>> guzman-juan@cs.yale.edu (Juan Carlos Guzman) (JCG) writes:

JCG> I have defined the environment `file', which should work essentially
JCG> like `document', except that I can have nested file environments, in
JCG> which case the nested ones behave as noop.

JCG> The environment works fine, but when it's time to close the
JCG> document, LaTeX complains:

JCG>    % latex myfile
JCG>    LaTeX error.  See LaTeX manual for explanation.
JCG> 		 Type  H <return>  for immediate help.
JCG>    ! \begin{file} ended by \end{document}.
JCG>    ...
JCG>    l.123 \end{file}

JCG>    ?

Apparently \begin{document} and \end{document} are hard to use in another
environment. However, the following works (but strictly speaking it is an
undocumented feature):

  \newenvironment{file}[1]
    {\ifnum\value{FileCounter}=0\document\fi
     \addtocounter{FileCounter}{1}}%
    {\addtocounter{FileCounter}{-1}
     \ifnum\value{FileCounter}=0\enddocument\fi}

Note: The % must also be given to get rid of a space token.
-- 
Piet* van Oostrum, Dept of Computer Science, Utrecht University,
Padualaan 14, P.O. Box 80.089, 3508 TB Utrecht, The Netherlands.
Telephone: +31 30 531806   Uucp:   uunet!mcsun!ruuinf!piet
Telefax:   +31 30 513791   Internet:  piet@cs.ruu.nl   (*`Pete')