0

How to fix the syntax of \draw (i-2) circle let \p{dgcr} = (i-2) in node at (i-2) {$d_{g,cr} = \fpeval{\x{dgcr}/0.35}$}; to make the node calculated number correct (i.e., it should be around 0.6, not 316)?

enter image description here

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

\begin{document} \begin{tikzpicture} \begin{axis}[ xmin = 0, xmax = 0.4, ymin = 0, ymax = 0.6, xtick distance = {0.2}, ytick distance = {0.2}, minor tick num = 1, grid = both, ] %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \addplot [ domain = 0:1.5, samples = 100, black, variable = dg, name path = OptBrakingDist, ] ( { dg / 2.5 * (1.2 - dg * 0.5) }, { dg / 2.5 * (1.3 + dg * 0.5) } ); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \addplot [ domain = 0:\fpeval{1/(1 - 0.35)}, %samples = 2, blue,
variable = dg, name path = fixeddist, ] ( { 0.35 * dg } , { (1 - 0.35) * dg } ) ; %%%%%%%%%%%%%%%%%%%%%%%% % Find and label the intersections \path [name intersections = {of = fixeddist and OptBrakingDist, name=i}]; % \draw[fill=red, draw=none] (i-2) circle [radius=3pt] let \p{dgcr} = (i-2) in node[left=3pt, inner sep=0pt, fill=white, scale=0.75] at (i-2) {$d_{g,cr} = \fpeval{\x{dgcr}/0.35}$}; \end{axis} \end{tikzpicture} \end{document}

Diaa
  • 9,599
  • See section 101.6 on page 1093. \pgfextractx and \pgfextracty use length registers and usually \pgfpointanchor, while \pgfgetlastxy uses macros, typically after \path locates the point. – John Kormylo Dec 05 '23 at 22:07
  • Maybe (no time to try now) https://tex.stackexchange.com/a/179946/38080 can help? – Rmano Dec 05 '23 at 22:32
  • @JohnKormylo \newdimen\mydim \pgfextractx\mydim{\pgfpointanchor{i-2}{center}} \draw (i-2) circle node at (i-2) {$d_{g,cr} = \fpeval{\mydim/0.35}$}; throws the error No shape named i-2 is known. – Diaa Dec 06 '23 at 06:32
  • @Rmano \extractcoord\x\y{i-2} \draw (i-2) circle node at (i-2) {$d_{g,cr} = \fpeval{round(\x/0.35, 1)}$}; prints 650, not 0.7 as expected. – Diaa Dec 06 '23 at 06:36
  • You can fix the unknown shape problem using \pgfplotsextra. Actually, you can move the entire intersections calculations outside the axis environment, since the path names are stored globally. OTOH, they give tikz coordinates in pt, not whatever units pgfplots uses for axis cs: coordinates. – John Kormylo Dec 06 '23 at 16:42

2 Answers2

5

I found this post Coordinates of intersection and I wanted to know if I knew it was suitable. The key is \pgfplotspointgetcoordinates{(i-2)}

the code

\documentclass{article}
\usepackage{pgfplots}
\usetikzlibrary{calc, intersections}
%%%%%%%%%%%%%%
%   https://tex.stackexchange.com/questions/220284/coordinates-of-intersection
%
%%%%%%%%%%%%%%
\newcommand*{\ShowIntersection}{
        \fill [
            name intersections={
                of = fixeddist and OptBrakingDist,
                name=i,
            },
            fill=red,
        ]  (i-2) circle (3pt)
            node [left=3pt, inner sep=0pt, fill=white, scale=0.75] {
                % -------------------------------------------------------------
                % using `\pgfplotspointgetcoordinates' stores the (axis)
                % coordinates in `data point' which then can be called by
                % `\pgfkeysvalueof'
                \pgfplotspointgetcoordinates{(i-2)}
                $d_{g,cr} \approx \pgfmathprintnumber{\fpeval{\pgfkeysvalueof{/data point/x}/0.35}}$
                % -------------------------------------------------------------
        };
    }
\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
            xmin = 0, xmax = 0.4, 
            ymin = 0, ymax = 0.6,
            xtick distance = {0.2},
            ytick distance = {0.2},
            minor tick num = 1,
            grid = both,
            ]
            %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
            \addplot 
            [
            domain = 0:1.5, 
            samples = 100, 
            black, 
            variable = dg,
            name path = OptBrakingDist,
            ] (
            { dg / 2.5 * (1.2 - dg * 0.5) },
            { dg / 2.5 * (1.3 + dg * 0.5) } 
            );
            %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
            \addplot 
            [
            domain = 0:\fpeval{1/(1 - 0.35)}, 
            %samples = 2,
            blue,           
            variable = dg,
            name path = fixeddist,
            ]
            ( { 0.35 * dg } , { (1 - 0.35) * dg } )
            ;
            %%%%%%%%%%%%%%%%%%%%%%%%
            % Find and label the intersections
            \ShowIntersection
            % \path [name intersections = {of = fixeddist and OptBrakingDist, name=i}];
            % %
            % \draw[fill=red, draw=none] (i-2) circle [radius=3pt] let \p{dgcr} = (i-2) in node[left=3pt, inner sep=0pt, fill=white, scale=0.75] at (i-2) {$d_{g,cr} = \fpeval{\x{dgcr}/0.35}$};
        \end{axis}
    \end{tikzpicture}
\end{document}

enter image description here

pascal974
  • 4,652
1

The tricky bit is getting the answer in axis cs: units. So first I get the screen coordinates for a known location, then used them to compute the axis cs: coordinates given the screen coordinates for the intersection.

Note that the axis origin is not always the same as the tikz origin.

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

\begin{document} \begin{tikzpicture} \begin{axis}[ xmin = 0, xmax = 0.4, ymin = 0, ymax = 0.6, xtick distance = {0.2}, ytick distance = {0.2}, minor tick num = 1, grid = both, ] %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \addplot [ domain = 0:1.5, samples = 100, black, variable = dg, name path = OptBrakingDist, ] ( { dg / 2.5 * (1.2 - dg * 0.5) }, { dg / 2.5 * (1.3 + dg * 0.5) } ); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \addplot [ domain = 0:\fpeval{1/(1 - 0.35)}, %samples = 2, blue,
variable = dg, name path = fixeddist, ] ( { 0.35 * dg } , { (1 - 0.35) * dg } ) ; \path (axis cs: 0,0) coordinate (origin);% axis origin \path (axis cs: 0.1,0.1) coordinate (unit);% 0.1 unit vector \end{axis} \path (origin); \pgfgetlastxy{\xo}{\yo}% \path (unit); \pgfgetlastxy{\xunit}{\yunit}% %%%%%%%%%%%%%%%%%%%%%%%% % Find and label the intersections \path [name intersections = {of = fixeddist and OptBrakingDist, name=i}]; % \draw[fill=red, draw=none] (i-2) circle [radius=3pt] let \p{dgcr} = (i-2) in node[left=3pt, inner sep=0pt, fill=white, scale=0.75] at (i-2) {$d_{g,cr} = \fpeval{\x{dgcr}/0.35}$}; \path (i-2); \pgfgetlastxy{\xint}{\yint}% \draw (i-2) circle node[above] at (i-2) {$d_{g,cr} = \fpeval{0.1*(\xint-\xo)/(\xunit-\xo)}$};

\end{tikzpicture}

\end{document}

John Kormylo
  • 79,712
  • 3
  • 50
  • 120