3

When I compile the following code with ybar stacked option and the request for newsprint background the compiler tells me

! Extra }, or forgotten \endgroup.
\endpgfonlayer ->\endgroup \hss \egroup 
                                        \endgroup 
l.31 \end{axis}

When I compile with just ybar I get what I expect, with the background image. When I compile with ybar stacked and the background request commented out I get what I expect.

Here's my MWE. Note that the plot makes no sense and there's nothing there to stack. The stacking works just fine with the real data and labels, which I removed to isolate the problem and minimize the MWE. (That took me quite a while.)

TeX version:

$ pdflatex -version
MiKTeX-pdfTeX 2.9.4535 (1.40.13) (MiKTeX 2.9)

code:

\documentclass{standalone}
\usepackage{tikz,pgfplots}
\usepackage{pgfplotstable}

%deal with warning message in log
\pgfplotsset{compat=1.8}

\newcommand{\images}{../images}

\begin{document}
\pgfdeclarelayer{background}
\pgfsetlayers{background,main} 

\pgfplotstableread[col sep=comma]{
category, mass, other
{dollars\\\$3.14m}, 31, 69
}\warren

\begin{tikzpicture}
\begin{axis}[ 
    ybar stacked,
%    ybar,
]
\addplot table [x expr =\coordindex, y=mass] {\warren};
% with the following lines in place
% compilation fails with ybar stacked, works with ybar
\begin{pgfonlayer}{background}
  \node{\includegraphics{\images/newsprint}};
\end{pgfonlayer}

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

See Combine pgfplots on common background for what the background will look like when the question has been answered.

Ethan Bolker
  • 9,333
  • 3
  • 42
  • 69

1 Answers1

5

Inside axis environment pgfplots rules. It works a little different than TikZ so TikZ parsing needs to be placed in such a way that pgfplots cooperate. Moreover, pgfplots have different layering in action so you need to introduce what you are doing with TikZ to it too.

What Jake did in the linked question is to stay outside the axis environment and operate on the background layer, then use the axis environment. That's different than sneaking into pgfplots layers.

\documentclass{standalone}
\usepackage{pgfplots,mwe}
\usepackage{pgfplotstable}

\pgfplotsset{compat=1.8,set layers}


\pgfplotstableread[col sep=comma]{
category, mass, other
{dollars\\\$3.14m}, 31, 69
{dollars\\\$3.14m}, 31, 18
{dollars\\\$3.14m}, 34, 40
{dollars\\\$3.14m}, 34, 20
}\warren

\begin{document}

\begin{tikzpicture}
\begin{axis}[ 
    ybar stacked,
%    ybar,
]
\addplot+[] table [x=mass, y=other] {\warren};

\pgfplotsextra{\begin{scope}[on layer=axis background]
  \node at (axis cs:32.5,50) {\includegraphics[scale=0.5]{example-image-a}};
\end{scope}
}

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

enter image description here

percusse
  • 157,807
  • So stacking and background interfere with one another because each is implemented by layering - but in different ways in different environments. Either kind of layering works fine by itself. How could I possibly have figured that out without SE?! – Ethan Bolker Jun 30 '13 at 20:54
  • @EthanBolker Your layer stacking is invalid inside the axis environment because pgfplots declare once a again a new list (you can find it in the manual). So your stacking of layers will be unknown there, you need to say pgfplots that you want it in one of its layers. Or you can specifically add your layer into its layers. Otherwise it will be unknown. My guess is that ybar doesn't turn on the layers so you get away with it but stacked does and previous layers are overwritten. – percusse Jun 30 '13 at 20:59
  • OK I understand a little bit more. I'm still having trouble getting what I need. Jakes answer does seem to have the background inside the axis, but creates a background image larger than the plot, as desired. I am having trouble making your solutions do that. Should I post another question for you to answer? – Ethan Bolker Jun 30 '13 at 21:19
  • @EthanBolker Axis environment also clips and can be turned off via clip=false. Does Jake's solution work when you switch to stacked plots? – percusse Jun 30 '13 at 21:31
  • Jake's solution works when I change plot type to stacked, but his code pasted into my example produces the compiler error I've asked about. clip=false does help with your code, but the background image covers material (legend and title) I've placed outside the plot area. Should I post a new question, or dramatically edit this one, in either case incorporating your idea and asking for further help? PS - isn't it rather late in Rotterdam for you to be responding to queries in real time? – Ethan Bolker Jun 30 '13 at 21:48
  • @EthanBolker Well let's get down to it in another question. You can use mwe package for dummy images. Just put your expected workflow and we work on it. Otherwise every piece is adding another complication :) – percusse Jun 30 '13 at 21:50
  • Questions reformulated at http://tex.stackexchange.com/questions/121888/background-for-pgfplots-and-other-tikz-constructs – Ethan Bolker Jul 01 '13 at 17:37