john mangual john mangual - 28 days ago 6
LaTeX Question

code defining colors in LaTeX

I am looking for the TeX source code for the

\color
function call in the
xcolor
library which I am importing

\documentclass[12pt]{article}
\usepackage{xcolor}
\begin{document}

{ \color{blue!60!white} abc }

\end{document}


I am asking here rather than on tex.stackexchange because I am asking for the programming rather than just the tex commands. In theory, this is working TeX code and I should just move on.

Answer

Technically, if you're just interested in what \color does, then you can issue

{\let\protect\show\color}

in your document and check your .log:

> \color =\long macro:
->\@ifnextchar [\@undeclaredcolor \@declaredcolor .

This works because \color is declared to be robust, otherwise issuing \show\color would have worked.

The code shows that \color conditions on whether or not the input stream contains [. That is,

\color[<options>]{<colour>}

would lead to executing \@undeclaredcolor, while calling

\color{<colour>}

would lead to executing \@declaredcolor. In both cases, the {<colour>} argument is gobbled by either \@undeclaredcolor or \@declaredcolor.


If you wish to follow a more detailed path of macro execution and expansion, issuing the tracing command

{\tracingmacros1
\color{blue!60!white} abc }

would allow you to debug quite a bit, revealing a lengthy macro expansion pattern of about 800 lines as one follows deeper down the rabbit hole, starting, of course, with the \@ifnextchar condition mentioned above.