我想修补 \itemize 的外层,而以下 MWE(预期)会修补所有层:
\documentclass{article}
\begin{document}
\setlength{\fboxsep}{0pt}
\let\saveditemize=\itemize%
\let\savedenditemize=\enditemize%
\renewenvironment{itemize}
{\begin{minipage}{5cm}
=foo=\saveditemize}
{\savedenditemize\end{minipage}}
\begin{itemize}
\item {External item one\\
\begin{itemize}
\item{Internal item one}
\item{Internal item two}
\end{itemize}
}
\item{External item two}
\end{itemize}
\end{document}
..这是显而易见且意料之中的:
到目前为止,我最好的尝试是或多或少直接的方法,即用以下内容包装内容\bgroup\let\itemize=..\let\savedenditemize=..} {\egroup
,或者更准确地说,用\renewenvironment
以下内容替换该部分:
\renewenvironment{itemize}
{\begin{minipage}{5cm}
=foo=\saveditemize\bgroup\let\itemize=\saveditemize\let\enditemize=\savedenditemize}
{\egroup\savedenditemize\end{minipage}}
然而,这失败了,因为\begin\end
环境追踪器显然脱轨了:
! LaTeX Error: \begin{minipage} on input line 14 ended by \end{itemize}.
See the LaTeX manual or LaTeX Companion for explanation.
Type H <return> for immediate help.
...
l.22 \end{itemize}
?
! Emergency stop.
...
l.22 \end{itemize}
No pages of output.
我已经从多个角度研究这个问题很长一段时间了,我没有主意了。
答案1
嵌套级别存储在 \@listdepth 中,因此您可以检查其值:
\documentclass{article}
\begin{document}
\setlength{\fboxsep}{0pt}
\let\saveditemize=\itemize%
\let\savedenditemize=\enditemize%
\makeatletter
\renewenvironment{itemize}
{\ifnum \@listdepth=0\begin{minipage}{5cm}=foo=\fi\saveditemize}
{\savedenditemize\ifnum \@listdepth=0 \end{minipage}\fi}
\makeatother
\begin{itemize}
\item {External item one
\begin{itemize}
\item{Internal item one}
\item{Internal item two}
\end{itemize}
}
\item{External item two}
\end{itemize}
\end{document}