6

I was wondering if anyone could point me in the direction of some good sources to learn about user-defined styles in pgfplots? I've been through the pgfplots manual but it seems to only touch on this subject. So far I've been able to define some styles of my own, and call them without any issues, but I'd like to expand on this functionality, and for that I need some more detail about what styles are and aren't capable of, and what the appropriate syntax is. Some of my questions are:

Q1: Can I define nested styles? For example, my preamble defines MyStyle1 like so:

\pgfplotsset{MyStyle1/.style={code-goes-here}}

I then call this style in my document with something like this:

\begin{tikzpicture}
    \begin{axis}[MyStyle1]  
        \addplot[MyStyleA] pseudo-code;
    \end{axis}      
\end{tikzpicture}

What I'd like to do is have MyStyleA have one definition if the axis uses MyStyle1, and a different definition if the axis is using MyStyle2. Can this be done? How would I go about this?

Q2: How do I overwrite parts of a style? Let's say MyStyle1 includes the line xmin=5, and I apply MyStyle1 to an axis environment as above. I want the minimum x value to instead be 0, but to otherwise use all other settings from MyStyle1. How do I override this for just this axis environment (i.e. not affect any other things that use MyStyle1)?

Q3: Are there any limitations to what I can put in a style? I'm assuming any settings I can put in [] brackets can also be put in a style. Any exceptions or additions to this rule?

1 Answer 1

8

Let me take your questions in turn.

Q1. You absolutely can have one style define another style. The details of what exactly you can do are spelled out in the tikz manual. But here's an example.

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}

\pgfplotsset{
    compat=1.15,
    MyStyle1/.style={
        MyStyleA/.style={red},
    },
    MyStyle2/.style={
        MyStyleA/.style={blue},
    },
}


\begin{document}
\begin{tikzpicture}
\begin{axis}[MyStyle1]
    \addplot [
        MyStyleA,
        domain=-3e-3:3e-3,
        samples=201,
    ]
    {exp(-x^2 / (2e-3^2))/(1e-3*sqrt(2*pi))};
\end{axis}
\end{tikzpicture}

\begin{tikzpicture}
\begin{axis}[MyStyle2]
    \addplot [
        MyStyleA,
        domain=-3e-3:3e-3,
        samples=201,
    ]
    {exp(-x^2 / (2e-3^2))/(1e-3*sqrt(2*pi))};
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

As you can see, MyStyle1 defines MyStyleA to be red and MyStyle2 defines MyStyleA to be blue. If you want, you can simply append styles using Foo/.append style={...} rather than Foo/.style={...}.

It's worth pointing out that if your reason for doing this is you want some particular style to apply to every plot, then rather than set MyStyleA and explicitly include it in every \addplot [MyStyleA], you can use every axis plot/.append style={...} like this.

\pgfplotsset{
    MyStyle1/.style={
        every axis plot/.append style={red},
    },
    MyStyle2/.style={
        every axis plot/.append style={blue},
    },
}

\begin{axis}[MyStyle1]
    \addplot [
        domain=-3e-3:3e-3,
        samples=201,
    ]
    {exp(-x^2 / (2e-3^2))/(1e-3*sqrt(2*pi))};
\end{axis}

This has the same effect as the previous code.

Q2. In general, later styles override earlier ones. So in this case, you would use [MyStyle1, xmin=0].

Q3. As far as I know, you can put anything into a style that you could put in the optional arguments/\pgfset/\tikzset/\pgfplotsset.

1
  • Thanks for your help. This all worked. I think I might have over-simplified my example code though - I didn't clarify that I have multiple instances of \addplot within an axis environment, so using every axis plot probably isn't appropriate in my case. Essentially what I'm trying to achieve with nested styles is use different colour schemes depending on the type of data shown. Your first set of code achieves the desired outcome. Thanks again. :)
    – Ulysses
    Jun 14, 2017 at 3:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .