[Tex/LaTex] Variable parameters in xcolor package

colormacros

This doesn't work and I'm having trouble thinking of a minimum working example, but I'd like to do something like this where I have a parameter to a command as a variable:

\usepackage{xcolor}
\definecolor{mycolor1}{RGB}{126,169,105}
\definecolor{mycolor2}{RGB}{26,69,15}

\newcommand{\ColorVariable1}{mycolor1}
% or
\DeclareRobustCommand{\RobustColorVariable1}{mycolor1}

to allow me to have several places in the body of my document where I do things like this:

\textcolor{\ColorVariable1}{Some text in a variable color}

and I can change the colors of all those colored text portions with one fell swoop by assigning a new color (as below) to the variable rather than replacing mycolor1 with mycolor2 with an editor global search and replace in all the places in the text as I'm doing now.

\newcommand{\ColorVariable1}{mycolor2}

I know that I can do something similar (but not for parameters) in the body of the document:

\def\somestring{Replace \somestring with this text.}

And something also similar with commands, but this doesn't seem to work for parameters:

\newcommand{name}[num][default]{definition}

Is there any way to do something like this in LaTeX?

Best Answer

Package xcolor provides \colorlet. Defining a "color variable", using it and reassigning the color can be seen in the following example:

\documentclass{article}

\usepackage{xcolor}
\definecolor{mycolor1}{RGB}{126,169,105}
\definecolor{mycolor2}{RGB}{26,69,15}

\colorlet{ColorVariable1}{mycolor1}

\begin{document}
\textcolor{ColorVariable1}{Some text in a variable color}

\verb|\colorlet{ColorVariable1}{mycolor2}|
\colorlet{ColorVariable1}{mycolor2}

\textcolor{ColorVariable1}{The color of the variable color can also be
changed.}
\end{document}

Result