# Terminate and Start and environment from within the environment

How can I terminate and resume an environment by another environment? This also seems to have broken the resume feature which was resolved in Define a List that is Always Resumed.

The code

\begin{MyEnvironment}
First entry
\end{MyEnvironment}
\hrule\smallskip\par
\begin{MyEnvironment}
Some alternate entry
\end{MyEnvironment}


yields the desired output (except for breaking the resume feature of the list):

I would like to eliminate the lines:

\end{MyEnvironment}
\hrule\smallskip\par
\begin{MyEnvironment}


so thought that a solution would be to define a new enviroment:

\newenvironment{MyEnvironmentAlternate}{%
\end{MyEnvironment}
\hrule\smallskip\par
\begin{MyEnvironment}
}{%
%\end{MyEnvironment}
}%


but this does not compile -- usage of this is commented out in the MWE.

Next I tried replacing the three lines with

\newcommand*{\EndAndResume}{%
\end{MyEnvironment}
\hrule\smallskip\par
\begin{MyEnvironment}
}%


which seems to compile, however it still breaks the resume feature of the counter as per Define a List that is Always Resumed.

## Notes:

• The commented code in MyEnvironmentAlternate is my attempt to adapt the solution from Suspending environments and resuming by another environment. David warned about not trying this at home, and since that is where I am, I attempted the alternate \EndAndResume solution.

## Code:

\documentclass{article}

\usepackage{enumitem}
\usepackage{environ}

\newlist{MyList}{enumerate}{1}
\setlist[MyList]{label={\arabic*)},leftmargin=2cm, resume}

\newenvironment{MyEnvironment}{%  Don't want to have to change this enviromnent
\noindent\textbf{MyEnvironment}
\begin{MyList}
\item
}{%
\end{MyList}%
}%

%% The commented code here is my attempt to adapt the solution from
%% https://tex.stackexchange.com/questions/237478/suspending-environments-and-resuming-by-another-environment
\newenvironment{MyEnvironmentAlternate}{% Need to define this environment
%%\endgroup%
\end{MyEnvironment}%
\hrule\smallskip\par
\begin{MyEnvironment}%
%%\begingroup%
%%\def\@currenvir{MyEnvironmentAlternate}%
}{%
\end{MyEnvironment}%
}%

\begin{document}
%Resumed list works here:
%
%\begin{MyList}
%    \item A
%\end{MyList}
%\begin{MyList}
%    \item B
%\end{MyList}

\begin{MyEnvironment}
First entry
\end{MyEnvironment}
\hrule\smallskip\par
\begin{MyEnvironment}
Some alternate entry
\end{MyEnvironment}

\noindent
The above is the desired result (except for the problem with counter), but want to do it within  \verb|MyEnvironment| as below:

%\begin{MyEnvironment}
%    First entry
%    \begin{MyEnvironmentAlternate}
%        Some alternate entry
%    \end{MyEnvironmentAlternate}
%\end{MyEnvironment}
\end{document}

• resume works locally, you need series if you want the counter to survive additional grouping. Beside this you imho mixing to much in your question. Also it don't find the "MyEnvironmentAlternate"-syntax logical. The alternate entry is not a level down. – Ulrike Fischer Mar 10 '16 at 13:53
• do you plan to use it exactly as your example above? – touhami Mar 10 '16 at 14:08
• @UlrikeFischer: I will post a separate question regarding the counter. Yes, the example as is does seem wacky. My actual use case is a bit more complicated, but thought that this MWE was the simplest I could make it. Basically I am trying to hack one of my own environments -- explained in following comment – Peter Grill Mar 10 '16 at 23:02
• @touhami: No, the actual use case is different. The content of MyEnvironment is set by a macro and saved for later processing. This content does not contain the title, \begin{MyList} including the \item. This macro expects only one item. So, I could define a separate MyEnvironmentAlternate, but thought I could just hack the content passed to MyEnvironment. – Peter Grill Mar 10 '16 at 23:02