# More: Generalised: Accessing the logic values of a TikZ coordinate

This is a follow-up question to: Generalised: Accessing the logic values of a TikZ coordinate.

In that question a PGFplot answer was suggested by jake.

I took that answer and modified it to the following MWE:

\documentclass{article}
\usepackage{pgfplots}
\usetikzlibrary{intersections}

\newcommand\xcoord[center]{{%
\pgfpointanchor{#2}{#1}%
\pgfgetlastxy{\ix}{\iy}%
\pgfplotspointaxisorigin%
\pgfgetlastxy{\ox}{\oy}
\pgfmathparse{(\ix-\ox)/\pgfplotsunitxlength/1000}
\pgfmathprintnumber{\pgfmathresult}}
}

\begin{document}

\vspace{1cm}
\begin{tikzpicture}
\begin{axis}[
domain=1:8.2,
samples=150,
no markers,
axis lines=middle,
enlarge x limits=upper,
enlarge y limits=true,
x axis line style={{name path global=xaxis}}
]
\pgfplotsextra{
\fill [name intersections={of=xaxis and plot, name=i, total=\t}]
[red, every node/.style={black}]
(i-1) circle (2pt) node [pin={\xcoord{i-1}}] {}
(i-2) circle (2pt) node [pin={\xcoord{i-2}}] {};
}
\end{axis}
\end{tikzpicture}
\end{document}


This produces the following: So the coordinates is off by a factor 10.

How would one correct that and I was wondering how one would access them through a foreach loop in stead of specifically making a node for each of them.

Hm, something's not quite right with the \pgfplotsunitxlength macro. I've written a new macro \xcoord macro that works by extracting the values for the plot points (0,0) and (1,1) and using that for the unit vector. Note also that \pgfplotspointorigin points to the point where the two axes meet, not to the point (0,0), so the value was not only off by a factor of 10, but also by a shift of 1. I used the point (0,0) in the code below to correct for this.

You can just loop through the intersections by using \foreach \n in {1,...,\t}, where \t is the macro that holds the total number of intersections specified in total=\t.

There are sometimes issues with intersections being found twice if they lie exactly on one of the support points of the plot. In that case, you should adjust the domain slightly.

\documentclass{article}
\usepackage{pgfplots}
\usetikzlibrary{intersections}

\newcommand\xcoord[center]{{%
% The actual point of interest
\pgfpointanchor{#2}{#1}%
\pgfgetlastxy{\ix}{\iy}%
% (0,0)
\pgfplotspointaxisxy{0}{0}%
\pgfgetlastxy{\ox}{\oy}
% (1,1)
\pgfplotspointaxisxy{1}{1}%
\pgfgetlastxy{\ux}{\uy}
\pgfmathparse{(\ix-\ox)/(\ux-\ox)}
\pgfmathprintnumber{\pgfmathresult}}
}

\begin{document}

\vspace{1cm}
\begin{tikzpicture}
\begin{axis}[
xmin=0,
domain=0:8.4,
restrict y to domain=-100:50,
samples=149,
no markers,
axis lines=middle,
enlarge x limits=upper,
enlarge y limits=true,
x axis line style={{name path global=xaxis}}
]
\pgfplotsextra{
\fill [name intersections={of=xaxis and plot, name=i, total=\t},
red,
every node/.style={black}]
\foreach \n in {1,...,\t} {
(i-\n) circle (2pt) node [pin={\xcoord{i-\n}}] {}
};
}
\end{axis}
\end{tikzpicture}
\end{document} • TNX it works exactly as you indicated. – Louis Jul 24 '11 at 17:22