4

Is it possible to create a macro that would close all current open environments? Or even an explicit subset of all current open environments (where the subset would be standard user environments)?

Usage would be something like

\begin{document}
   \begin{example}
   \begin{enumerate}
   \item blah
   \item blah

   \CloseEverything

where \CloseEverything would be equivalent to

   \end{enumerate}
   \end{example}
   \end{document}
2
  • 3
    While that might be possible, it would not really be desirable. One of the main differences between LaTeX and plain TeX is that in LaTeX large parts of the document are presented in balanced \begin...\end pairs, whereas TeX often uses single macros, making it easier to parse the material, and giving access points for the beginning and of an element. If you are annoyed by the typing effort, it is better to use an editor that inserts these constructs as pairs for you. Oct 9, 2014 at 15:15
  • Thanks Andrew, I'm not annoyed by the typing effort. Here's the application I have. I have 200 pages of lecture notes. Each day I mark where we finished in class with the following command \endclass{Wednesday, October 9} Then I set a flag and process the whole document. With the flag I would like the document to terminate at the point marked with \endclass, even if that's inside of a list, inside of my solutions, inside of an example. (You can see the result on the last page of this file evergreen.loyola.edu/educkworth/www/351_Fall_2014/master.pdf) Ethan Oct 9, 2014 at 17:35

1 Answer 1

8

enter image description here

\documentclass{article}
\newenvironment{example}{\par\hrule\bfseries}{\par\hrule}

\makeatletter
\def\CloseEverything{%
\expandafter\end\expandafter{\@currenvir}\CloseEverything
}
\makeatother

\begin{document}
   \begin{example}
   \begin{enumerate}
   \item blah
   \item blah


   \CloseEverything
5
  • 5
    Note that (a) it isn't a good idea to do this and (b) it won't work for special environments eg ammath or tabularx or verbatim etc Oct 9, 2014 at 15:23
  • Thanks David, I'll give it a try in a little while. Even if it works for just the simplest environments that would help. Ethan Oct 9, 2014 at 17:28
  • @EthanDuckworth honestly I would never do this. Oct 9, 2014 at 17:57
  • See my response to Andrew above for a scenario where it might make sense. Oct 9, 2014 at 18:02
  • 1
    @EthanDuckworth sort of sounds almost like a justification but still, I'd never use it personally:-) If you want an emergency stop you can just use \stop latex will moan a bit if its inside an environment but it will close up the document one way or another. Oct 9, 2014 at 18:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.