4

Please be patient, this is a bit complex. I encountered some weird things when trying to answer this nice question. Start with the MWE

\documentclass[tikz,border=3.14mm]{standalone}
%\usetikzlibrary{intersections}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usepgfplotslibrary{fillbetween}

\begin{document}
\begin{tikzpicture}
 \begin{axis}[axis equal]
    \addplot[thick,samples=80,name path global=duck] {(-3)*x/abs(x)^(2/3)};    % From https://tex.stackexchange.com/a/144463/152550
    \addplot[thick,samples=80,name path global=koala] ({sqrt(16/3)*cos((x) r)}, {sqrt(16)*sin((x) r)});
    \path (0,0) coordinate (O) (1,0) coordinate(X);
    \fill[red,name intersections={of=duck and koala,by={i1,i2}}]
    (i1) circle (2pt) (i2) circle (2pt);
   \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

So far so good. However, if one activates \usetikzlibrary{intersections} and deactivates \usepgfplotslibrary{fillbetween} by just moving the % down, one receives the error

 ! Package pgf Error: No shape named intersection-2 is known.

that tells us that the second intersection cannot be found. If we stick with this, but comment out [axis equal], the error disappears, i.e. the second interaction is found. What's going on here?

Even more, if one moves the determination of the intersections out of the axis environment,

\documentclass[tikz,border=3.14mm]{standalone}
%\usetikzlibrary{intersections}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usepgfplotslibrary{fillbetween}

\begin{document}
\begin{tikzpicture}
 \begin{axis}[axis equal]
    \addplot[thick,samples=80,name path global=duck] {(-3)*x/abs(x)^(2/3)};    % From https://tex.stackexchange.com/a/144463/152550
    \addplot[thick,samples=80,name path global=koala] ({sqrt(16/3)*cos((x) r)}, {sqrt(16)*sin((x) r)});
    \path (0,0) coordinate (O) (1,0) coordinate(X);
   \end{axis}
    \fill[red,name intersections={of=duck and koala,by={i1,i2}}]
    (i1) circle (2pt) (i2) circle (2pt);
\end{tikzpicture}
\end{document}

one gets

enter image description here

meaning that the intersections are not correct. On the other hand, it has been a common practice to compute the intersections outside the axis, see e.g. this nice answer. Notice that even if one drops axis equal in the last example the intersections are still incorrect.

3

The reason that coordinates for the computed coordinates do not match up is a bug in pgfplots.

The underlying reason is explained in the pgfplots manual section "4.26 TikZ Interoperability": pgfplots rescales and translates the coordinate system. Pgfplots does its best to hide this rescaling from the end user. For example, named coordinates within an axis are automatically rescaled after leaving the axis such that you can refer to them after \end{axis}. This feature does not exist for the coordinates of named paths.

A workaround is to ensure that the coordinate systems match up, i.e. to disable the rescaling features of pgfplots.

This is explained in the aforementioned section of the manual. Applied to your example, this might look like

\documentclass[tikz,border=3.14mm]{standalone}
%\usetikzlibrary{intersections}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usepgfplotslibrary{fillbetween}

\begin{document}
\begin{tikzpicture}
 \begin{axis}[axis equal,
    disabledatascaling,
    anchor=origin,
    x=1cm,y=1cm,
    ]
    \addplot[thick,samples=80,name path=duck] {(-3)*x/abs(x)^(2/3)};
    \addplot[thick,samples=80,name path=koala] ({sqrt(16/3)*cos((x) r)}, {sqrt(16)*sin((x) r)});
    \path (0,0) coordinate (O) (1,0) coordinate(X);
   \end{axis}
    \fill[red,name intersections={of=duck and koala,by={i1,i2}}]
    (i1) circle (2pt) (i2) circle (2pt);
\end{tikzpicture}
\end{document}

enter image description here

This might affect rescaling capabilities like axis equal, though.

But again: the reason that this does not work out of the box is actually a bug: pgfplots does not translate the coordinates of named paths when it leaves \end{axis}.

0
2

This is most likely because PGFPlots uses a "newer" version of the intersections library than PGF does and thus this is similar to the external library (compare https://tex.stackexchange.com/a/341662/95441). To support this you can do a diff on the both files

pgflibraryintersections.code.tex
pgfplotsoldpgfsupp_pgflibraryintersections.code.tex
2
  • 1
    Thanks! But this only addresses the more minor problem. The main issue is why the intersections are off if one computes them outside of the axis environment.
    – user121799
    Oct 28 '18 at 18:34
  • 1
    There I cannot help you. But I would compute the intersections inside the axis environment (and label them) and then you can do a foreach loop outside if you want. I'll contact Christian and ask him to have a look at this issue. Oct 28 '18 at 18:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy