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}