# Using pgfkeys to switch between "enumerate" and "enumerate*" when using the enumitem package with the package option "inline""

No matter what I seem to do, I'm just getting this wrong.

I would like to be able to set a key to determine how my list is arranged.

\documentclass{article}
\usepackage[inline]{enumitem}
\usepackage{xparse}
\usepackage{pgfkeys}
\usepackage{pgffor}
\pgfkeys{/ae/testing/.cd,
list collection/.store in=\mylistcollection,
arrange horizontally/.code={\def\myenumitem{enumerate*}},
}
\def\myenumitem{enumerate}
\def\aeset#1{\pgfkeys{/ae/testing/.cd,#1}}
\def\aeget#1{\pgfkeysvalueof{/ae/testing/#1}}

\NewDocumentCommand{\mylist}{ m }
{\bgroup
\aeset{#1}
\expandafter\begin\expandafter{\myenumitem}[label=(\alph*)]
\foreach \x in \mylistcollection {\item \x}
\expandafter\end\expandafter{\myenumitem}
\egroup
}

\pagestyle{empty}
\begin{document}

\mylist{list collection={a,b,c,d}}

\mylist{list collection={a,b,c,d},arrange horizontally}

\end{document}


Initially, I wanted to have the key arrange horizontally be defined as

arrange horizontally/.is if=ArrangeHorizontally,


and then also define

\newif\ifArrangeHorizontally
\ArrangeHorizontallyfalse


but that too was giving me problems (in fact, I've never been able to get .is if to work the way I expect them to.

Nevertheless, the code as I've written it above results in an error:

! Extra }, or forgotten \endgroup.
\item ...ctor \relax \fi }\color@endgroup \egroup
\enit@outeritem
l.32 ...collection={a,b,c,d},arrange horizontally}

?


I don't see what's going on. I would think everything should expand as expected.

I also tried working around using \expandafter\begin\expandafter{.... by directly calling \enit@enumerate* or enit@enumerate. But that too resulted in errors.

UPDATE

Here is my attempt with the .is if approach

\documentclass{article}
\usepackage[inline]{enumitem}
\usepackage{xparse}
\usepackage{pgfkeys}
\usepackage{pgffor}
\pgfkeys{/ae/testing/.cd,
list collection/.store in=\mylistcollection,
arrange horizontally/.is if=ArrangeHorizontally,
}
\newif\ifArrangeHorizontally
\ArrangeHorizontallyfalse

\def\myenumitem{enumerate}
\def\aeset#1{\pgfkeys{/ae/testing/.cd,#1}}
\def\aeget#1{\pgfkeysvalueof{/ae/testing/#1}}

\makeatletter
\NewDocumentCommand{\mylist}{ m }
{\bgroup
\aeset{#1}
\ifArrangeHorizontally\@nameuse{enumerate*}\else\expandafter\@nameuse{enumerate}\fi
[label=(\alph*)]
\foreach \x in \mylistcollection {\item \x}
\ifArrangeHorizontally\@nameuse{endenumerate*}\else\expandafter\@nameuse{endenumerate}\fi

\egroup
}

\makeatother
\pagestyle{empty}
\begin{document}

\mylist{list collection={a,b,c,d}}

\mylist{list collection={a,b,c,d},arrange horizontally=true}

\end{document}


Here's how I would do it. The code uses \etoolbox's list processing tools, especially \forlistloop{<handler>}{<list macro>} (which is expandable). And since it's already loaded it also uses \etoolbox's boolean tests and its \ifdefempty and \csuse.

I added a warning message in case an empty list or no list has been given.

\documentclass{article}

\usepackage[inline]{enumitem}
\usepackage{pgfkeys,etoolbox}

\makeatletter
\newbool{ae@enumerate@inline}
\def\ae@list@collection{}

\pgfkeys{
ae/.cd ,
list collection/.code =
arrange horizontally/.is if = ae@enumerate@inline
}

\newrobustcmd\aeset[1]{\pgfkeys{ae/.cd,#1}}

\newrobustcmd\aelist[1]{%
\begingroup
\aeset{#1}%
\ifdefempty\ae@list@collection{%
\@latex@warning@no@line{%
\string\aelist: Empty List! I'll do nothing\on@line
}%
}
{%
\ifbool{ae@enumerate@inline}
{\csuse{enumerate*}}
{\enumerate}[label=(\alph*)]
\forlistloop{\item}\ae@list@collection
\ifbool{ae@enumerate@inline}
{\csuse{endenumerate*}}
{\endenumerate}%
}%
\endgroup
}

\begin{document}

\aelist{list collection={one,two}}

\aelist{list collection={one,two},arrange horizontally}

\aelist{list collection={one,two}}

\aeset{arrange horizontally=true}

\aelist{list collection={one,two}}

\end{document}