3

I am trying to branch on whether a color (defined in xcolor or xcolor-material or using the utilities provided by these packages) has a name that can be used in the \color command (from the xcolor package). The current code is:

% Preamble
\documentclass[varwidth=true]{standalone}
\usepackage{xcolor} 
\usepackage{xcolor-material} 

% Should return TRUE if the \color command from xcolor works with the 
% provided color name, and FALSE otherwise.
\ExplSyntaxOn
\makeatletter
\newcommand{\checkxcolor}[1]{
    \cs_if_exist:cTF{color@#1}{[#1=TRUE]}{[#1=FALSE]}\newline
}
\makeatother
\ExplSyntaxOff

% Test
\begin{document}
\checkxcolor{blue}              % should return TRUE
\checkxcolor{red}               % should return TRUE
\checkxcolor{nocolor}           % should return FALSE
\checkxcolor{lime}              % should return TRUE
\checkxcolor{MaterialIndigo900} % should return TRUE
\checkxcolor{MaterialIndigo999} % should return FALSE
\end{document}

Currently it returns FALSE in all cases. However given this answer to the question Check whether a color passed as argument to a package is defined my guess is that it should work. How to make it work?

Note: I know that latex3 now has \color_if_exist:nTF, but I'm working with older packages and colors defined with latex2e.

1
  • Are user-defined colour names included in your use case? \colorlet{blue}{red}, \newcommand\niceorange{red!94!yellow}. Other packages can also define colour names. Is the question about validating Material3 names (only)?
    – Cicada
    Commented Aug 9 at 9:26

1 Answer 1

7

It is better to not rely on internal data structures, they can change. E.g. I have an open issue for pgf/tikz regarding the use of color internals which prevents the merge of l3color and xcolor: https://github.com/pgf-tikz/pgf/issues/1244

xcolor contains a test for color names (and we should probably provide an user interface ...). Side remark: the standalone class is not the best class for minimal examples as it does lots of stuff that is quite unneeded here.

\documentclass{article}
\usepackage{xcolor} 
\usepackage{xcolor-material} 

% Should return TRUE if the \color command from xcolor works with the 
% provided color name, and FALSE otherwise.
\ExplSyntaxOn
\makeatletter
\newcommand{\checkxcolor}[1]{
    \@ifundefinedcolor{#1}{[#1=FALSE]}{[#1=TRUE]}\newline
}
\makeatother
\ExplSyntaxOff

% Test
\begin{document}
\checkxcolor{blue}              % should return TRUE
\checkxcolor{red}               % should return TRUE
\checkxcolor{nocolor}           % should return FALSE
\checkxcolor{lime}              % should return TRUE
\checkxcolor{MaterialIndigo900} % should return TRUE
\checkxcolor{MaterialIndigo999} % should return FALSE
\end{document}

screenshot

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .