7

I want to create an automatic itemize environment where the item's color font is alternating.

Suposing the following list:

\begin{itemize}
   \item A
   \item B
   \item C
   \item D
   \item E
\end{itemize}

I want a code to A, C, and E have color #1 and B and D, color #2.

Yes, I know I can create an alternative \item command (\coloreditem or something such that), but I am asking for an automated way. So, if I insert on the fly a new item between other two items of that list, I do not need to (manually) change the color scheme of every item after the new one.

0

2 Answers 2

10

Like this?

\documentclass[]{article}

\usepackage{xcolor}
\usepackage{duckuments}
\usepackage{letltxmacro}

\newif\iffirstcoloreditem
\newcommand*\colorizeitemize[2]
  {%
    \LetLtxMacro\itemBAK\item
    \renewcommand*\item[1][]
      {%
        \iffirstcoloreditem
          \firstcoloreditemfalse
          \color{#1}%
        \else
          \firstcoloreditemtrue
          \color{#2}%
        \fi
        \if\relax\detokenize{##1}\relax
          \itemBAK
        \else
          \itemBAK[##1]%
        \fi
      }%
  }

\begin{document}
\bgroup
\colorizeitemize{blue}{red}
\duckitemize
\duckenumerate
\duckdescription
\egroup
\end{document}

enter image description here

5
  • Good choice of colors :)
    – Marijn
    Mar 16, 2018 at 15:50
  • @Marijn you too :)
    – Skillmon
    Mar 16, 2018 at 15:51
  • 2
    I did not know the duckuments package. Funnier than lipsum ;) Mar 16, 2018 at 16:24
  • @FabianoFagundes no wonder you didn't know, I pushed it to CTAN only a few days ago :)
    – Skillmon
    Mar 16, 2018 at 16:25
  • This is ducking clever!
    – FeRD
    Mar 17, 2018 at 8:26
10

You can add a custom macro before each \item with the xpatch package. In the example below the \choosecolor macro is defined which alternates the color using a toggle.

MWE:

\documentclass{article}
\usepackage{xcolor}
\usepackage{xpatch}
\newtoggle{tcolor}
\newcommand{\choosecolor}{%
\iftoggle{tcolor}%
{\color{\colorone}\togglefalse{tcolor}}%
{\color{\colortwo}\toggletrue{tcolor}}%
}
\pretocmd{\item}{\choosecolor}{}{}

\begin{document}
\def\colorone{red}
\def\colortwo{blue}
\begin{itemize}
\item first item
\item second item
\item third item
\end{itemize}
Text after itemize
\end{document}

Result:

enter image description here

2
  • So, in your example, how I am sure that every new itemize list starts with tcolor=true? I think the tcolor toggle does not reset automatically after each itemize list. EDIT: Nevermind, I just tested and works very fine, reseting the toggle. Thanks :) Mar 16, 2018 at 16:37
  • In my nature I always thank from the heart those who help with the heart Sebastiano.
    – Sebastiano
    Sep 7, 2019 at 16:20

You must log in to answer this question.

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