2

I'm trying to define multiple cycle lists based on macro variables. The reason for this is that I need more than one list and I want to be able to change the appearance quickly while keeping the coherence.

\documentclass{standalone}
\usepackage{pgfplots}


\begin{document}

\newcommand{\var1}{solid, red, every mark/.append style={solid, fill=red}, mark=o}
\newcommand{\var2}{dashed, blue, every mark/.append style={solid, fill=blue}, mark=x}

\pgfplotscreateplotcyclelist{cycle1}{
\var1\\%
\var2\\%
}
\pgfplotscreateplotcyclelist{cycle2}{
\var1\\%
}
\begin{tikzpicture}
   \begin{axis}[cycle list name = cycle1]
        \addplot
        coordinates
        {
            (0,0)
            (10,5)
        };
        \addplot+[only marks,forget plot]
        coordinates
        {
            (0,0)
            (10,7)
        };
    \end{axis}     
\end{tikzpicture}
\end{document}

This code-example fails with the message

! Undefined control sequence.
\pgfkeyscurrentkey ->\var1

It has probably got something to do with macro-expansion, so I tried \noexpand and \expandafter but none of them seem to work.

1 Answer 1

3

We can't use numbers in LaTeX macros- see Defining commands/abbreviations that contain numbers and the links within for details.

We can achieve close to your desired approach using a pgfplots-based approach by using styles:

\pgfplotsset{
    mystyle/.style={
        solid, red, every mark/.append style={solid, fill=red}, mark=o},
    myotherstyle/.style={dashed, blue, every mark/.append style={solid, fill=blue}, mark=x}
}

\pgfplotscreateplotcyclelist{cycle1}{
    mystyle\\%
    myotherstyle\\%
}
\pgfplotscreateplotcyclelist{cycle2}{
    mystyle\\%
}

The idea is to set styles called mystyle and myotherstyle, and use them within the cyclelist declaration. If you wish to change these styles midway through your document, you can use, for example,

\pgfplotsset{mystyle/.style={blue}}

Here's a complete MWE to play with.

% arara: pdflatex
\documentclass{article}
\usepackage{pgfplots}

\begin{document}

\pgfplotsset{
    mystyle/.style={
        solid, red, every mark/.append style={solid, fill=red}, mark=o},
    myotherstyle/.style={dashed, blue, every mark/.append style={solid, fill=blue}, mark=x}
}

\pgfplotscreateplotcyclelist{cycle1}{
    mystyle\\%
    myotherstyle\\%
}
\pgfplotscreateplotcyclelist{cycle2}{
    mystyle\\%
}

\begin{tikzpicture}
    \begin{axis}[cycle list name = cycle1]
        \addplot
        coordinates
        {
            (0,0)
            (10,5)
        };
        \addplot+[only marks,forget plot]
        coordinates
        {
            (0,0)
            (10,7)
        };
    \end{axis}
\end{tikzpicture}
\end{document}
1
  • Staying inside the pgfplot environment did the trick - thank you!
    – mschrimpf
    Mar 6, 2014 at 6:07

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.