0

Disambiguation. For tables and odd even row color, see if this search at Stack Exchange helps: https://tex.stackexchange.com/search?q=table+row+color

Disambiguation. For just unilaterally setting a color, see xcolor package \color and \textcolor. While \color is used below, it's not the main focus.

How does one set color conditionally (based on current color)?

And, if someone with sufficient reputation would do so, we could benefit by some new tags:

extractcolorspec

extractcolorspecs

testcolor

ifstreqcase

1 Answer 1

1

Programmers coming into \LaTeX have various debugging techniques. Even once we get past the sometimes cryptic compile errors, we don't always have the typesetting result desired. To assist with some of those types of debug issues, we can pass notes to ourselves via \def "variables." We can also use color.

Color won't help with compile issues. For example, while this very matter was still "in the lab," borrowed a code segment from a Stack Exchange thread for a \newcommand. Placed it after a section of \def definitions. Wouldn't compile. "Missing \begin{document}." On a lark, because things were just fine before that copy/paste of that \newcommand, relocated the \newcommand to before the section of \def definitions, and compiling was smooth as silk. How and why that mix of \TeX (\def) and \LaTeX (\newcommand) commands operated differently due to sequence is a question for a different thread. How do we debug for that? Tough to know; just more trial and err-roar https://ctan.org/lion/ (to describe certain levels of \LaTeX-induced frustration.) Here, the point is that color-based debugging helps once the program compiles.

That begs the question: How does one set color conditionally based on color?

Key is this command, out of context, which retrieves the current color information.

\extractcolorspecs{.}{\dfltColorModel}{\dfltColor}

In context, there's this.

% !TEX encoding = Mac Western
% What is the default font color model and spec? 
% TeXShop (ver. 2.47) (Mac OSX 10.5.8)
% compliation method LaTeX pdfTeX

\documentclass[12pt]{article}

\usepackage[margin=0.5in]{geometry}
\usepackage[dvipsnames*, x11names, svgnames, hyperref]{xcolor}

% Create some quick \TeX commands for color-info parsing
\def\dfltColorModel
\def\dfltColor

% Get the system's default color
\AtBeginDocument{\colorlet{defaultcolor}{.}}

\begin{document}

% extract the color into Model and Color
\extractcolorspecs{.}{\dfltColorModel}{\dfltColor}

Default Color Model: \dfltColorModel

Default Color Spec: \dfltColor

\bigskip

%And, to demonstrate another xcolor feature, here's the \testcolor output.
\begin{testcolors}[rgb,cmyk,hsb,HTML,gray]
\testcolor{.}
\end{testcolors}

\end{document}

Default color via \testcolor

\dfltColorModel and \dfltColor are created via \def, and could just as easily be \a and \b. The descriptive labels help with identifying what information this command produces, which is both a "color command," {(color-cmd)} and a "model command," {(model-cmd)} .

https://ctan.org/pkg/xcolor?lang=en

"Extending \LaTeX's color facilities: the xcolor package" (v2.12) (11 May 2016) Dr. Kern

xcolor.pdf

pp.28-29

¶ 2.13 Color information

\extractcolorspec {(color)} {(cmd)}

Extracts the color specification of (color) and puts it into (cmd) equivalent to \def\cmd{{(model)} {(spec)}}.

\extractcolorspecs {(color)} {(model-cmd)} {(color-cmd)}

Extracts the color specification of (color) and puts it into (model-cmd) and (color-cmd), respectively.

And, for seeing what a color looks like (at least on the system at hand, i.e., our own system):

2.6.4 Color testing (p.23)

\testcolor[(model-list)] {(spec-list)}

Each \testcolor command generates a table row, containing a display sample plus the respective parameters for each of the models. If the column-model matches the model of the color in question, its parameters are underlined. Note that this command is only available within the testcolors environment. For applications see figure 2 on page 31 and figures 11, 12.

In the example output, above, what we see is that "default" (on this typesetter's system) isn't "black;" it's "gray 0." The difference? It may be likened to the "sound in the forest" intro to philosophy, where the difference is in the definition. The expected definition (see, e.g., http://latexcolor.com/ (there are "sort by" arrows in a couple of the headings by which to rearrange the sequence, as may be helpful) ) is "black," defined per an rgb or html, etc., standard. On some systems (considering chipset and operating system), it very well may be rgb, etc., based. However, here, on this chipset (PPC) and operating system, Mac OS X 10.5.8, the actual model and spec are "gray" and "0," respectively.

The next step is changing the color. Out of context, here's the command:

\color[rgb]{0,1,0}

Changes the color to "green." Could use any of the color models. Using rgb for simplicity of setting colors.

In context, there's this:

% !TEX encoding = Mac Western
% How to change default font color model and spec? 
% TeXShop (ver. 2.47) (Mac OSX 10.5.8)
% compliation method LaTeX pdfTeX

\documentclass[12pt]{article}

\usepackage[margin=0.5in]{geometry}
\usepackage{lipsum}
\usepackage[dvipsnames*, x11names, svgnames, hyperref]{xcolor}

% Create some quick \TeX commands for color-info parsing
\def\dfltColorModel
\def\dfltColor

% Get the system's default color
\AtBeginDocument{\colorlet{defaultcolor}{.}}

\begin{document}

\section{Section}

\lipsum[12]

% extract the present color into Model and Color
\extractcolorspecs{.}{\dfltColorModel}{\dfltColor}

\begin{testcolors}[rgb,cmyk,hsb,HTML,gray]
\testcolor{.}
\end{testcolors}

\bigskip
\bigskip

% change the "default" color scheme
\color[rgb]{0,1,0}

\section{Section}

\lipsum[13]

% extract the present color into Model and Color
\extractcolorspecs{.}{\dfltColorModel}{\dfltColor}

\begin{testcolors}[rgb,cmyk,hsb,HTML,gray]
\testcolor{.}
\end{testcolors}

\end{document}

Basic color change and \testcolor

xcolor.pdf

pp.22

¶ 2.6 Color application

¶ 2.6.1 Standard color commands

\color {(color)}

{(model-list)] {{spec-list)}

Switches to the color given either by name/expression or by model/specification. This color will stay in effect until the end of the current TEX group.

Note: I.e., until the next \color command.

\textcolor {(color)} {(text)}

[(model-list)] {(spec-list)} {(text)}

[these] are just alternative syntax for \color, in which the groups are added implicitly. Thus (text) appears in the specified color, but then the color reverts to its previous value. Additionally, it calls \leavevmode to ensure the start of horizontal mode.

Note that what we get back from "extracting" from the "." is the "current" color information. What's "current" at startup is the "default" setting.

With that as the intro, here's a basic method implementing the concept of changing color conditionally.

% !TEX encoding = Mac Westsern
% Changing color based on color 
% TeXShop (ver. 2.47) (Mac OSX 10.5.8)
% compliation method LaTeX pdfTeX

\documentclass[12pt]{article}

\usepackage[T1]{fontenc}
\usepackage[margin=0.5in]{geometry}
%\usepackage{lipsum}
\usepackage[dvipsnames*, x11names, svgnames, hyperref]{xcolor}
\usepackage{xstring}

% see also \substitutecolormodel
\newcommand\useRgb{%
    \extractcolorspecs{.}{\currentColorModel}{\currentColorSpec}
    \IfStrEqCase*{\currentColorModel}{%
        {rgb}{}%
        {cmyk}{\selectcolormodel{rgb}}%
        {hsb}{\selectcolormodel{rgb}}%
        {HTML}{\selectcolormodel{rgb}}%
        {html}{\selectcolormodel{rgb}}%
        {gray}{\selectcolormodel{rgb}}%
        [No change: Color model didn't match\\]
    }
}

\newcommand\changeColor{%
    \extractcolorspecs{.}{\currentColorModel}{\currentColorSpec}
    \IfStrEqCase*{\currentColorSpec}{%
        {0}{\color[rgb]{0,1,0}}% black to green
        {0,0,0}{\color[rgb]{0,1,0}}% black to green
        {0,1,0}{\color[rgb]{0,0,1}}% green to blue
        {0,0,1}{\color[rgb]{1,0,0}}% blue to red
        {1,0,0}{\color[rgb]{0,0,0}}% red to black
        [No color change: Color spec didn't match\\]
    }
}

% Create some quick \TeX commands for color-info parsing
\def\currentColorModel
\def\currentColorSpec

% Get the system's default color
\AtBeginDocument{\colorlet{defaultcolor}{.}}

\begin{document}

\useRgb

Cycle 1

\bigskip

\begin{testcolors}[rgb,cmyk,hsb,HTML,gray]
\testcolor{.}
\end{testcolors}

\bigskip

\changeColor

Cycle 2

\bigskip

\begin{testcolors}[rgb,cmyk,hsb,HTML,gray]
\testcolor{.}
\end{testcolors}

\bigskip 

\changeColor

Cycle 3

\bigskip

\begin{testcolors}[rgb,cmyk,hsb,HTML,gray]
\testcolor{.}
\end{testcolors}

\bigskip

\changeColor

Cycle 4

\bigskip

\begin{testcolors}[rgb,cmyk,hsb,HTML,gray]
\testcolor{.}
\end{testcolors}

\bigskip

\changeColor

Cycle 1/5

\bigskip

\begin{testcolors}[rgb,cmyk,hsb,HTML,gray]
\testcolor{.}
\end{testcolors}

\bigskip
\bigskip

\section{Section}

%\lipsum[13]
Some Section text.


\changeColor

\subsection{Subsection}

%\lipsum[14]
Some subsection text.

\changeColor

\subsection{Subsection}

\changeColor

%\lipsum[15]
Some more subsection text.


\changeColor

%\lipsum[15]
Yet additional subsection text.

\changeColor

\section{Section}

\changeColor

%\lipsum[16]
Next section text.

\changeColor

%\lipsum[17]
Some more text.

\changeColor

%\lipsum[18]
And, some more text.

\end{document}

Changing color conditionally based on color

The \IfStrEqCase command (from the xstring package) detokenizes (\detokenize) by the use of the " * " version of the command.

Closing note.

xcolor p.41 Table 5.

Note the reference to \MiKTeX. Not realistic to expect testing of all packages on all installation variations. Installation variation is the type of information for which discussion forums like Stack Exchange exist.

This site is temporarily in read-only mode and not accepting new answers.

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