颜色选择

颜色选择

欢迎大家。我必须在文本中使用多种颜色(超过 10 种)。我想构建一个命令,该命令接受 2 个强制项(单词和颜色编号)并使用所需颜色打印给定的单词。以下 MWE 提供了更多详细信息:

\documentclass{article}
\usepackage{xparse}
\usepackage[dvipsnames]{xcolor}

\colorlet{col1}{red}
\colorlet{col2}{green}
\colorlet{col3}{blue}

\NewDocumentCommand{\cText}{mm}{\textcolor{#2}{#1}}

\begin{document}
  this is \cText{red}{col1}, this is \cText{green}{col2} and this is \cText{blue}{col3}.
  this is \cText{another red}{col1}, this is \cText{another green}{col2} and this is \cText{another red}{col1}.
  % The desired version is 
  %this is \cText{red}{1}, this is \cText{green}{2} and this is \cText{blue}{3}.
  %this is \cText{another red}{1}, this is \cText{another green}{2} and this is \cText{another red}{1}.{3}.
\end{document}

任何帮助,特别是基于的帮助expl3,都将受到高度赞赏。

答案1

使用更简单的界面来定义颜色。“左侧”可以是任何字符串。

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

\ExplSyntaxOn

\prop_new:N \g_aloui_colors_prop

\NewDocumentCommand{\setdocumentcolors}{m}
 {
  \prop_gset_from_keyval:Nn \g_aloui_colors_prop { #1 }
 }

\NewDocumentCommand{\cText}{mm}
 {
  \textcolor{ \prop_item:Nn \g_aloui_colors_prop { #2 } } { #1 }
 }

\ExplSyntaxOff

\setdocumentcolors{
  1=red,
  2=green,
  3=blue,
  x=pink,
}

\begin{document}

\cText{red}{1} \cText{green}{2} \cText{blue}{3} \cText{pink}{x}

\end{document}

在此处输入图片描述

相关内容