Why does pgfplots package cause tikz layer to disappear?

I wish to include a pgf-generated line plot and a tikz-generated graphical model in the same document. However, the presence of the pgfplots package (needed for the axis environment) seems to turn one of the layers in the graphical model invisible, as in the following almost-minimal example:

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

\pgfdeclarelayer{f}
\pgfsetlayers{main,f}

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

\begin{tikzpicture}
\node (a) [draw, circle, minimum size=40pt] {$a$};
\begin{pgfonlayer}{f}
\node (plate) [draw, minimum size=60pt] {};
\end{pgfonlayer}
\end{tikzpicture}
\end{document}


What I want to be drawn is a simple line graph followed by a circular node inside of a rectangular plate. What ends up being drawn is a simple line graph followed by a circular node, without the rectangular plate. Basically, the f layer seems to disappear when pgfplots is imported. This occurs in both LaTeX and XeTeX.

It should be noted that the first tikzpicture environment makes this a non-minimal example. It can be removed, and the invisibility of the layer toggled simply by commenting or uncommenting the \usepackage{pgfplots} line. However, I retained it as it illustrates my use case.

How can I use both the axis environment from pgfplots and have a second layer in my graphical model? Ideas? Workarounds? Explanations?

-

Update: This was a bug. The currently pgfplots version 1.7 has fixed this issue.

The following code works and gives the expected output.

Code

\documentclass{article}
\usepackage{pgfplots}
\pgfdeclarelayer{f}
\pgfsetlayers{main,f}

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

\begin{tikzpicture}
\node (a) [draw, circle, minimum size=40pt] {$a$};
\begin{pgfonlayer}{f}
\node (plate) [draw, minimum size=60pt] {};
\end{pgfonlayer}
\end{tikzpicture}
\end{document}


Output

This is a bug.

The suggested code before \usepackage{pgfplots} kind of works.

\ifx\pgfplothandlerjumpmarkmid\undefined
\def\pgfplothandlerjumpmarkmid{123}
\fi


There are still “Undefined control sequence” errors in the axis environment—which is not present in the bug reporter's code. (Those error message don't appear to have any repercussion on the output, though).

To get rid of them, too, I defined three other macros before loading pgfplots.

(The \if-clauses are only there to check if the macros really aren't defined. {123} is just any arbitrary thing that makes them defined, could be {42} or even {} instead.)

I suggest you use the recent pgfplots unstable version (second option in the newest comment on the bug tracker linked above)!

Code

\documentclass{article}
\ifx\pgfplothandlerjumpmarkmid\undefined
\def\pgfplothandlerjumpmarkmid{123}
\fi
\ifx\pgfplothandlerconstantlinetomarkmid\undefined
\def\pgfplothandlerconstantlinetomarkmid{123}
\fi
\makeatletter
\ifx\pgf@remember@layerlist@globally\undefined
\def\pgf@remember@layerlist@globally{123}
\fi
\ifx\pgf@restore@layerlist@from@global\undefined
\def\pgf@restore@layerlist@from@global{123}
\fi
\makeatother
\usepackage{pgfplots}
\pgfdeclarelayer{f}
\pgfsetlayers{main,f}

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

\begin{tikzpicture}
\node (a) [draw, circle, minimum size=40pt] {$a$};
\begin{pgfonlayer}{f}
\node (plate) [draw, minimum size=60pt] {};
\end{pgfonlayer}
\end{tikzpicture}
\end{document}

-
Thank you for such a detailed and instantly helpful answer! Your code works for me :-) –  user17219 Oct 16 '12 at 4:44
@user17219 I'm glad I could help. Just remember that you probably can remove those lines when pgfplots gets updated. –  Qrrbrbirlbel Oct 16 '12 at 17:31
Fixed in pgfplots 1.7 –  Christian Feuersänger Oct 29 '12 at 20:51
I can confirm that this is fixed in 1.7. Hooray! –  user17219 Nov 2 '12 at 23:17