2

Consider the following MWE: I find it amusing that the second tikzpicture cannot compile without the first, even though the second value for (A) is the one being used. Can anyone explaing this?

In my real example I have a a function plot from which I store some coordinates. Later on I need to add some extra drawings at these named points. Are there any way of doing this other than delaying the scope I use to place things until after \end{axis}?

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.13}
\begin{document}
\begin{tikzpicture}
  % remove this and the second tikzpicture does not compile
   \coordinate (A) at (0,0);
\end{tikzpicture}
\begin{tikzpicture}
  \begin{axis}[
    xmax=3,xmin=0,ymax=3,ymin=0
    ]
     \draw (axis cs: 0,0) -- (axis cs:3,3)
     coordinate [pos=0.4] (A)
     ;
     \fill (A) circle (2pt);

     % more this scope until after \end{axis} and we do not need
     % to have (A) defined in another image
     \begin{scope}[
       shift={(A)},
       ]
       \draw (0,0) circle (1cm);
     \end{scope}
  \end{axis}
\end{tikzpicture}

\end{document}
daleif
  • 54,450
  • 1
    Put the scope inside \pgfplotsextra{...} – percusse May 03 '16 at 13:31
  • 1
    @percusse That worked, never heard of that one, given an answer. BTW: do you know if the pgfplots have something similar to \tikzstyle such that common options to axis can be collected into one style? – daleif May 03 '16 at 13:43
  • The axis environment automatically delays some things to run after \end{axis}. but not others. – John Kormylo May 03 '16 at 13:50
  • 1
    Yes \pgfplotstyle{every axis={...}} and related ones are in the manual. Note that it's not every axis/.style but only the key. More is given in the manual for post processing etc. – percusse May 03 '16 at 13:50
  • 1
    You can use style with pgfplotsset, see http://tex.stackexchange.com/a/139084/38080 --- @percusse, I'd like to see an answer on this, I never heard about \pgfplotsextra{} too.... – Rmano May 03 '16 at 13:51
  • pgfplotsstyle is stupid sorry I meant pgfplotsset – percusse May 03 '16 at 14:02
  • @percusse do you know if one can use the delaying stuff to get outside the clipping. At least in my case I often want to use the clipping to limit the plot, but other stuff that I add can go beyond the borders. – daleif May 03 '16 at 14:04
  • clip mode=individual ? http://tex.stackexchange.com/questions/301585/axis-label-positioning-in-pgfplots – Rmano May 03 '16 at 14:09
  • 1
    @Rmano, that worked, thanks. Too many options – daleif May 03 '16 at 14:16

1 Answers1

1

Since pgfplots has a two phase workflow, first collect and queue and second do the visualization non plotting commands need to be collected in the right order if depends on the previous commands. Otherwise they can be prematurely executed as happened here.

Inspired by \pgfextra (I think), there is a similar command in pgfplots which is \pgfplotsextra that actually helps to queue nonpgfplots commands to the existing ones. Though I would expect this to work automatically because recent versions of pgfplots automatically recognizes the raw TikZ commands somehow didn't go through. Hence we do it manually by putting the scope inside the \pgfplotsextra.

percusse
  • 157,807