[Tex/LaTex] Make enumerate numbering different colours

#enumeratecolor

Is there a way to make the different numbers within the enumerate environment different colors.

\begin{enumerate}
\item First
\item Second
\item Third
\end{enumerate}

I wish to make the 1. green, the 2. orange and the 3. red

i.e Not the text that follows but the preceding number a particular colour

Thanks

Best Answer

The following example defines colors enum1, enum2, ... using \colorlet of package xcolor. Then it hooks into \labelenumi to add the color command with the color that consists of the value of counter enumi. For deeper nesting levels of the enumerate environment, \labelenumii upto \labelenumiv can also be changed accordingly.

\documentclass{article}

\usepackage{xcolor}
\colorlet{enum1}{green}
\colorlet{enum2}{orange}
\colorlet{enum3}{red}

\makeatletter
\newcommand*{\IfColorUndefined}[1]{%
  \begingroup
    \escapechar=`\\ %
    \expandafter\expandafter\expandafter
  \endgroup
  \expandafter\ifx\csname\string\color @#1\endcsname\relax
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi
}
\makeatother

\usepackage{etoolbox}
\preto\labelenumi{%
  \IfColorUndefined{enum\the\value{enumi}}{}{%
    \color{enum\the\value{enumi}}%
  }%
}

\begin{document}
  \begin{enumerate}
    \item First
    \item Second
    \item Third
  \end{enumerate}
\end{document}

Result

Extension for nested lists:

\documentclass{article}

\usepackage{xcolor}
\colorlet{enum1}{green}
\colorlet{enum2}{orange}
\colorlet{enum3}{red}

\makeatletter
\newcommand*{\IfColorUndefined}[1]{%
  \begingroup
    \escapechar=`\\ %
    \expandafter\expandafter\expandafter
  \endgroup
  \expandafter\ifx\csname\string\color @#1\endcsname\relax
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi
}
\makeatother

\usepackage{etoolbox}
\newcommand*{\DefColorListLabel}[1]{%
  \expandafter\preto\csname label#1\endcsname{%
    \IfColorUndefined{enum\the\value{#1}}{}{%
      \color{enum\the\value{#1}}%
    }%
  }%
}
\DefColorListLabel{enumi}
\DefColorListLabel{enumii}
\DefColorListLabel{enumiii}
\DefColorListLabel{enumiv}

\begin{document}
  \begin{enumerate}
    \item First
    \item Second
    \item Third\label{third}
    \item Fourth
    \begin{enumerate}
      \item Nested first
      \item Nested second
      \item Nested third\label{nestedthird}
      \item Nested fourth
    \end{enumerate}  
  \end{enumerate}
  References: \ref{third} and \ref{nestedthird}.
\end{document}

Result nested