[Tex/LaTex] Convert from physical dimensions to axis cs coordinate values

lengthspgfplotstikz-pgf

In answering PGFPlots: How to draw a label at a zero of a function?, I got stuck on converting a length in pt to the actual coordinate value in the axis cs system.

Here I only need the x value, but my question is how does one obtain the actual x/y values of a coordinate once you have their physical location.

Sub-Question:

  • Why does the circle not get drawn for the intersection on the left once I use \pgfextra?

enter image description here

Notes:

Code:

\documentclass[border=2pt]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{intersections}

% https://tex.stackexchange.com/questions/33703/extract-x-y-coordinate-of-an-arbitrary-point-in-tikz
\newdimen\XCoord
\newdimen\YCoord
\newcommand*{\ExtractCoordinate}[1]{\path (#1); \pgfgetlastxy{\XCoord}{\YCoord};}%

\begin{document}

\begin{tikzpicture}
    \begin{axis}[
            axis x line=middle,
            axis y line=middle,
            domain=-4:7,
            xmax=7,
        ]
        \addplot[no marks,blue,thick, name path global=My Graph] {x*x-4*x-7};
        \addplot[no marks,draw=none,  name path global=x Axis  ] {0};
        \fill[red,name intersections={of=My Graph and x Axis,total=\t}]
            \foreach \s in {1,...,\t}{
                \pgfextra{\ExtractCoordinate{intersection-\s}}
                (intersection-\s) circle (2pt) 
                node [above] {$\XCoord$}
            };
    \end{axis}
\end{tikzpicture}
\end{document}

Best Answer

Originally posted on PGFPlots: How to draw a label at a zero of a function? There may be a better way than this, but I haven't found one yet.

tick marks

\documentclass[border=2pt]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{intersections}

\newlength{\len}
\newlength{\plotwidth}
\newcommand{\getvalue}[1]{\pgfkeysvalueof{/pgfplots/#1}}

%output will be given by \pgfmathresult
\newcommand{\xcoord}[1]% #1 = node name
{\pgfplotsextra{%
  \pgfextractx{\len}{\pgfpointdiff{\pgfplotspointaxisxy{0}{0}}{\pgfpointanchor{#1}{center}}}%
  \pgfextractx{\plotwidth}{\pgfpointdiff{\pgfplotspointaxisxy{\getvalue{xmin}}{0}}%
    {\pgfplotspointaxisxy{\getvalue{xmax}}{0}}}%
  \pgfmathparse{\len*(\getvalue{xmax}-\getvalue{xmin})/\plotwidth}%
}}

\begin{document}

\begin{tikzpicture}
    \begin{axis}[
            axis x line=middle,
            axis y line=middle,
            domain=-4:7
        ]
        \addplot[no marks,blue,thick, name path global=My Graph] {x*x-4*x-7};
        \addplot[no marks,draw=none, name path global=x Axis] {0};
        \path[name intersections={of=My Graph and x Axis,total=\t}];

        \draw[very thin,color=gray] (intersection-1) -- +(0,-5mm) coordinate(tick1);
        \xcoord{tick1}%
        \node[below] at (tick1) {$\pgfmathresult$};

        \draw[very thin,color=gray] (intersection-2) -- +(0,-5mm) coordinate(tick2);
        \xcoord{tick2}%
        \node[below] at (tick2) {$\pgfmathresult$};
     \end{axis}

\end{tikzpicture}
\end{document}

It should be noted than you get the wrong answer if \xcoord is inside a node (text field) or a path, and obviously if any other \pgfmath calculation occurs between it an \pgfmathresult. Also, \pgfplotsconvertunittocoordinate and \pgfplotsunitxlength were tried unsucessfully.