11

So this seems to be impossible:

OrangeRed!\c!white

with \c being any kind of \def formula, or precisely in my case, with a \pgfmathsetmacro. Here is MWE

\documentclass{article}

\usepackage{mathtools}

% TIKZ & PGF
\usepackage[usenames,dvipsnames,svgnames]{xcolor}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepackage{tikz}

\usetikzlibrary{backgrounds, decorations.shapes, decorations.markings, shapes, patterns}

% % % % % % % % CIRCLES % % % % % % % %
% #1 angle
% #2 ray
% #3 shift
\pgfmathdeclarefunction{circleX}{3}
{%
    \pgfmathparse{#3 + #2*cos(deg(#1))}%
}
\pgfmathdeclarefunction{circleY}{3}
{%
    \pgfmathparse{#3 + #2*sin(deg(#1))}%
}

\begin{document}
    \begin{tikzpicture}


        \tikzset{
            small dot/.style={fill=black, circle, scale=2}
        }

        \pgfmathsetmacro{\halfPi}{.5*pi}
        \pgfmathsetmacro{\twoPi}{2*pi}

        \pgfmathsetmacro{\r}{1}
        \pgfmathsetmacro{\yshift}{0.25}

        \begin{axis}[axis x line=center, axis y line=none, width=.5\textwidth, axis equal,
                     xtick={0,\halfPi,...,\twoPi}, xticklabels={$0$,$\frac{\pi}{2}$,$\pi$,$\frac{3\pi}{2}$,$2\pi$}]


            \foreach \i in {0.25,0.5,...,1} { %
                \pgfmathsetmacro{\c}{(1.2-\i)*100}
                \pgfmathsetmacro{\xshift}{\i*2*pi}

                %\addplot[OrangeRed!\c!white -> nope
                %\addplot[color=OrangeRed!\c!white -> nope
                %\addplot[color={OrangeRed!\c!white} -> nope
                %\addplot[color/.expanded=OrangeRed!\c!white -> nope

                \addplot[color=OrangeRed!50!white, samples=80, domain=0:2*pi]
                    ({circleX(x,\r,\xshift)},{circleY(x,\r,\r+\yshift)});
            }

        \end{axis}
    \end{tikzpicture}
\end{document}

Is there any way to use a macro within an xcolor expression without the need of a \definecolor{colorname}{rgb|cmyk|...}{color stuff definition}?

  • 1
    This should work, can you test color=OrangeRed!\c!white or color/.expanded=OrangeRed!\c!white? There may be problems with xcolor and different color schemes (OrangeRed is not available by default) or PGFPlots or \foreach or ... Can you post a minimal working example (MWE) and the error message you get or what does not work as expected? – Qrrbrbirlbel Dec 3 '13 at 17:20
  • your solution seems to be inefficient, I added the MWE. Regarding the errors, with OrangeRed!\c!white, I have Missing \endcsname inserted, with color=OrangeRed!\c!white, I have Missing number, treated as zero and with color/.expanded=OrangeRed!\c!white, I have pgfkeys: I do not know the key '/pgfplots/color' and I am going to ignore it. Perhaps you misspelled it. – nadous Dec 3 '13 at 17:46
12

You have to expand \c (but I'd use a different name) before \addplot sees it:

\documentclass{article}

% TIKZ & PGF
\usepackage[usenames,dvipsnames,svgnames]{xcolor}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepackage{tikz}

\usetikzlibrary{backgrounds, decorations.shapes, decorations.markings, shapes, patterns}

% % % % % % % % CIRCLES % % % % % % % %
% #1 angle
% #2 ray
% #3 shift
\pgfmathdeclarefunction{circleX}{3}
 {%
  \pgfmathparse{#3 + #2*cos(deg(#1))}%
 }
\pgfmathdeclarefunction{circleY}{3}
 {%
  \pgfmathparse{#3 + #2*sin(deg(#1))}%
 }

\begin{document}
\begin{tikzpicture}
\tikzset{small dot/.style={fill=black, circle, scale=2}}
\pgfmathsetmacro{\halfPi}{.5*pi}
\pgfmathsetmacro{\twoPi}{2*pi}
\pgfmathsetmacro{\r}{1}
\pgfmathsetmacro{\yshift}{0.25}
\begin{axis}[
  axis x line=center,
  axis y line=none,
  width=.5\textwidth,
  axis equal,
  xtick={0,\halfPi,...,\twoPi},
  xticklabels={$0$,$\frac{\pi}{2}$,$\pi$,$\frac{3\pi}{2}$,$2\pi$}
]
\foreach \i in {0.25,0.5,...,1} {%
  \pgfmathsetmacro{\mycol}{(1.2-\i)*100}
  \edef\mycol{OrangeRed!\mycol}
  \pgfmathsetmacro{\xshift}{\i*2*pi}
  \expandafter\addplot\expandafter[%
    \mycol!white,
    samples=80,
    domain=0:2*pi
  ]({circleX(x,\r,\xshift)},{circleY(x,\r,\r+\yshift)});
}
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

7

This is the best I've found but I don't know if you'll find it easy enough. It's more or less the same effort required to define a color.

\documentclass{article}
\usepackage{tikz,pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\pgfmathsetmacro{\a}{1}
\pgfmathsetmacro{\yshift}{1}
\foreach \i in {0.25,0.5,...,1} {%
  \pgfmathsetmacro{\c}{(1.2-\i)*100}
  \pgfmathsetmacro{\cx}{\i*2*pi}
  \xdef\doplot{\noexpand\addplot[color=red!\c!yellow, samples=80]}
  \doplot ({\cx + \a*cos(deg(x))},{\a + \yshift + \a*sin(deg(x))});
}
\end{axis}
\end{tikzpicture}
\end{document}

result


EDIT: This answered the original question.

This works OK, unless I'm missing something:

\documentclass{article}
\usepackage{xcolor}
\begin{document}
\def\c{50}
Text in a {\color{red!\c!yellow} fancy} color.
\end{document}

result

  • my bad, I should have been posting the full code I'm working on, see my edit... – nadous Dec 3 '13 at 16:52
6

It is known that PGFPlots and \foreachs don’t go so nicely together because PGFPlots doesn’t process the plots firstly and then later draws them. (See PGFplots foreach equivalent to TikZ's with multiple variables separated by a slash for more information and explanations of the author of PGFPlot himself.) The package provides the macros

\pgfplotsinvokeforeach{<list>}{<body>}

and

\pgfplotsforeachungrouped <list of variables> in {<list>}{<body>}

The manual even explains the problems and the usage of \edef\temp to “pre-expand” macros.

In your example however, we can avoid this if we postpone the calculation of the color until it is actually plotted/drawn by moving the calculation inside the plot (the \xshift macro is properly expanded in all cases since PGFPlots expects there to be things that needs to be expanded):

\pgfplotsinvokeforeach{0.25,0.5,...,1}{%
  \pgfmathsetmacro{\xshift}{#1*2*pi}
  \addplot[
    /utils/exec=\pgfmathsetmacro\col{(1.2-#1)*100},
    color=OrangeRed!\col!white, samples=80, domain=0:2*pi]
      ({circleX(x,\r,\xshift)},{circleY(x,\r,\r+\yshift)});
}

With my qrr.misc library and its .pgfmath strcat handler you can even do

color/.pgfmath strcat={"OrangeRed!",(1.2-#1)*100,"!white"}

The forest package provides similar handlers (.wrap pgfmath arg), though they fail here, too.

  • Yeah, pgfkeys/handlers, never used that but it was popin' around while I was seeking a solution. The /utils/exec portion of your lib can hold as much stuff that I want? – nadous Dec 3 '13 at 21:10
  • The /utils/exec key is not mine but defined either by TikZ or PGF and simply executes TeX code as it is given to it. Similar how \pgfextra can be used for TikZ paths which doesn't have PGFPlots equivalent as far as I know. The .pgfmath strcat simply evaluates \pgfmathparse{strcat(<arg>)} and forwards its result to the key. The strcat function is also defined in qrr.misc and concatenates its arguments. The " are needed to use stuff as text and not let it be evaluated by PGFmath (see PGF manual). – Qrrbrbirlbel Dec 3 '13 at 21:29

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.