6

Since it was pointed out that redefining \item can be dangerous in strange interaction between `mdframed` and `\item`, I am wondering if there are going to be problems with this solution.

My goal is to replace:

\begin{enumerate}[label={(\alph*)}]
    \MyItem \TextA
    \MyItem \TextB
\end{enumerate}

enter image description here

with (i.e., only the environment name is changed):

\begin{MyEnumerate}[label={(\alph*)}]
    \MyItem \TextA
    \MyItem \TextB
\end{MyEnumerate}

to obtain an mdframe around each item:

enter image description here

The solution as below seems to work, at least for this test case. So, is this way of using \MyItem instead of \item have hidden dangers that are not obvious in this test case.

Notes:

Code:

\documentclass{article}
\usepackage{etoolbox}% for toggles
\usepackage{enumitem}
\usepackage{mdframed}

\newcommand{\TextA}{%
        Lorem ipsum dolor sit amet, consectetur adipiscing elit. 
        Sed accumsan hendrerit velit, vitae ultrices sapien porta nec.  
}%
\newcommand{\TextB}{%
        Duis blandit tempus placerat. 
        Nulla vitae erat ante. Nulla facilisi. 
        Aliquam tristique interdum suscipit. 
        Duis posuere orci vel velit suscipit in porttitor purus eleifend. 
}%


\newcommand{\MyItem}{\item}

\newtoggle{FirstItem}%
\toggletrue{FirstItem}%
\newenvironment{MyEnumerate}[1][]{%
    \renewcommand{\MyItem}{%
        \iftoggle{FirstItem}{%
            \global\togglefalse{FirstItem}
            %--------- start new frame
            \begin{mdframed}[skipbelow=2pt]%
            \begin{enumerate}[#1,series=MySeries]%
        }{%
            \end{enumerate}%
            \end{mdframed}%
            %--------- end previous frame and start new one
            \begin{mdframed}[skipbelow=2pt]%
            \begin{enumerate}[#1,resume*=MySeries]%
        }%
        \item%
    }%
}{%
    \end{enumerate}%
    \end{mdframed}% --------- end last frame
    \global\toggletrue{FirstItem}%
}%

\begin{document}\noindent
\textbf{enumerate:}
\begin{enumerate}[label={(\alph*)}]
    \MyItem \TextA
    \MyItem \TextB
\end{enumerate}
%
\medskip
\textbf{MyEnumerate:}
\begin{MyEnumerate}[label={(\alph*)}]
    \MyItem \TextA
    \MyItem \TextB
\end{MyEnumerate}
\end{document}
5
  • I think it's easier to provide a command like mydoubleline which will be passed before every item is set. Commented May 18, 2012 at 20:55
  • @MarcoDaniel: That seems like redefining \item which according to the linked question is dangerous. Or, perhaps I misunderstood what you meant. Commented May 18, 2012 at 20:58
  • The command must be set by hand. You are right your solution is more comfortable. Commented May 18, 2012 at 21:01
  • 1
    I tested your example and it works well with the current version at github -- Great. (Side note: mdframed loads etoolbox) Commented May 18, 2012 at 21:14
  • I suggest to answer your own question with works well ;-) Commented May 21, 2012 at 16:20

1 Answer 1

3

this cw is to reduce the unanswered questions

In my opinion your code works well.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .