# pgfplot addplot with predefined coordinates

I have a bunch of coordinates predefined, where I want to draw multiple lines in a plot by reusing coordinates. If I change a coordinate value, I only want to have to change it once, and not in multiple lists of coordinates. Hence I want to do something like the following

\documentclass[tikz]{standalone}

\usepackage{tikz,pgfplots}
\pgfplotsset{compat=1.9}

\begin{document}
\begin{tikzpicture}

\begin{axis}
\coordinate (p1) at (1,1);
\coordinate (p2) at (2,2);
\end{axis}

\end{tikzpicture}
\end{document}


To generate something that looks like this

But apparently life can't be that easy, because I get the following error

Runaway argument?
p1)(p2)\pgfplots@EOI \pgfplotsscanlinelengthcleanup
\pgfplots@coord@stream@end
\ETC.
Paragraph ended before \pgfplots@loop@next was complete.
\par
l.15


I've tried defining the coordinates outside the axis environment as well, but that didn't work either.

Any ideas on how to use predefined coordinates in an \addplot coordinates list?

• This is one of the so-called expansion problems, I believe. So in this way I think it is related to this but cannot solved with the same methods I think. – marmot Feb 17 '18 at 23:42
• There must be a key to turn off the math parser of plot coordinates but I don't remember the actual name. I think if you turn it off it will read p1 in proper context – percusse Feb 18 '18 at 10:38
• Just an idea: Maybe you can store the values in an external data file and read them in. So you only have to store it once. – Dr. Manuel Kuehner Feb 18 '18 at 13:31

As I mentioned, this is probably because pgfplots delays its expansions. This way it is possible to zoom automatically into the relevant region and so on. Therefore I cannot offer you a pretty solution. The best workaround I was able to produce is

\documentclass[tikz]{standalone}
\makeatletter
% from https://tex.stackexchange.com/q/56353/121799
\newcommand{\gettikzxy}[3]{%
\tikz@scan@one@point\pgfutil@firstofone#1\relax
\global\edef#2{\the\pgf@x}%
\global\edef#3{\the\pgf@y}%
}
\newcommand{\gettikzcoordinates}[2]{%
\tikz@scan@one@point\pgfutil@firstofone#1\relax
\pgfmathsetmacro{\myx}{round(0.99626*\the\pgf@x/0.0283465)/1000}
\pgfmathsetmacro{\myy}{round(0.99626*\the\pgf@y/0.0283465)/1000}
\global\edef#2{(\myx,\myy)}%
}
\makeatother
\usepackage{tikz,pgfplots}
\pgfplotsset{compat=1.9}

\begin{document}
\begin{tikzpicture}
\coordinate (p1) at (1,1);
\coordinate (p2) at (2,2);
\begin{axis}
\gettikzcoordinates{(p1)}{\Pone}
\gettikzcoordinates{(p2)}{\Ptwo}

This code produces the desired plot. As you can check, if you move the \begin{axis} before the \coordinate statements, you'll get an error, essentially because the coordinates were not defined when they are needed. Very often you can get away by using \edef\temp{<something>}\temp, but in this situation I only got trapped in endless loops. (The \gettikzxy is not relevant here, I just wanted to indicate where I got the idea how to extract the coordinates from.)