3

In the following MWE

\documentclass[draft]{article}

\newenvironment{myitem}{\item}{}%

\begin{document}

\begin{enumerate}
\item \begin{enumerate}
  \item Item 1
  \item Item 2
  \end{enumerate}
\end{enumerate}

\begin{enumerate}
\item \begin{enumerate}
    \begin{myitem} Item 1 \end{myitem}  % This is the only difference
    \item Item 2
  \end{enumerate}
\end{enumerate}

% even this works!!!!
\begin{enumerate}
\item \begin{enumerate}
    \myitem Item 1 \endmyitem  % This is the only difference
    \item Item 2
  \end{enumerate}
\end{enumerate}

\end{document}

The output of the first part is correct, but the output of my verbose myitem environment (which should just replicate \item) is missing the number on the second line. EDIT: Any idea why this is happening? Note that if I add any text in the outer \item, everything works as expected (the inner enumerate starting on a new line).

In my real use-case, I have to execute some code at the beginning and end of each item, which is why I adopted the use of an environment.

EDIT: thanks for the suggestions regarding other ways to resolve this. But I am also quite interested to learn why the environment approach is failing.

EDIT2: I found out that I can replicate the wrong behaviour with.


\begin{enumerate}
\item \begin{enumerate}
    \begingroup \item test 1 \endgroup
    \item item 2
  \end{enumerate}
\end{enumerate}

So clearly the fact that the environments are starting groups around the \item is the issue. I thought I could change the definition of my environment as follows:

\newenvironment{myitem}{\endgroup\item\begingroup}{}%

To cause the group to be created outside \item, but then I get the error

\begin{enumerate} on input line 15 ended by \end{myitem}.
1

2 Answers 2

2

Description

I've used another approach to get what you want.

The code defined in \itemBeginCode and \itemEndCode is executed before and after each \myitem within the myenumerate environment.

Result

enter image description here

Code

\documentclass{article}

\usepackage{etoolbox}

\newcommand{\itemBeginCode}{\emph{(start)}}
\newcommand{\itemEndCode}{\emph{(end)}}

\newtoggle{myfirstitem}
\newenvironment{myenumerate}{%
    \toggletrue{myfirstitem}%
    \begin{enumerate}%
}{%
    \itemEndCode% for the last item
    \end{enumerate}%
}

\newcommand{\myitem}{%
    \iftoggle{myfirstitem}{}{\itemEndCode}% if not the first item
    \item%
    \togglefalse{myfirstitem}%
    \itemBeginCode%
}

\begin{document}

\begin{enumerate}
\item
    \begin{myenumerate}
    \myitem Item 1
    \myitem Item 2
    \end{myenumerate}
\end{enumerate}

\end{document}
1
  • Thanks. I actually would like to keep using the environment as it has other advantages in my case. I am just wondering why using the environment has this weird behaviour.
    – Tohiko
    Feb 9 at 8:25
0

Found a solution from one of my own previous questions. Sorry for the noise.

I just needed to define the environment like this.

\makeatletter
\long\def\myitem{{\ifnum0=`}\fi\@myitem\@argend}%
\long\def\@myitem#1\@argend#2\end#3{%
\expandafter\ifstrequal\expandafter{#3}{myitem}%
{\ifnum0=`{\fi}\end{myitem}\@myrealitem{#1#2}}{\@myitem#1#2\end{#3}\@argend}}
\def\@myrealitem#1{\item #1}
\makeatother

You must log in to answer this question.

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