Latex:使用颜色而不定义新颜色

Latex:使用颜色而不定义新颜色

是否可以使用新颜色而不定义新颜色\definecolor{mycolor}{HTML}{a65dbd}?当颜色仅使用一次时,使用起来确实很麻烦,并且如果文件的多个部分对不同的颜色使用相同的颜色名称,则很容易导致冲突(特别是,如果我们使用\definecolor{mycolor}两次,则不会出现任何错误,因此颜色似乎毫无原因地出错了)。

由于 HTML 颜色很容易在程序之间复制/粘贴,我很乐意能够执行以下操作:

\textcolor{HTML(00AA00)}{\textbf{This is not working}}

梅威瑟:

在此处输入图片描述

\documentclass{article}
\usepackage{xcolor}
\usepackage{tikz}
\usepackage{verbatim}
\definecolor{mycolor}{HTML}{a65dbd} % Equal to RGB(166,93,189).
\begin{document}
\noindent
\textcolor{mycolor}{\textbf{This is my color but annoying to use.}}\\
\textcolor{red!65!green!36!blue!74!}{\textbf{This is better but not accurate nor easily copy/pastable.}}\\
\textcolor[HTML]{a65dbd}{\textbf{This is working, but not generalizable (tikz, tabularray\dots).}}\\
\verb|\tikz \node[text=[HTML]{a65dbd]{This is not working};|
\end{document}

编辑

Phelype 在评论中建议使用\textcolor[HTML]{00AA00}{...},但不幸的是它不能推广到其他命令。例如,这在 tikz 中不起作用,在 tabularray 单元格颜色中也不起作用:

\tikz \node[text=[HTML]{a65dbd]{This is not working};

难道不可能定义一个通用的颜色参数(比如green!50!white适用于 tikz、tabularray、textcolor……)但以 HTML 颜色描述作为输入吗?

编辑

我在这里创建了一个功能请求https://github.com/latex3/xcolor/issues/13但如果有人有解决方案,我很想听听。

答案1

虽然我不太喜欢@LJR 的方法,但下面的例子可以扩展他/她的回答获得支持tikz

显著的改进:

  • 通过修补内部\colorlet,它现在支持tikz
  • 当颜色表达式不包含时跳过修补|
\documentclass{article}
\usepackage{xpatch}
\usepackage{tikz}
\usepackage{tabularray}

\usepackage{unravel}
\unravelsetup{max-action=1000, max-input=1000, max-output=1000}
\long\def\beginunravel#1\endunravel{\unravel{#1}}
\providecommand\beginunravel{}
\def\endunravel{}

\makeatletter

% The following set of ugly patches based on
%   https://tex.stackexchange.com/a/629719
% extend xcolor <expr> (see `texdoc xcolor`, Table 4) to
%   <expr> ::= <mode> "|" <spec>
%            | <original expr>
% and add constraint that a (user-defined) color name must NOT contaion `|`

% for \color, \textcolor, etc.
\xpretocmd\@declaredcolor
  {\my@hack@definetempcolor{#1}}
  {}{\PatchFailed}

\def\my@hack@color{\xglobal\definecolor}

% for \colorlet (used by tikz)
\xpretocmd\XC@col@rlet
  {\my@hack@definetempcolor{#4}}
  {}{\PatchFailed}

% shared inner helper macros

% if color expression #1 contains `|` and is an undefined color name, execute
%   \xglobal\definecolor{#1}{<#1 pre |>}{<#1 post |>}
\def\my@hack@definetempcolor#1{%
  % a bit normalization: remove all spaces from #1
  \expanded{\my@hack@@definetempcolor{\zap@space#1 \@empty}}}%
\protected\def\my@hack@@definetempcolor#1{%
  \in@|{#1}%
  \ifin@
    \@ifundefinedcolor{#1}{\my@hack@splitcolor#1\@nil{\xglobal\definecolor}}{}%
  \fi
}

\def\my@hack@splitcolor#1|#2\@nil#3{#3{#1|#2}{#1}{#2}}
\makeatother

\begin{document}
{ \color{rgb|0.5,0.7,0.9}TEXT }

\textcolor{HTML|a65dbd}{TEXT}

\begin{tblr}{row{1} = {bg={HTML|11AA33}}}
  Alpha & Beta & Gamma
\end{tblr}

\tikz[line width=3pt]
  \draw[{HTML|a65dbd}, fill={rgb|0.5,0.7,0.9}] (0,0) rectangle (1,1);
\end{document}

在此处输入图片描述

答案2

至少下面的脏代码对tabularray包有效:-)

\documentclass{article}

\usepackage{xcolor}
\usepackage{etoolbox}
\usepackage{tikz}
\usepackage{tabularray}

\makeatletter
\ExplSyntaxOn

\pretocmd{\@declaredcolor}{\my@hack@color{#1}}{}{}

\def\my@hack@color#1{\@ifundefined{\@backslashchar color@#1}{\MyDefineColor{#1}}{\relax}}

\cs_generate_variant:Nn \seq_set_split:Nnn { Nnx }

\NewDocumentCommand \MyDefineColor { m } {
  \seq_set_split:Nnx \l_tmpa_seq {|} {#1}
  %\seq_show:N \l_tmpa_seq
  \exp_args:Nnxx \definecolor{#1} { \seq_item:Nn \l_tmpa_seq {1} } { \seq_item:Nn \l_tmpa_seq {2} }
}

\ExplSyntaxOff
\makeatother

\begin{document}

{ \color{rgb|0.5,0.7,0.9}TEXT }

\textcolor{HTML|a65dbd}{TEXT}

\begin{tblr}{row{1} = {bg={HTML|11AA33}}}
  Alpha & Beta & Gamma
\end{tblr}

%% This is not working:
%\tikz \path[draw={HTML|012acd}] (0,0) -- (1,1);

\end{document}

在此处输入图片描述

答案3

对于tikz基于它的和包(和pgfkeys),以下新的密钥处理程序/.raw color可能会有所帮助。

用法:

  • \draw[<color>]->\draw[.raw color={<color model>}{<color spec>}]
  • key=<color>->key/.raw color={<color model>}{<color spec>}

为了支持\draw[<extended color spec>]和/或key=<extended color spec>,必须修补的几个地方tikz,甚至可能是pgf,我怀疑这是否值得。

\documentclass{article}
\usepackage{tikz}
\usepackage{tcolorbox}

\makeatletter
\newcount\pgfutil@colorcnt

%   \pgfkeys{key/.raw color={rgb}{1,0,0}}
% is equivalent to
%   \definecolor{<name>}{rgb}{1,0,0}
%   \pgfkeys{key=<name>}
% where <name> is `pgf@color@rgb@1,0,0`
\pgfkeys{/handlers/.raw color/.code 2 args={%
  \expandafter\pgfkeys@rawcolor\expanded{{pgf@color@\pgfkeys@zap@space #1@#2 \@empty}}{#1}{#2}%
}}

% #1 = (internal) color name, #2 = color model, #3 = color spec
\def\pgfkeys@rawcolor#1#2#3{%
  \expandafter\xglobal % for efficiency concerns, define the color globally
                       % note \xglobal is provided by `xcolor`, hence importable
  \pgfutil@definecolor{#1}{#2}{#3}%
  \edef\pgf@expanded@path{\pgfkeyscurrentpath}% was `\the\pgfkeys@pathtoks`
  % Note `/tikz/<name>` is different from `tikz/.cd, <name>`. Only the latter 
  % will be directed to `/tikz/.unknown` hence parse color name correctly.
  \ifx\pgf@expanded@path\pgfkeys@defaultpath@tikz@text
    \pgfkeysalso{{#1}}%
  \else
    \pgfkeysalso{\pgfkeyscurrentpath={#1}}%
  \fi
}

% helper, the same as \zep@space in latex2e
\def\pgfkeys@zap@space#1 #2{%
  #1%
  \ifx#2\@empty\else\expandafter\pgfkeys@zap@space\fi
  #2}

\def\pgfkeys@defaultpath@tikz@text{/tikz}
\makeatother


\begin{document}
\begin{tikzpicture}[line width=2pt]
  \draw[red, fill=blue]
    (0,0) rectangle +(1,1) node[text=black!30] {node text};
  \draw[.raw color={rgb}{1,0,0}, fill/.raw color={HTML}{0000FF}]
    (2,0) rectangle +(1,1) node[text/.raw color={cmyk}{0,0,0,.3}] {node text};
\end{tikzpicture}

\tcbset{nobeforeafter}
\tcbox[colback=green!50]{tcbox content}\qquad
\tcbox[colback/.raw color={rgb}{.5,1,.5}]{tcbox content}
\end{document}

在此处输入图片描述

答案4

xcolor正如我所说,它也可以用非常简单的工具和cellspace环境来完成tabular

\documentclass[12pt]{article}
\usepackage[svgnames, table]{xcolor}
\usepackage{cellspace}
\setlength{\cellspacetoplimit}{18pt}
\setlength{\cellspacebottomlimit}{18pt}

\begin{document}

{\centering \Huge\bfseries
\begin{tabular}{llSl}
\color{PowderBlue} TEXT \\
\color{MediumOrchid} TEXT \\
\rowcolor{MediumSeaGreen!60!OliveDrab} \enspace Alpha\enspace &\enspace Beta\enspace &\enspace Gamma\enspace
\end{tabular}
}
\end{document} 

在此处输入图片描述

相关内容