Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

share|improve this question
    
Congratulations to you question. I guess there is no other question on the whole stackexchange which was answered by only four (!) people (as of Jan. 26, 2013: Heiko, Gonzalo, Werner, egreg), having a weight of 650.000 reputation points! If points were gravity, your thread would instantly turn into a black hole. And something else is symptomatic: no solution uses Lua. –  Keks Dose Jan 26 at 20:00

4 Answers 4

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

share|improve this answer
    
Dear Heiko, when I begun reading the code, I thought »Heiko has written that!«. Scrolling down, it was true and it seems I recognise your style of coding sometimes. –  Keks Dose Jan 26 at 19:53

Another approach. Using \ColorList, you define a comma separated list of colors to be applied to the labels; the colors will cycle if required:

\documentclass{article}
\usepackage{xcolor}
\usepackage{xpatch}

\makeatletter
\def\ColorList#1{\def\xcolorlist{#1}}
\let\@xitem\@item
\def\xitem{%
\@inmatherr\item
\@ifnextchar [\@xitem{\@noitemargtrue \@xitem[\@itemlabel]}}
\def\item{\expandafter\xxcycle\xcolorlist,\xcolorlist\xitem}
\def\xxcycle#1,#2{%
  \ifx\relax#1\relax\else
   \color{#1}%
   \ifx\xcolorlist#2\else
    \xcycle#1,#2%
   \fi
  \fi}
\def\xcycle#1,#2\fi\fi#3\xcolorlist{\fi\fi\ColorList{#2#3#1}}
\xpatchcmd{\@xitem}{\hskip \labelsep}{\hskip \labelsep\normalcolor}{}{}
\makeatother

\ColorList{green,orange,red,magenta}

\begin{document}

\begin{enumerate}
\item First
\item Second
\item Third
  {\ColorList{cyan,gray,olive}
  \begin{enumerate}
  \item First
  \item Second
  \item Third
  \item[($\ast$)] Fourth
  \item Fifth
  \item Sixth
  \end{enumerate}}
\item Fourth
\item Fifth
\item Sixth
\end{enumerate}

\end{document}

enter image description here

I used a variation of David Carlisle's answer to How to define a command to feed the contents of a list into an \ifcase construct?.

share|improve this answer

This will cycle through colors defined for each level. A new \listcolors command will redefine the colors from that point on.

With this implementation, color cycling will restart from where it ended; it's possible to modify the setup in various ways, for instance making any level one colorenumerate start again from red.

\documentclass{article}
\usepackage{xparse}
\usepackage{enumitem,xcolor}

\ExplSyntaxOn
\NewDocumentCommand{\listcolors}{ m m }
 {
  \listcolor_set:nn { #1 } { #2 }
 }

\NewDocumentEnvironment{colorenumerate}{ O{} }
  {\enumerate[format=\listcolor_format:,#1]}
  {\endenumerate}

\tl_new:N \l_listcolor_currentcolor_tl

% syntactic sugar
\cs_new:Npn \listcolor_makename:n #1
 {
  g_listcol_level_ \int_to_roman:n { #1 } _seq
 }

\cs_new_protected:Npn \listcolor_set:nn #1 #2
 {
  \seq_gclear_new:c { \listcolor_makename:n { #1 } }
  \seq_gset_from_clist:cn { \listcolor_makename:n { #1 } } { #2 }
 }

\cs_new_protected:Npn \listcolor_format:
 {
  \seq_if_empty:cF { \listcolor_makename:n { \int_use:c { enit@depth } } }
   {
    % Fetch the color name from the start of the sequence
    \seq_gpop_left:cN  { \listcolor_makename:n { \int_use:c { enit@depth } } }
                       \l_listcolor_currentcolor_tl
    % Put it back at the end of the sequence
    \seq_gput_right:cV { \listcolor_makename:n { \int_use:c { enit@depth } } }
                       \l_listcolor_currentcolor_tl
    % Use the current color
    \color{ \l_listcolor_currentcolor_tl }
   }
 }

\ExplSyntaxOff

\listcolors{1}{red,green,blue}
\listcolors{2}{cyan,magenta,yellow}

\begin{document}
\begin{colorenumerate}
\item Abc
\item Def
  \begin{colorenumerate}
  \item Inner
  \item Again
  \item[??] Well
  \end{colorenumerate}
\item Ghi
\item Jkl
\end{colorenumerate}
\end{document}

enter image description here

share|improve this answer

The following just updates \labelenumi - the macro responsible for printing the label:

enter image description here

\documentclass{article}
\usepackage{xcolor}% http://ctan.org/pkg/xcolor
\renewcommand\labelenumi{%
  {\ifcase\value{enumi}% 0
  \or\color{green}% 1
  \or\color{orange}% 2
  \or\color{red}% 3
  \else\fi%
  \arabic{enumi}.}%
}
\makeatother
\begin{document}
\begin{enumerate}
  \item First
  \item Second
  \item Third
  \item Last
\end{enumerate}
\end{document}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.