# Suspending environments and resuming by another environment

Is it possible to interrupt a LaTeX environment, say OuterEnv, by another environment, say InnerEnv, the later closing the first in the environment startup code, doing 'whatever' and then resuming the outer one in the end code, proceeding as if nothing has been done before?

This is not a question about resuming enumerate etc. lists.

I know there might be issues with environment arguments and especially with the environment startup code, let alone the grouping feature of environments.

I fear the answer is no :-(

Here is a non-working MWE. LaTeX complains about the InnerEnv ending the OuterEnv (of course ;-))

\documentclass{article}

\usepackage{xcolor}
\usepackage{blindtext}

\newenvironment{OuterEnv}{%
\parindent0pt
Foo

\textcolor{red}{\blindtext}%
}{%

Bar\vskip2\baselineskip% Just for debugging
}

\newenvironment{InnerEnv}{%
\end{OuterEnv} % Fails
Closing Outer...

\textcolor{blue}{\blindtext}%
}{%

Resuming Outer\vskip3\baselineskip
\begin{OuterEnv} %Fails too
}

\begin{document}

\begin{OuterEnv}

\begin{InnerEnv}
\end{InnerEnv}
\end{OuterEnv}

\end{document}


I dot not stick on \newenvironment. \NewDocumentEnvironment for xparse will do too, of course!

• Interesting idea! Basically this means that you want to temporarily escape the current group (or inject stuff in the outer group), a bit like \global, but just "one level up". I don't think this is possible, but I am looking forward to the answers of egreg, David, Bruno et al. – Daniel Apr 8 '15 at 11:34
• @Daniel: Yes, I am aware about the grouping stuff. Perhaps something with \aftergroup? I think our Gurus will have answers (either good or bad ;-)) – user31729 Apr 8 '15 at 11:36

Don't do this at home...

\documentclass{article}

\usepackage{xcolor}
\usepackage{blindtext}
\makeatletter
\newenvironment{OuterEnv}{%
\parindent0pt
Foo

\textcolor{red}{\blindtext}%
}{%

Bar\vskip2\baselineskip% Just for debugging
}

\newenvironment{InnerEnv}{%
\endgroup
\end{OuterEnv} % Fails
Closing Outer...

\textcolor{blue}{\blindtext}%
}{%

Resuming Outer\vskip3\baselineskip
\begin{OuterEnv} %Fails too
\begingroup
\def\@currenvir{InnerEnv}%
}

\makeatother
\begin{document}

\begin{OuterEnv}

\begin{InnerEnv}
\end{InnerEnv}
\end{OuterEnv}

\end{document}

• Ah, I was missing the \@currenvir stuff. \endgroup and \begingroup was there in previous version, but it was not enough, obviously. What are the caveats? – user31729 Apr 8 '15 at 11:50
• @ChristianHupfer caveats? a list as long as your arm:-) It makes all kinds of assumptions about both environments. Any environment using special formatting is likely to fail (tabular and friends) any environment using catcode tricks will fail (verbatim, listings, ...) any environment that traps the environment body as a macro will fail (ams alignments, tabularx, anything defined by the environ package commands) any environment that assumes its begin and end code are in the same group will fail, some other things not listed will fail. Apart from that, it should always work. – David Carlisle Apr 8 '15 at 12:10
• Ok, three or four environments remaining then ;-) – user31729 Apr 8 '15 at 12:36