用不同的颜色进行枚举编号

用不同的颜色进行枚举编号

有没有办法让枚举环境中的不同数字具有不同的颜色。

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

我希望将 1 设为绿色,将 2 设为橙色,将 3 设为红色

即不是后面的文字而是前面的数字特定颜色

谢谢

答案1

下面的例子使用包定义了 colors enum1, enum2, ... 。然后它挂钩到添加 color 命令,其颜色由 counter 的值组成。对于更深层次的环境嵌套,upto也可以相应地更改。\colorletxcolor\labelenumienumienumerate\labelenumii\labelenumiv

\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}

结果

嵌套列表的扩展:

\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}

结果嵌套

答案2

另一种方法。使用\ColorList,您可以定义要应用于标签的颜色的逗号分隔列表;如果需要,颜色将循环:

\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}

在此处输入图片描述

我使用了大卫·卡莱尔的回答如何定义一个命令来将列表的内容输入到 \ifcase 构造中?

答案3

以下只是更新\labelenumi-负责打印标签的宏:

在此处输入图片描述

\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}

答案4

这将循环显示每个级别定义的颜色。新\listcolors命令将从该点开始重新定义颜色。

通过此实现,颜色循环将从结束的地方重新开始;可以通过各种方式修改设置,例如使任何级别colorenumerate从红色重新开始。

\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}

在此处输入图片描述

相关内容