在 PdfLatex 中使用 \newcommand 包含分類顏色

在 PdfLatex 中使用 \newcommand 包含分類顏色

我希望有一個預先定義的 HTML 顏色代碼列表,並將它們轉換為編號的可重複使用變量,以便稍後引用。當您希望對一定數量的變數或類別進行內聯著色而無需記住定義名稱,或\textcolour{colname}{text}每次建立大量定義並鍵入時,這非常有用。

定義:

cols = ['#222','#ffc','#123']

用法:

\c1 {text in firstcolour} and then \c2{text in secondcolour}

答案1

我們可以將計數器與 newcommand 巨集結合使用來將來產生可以為文字著色的新命令。下面給出一個簡單的例子:

%define colours pre document 

\newcounter{cnt}
\newcommand{\cdef}[1]{%
 \stepcounter{cnt}%
\xglobal \definecolor{#1}{HTML}{#1}
\expandafter\newcommand\csname c\Roman{cnt}\endcsname{\textcolor{#1}}%
}


%start the main body
\begin{document}



% Define list of colours to run here, reference with \cI \cII \cIII etc... 
\cdef{00CCFF}
\cdef{99CC99}


%write away within the document
\cI {test c1} and then \cII{test c2}

這會產生以下結果:

在此輸入影像描述

答案2

您可以建立一個由數字引用的清單:

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

\ExplSyntaxOn
\NewDocumentCommand{\colorlist}{m}
 {
  \clist_map_inline:nn {#1}
   {
    \seq_gput_right:Nn \g_colorlist_list_seq { ##1 }
   }
 }

\NewDocumentCommand{\usecolor}{mm}
 {
  \textcolor[HTML]{ \seq_item:Nn \g_colorlist_list_seq { #1 } } { #2 }
 }

\seq_new:N \g_colorlist_list_seq

\ExplSyntaxOff

\colorlist{828282,FF0000,00FF00}

\begin{document}

\usecolor{1}{text in first color}

\usecolor{2}{text in second color}

\usecolor{3}{text in third color}

\end{document}

請注意,xcolor需要以字串形式給出 HTML 顏色十六進制數字,大寫或小寫。

在此輸入影像描述

答案3

你請求命令\c
\cLaTeX 2e 核心已經定義了用變音符號來傳送信件。
因此,在下面的範例中,\ColorNumber定義了一個命令。


以下是您自己提供的答案的變體:

\documentclass{article}
\usepackage{xcolor}

\makeatletter

% define colours pre document 

\newcommand\MYNAMESPACEDefinedColorTotal{0}%
\newcommand\cdef[1]{%
 \@bsphack
 \begingroup
 \@tempcnta=\MYNAMESPACEDefinedColorTotal\relax
 \advance\@tempcnta by 1\relax
 \xdef\MYNAMESPACEDefinedColorTotal{\number\@tempcnta}%
 \endgroup
 \xglobal\definecolor{%
   MYNAMESPACEDefinedColor\MYNAMESPACEDefinedColorTotal%
 }{HTML}{#1}%
 \@esphack
}

\newcommand\ColorNumber[1]{\textcolor{MYNAMESPACEDefinedColor#1}}%

\makeatother

%start the main body
\begin{document}

\cdef{00CCFF}
\cdef{99CC99}

%write away within the document
\ColorNumber{1}{test c1} and then \ColorNumber{2}{test c2}


\end{document}

在此輸入影像描述


這是您作為自己的答案提供的另一種變體,這裡\ColorNumber的第一個參數通過參數文本中的 - 符號用大括號分隔#1#- 這更接近您在問題中要求的符號:

\documentclass{article}
\usepackage{xcolor}

\makeatletter

% define colours pre document 

\newcommand\MYNAMESPACEDefinedColorTotal{0}%
\newcommand\cdef[1]{%
 \@bsphack
 \begingroup
 \@tempcnta=\MYNAMESPACEDefinedColorTotal\relax
 \advance\@tempcnta by 1\relax
 \xdef\MYNAMESPACEDefinedColorTotal{\number\@tempcnta}%
 \endgroup
 \xglobal\definecolor{%
   MYNAMESPACEDefinedColor\MYNAMESPACEDefinedColorTotal%
 }{HTML}{#1}%
 \@esphack
}

\newcommand\ColorNumber{}%
\def\ColorNumber#1#{\textcolor{MYNAMESPACEDefinedColor#1}}%

\makeatother

%start the main body
\begin{document}

\cdef{00CCFF}
\cdef{99CC99}

%write away within the document
\ColorNumber1{test c1} and then \ColorNumber2{test c2}


\end{document}

在此輸入影像描述


這是另一種變體,其中數字後面的空格被刪除:

\documentclass{article}
\usepackage{xcolor}

\makeatletter
%%=============================================================================
%% Paraphernalia:
%%    \UD@firstoftwo, \UD@secondoftwo,\UD@CheckWhetherNull,
%%=============================================================================
\newcommand\UD@firstoftwo[2]{#1}%
\newcommand\UD@secondoftwo[2]{#2}%
%%-----------------------------------------------------------------------------
%% Check whether argument is empty:
%%.............................................................................
%% \UD@CheckWhetherNull{<Argument which is to be checked>}%
%%                     {<Tokens to be delivered in case that argument
%%                       which is to be checked is empty>}%
%%                     {<Tokens to be delivered in case that argument
%%                       which is to be checked is not empty>}%
%%
%% The gist of this macro comes from Robert R. Schneck's \ifempty-macro:
%% <https://groups.google.com/forum/#!original/comp.text.tex/kuOEIQIrElc/lUg37FmhA74J>
\newcommand\UD@CheckWhetherNull[1]{%
  \romannumeral0\expandafter\UD@secondoftwo\string{\expandafter
  \UD@secondoftwo\expandafter{\expandafter{\string#1}\expandafter
  \UD@secondoftwo\string}\expandafter\UD@firstoftwo\expandafter{\expandafter
  \UD@secondoftwo\string}\expandafter\expandafter\UD@firstoftwo{ }{}%
  \UD@secondoftwo}{\expandafter\expandafter\UD@firstoftwo{ }{}\UD@firstoftwo}%
}%
%%-----------------------------------------------------------------------------
%% \UD@RemoveTrailingSpaces{<argument>} removes trailing spaces from <argument>.
%% !!!! <argument> must not contain the token \UD@seldom !!!!
%%.............................................................................
\begingroup
\newcommand\UD@RemoveTrailingSpaces[1]{%
  \endgroup
  \newcommand\UD@RemoveTrailingSpaces[1]{%
    \romannumeral0%
    \UD@trimtrailspaceloop.##1\UD@seldom#1\UD@seldom\UD@seldom#1{.##1}%
  }%
  \newcommand\UD@trimtrailspaceloop{}%
  \long\def\UD@trimtrailspaceloop##1#1\UD@seldom##2\UD@seldom#1##3{%
    \UD@CheckWhetherNull{##2}{%
      \UD@firstoftwo{ }##3%
    }{%
      \UD@trimtrailspaceloop##1\UD@seldom#1\UD@seldom\UD@seldom#1{##1}%
    }%
  }%
}%
\UD@RemoveTrailingSpaces{ }%

% define colours pre document 

\newcommand\MYNAMESPACEDefinedColorTotal{0}%
\newcommand\cdef[1]{%
 \@bsphack
 \begingroup
 \@tempcnta=\MYNAMESPACEDefinedColorTotal\relax
 \advance\@tempcnta by 1\relax
 \xdef\MYNAMESPACEDefinedColorTotal{\number\@tempcnta}%
 \endgroup
 \xglobal\definecolor{%
   MYNAMESPACEDefinedColor\MYNAMESPACEDefinedColorTotal%
 }{HTML}{#1}%
 \@esphack
}

\newcommand\ColorNumber{}%
\def\ColorNumber#1#{%
  \expandafter\expandafter\expandafter\@ColorNumber
  \expandafter\expandafter\expandafter{\UD@RemoveTrailingSpaces{#1}}%
}%
\newcommand*\@ColorNumber[1]{%
  \textcolor{MYNAMESPACEDefinedColor#1}%
}%

\makeatother

%start the main body
\begin{document}

\cdef{00CCFF}
\cdef{99CC99}

%write away within the document
\ColorNumber1{test c1} and then \ColorNumber2{test c2}

\ColorNumber 1 {test c1} and then \ColorNumber 2 {test c2}

\end{document}

在此輸入影像描述


如果顏色清單很小,您可以使用類似的方法\UD@PassKthArg提取該清單的第 K 個元素並將其傳遞給\textcolor— 這樣您甚至可以調用,\textcolor甚至無需透過以下方式定義顏色\definecolor

\documentclass{article}
\usepackage{xcolor}%

\makeatletter
%% Code for \UD@PassKthArg
%%=============================================================================
%% Paraphernalia:
%%    \UD@firstoftwo, \UD@secondoftwo, UD@Exchange, \UD@PassFirstToSecond, 
%%    \UD@CheckWhetherNull, UD@CheckWhetherBlank
%%=============================================================================
\newcommand\UD@firstoftwo[2]{#1}%
\newcommand\UD@secondoftwo[2]{#2}%
\newcommand\UD@Exchange[2]{#2#1}%
\newcommand\UD@PassFirstToSecond[2]{#2{#1}}%
%%-----------------------------------------------------------------------------
%% Check whether argument is empty:
%%.............................................................................
%% \UD@CheckWhetherNull{<Argument which is to be checked>}%
%%                     {<Tokens to be delivered in case that argument
%%                       which is to be checked is empty>}%
%%                     {<Tokens to be delivered in case that argument
%%                       which is to be checked is not empty>}%
%%
%% The gist of this macro comes from Robert R. Schneck's \ifempty-macro:
%% <https://groups.google.com/forum/#!original/comp.text.tex/kuOEIQIrElc/lUg37FmhA74J>
\newcommand\UD@CheckWhetherNull[1]{%
  \romannumeral0\expandafter\UD@secondoftwo\string{\expandafter
  \UD@secondoftwo\expandafter{\expandafter{\string#1}\expandafter
  \UD@secondoftwo\string}\expandafter\UD@firstoftwo\expandafter{\expandafter
  \UD@secondoftwo\string}\expandafter\expandafter\UD@firstoftwo{ }{}%
  \UD@secondoftwo}{\expandafter\expandafter\UD@firstoftwo{ }{}\UD@firstoftwo}%
}%
%%------------------------------------------------------------------------------
%% Check whether argument is blank (empty or only spaces):
%%..............................................................................
%% -- Take advantage of the fact that TeX discards space tokens when
%%    "fetching" _un_delimited arguments: --
%% \UD@CheckWhetherBlank{<Argument which is to be checked>}%
%%                      {<Tokens to be delivered in case that
%%                        argument which is to be checked is blank>}%
%%                      {<Tokens to be delivered in case that argument
%%                        which is to be checked is not blank}%
\newcommand\UD@CheckWhetherBlank[1]{%
  \romannumeral\expandafter\expandafter\expandafter\UD@secondoftwo
  \expandafter\UD@CheckWhetherNull\expandafter{\UD@firstoftwo#1{}.}%
}%
%%=============================================================================
%% Put K-th inner undelimited argument behind sequence of tokens:
%%
%% \UD@PassKthArg{<tokens>}{<ErrorTokens>}{<integer K>}{<list of undelimited args>} 
%% 
%% In case there is no K-th argument in <list of indelimited args> : 
%%   Does deliver <ErrorTokens>.
%% In case there is a K-th argument in <list of indelimited args> : 
%%   Does place that K-th argument with one level of braces removed behind <tokens>.
%%
%% Examples:
%%
%%   \UD@PassKthArg{ELEMENT-}{ERROR}{0}{ABCDE} yields: ERROR
%%
%%   \UD@PassKthArg{ELEMENT-}{ERROR}{3}{ABCDE} yields:  ELEMENT-C
%%
%%   \UD@PassKthArg{ELEMENT-}{ERROR}{3}{AB{CD}E} yields:  ELEMENT-CD
%%
%%   \UD@PassKthArg{ELEMENT-}{ERROR}{4}{{001}{002}{003}{004}{005}} yields: ELEMENT-004
%%
%%   \UD@PassKthArg{ELEMENT-}{ERROR}{6}{{001}{002}{003}} yields: ERROR
%% 
%%=============================================================================
\newcommand\UD@PassKthArg[3]{%
  \romannumeral0%
  % #1: <Tokens>
  % #2: <ErrorTokens>
  % #3: <integer number K>
  \expandafter\UD@PassKthArgCheck
  \expandafter{\romannumeral\number\number#3 000}{#1}{#2}%
}%
\newcommand\UD@PassKthArgCheck[4]{%
  % #1 - <K letters m>
  % #2 - <Tokens>
  % #3 - <ErrorTokens>
  % #4 - <list>
  \UD@CheckWhetherNull{#1}{ #3}{%
    \expandafter\UD@PassKthArgLoop\expandafter{\UD@firstoftwo{}#1}{#4}{#2}{#3}%
  }%
}%
\newcommand\UD@PassKthArgLoop[4]{%
  % #1 - <letters m in an amount that corresponds to the number
  %       of arguments trailing the to-be-kept argument>
  % #2 - <list>
  % #3 - <Tokens>
  % #4 - <ErrorTokens>
  \UD@CheckWhetherBlank{#2}{ #4}{%
    \UD@CheckWhetherNull{#1}{%
      \UD@ExtractFirstArgLoop{#2\UD@SelDOm}{#3}%
    }{%
      \expandafter\UD@PassFirstToSecond\expandafter{\UD@firstoftwo{}#2}%
      {\expandafter\UD@PassKthArgLoop\expandafter{\UD@firstoftwo{}#1}}%
      {#3}{#4}%
    }%
  }%
}%
\newcommand\UD@RemoveTillUD@SelDOm{}%
\long\def\UD@RemoveTillUD@SelDOm#1#2\UD@SelDOm{{#1}}%
\newcommand\UD@ExtractFirstArgLoop[2]{%
  \expandafter\UD@CheckWhetherNull\expandafter{\UD@firstoftwo{}#1}%
  {\UD@Exchange#1{ #2}}%
  {\expandafter\UD@ExtractFirstArgLoop\expandafter{\UD@RemoveTillUD@SelDOm#1}{#2}}%
}%
%% End of code for \UD@PassKthArg

\newcommand\ColorNumber[1]{%
  \expandafter\UD@PassFirstToSecond\expandafter{\Mycolorlist}{\UD@PassKthArg{\textcolor}{%
    \GenericError{%
      \@spaces\@spaces\@spaces\@spaces\@spaces\@spaces\@spaces\@spaces\space\space\space
    }{%
       Error with command \string\ColorNumber: \string\Mycolorlist\space does not have a%
       \MessageBreak color-specification-element #1%
    }{Color will not be changed.}{Specify the number of an element which is in the list.}%
  }{#1}}%
}%
\makeatother

\definecolor{myweirdcolor}{HTML}{800080}% PURPLE

% List of color-specifications.
% Each specification must be nested in braces.
% You are not bound to the HTML-Hex-Code-model.

\newcommand\Mycolorlist{%
  {[HTML]{00CCFF}}% CYAN
  {[HTML]{C0C0C0}}% SILVER
  {[HTML]{808080}}% GRAY
  {[HTML]{000000}}% BLACK
  {[rgb]{1,0,0}}  % RED
  {[HTML]{800000}}% MAROON
  {[HTML]{FFFF00}}% YELLOW
  {[HTML]{808000}}% OLIVE
  {[HTML]{00FF00}}% LIME
  {[HTML]{008000}}% GREEN
  {[HTML]{00FFFF}}% AQUA
  {[HTML]{008080}}% TEAL
  {[HTML]{0000FF}}% BLUE
  {[HTML]{000080}}% NAVY
  {[HTML]{FF00FF}}% FUCHSIA
  {{myweirdcolor}}% PURPLE
}

\begin{document}
\noindent
\ColorNumber{1}{test C1 - CYAN}\\
\ColorNumber{2}{test C2 - SILVER}\\
\ColorNumber{3}{test C3 - GRAY}\\
\ColorNumber{4}{test C4 - BLACK}\\
\ColorNumber{5}{test C5 - RED}\\
\ColorNumber{6}{test C6 - MAROON}\\
\ColorNumber{7}{test C7 - YELLOW}\\
\ColorNumber{8}{test C8 - OLIVE}\\
\ColorNumber{9}{test C9 - LIME}\\
\ColorNumber{10}{test C10 - GREEN}\\
\ColorNumber{11}{test C11 - AQUA}\\
\ColorNumber{12}{test C12 - TEAL}\\
\ColorNumber{13}{test C13 - BLUE}\\
\ColorNumber{14}{test C14 - NAVY}\\
\ColorNumber{15}{test C15 - FUCHSIA}\\
\ColorNumber{16}{test C16 - PURPLE}\\
%\ColorNumber{17}{test C17 - ERROR}\\
%\ColorNumber{0}{test C0 - ERROR}
\end{document}

在此輸入影像描述


結合上面的方法(其中數字後面的尾隨空格被刪除)與 \UD@PassKthArg用於提取第 K 個元素的方法 where 會產生:

\documentclass{article}
\usepackage{xcolor}%

\makeatletter
%% Code for \UD@PassKthArg
%%=============================================================================
%% Paraphernalia:
%%    \UD@firstoftwo, \UD@secondoftwo, UD@Exchange, \UD@PassFirstToSecond, 
%%    \UD@CheckWhetherNull, UD@CheckWhetherBlank
%%=============================================================================
\newcommand\UD@firstoftwo[2]{#1}%
\newcommand\UD@secondoftwo[2]{#2}%
\newcommand\UD@Exchange[2]{#2#1}%
\newcommand\UD@PassFirstToSecond[2]{#2{#1}}%
%%-----------------------------------------------------------------------------
%% Check whether argument is empty:
%%.............................................................................
%% \UD@CheckWhetherNull{<Argument which is to be checked>}%
%%                     {<Tokens to be delivered in case that argument
%%                       which is to be checked is empty>}%
%%                     {<Tokens to be delivered in case that argument
%%                       which is to be checked is not empty>}%
%%
%% The gist of this macro comes from Robert R. Schneck's \ifempty-macro:
%% <https://groups.google.com/forum/#!original/comp.text.tex/kuOEIQIrElc/lUg37FmhA74J>
\newcommand\UD@CheckWhetherNull[1]{%
  \romannumeral0\expandafter\UD@secondoftwo\string{\expandafter
  \UD@secondoftwo\expandafter{\expandafter{\string#1}\expandafter
  \UD@secondoftwo\string}\expandafter\UD@firstoftwo\expandafter{\expandafter
  \UD@secondoftwo\string}\expandafter\expandafter\UD@firstoftwo{ }{}%
  \UD@secondoftwo}{\expandafter\expandafter\UD@firstoftwo{ }{}\UD@firstoftwo}%
}%
%%------------------------------------------------------------------------------
%% Check whether argument is blank (empty or only spaces):
%%..............................................................................
%% -- Take advantage of the fact that TeX discards space tokens when
%%    "fetching" _un_delimited arguments: --
%% \UD@CheckWhetherBlank{<Argument which is to be checked>}%
%%                      {<Tokens to be delivered in case that
%%                        argument which is to be checked is blank>}%
%%                      {<Tokens to be delivered in case that argument
%%                        which is to be checked is not blank}%
\newcommand\UD@CheckWhetherBlank[1]{%
  \romannumeral\expandafter\expandafter\expandafter\UD@secondoftwo
  \expandafter\UD@CheckWhetherNull\expandafter{\UD@firstoftwo#1{}.}%
}%
%%==============================================================================
%% Put K-th inner undelimited argument behind sequence of tokens:
%%
%% \UD@PassKthArg{<tokens>}{<ErrorTokens>}{<integer K>}{<list of undelimited args>} 
%% 
%% In case there is no K-th argument in <list of indelimited args> : 
%%   Does deliver <ErrorTokens>.
%% In case there is a K-th argument in <list of indelimited args> : 
%%   Does place that K-th argument with one level of braces removed behind <tokens>.
%%
%% Examples:
%%
%%   \UD@PassKthArg{ELEMENT-}{ERROR}{0}{ABCDE} yields: ERROR
%%
%%   \UD@PassKthArg{ELEMENT-}{ERROR}{3}{ABCDE} yields:  ELEMENT-C
%%
%%   \UD@PassKthArg{ELEMENT-}{ERROR}{3}{AB{CD}E} yields:  ELEMENT-CD
%%
%%   \UD@PassKthArg{ELEMENT-}{ERROR}{4}{{001}{002}{003}{004}{005}} yields: ELEMENT-004
%%
%%   \UD@PassKthArg{ELEMENT-}{ERROR}{6}{{001}{002}{003}} yields: ERROR
%% 
%%=============================================================================
\newcommand\UD@PassKthArg[3]{%
  \romannumeral0%
  % #1: <Tokens>
  % #2: <ErrorTokens>
  % #3: <integer number K>
  \expandafter\UD@PassKthArgCheck
  \expandafter{\romannumeral\number\number#3 000}{#1}{#2}%
}%
\newcommand\UD@PassKthArgCheck[4]{%
  % #1 - <K letters m>
  % #2 - <Tokens>
  % #3 - <ErrorTokens>
  % #4 - <list>
  \UD@CheckWhetherNull{#1}{ #3}{%
    \expandafter\UD@PassKthArgLoop\expandafter{\UD@firstoftwo{}#1}{#4}{#2}{#3}%
  }%
}%
\newcommand\UD@PassKthArgLoop[4]{%
  % #1 - <letters m in an amount that corresponds to the number
  %       of arguments trailing the to-be-kept argument>
  % #2 - <list>
  % #3 - <Tokens>
  % #4 - <ErrorTokens>
  \UD@CheckWhetherBlank{#2}{ #4}{%
    \UD@CheckWhetherNull{#1}{%
      \UD@ExtractFirstArgLoop{#2\UD@SelDOm}{#3}%
    }{%
      \expandafter\UD@PassFirstToSecond\expandafter{\UD@firstoftwo{}#2}%
      {\expandafter\UD@PassKthArgLoop\expandafter{\UD@firstoftwo{}#1}}%
      {#3}{#4}%
    }%
  }%
}%
\newcommand\UD@RemoveTillUD@SelDOm{}%
\long\def\UD@RemoveTillUD@SelDOm#1#2\UD@SelDOm{{#1}}%
\newcommand\UD@ExtractFirstArgLoop[2]{%
  \expandafter\UD@CheckWhetherNull\expandafter{\UD@firstoftwo{}#1}%
  {\UD@Exchange#1{ #2}}%
  {\expandafter\UD@ExtractFirstArgLoop\expandafter{\UD@RemoveTillUD@SelDOm#1}{#2}}%
}%
%% End of code for \UD@PassKthArg
%%==============================================================================
%% \UD@RemoveTrailingSpaces{<argument>} removes trailing spaces from <argument>.
%% !!!! <argument> must not contain the token \UD@seldom !!!!
%%==============================================================================
\begingroup
\newcommand\UD@RemoveTrailingSpaces[1]{%
  \endgroup
  \newcommand\UD@RemoveTrailingSpaces[1]{%
    \romannumeral0%
    \UD@trimtrailspaceloop.##1\UD@seldom#1\UD@seldom\UD@seldom#1{.##1}%
  }%
  \newcommand\UD@trimtrailspaceloop{}%
  \long\def\UD@trimtrailspaceloop##1#1\UD@seldom##2\UD@seldom#1##3{%
    \UD@CheckWhetherNull{##2}{%
      \UD@firstoftwo{ }##3%
    }{%
      \UD@trimtrailspaceloop##1\UD@seldom#1\UD@seldom\UD@seldom#1{##1}%
    }%
  }%
}%
\UD@RemoveTrailingSpaces{ }%
%% End of code for \UD@RemoveTrailingSpaces

\newcommand\ColorNumber{}%
\def\ColorNumber#1#{%
  \expandafter\expandafter\expandafter\@ColorNumber
  \expandafter\expandafter\expandafter{\UD@RemoveTrailingSpaces{#1}}%
}%
\newcommand\@ColorNumber[1]{%
  \expandafter\UD@PassFirstToSecond\expandafter{\Mycolorlist}{\UD@PassKthArg{\textcolor}{%
    \GenericError{%
      \@spaces\@spaces\@spaces\@spaces\@spaces\@spaces\@spaces\@spaces\space\space\space
    }{%
       Error with command \string\ColorNumber: \string\Mycolorlist\space does not have a%
       \MessageBreak color-specification-element #1%
    }{Color will not be changed.}{Specify the number of an element which is in the list.}%
  }{#1}}%
}%
\makeatother

\definecolor{myweirdcolor}{HTML}{800080}% PURPLE

% List of color-specifications.
% Each specification must be nested in braces.
% You are not bound to the HTML-Hex-Code-model.

\newcommand\Mycolorlist{%
  {[HTML]{00CCFF}}% CYAN
  {[HTML]{C0C0C0}}% SILVER
  {[HTML]{808080}}% GRAY
  {[HTML]{000000}}% BLACK
  {[rgb]{1,0,0}}  % RED
  {[HTML]{800000}}% MAROON
  {[HTML]{FFFF00}}% YELLOW
  {[HTML]{808000}}% OLIVE
  {[HTML]{00FF00}}% LIME
  {[HTML]{008000}}% GREEN
  {[HTML]{00FFFF}}% AQUA
  {[HTML]{008080}}% TEAL
  {[HTML]{0000FF}}% BLUE
  {[HTML]{000080}}% NAVY
  {[HTML]{FF00FF}}% FUCHSIA
  {{myweirdcolor}}% PURPLE
}

\begin{document}
\noindent
\ColorNumber1{test C1 - CYAN}\\
\ColorNumber2{test C2 - SILVER}\\
\ColorNumber3{test C3 - GRAY}\\
\ColorNumber4{test C4 - BLACK}\\
\ColorNumber5{test C5 - RED}\\
\ColorNumber6{test C6 - MAROON}\\
\ColorNumber7{test C7 - YELLOW}\\
\ColorNumber8{test C8 - OLIVE}\\
\ColorNumber9{test C9 - LIME}\\
\ColorNumber10{test C10 - GREEN}\\
\ColorNumber11{test C11 - AQUA}\\
\ColorNumber12{test C12 - TEAL}\\
\ColorNumber13{test C13 - BLUE}\\
\ColorNumber14{test C14 - NAVY}\\
\ColorNumber15{test C15 - FUCHSIA}\\
\ColorNumber16{test C16 - PURPLE}\\
%\ColorNumber17{test C17 - ERROR}\\
%\ColorNumber0{test C0 - ERROR}
\end{document}

在此輸入影像描述

答案4

我會使用 xColors 套件:

\usepackage{xcolor}

%Color palette that looks great
\definecolor{xRed}{HTML}{B51E0E}    %[0.71 0.12 0.06]
\definecolor{xGreen}{HTML}{6DB565}  %[0.23 0.51 0.20]
\definecolor{xBlue}{HTML}{074E82}   %[0.03 0.31 0.51]
\definecolor{xBrown}{HTML}{9C5C19}  %[0.61 0.36 0.10]
\definecolor{xYellow}{HTML}{F7B538} %[0.97 0.71 0.22]
\definecolor{xOrange}{HTML}{EC6D00} %[0.93 0.43 0.00]
\definecolor{xCyan}{HTML}{0094AC}   %[0.00 0.58 0.68]
\definecolor{xPurple}{HTML}{8711A1} %[0.53 0.07 0.64]
\definecolor{xPink}{HTML}{D30580}   %[0.83 0.02 0.51]

然後像這樣使用:

\textcolor{xCyan}{cyan}

哦,順便說一句,當為文字著色時,通常用粗體看起來更好:

\textcolor{xCyan}{\textbf{cyan}}

相關內容