# How to “shallow” \renewenvironment of \itemize?

I want to patch exactly the outer level of \itemize, whereas the following MWE (expectedly) patches all of them:

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


..which is both evident and expected:

..as =foo= appears twice.

My best attempt so far had the more-or-less direct approach, namely wrapping the contents with \bgroup\let\itemize=..\let\savedenditemize=..} {\egroup or, more precisely, replacing the \renewenvironment piece with:

\renewenvironment{itemize}
{\begin{minipage}{5cm}
=foo=\saveditemize\bgroup\let\itemize=\saveditemize\let\enditemize=\savedenditemize}
{\egroup\savedenditemize\end{minipage}}


This, however, fails, since the \begin\end environment tracker apparently goes off rails:

! 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.


Having approached this problem from multiple angles for quite a while, I'm out of ideas..

• What exactly are you trying to achieve in format? This seems like something better handled with the enumitem package. – Alan Munn Jun 18 '16 at 21:05
• I'm sorry, I don't see what you're trying to obtain. – Bernard Jun 18 '16 at 21:07
• The general context is this: 0. Beamer, 1. I don't control the TeX file, only the Beamer theme, 2. The (invariant) environment hierarchy is \frame-\customenv-\itemize-\item-\itemize-\item, 3. I need to be able to thoroughly rewrite the outer \itemize – deepfire Jun 18 '16 at 21:14
• Sorry, but that's really too vague. Also, if you're using beamer you should have made the example using beamer. The lists in beamer are not customizable using the same methods as in regular document classes, so this may make a big difference. – Alan Munn Jun 18 '16 at 21:21
• Alan, and you're perfectly right, indeed -- I have failed to port Ulrike's entirely valid answer into my beamer problem. Shame on my head! Would it be appropriate for me to ask another, similar question with the extended context? – deepfire Jun 18 '16 at 21:34

The nesting level is stored in \@listdepth, so you could check its value:

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


• I have failed to transplant the solution to my beamer problem, but this is a good step. – deepfire Jun 18 '16 at 21:27