8

When I want to set a style using groupplot, I will do something like this:

\begin{groupplot}[
  group style={
    group size=1 by 2,
    vertical sep=1cm,
  },
]

However, I've found that if I try to save that as a style using pgfplotsset, like so:

\pgfplotsset{
  mygroup/.style={
    group style={
      group size=1 by 2,
      vertical sep=1cm,
    },
  },
}

applying the style seems to ignore the contents of the group style key. The value of other keys are observed and correctly applied, but not that of group style. If I want to make changes to the keys in the group path, I have to specify them entirely:

\pgfplotsset{
  mygroup/.style={
    group/group size=1 by 2,
    group/vertical sep=1cm,
  },
}

But this seems to me to be an odd, non-standard behaviour, since you wouldn't use this way of writing when specifying options to \begin{groupplot}.

Is this the expected behaviour? What is the recommended way to define group styles in pgfplotsset? Here's a MWE, where you can uncomment the different options:

\documentclass{article}
\usepackage{pgfplots}
\usetikzlibrary[pgfplots.groupplots,calc]

\pgfplotsset{
  compat=1.12,
  mygroup/.style={
    group style={
      group size=1 by 2,
      vertical sep=1cm,
    },
    every axis plot/.append style={
      smooth,
    }
  },
  mygroupb/.style={
    group/group size=1 by 2,
    group/vertical sep=1cm,
    every axis plot/.append style={
      smooth,
    }
  },
}

\begin{document}

\begin{figure}
  \centering
  \begin{tikzpicture}
    \begin{groupplot}[
        % This style works without "group style"
        % mygroupb,
        % This style doesn't apply "group style"
        % mygroup,
        % But adding it manually here works
        % group style={
        %   group size=1 by 2,
        %   vertical sep=1cm,
        % },
      ]  
      \nextgroupplot
        \addplot+[samples=5,domain=-3:3] {exp(-x^2+1)};
        \addplot+[samples=5,domain=-3:3] {exp(-x^2)};
        \addplot+[samples=5,domain=-3:3] {exp(-x^2-1)};

      \nextgroupplot
        \addplot+[samples=5,domain=-3:3] {exp(-x^2+1)};
        \addplot+[samples=5,domain=-3:3] {exp(-x^2)};
        \addplot+[samples=5,domain=-3:3] {exp(-x^2-1)};
    \end{groupplot}
  \end{tikzpicture}
\end{figure}

\end{document}
5
  • 1
    I once encountered such scoping issue in pgfkeys. To debug, try .show value or .show code and you will see if pgfkeys does forget those settings. (not guarantee.) If this is the case, you might need to pack your settings into an every foobar, which is executed at the right time.
    – Symbol 1
    Apr 21, 2015 at 14:57
  • @Symbol1 That seems like a useful suggestion, but I don't know when I could try to plug a .show value to try this out. Particularly since, as you note, this is scoped and will change. I made a small test with every axis plot/.style={extra description/.code={\pgfkeys{/pgfplots/group style/.show value}}}, but that definitely did not show anything useful. In any case, this seems to me to be a bug. I just hope someone can show me why it's not.
    – jja
    Apr 21, 2015 at 15:26
  • If you want something to be shown, you should put it in \message{}. Otherwise you cannot distinguish whether it is forgotten or overridden.
    – Symbol 1
    Apr 21, 2015 at 16:43
  • Yes, this is a bug, but rather it is a peculiar feature of the family key which is used to install group styles before. I am sadly not proficient enough in the family key of pgf to decipher the bugfix. Basically the family filter should expand all styles, then subsequently fetch the group style. Yet, currently you have to put group style in the groupplot env to get to work directly. Instead do: .../.style={...,/pgfplots/group/.cd,!allgroupstyles!}. I agree, it isn't beautiful, but it will work.
    – nickpapior
    May 19, 2015 at 11:50
  • 1
    for the record: I added that bug to the PGFPlots tracker. Oct 9, 2016 at 3:02

1 Answer 1

2

The problem is actually simpler than I used to think.

In the definition of \begin{groupplot}[#1] we see the following

\pgfkeysinstallkeyfilter{/pgf/key filters/equals}{/pgfplots/group style}%
\pgfkeysinstallkeyfilter{/pgf/key filters/is descendant of}{/pgfplots/group}%
\pgfqkeysfiltered{/pgfplots}{#1}%

Intuitively it tells that

  • if you say [group style=something] then it is processed;
  • if you say [group/something=else] then it is processed;
  • otherwise the key is ignored.

And through some experiments what it really says is that

  • if you say [group style=something] then it is processed;
    • if you say [mygroup], which is defined to be the same as above, then it is ignored.
  • if you say [group/something=else] then it is processed;
    • if you say [mygroup], which is defined to be the same as above, then it is processed.
  • otherwise the key is ignored.

I am afraid there is no easy way to fix this. Filtering is one of the core features of . Maybe there exists a trick playing around with .cd and .retry and so on. I cannot tell.

After all, does a lot of bizarre things. For example the manual claims that

There is exactly one key filter in effect, and it is installed by the .install key filter handler or by \pgfkeysinstallkeyfilter.

But here it just installs two filters.

1
  • This seems to explain there behaviour I write about. The "correct" usage (ie. the one that works) is quite unintuitive, but it's good to know how to get it to work.
    – jja
    Nov 20, 2016 at 18:28

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.