# stack.sty and \end{environment}

I'm writing a multilevel itemize list environment to simplify the process of putting nested lists in Beamer slides, and I want it to support enumerate lists as well. The mitemize environment is supposed to work as follows:

\begin{mitemize}
\enumeratelist
\mitem{1} Candidate Generation
\mitem{2} From annotated data
\mitem{2} Using heuristics from unannotated data
\mitem{1} Connect the slots to create trees
\mitem{1} Pruning and Sorting
\mitem{2} Covering algorithm on annotated corpus
\mitem{2} Sorting linkage specifications by structure
\end{mitemize}


Which is supposed to look as follows:

I've gotten the package working with one problem: whenever mitemize.sty tries to close an enumerate or itemize list, the result is an error message:

\begin{itemize} on input line 104 ended by \end{itemize}.


or

\begin{enumerate} on input line 504 ended by \end{enumerate}.


Apparently \Push{stackname}{itemize}\end{\Stack{stackname}} isn't the same as \end{itemize}. Any ideas how to fix this:

\ProvidesPackage{mitemize}
\RequirePackage{stack}
\RequirePackage{ifthen}

\NewStack{mitemizeStack}{\empty}
\newcounter{mitemize@depth}

\newenvironment{mitemize}{
\newcommand{\mitem}[1]{%
\whiledo{\value{mitemize@depth}>##1}{%
\end{\Stack{mitemizeStack}}%
\Pop{mitemizeStack}%
\addtocounter{mitemize@depth}{-1}%
}%
\whiledo{\value{mitemize@depth}<##1}{%
\addtocounter{mitemize@depth}{1}%
\Push{mitemizeStack}{itemize}
\begin{itemize}%
}%
\item%
}%
\newcommand{\enumeratelist}{%
\addtocounter{mitemize@depth}{1}%
\Push{mitemizeStack}{enumerate}
\begin{enumerate}%
}%
}{
\whiledo{\value{mitemize@depth}>0}{%
\end{\Stack{mitemizeStack}}%
\Pop{mitemizeStack}%
\addtocounter{mitemize@depth}{-1}%
}%
}


Here's a more minimal example to play with. The output is correct, but I get the error \begin{itemize} on input line 7 ended by \end{itemize}.

\documentclass{article}
\usepackage{stack}
\NewStack{endenvstack}{\empty}
\begin{document}

\Push{endenvstack}{itemize}
\begin{itemize}
\item Foo
\item Bar
\item Baz
\end{\Stack{endenvstack}}

Text that comes after the list

\end{document}

-

## 1 Answer

I got a working version by putting creating new commands to hide the \end{enumerate} and \end{itemize} and calling them using csname.

In my minimal example, this works out to:

\documentclass{article}
\usepackage{stack}

\newcommand{\wrapenditemize}{\end{itemize}}    %create a wrapper

\NewStack{endenvstack}{\empty}
\begin{document}

\Push{endenvstack}{wrapenditemize}             %the wrapper's name is pushed as normal text
\begin{itemize}
\item Foo
\item Bar
\item Baz
\csname\Stack{endenvstack}\endcsname           %the wrapper's name is popped as normal text
%and then turned into a macro call
%using \csname

Text that comes after the list

\end{document}

-
The problem is the way LaTeX defines \@checkend as \def\@checkend#1{\def\reserved@a{#1}\ifx\reserved@a\@currenvir \else\@badend{#1}\fi}. The use of ifx means that the unexpanded contents of \reserved@a (in this case \Stack{endenvstack}) are checked for to see whether they match the environment name, and if they don't, an error message is printed. The rest of the \end macro works the way it should in the presence of commands that evaluate to the environment name. –  Ken Bloom Aug 22 '11 at 14:51