1

Possible Duplicate:
Coordinates of intersections

I have the following minimal plot

\documentclass{standalone}

\usepackage{pgfplots}

\usetikzlibrary{intersections}

\newcommand*{\ShowIntersection}[2]{
\fill 
    [name intersections={of=#1 and #2, name=i, total=\t}] 
    [red, opacity=1, every node/.style={above left, black, opacity=1}] 
    \foreach \s in {1,...,\t}{(i-\s) circle (2pt)
        node [above left] {\s}};
}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    scale only axis,
    grid=major,
    grid style={dashed, gray!30},
    axis lines=middle,
    inner axis line style={=>},
    xlabel={\large $x$},
    ylabel={\large $y$},
    yticklabel style={inner ysep=0pt, anchor=south east},
    ytick={-4,-3.5,...,7},
    xticklabel style={inner xsep=0pt, anchor=north west},
    xtick={-3,-2.5,...,5},
    ymin=-0.25,
    ymax= 4.24,
    xmin=-0.25,
    xmax= 4.25,
    after end axis/.code={
        \path (axis cs:0,0) 
            node [anchor=north west,yshift=-0.075cm] {0}
            node [anchor=south east,xshift=-0.075cm] {0};
    }
]
\addplot[name path global=a,color=red,thick,samples=50,domain=-1:5] {0.25*x+2};
\addplot[name path global=b,color=blue,thick,samples=50,domain=-1:5] {2*x-2.5};
\ShowIntersection{a}{b}
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

and this works to automatically find the interesections between the lines. However how can I label the points with its x and y coordinates? Eg i want (2.57,2.64) to be shown above the point.

EDIT: Followings jakes advice, I got closer to a working solution with

\documentclass{standalone}

\usepackage{pgfplots}

\usetikzlibrary{intersections}

\makeatletter
\newcommand\transformxdimension[1]{
    \pgfmathparse{((#1/\pgfplots@x@veclength)+\pgfplots@data@scale@trafo@SHIFT@x)/10^\pgfplots@data@scale@trafo@EXPONENT@x}
}
\newcommand\transformydimension[1]{
    \pgfmathparse{((#1/\pgfplots@y@veclength)+\pgfplots@data@scale@trafo@SHIFT@y)/10^\pgfplots@data@scale@trafo@EXPONENT@y}
}
\makeatother


\newcommand*{\ShowIntersection}[2]{
\fill 
    [name intersections={of=#1 and #2, name=i, total=\t}] 
    [red, opacity=1, every node/.style={above left, black, opacity=1}] 
    \foreach \s in {1,...,\t}{(i-\s) circle (2pt)
        node [above left] {($\!\!\!$\pgfgetlastxy{\macrox}{\macroy}
        \transformxdimension{\macrox}
        \pgfmathprintnumber{\pgfmathresult},%
        \transformydimension{\macroy}%
        \pgfmathprintnumber{\pgfmathresult})}};
}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    scale only axis,
    grid=major,
    grid style={dashed, gray!30},
    axis lines=middle,
    inner axis line style={=>},
    xlabel={\large $x$},
    ylabel={\large $y$},
    yticklabel style={inner ysep=0pt, anchor=south east},
    ytick={-4,-3.5,...,7},
    xticklabel style={inner xsep=0pt, anchor=north west},
    xtick={-3,-2.5,...,5},
    ymin=-0.25,
    ymax= 4.24,
    xmin=-0.25,
    xmax= 4.25,
    after end axis/.code={
        \path (axis cs:0,0) 
            node [anchor=north west,yshift=-0.075cm] {0}
            node [anchor=south east,xshift=-0.075cm] {0};
    }
]
\addplot[name path global=a,color=red,thick,samples=50,domain=-1:5] {2};
\addplot[name path global=b,color=blue,thick,samples=50,domain=-1:5] {4*(x-2)^2-2};
\ShowIntersection{a}{b};
\end{axis}
\end{tikzpicture}
\end{document}

However this gives the wrong coordinates! (obviously the y-coordinates should be 2 and not 2.04)

enter image description here

  • I saw that but alas I could not get the \pgfgetlastxy{\macrox}{\macroy} to work in synergy with \ShowIntersection – N3buchadnezzar Aug 7 '12 at 13:30
  • Give a name to the coordinate of the intersection, and use -| and |- syntax with the axis ends. – JLDiaz Aug 7 '12 at 13:42