jbezos / enumitem

Customize enumerate, itemize and description
MIT License
48 stars 5 forks source link

\fi found in environment body #34

Open kalekje opened 2 years ago

kalekje commented 2 years ago

I think this is a bug. I want to make an EnumitemKey to wrap my \items in an environemnt. But the body of the itemize begins with a \fi. I see this in my log output: test >>>\fi \item one \item two \item three

\documentclass{scrartcl}
\usepackage{enumitem}
\usepackage{luacode}

\NewDocumentEnvironment{TestItemize}{ +b }{
   \luadirect{texio.write_nl('test >>>'..\luastringN{#1})}
   \luadirect{tex.print(\luastringN{#1})}
%    #1
}{}

\SetEnumitemKey{test}{
    first*=\TestItemize,
    after=\endTestItemize,
}

\begin{document}
    ...
\begin{itemize}[test]
\item one
\item two
\item three
\end{itemize}
    ...
\end{document}
muzimuzhi commented 2 years ago

The xparse arg-spec +b is the cause. first*=\TestItemize will append \TestItemize to \enit@keyfirst and when the latter expands, \TestItemize treats the \fi on line 1382 as part of the environment content. https://github.com/jbezos/enumitem/blob/1bdcad0987823b3716c86b126b3863f895ea9c4a/enumitem.sty#L1379-L1382

I'm surprised that such usage is ever supported. Usually it's suggested to use any envs that collect its content in the way that when \begin{envname} expands, \end{envname} is seen. But since your code is so close to work, with some knowledge about the enumitem internals, using

\SetEnumitemKey{test}{
    first*=\expandafter\TestItemize, % ensure this is the last part stored in `\enum@keyfirst`
    after=\endTestItemize,
}

seems to work.