3

Consider the following MWE:

\documentclass{article} \usepackage{amsmath} \usepackage{xcolor} \usepackage{enumitem} \newlist{enumatlib}{enumerate}{2}% \setlist[enumatlib]{ label=\arabic*, font=\bfseries, align=left, leftmargin=*, labelsep=1em, labelwidth=*, itemsep=2.5ex, before=\setupmodenumerate } \setlist[enumatlib,2]{ label=\Alph*, font=\bfseries, leftmargin=*, itemsep=1ex, before=\setupmodenumerate} \newif\ifmoditem \newif\ifcitem \newcommand{\setupmodenumerate}{% \global\moditemfalse \global\citemfalse \let\origmakelabel\makelabel \def\moditem##1{\global\moditemtrue\def\mesymbol{\footnotesize\normalfont\color{red}\parbox[t]{10em}{\raggedleft##1}}\item}% \def\citem##1{\global\citemtrue\def\cecolor{##1}\item}% \def\makelabel##1{% \origmakelabel{\ifmoditem\llap{\mesymbol\enspace}\else\ifcitem\color{\cecolor}\fi\fi##1}% \global\moditemfalse\global\citemfalse}% } %----------------- usage examples ----------------- \begin{document} Default behaviour (both features on): \begin{enumatlib} \moditem{M4.5 (KSC)} First question \begin{enumatlib} \citem{red} option 1 \item option 2 \item option 3 \item option 4 \end{enumatlib} \moditem{GT8.9 (KSC)} Second question \begin{enumatlib} \item option 1 \citem{red} option 2 \item option 3 \item option 4 \end{enumatlib} \end{enumatlib} \end{document} 

tmp1

I would like assistance in creating a key-value that toggles off and on the entries for moditem and citem. See below.

\begin{enumatlib}[mod=false,citem=false] \moditem{M4.5 (KSC)} First question \begin{enumatlib} \citem{red} option 1 \item option 2 \item option 3 \item option 4 \end{enumatlib} \moditem{GT8.9 (KSC)} Second question \begin{enumatlib} \item option 1 \citem{red} option 2 \item option 3 \item option 4 \end{enumatlib} \end{enumatlib} 

tmp2

By default \citem will always have red as the color but having the option to choose the color is ideal.

2 Answers 2

3

If I understood you correctly, you’d like \citem[<color>] to color the item label (defaulting to red) and to control this at the list level via the keys mod=true|false, citem=true|false, and citemcolor=<color>.

In the proposed solution, I believe both requirements are met. To achieve this, it’s convenient to load the additional package pgfkeys (and if your LaTeX installation is older than 2020, you’ll also need to load the xparse package). You can see what other changes to your code were necessary.

The task turned out to be non‑trivial, so I had to introduce quite a few modifications—but I hope the reasoning is clear. I’ll be happy to answer any further questions!

\documentclass{article} \usepackage{amsmath} \usepackage{xcolor} \usepackage{enumitem} %\usepackage{xparse} \usepackage{pgfkeys} %-------------------------------------------- % 1) Low-level list driven by enumitem %-------------------------------------------- \newlist{enumatliblist}{enumerate}{2} \setlist[enumatliblist]{% label=\arabic*, font=\bfseries, align=left, leftmargin=*, labelsep=1em, labelwidth=*, itemsep=2.5ex, before=\setupmodenumerate } \setlist[enumatliblist,2]{% label=\Alph*, font=\bfseries, leftmargin=*, itemsep=1ex, before=\setupmodenumerate } %-------------------------------------------- % 2) Per-list flags and keys (pgfkeys) %-------------------------------------------- \newif\ifenumatlibusemod \newif\ifenumatlibusecitem \enumatlibusemodtrue \enumatlibusecitemtrue \def\enumatlibcitemcolor{red} \pgfkeys{/enumatlib/.is family, /enumatlib, mod/.is if=enumatlibusemod, mod/.default=true, citem/.is if=enumatlibusecitem, citem/.default=true, citemcolor/.store in=\enumatlibcitemcolor, citemcolor/.default=red, default/.style={mod=true,citem=true,citemcolor=red} } % wrapper environment with key-value interface \NewDocumentEnvironment{enumatlib}{ O{} O{} }{% \pgfkeys{/enumatlib,#1}% \begin{enumatliblist}[#2] }{% \end{enumatliblist} } %-------------------------------------------- % 3) Label machinery %-------------------------------------------- \newif\ifmoditem \newif\ifcitem \newcommand{\setupmodenumerate}{% \global\moditemfalse \global\citemfalse \let\origmakelabel\makelabel \def\mesymbol{}% \def\cecolor{\enumatlibcitemcolor}% \def\makelabel##1{% \origmakelabel{% \ifmoditem\llap{\mesymbol\enspace}\fi \ifcitem\color{\cecolor}\fi ##1% }% \global\moditemfalse \global\citemfalse }% } %-------------------------------------------- % 4) User commands \moditem and \citem %-------------------------------------------- \NewDocumentCommand{\moditem}{m}{% \ifenumatlibusemod \global\moditemtrue \def\mesymbol{\footnotesize\normalfont\color{red}\parbox[t]{10em}{\raggedleft #1}}% \item \else \item \fi } \NewDocumentCommand{\citem}{o}{% \ifenumatlibusecitem \IfNoValueTF{#1}{\def\cecolor{\enumatlibcitemcolor}}{\def\cecolor{#1}}% \global\citemtrue \item \else \item \fi } %----------------- demo ----------------- \begin{document} Default (both features on): \begin{enumatlib} \moditem{M4.5 (KSC)} First question \begin{enumatlib} \citem option 1 \item option 2 \item option 3 \item option 4 \end{enumatlib} \moditem{GT8.9 (KSC)} Second question \begin{enumatlib} \item option 1 \citem[blue] option 2 % (we can change the color) \item option 3 \item option 4 \end{enumatlib} \end{enumatlib} Features off via keys: \begin{enumatlib}[mod=false,citem=false] \moditem{M4.5 (KSC)} First question \begin{enumatlib} \citem option 1 \item option 2 \item option 3 \item option 4 \end{enumatlib} \end{enumatlib} Per-list default color: \begin{enumatlib}[citemcolor=green!60!black][label=\Alph*] \item First \citem Second \item Third \end{enumatlib} \end{document} 

enter image description here

3

This is in principle similar to @kabenyuk's answer. However a few differences:

  • I used expkv (keys defined via expkv-def; disclaimer: I'm the author of those packages) instead of pgfkeys.

  • The environment still only takes one optional argument and forwards all keys not defined for enumatlib to the underlying enumitem-list.

  • There are only two keys citem and moditem. Both take true or false as values or a colour definition. true will activate the respective item type without changing the used colour, false will deactivate them (and no value will behave like true). Any colour definition will also activate it additionally to setting the colour (everything except true or false or no value is considered a colour definition).

  • \citem doesn't take an optional colour. If you need that I can provide it, but the two optional items following each other provide a poor interface, imho.

  • I further changed your code's internals, using unified names for internal things

  • \moditem doesn't break alignment if more than a single line is needed for it.

\documentclass{article} \usepackage{amsmath} \usepackage{xcolor} \usepackage{enumitem} \usepackage{expkv-def} \makeatletter \protected\long\def\enumatlib@addunknowns#1% {\edef\enumatlib@unknowns{\unexpanded\expandafter{\enumatlib@unknowns, #1}}} \ekvdefinekeys{enumatlib} { boolTF citem = \enumatlib@ifanycitem ,protected unknown-choice citem = \edef\enumatlib@citem@color{\unexpanded{#1}}% \ekvmorekv{citem=true}% ,boolTF moditem = \enumatlib@ifanymoditem ,protected unknown-choice moditem = \edef\enumatlib@moditem@color{\unexpanded{#1}}% \ekvmorekv{moditem=true}% ,protected unknown code = \enumatlib@addunknowns{#3 = {#2} } ,protected unknown noval = \enumatlib@addunknowns{#2 } } \ekvsetdef\enumatlib@kv{enumatlib} \enumatlib@kv{citem=red,moditem=red} \NewDocumentEnvironment{enumatlib}{O{}} {% \let\enumatlib@unknowns\@empty \enumatlib@kv{#1}% \expandafter\enumatlib@list\expandafter[\enumatlib@unknowns]% } {\endenumatlib@list} \newlist{enumatlib@list}{enumerate}{2}% \setlist[enumatlib@list]{ label=\arabic*, font=\bfseries, align=left, leftmargin=*, labelsep=1em, labelwidth=*, itemsep=2.5ex, before=\enumatlib@setup } \setlist[enumatlib@list,2]{ label=\Alph*, font=\bfseries, leftmargin=*, itemsep=1ex, before=\enumatlib@setup} \NewDocumentCommand\citem{} {% \enumatlib@ifanycitem{\global\let\enumatlib@citem\enumatlib@citem@active}{}% \item } \NewDocumentCommand\moditem{m} {% \enumatlib@ifanymoditem {% \edef\enumatlib@moditem@text{\unexpanded{#1}}% \global\let\enumatlib@moditem\enumatlib@moditem@active } {}% \item } \protected\def\enumatlib@moditem@active {% \llap {% \footnotesize\normalfont\color{\enumatlib@moditem@color}% \parbox[t][0pt][t]{10em}{\raggedleft\enumatlib@moditem@text}% \enspace }% \global\let\enumatlib@moditem\@empty }% \protected\def\enumatlib@citem@active {% \color{\enumatlib@citem@color}% \global\let\enumatlib@citem\@empty } \protected\def\enumatlib@setup {% \global\let\enumatlib@moditem\@empty \global\let\enumatlib@citem\@empty \let\origmakelabel\makelabel \def\makelabel##1% {% \origmakelabel {% \enumatlib@moditem \enumatlib@citem ##1% }% }% } \makeatother %----------------- usage examples ----------------- \begin{document} Default behaviour (both features on): \begin{enumatlib} \moditem{M4.5 (KSC)} First question \begin{enumatlib} \citem option 1 \item option 2 \item option 3 \item option 4 \end{enumatlib} \moditem{GT8.9 (KSC) string strang strung} Second question \begin{enumatlib} \item option 1 \citem option 2 \item option 3 \item option 4 \end{enumatlib} \end{enumatlib} Changed behaviour, no moditems and citems in green. \begin{enumatlib}[moditem=false,citem=green,left=2cm] \moditem{M4.5 (KSC)} First question \begin{enumatlib} \citem option 1 \item option 2 \item option 3 \item option 4 \end{enumatlib} \moditem{GT8.9 (KSC)} Second question \begin{enumatlib}[label=\roman*] \item option 1 \citem option 2 \item option 3 \item option 4 \end{enumatlib} \end{enumatlib} \end{document} 

enter image description here

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.