# strange interaction between mdframed and \item

I would like to understand (and to find a solution for it) the following problem. First, the MWE :

\documentclass{article}
\usepackage{mdframed}
\usepackage{fancybox}

\newsavebox{\mcBox}

\newenvironment{monItemize}{
\renewcommand{\item}%
{\end{lrbox}%
\fbox{
\usebox{\mcBox}
}   \\\begin{lrbox}{\mcBox}}%
\begin{lrbox}{\mcBox}%
}
{\end{lrbox}
\fbox{%
\usebox{\mcBox}%
}%
}

\newcommand{\itemm}{}
\newenvironment{monItemizeMD}{
\renewcommand{\itemm}%
{\end{lrbox}%
\begin{mdframed}[userdefinedwidth=3cm]%
\usebox{\mcBox}
\end{mdframed}%
\begin{lrbox}{\mcBox}}%
\begin{lrbox}{\mcBox}%
}
{\end{lrbox}
\begin{mdframed}[userdefinedwidth=3cm]%
\usebox{\mcBox}
\end{mdframed}%
}

\begin{document}

\begin{monItemize}
\item aaa
\item bbb
\end{monItemize}

\begin{monItemizeMD}
\itemm ccc
\itemm ddd
\end{monItemizeMD}

\end{document}


In the environment monItemize, I redefine \item, which puts the content in a fbox.

In the environment monItemizeMD, I define the items thru a command \itemm, instead of \item, and the content lies in a mdframed-Box instead of a \fbox.

1. Question: If I use \item in monItemizeMD (of course, both in the definition and in the Instance of the environment), it fails to compile. Why ?
2. Question: How secure is it, generally speaking, to redefine \item ?
3. Question: Can i solve the problem of Question 1, still using \item ? Because my problem is, that, in many already existing documents, i have such an environment with \item redefined. And in my documentclass, i wanted to replace the \fbox by mdframed boxes.

1) \item is a vertical mode command, it starts a new paragraph, basically, but lrbox is like \fbox which is like \mbox a horizontal (LR) mode construct that doesn't allow vertical material unless you first nest a parbox or minipage or similar.

2) As noted the other day in Use of \trivlist and \list in defining environments LaTeX uses lists everywhere. If you redefine \item you have redefined center and verbatim environments for example. UPDATE In particular you would also be redefining mdframed.

3) I'm not sure quite what the top level requirement is that you want to solve, you want to box each item of an enumerate and allow breaking within the box?

• about 2 : just for science, i would like to know what makes in this case the interference with \item. the center – nicolas roy Mar 9 '12 at 18:19
• like all trivlist environments (actually mdf@trivlist variant here), the mdframed environment is implemented as a single item list so it calls \item\relax internally, so if you define \item to end one environment and start another it's bound to go wrong. – David Carlisle Mar 9 '12 at 18:50

Redefining \item can be dangerous and have impredictable results, as you discovered. If all you want is to box the text after \item with a width of 3cm, then

\newsavebox{\mcBox}
\newif\ifstartmonItemize
\newenvironment{monItemize}
{\startmonItemizetrue
\renewcommand{\item}{%
\ifstartmonItemize
\startmonItemizefalse
\else
\end{lrbox}
\noindent
\framebox[3cm][l]{\usebox{\mcBox}}\\
\fi
\begin{lrbox}{\mcBox}\strut}%
}
{\end{lrbox}\noindent\framebox[3cm][l]{\usebox{\mcBox}}}


will do.

• Unfortunately, i want more. Depending on some boolean it should display the items in a itemizeor in a tabular, with some extra feature. But this all works without mdframd... or wit another command name as \item. I think i learnt a lesson : \item should be used ... only for what it is intended to :) – nicolas roy Mar 9 '12 at 15:46
• As a linguist I really enjoy reading impredictable, that's a great example of partial anticipatory assimilation – thank you! – dessert Oct 23 '17 at 16:48
• @dessert False friend, should be unpredictable, I guess – egreg Oct 23 '17 at 16:59