4

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?

  • Control sequence names can't contain digits (at least not in an easy and robust way). – egreg Sep 15 '13 at 18:20
  • 1
    You know \colorlet of package xcolor? Then you can do \colorlet{colorvariable1}{mycolor1} and using \textcolor{colorvariable1}{...}. – Heiko Oberdiek Sep 15 '13 at 19:24
  • @HeikoOberdiek \colorlet was exactly what I was looking for. If you'll write this as an answer then I'll gladly accept it. Thank you. – TeXnewbie Sep 15 '13 at 20:44
6

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

4

The following seems to be a more suitable usage where you define \ColorVariable to take two arguments - the first being a colour number and the second being the text you want coloured:

enter image description here

\documentclass{article}
\usepackage{xcolor}% http://ctan.org/pkg/xcolor
\definecolor{mycolor1}{RGB}{126,169,105}
\definecolor{mycolor2}{RGB}{26,69,15}
\makeatletter
\newcommand{\ColorVariable}[2]{%
  \textcolor{\ifnum#1>2 black\else mycolor#1\fi}{#2}}
\makeatother
\begin{document}
\ColorVariable{1}{Some text in a variable colour}.

Some text in a variable \ColorVariable{2}{colour}.

Some \ColorVariable{3}{text} in a \ColorVariable{1}{variable} \ColorVariable{2}{colour}.
\end{document}

A test is built in to set the colour to black if the number specified is larger that the total X number of colours defined as mycolorX.

  • Thank you @Werner although I may have written my question poorly (or else I may not understand your answer). It looks like in your answer, if I wrote a document with most of the text in black and many various pieces of text all colored green, for example, and then later decided that I wanted them all blue instead, then I would need to do an editor-based global search-and-replace of your \ColorVariable{3} with \ColorVariable{1} which is like what I was doing before. I was looking for a way to make the color I assign to text a variable that I could use everywhere & change only once if des'd. – TeXnewbie Sep 15 '13 at 20:50
  • 1
    You can redefine the colour as you wish later (as in Heiko's answer). So, if mycolor2 should change mid-document to mycolor3, then that's possible with a single \colorlet command and you can leave everything else unchanged. – Werner Sep 15 '13 at 21:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.