我正在编写一个多级itemize
列表环境,以简化在 Beamer 幻灯片中放置嵌套列表的过程,并且我希望它enumerate
也支持列表。该mitemize
环境应该按如下方式工作:
\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}
其看起来应该如下所示:
我让这个包工作时遇到了一个问题:每当mitemize.sty
尝试关闭一个enumerate
或itemize
列表时,都会出现一条错误消息:
\begin{itemize} on input line 104 ended by \end{itemize}.
或者
\begin{enumerate} on input line 504 ended by \end{enumerate}.
显然\Push{stackname}{itemize}\end{\Stack{stackname}}
与 不一样\end{itemize}
。有什么办法可以解决这个问题:
\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}%
}%
}
这是一个更简单的例子。输出是正确的,但我得到了错误\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
我通过创建新命令来隐藏\end{enumerate}
并\end{itemize}
使用 csname 调用它们,从而获得了一个工作版本。
在我的最小示例中,其结果为:
\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}