从环境内部终止和启动环境

从环境内部终止和启动环境

如何终止一个环境并通过另一个环境恢复该环境?这似乎也破坏了resume定义始终恢复的列表

代码

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

产生所需的输出(除了破坏resume列表的功能):

enter image description here

我想删除以下几行:

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

因此认为解决方案是定义一个新的环境:

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

但这不能编译——这个用法在MWE中被注释掉了。

接下来我尝试用

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

这似乎可以编译,但它仍然破坏了resume计数器的功能定义始终恢复的列表

笔记:

  • 注释的代码MyEnvironmentAlternate是我尝试改编的解决方案暂停环境并由另一个环境恢复。大卫警告不要在家里尝试这个,而由于我在那里,所以我尝试了替代\EndAndResume解决方案。

代码:

\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}

相关内容