2

I want to create an array of custom defined styles and afterwards use these styles by getting them by index.

My approach so far, which isn't working:

  1. Defining some styles:

    \pgfplotsset{
        redline/.style={
            red
        },
        blueline/.style={
            blue
        }
    }
    
  2. Defining an array with the styles:

    \def\mystylesarray{
        {redline},
        {blueline}
    }
    
  3. Using them in a plot:

    \begin{tikzpicture}
        \begin{axis}
            \addplot[\mystylesarray[0]] {x};
            \addplot[\mystylesarray[1]] {x^2};
        \end{axis}
    \end{tikzpicture}
    

I also tried the following:

In step 2. I tried \newcommand instead of \def and using two braces around the array implementation. In step 3. I tried curly braces and normal braces. All sorts of combinations and looking for an answer online didn't bring up a solution.

Your help is appreciated.


MWE:

\documentclass{scrreprt}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\pgfplotsset{
    redline/.style={
        red
    },
    blueline/.style={
        blue
    }
}
\def\mystylesarray{
    {redline},
    {blueline}
}

\begin{document}
    \begin{tikzpicture}
    \begin{axis}
    \addplot[\mystylesarray{0}] {x};
    \addplot[\mystylesarray{1}] {x^2};
    \end{axis}
    \end{tikzpicture}
\end{document}
daniatic
  • 288
  • 4
    I think you want pgfplotscreatecyclelist – cmhughes Sep 26 '17 at 14:52
  • Please provide a complete minimal example if @cmhughes's comment does not answer your question. – cfr Sep 26 '17 at 14:57
  • I forgot to say: Welcome to the site :) I'll post a complete example later on demonstrating the pgfplotscreatecyclelist; of course, if you're able to construct one yourself that meets your needs, it's perfectly ok to post an answer to your own question! – cmhughes Sep 26 '17 at 15:10
  • @cmhughes Thanks for the warm welcome. No I don't want a cycle list. I need a collection of styles I can use throughout my pictures and plots. @cfr Isn't the example I posted enough? A complete example would only include usepackage command and a documentclass... – daniatic Sep 26 '17 at 15:22
  • @daniatic, what exactly should be the advantage to have an array of styles? I don't see a disadvantage of directly using the style, e.g. redline instead of \mystylesarray[0]. If you want consistency throughout your document I would suggest creating a cycle list or cycle lists of your need and combine them with styles for different types of diagrams. – Stefan Pinnow Sep 26 '17 at 15:42
  • @StefanPinnow, the advantage would be to have multiple arrays with different style definitions. At compilation I could provide the desired style array and I would not need to change all styles one by one. The actual goal is to have a switch for black&white and color print outs. The styles for b&w-print would be solid, dashed and so forth and for color-print different colors. I could implement different cycle list depending on the bw-color switch, but then I have a problem when I need specific colors of the cycle list in a different order. Or is it possible to access cycle list entries? – daniatic Sep 26 '17 at 15:49
  • Is this for pgfplots or Tikz? – percusse Sep 26 '17 at 15:58
  • @percusse I would like to find a solution for both! – daniatic Sep 26 '17 at 16:00
  • @daniatic, that should not be a problem. Define all the styles independently and then create some more styles that describe the use-case and in them call the style and/or cycle list that fits the current use-case. Create a second set of this styles fitting another use-case. Then you can simply comment or uncomment them. If you want to simplify it further, add the different use-case styles to different files and load the one you want to use by \input{...}. – Stefan Pinnow Sep 26 '17 at 16:02
  • @StefanPinnow Thanks for your answer. I just came up with the same solution and was writing a comment... Is it possible to close this question or do I have to delete it? – daniatic Sep 26 '17 at 16:09
  • @daniatic, we can either close it as "solved in the comments" or you can delete it. As you wish. – Stefan Pinnow Sep 26 '17 at 16:13
  • 4
    I'm voting to close this question because it was solved in the comments. – Stefan Pinnow Sep 26 '17 at 17:20
  • Does it help? https://tex.stackexchange.com/a/134368/1952 – Ignasi Sep 27 '17 at 08:27

0 Answers0