Changing an Enumerate Item Display When Using \newtcbox

#enumerateenumitemtcolorbox

To change the value of an enumerate item within a basic enumerate environment seems to be fairly straightforward; consider

\documentclass{book}
\begin{document}
\LARGE
\begin{enumerate}
\item Sentence.
\item[10.] Sentence.
\item Sentence.
\end{enumerate}
\end{document}

which produces

enter image description here

However, the same process does not necessarily work for a slightly more fancy enumerate environment—say, when making use of \newtcbox; for example,

\documentclass[openany]{book}
\usepackage{tcolorbox}

\newtcbox{\greenbox}[1][]{nobeforeafter,
     notitle,
     colframe=green!20!black,
     colback=green!50!black,
     top=4pt,
     left=5pt,
     right=5pt,
     bottom=2pt,
     fontupper=\sffamily\bfseries,
     colupper=white,
     tcbox raise base,
     #1}
\usepackage{enumitem}
\setlist{label*={\greenbox{\arabic*}}}

\begin{document}
\thispagestyle{empty}
\Large
\begin{enumerate}
\item Sentence.
\item Sentence.
%\item[2*] Description.
\item Sentence.
\end{enumerate}
\end{document}

produces the list

enter image description here

But when I run the same code with the second item replaced by \item[2*] Description, I get the following output.

enter image description here

QUESTION: How may I modify the above code so that, for instance, "2*" is displayed within the green box as the second item in the list?

Thank you.

Best Answer

First, I think it is worth noting that enumitem is consistent here, nothing that works in your first example fails in your second one. When you write a value manually, the format is never copied from the other entries. It's only that in your first example, the only formatting done on the counter is to add a dot after the number, which you did:

\item[10.] Sentence.

Using the same idea in the second example works just as well: replacing the line of item 2* with

\item[\greenbox{2*}] Description.

yields the expected output

Now, if you want to automatically add the formatting when writing a value manually, I think the easiest way is to modify slightly the behavior of \item to wrap the optional argument in \greenbox{...} when it is given. The following example outputs the list of the previous image.

\documentclass[openany]{book}
\usepackage{tcolorbox}

\newtcbox{\greenbox}[1][]{nobeforeafter,
     notitle,
     colframe=green!20!black,
     colback=green!50!black,
     top=4pt,
     left=5pt,
     right=5pt,
     bottom=2pt,
     fontupper=\sffamily\bfseries,
     colupper=white,
     tcbox raise base,
     #1}
\usepackage{enumitem}
\let\olditem\item
\newcommand*{\greenboxitems}{%
    \renewcommand*{\item}[1][]{%
        \ifblank{##1}{\olditem}{\olditem[\greenbox{##1}]}%
    }%
}
\setlist{
    label*={\greenbox{\arabic*}},
    before=\greenboxitems,
    after=\let\item\olditem,
}

\begin{document}
\thispagestyle{empty}
\Large
\begin{enumerate}
\item Sentence.
\item[2*] Description.
\item Sentence.
\end{enumerate}
\end{document}
Related Question