10

We have very nice Extract x, y coordinate of an arbitrary point in TikZ

\documentclass{article}
%\url{https://tex.stackexchange.com/q/33703/86}
\usepackage{tikz}
\usetikzlibrary{decorations.markings}

\makeatletter
\newcommand{\gettikzxy}[3]{%
  \tikz@scan@one@point\pgfutil@firstofone#1\relax
  \edef#2{\the\pgf@x}%
  \edef#3{\the\pgf@y}%
}
\makeatother
\begin{document}
\begin{tikzpicture}
\coordinate (A) at (2,1);
\gettikzxy{(A)}{\ax}{\ay}
\fill[red] (\ax,\ay) circle (5pt);

\tikzset{-dot-/.style={decoration={
  markings,
  mark=at position #1 with {\fill circle (2pt);}},postaction={decorate}}} %%% in this line added a ;

\begin{tikzpicture}
 \draw[-dot-=.5] (0,0) to [bend left] (2,4);
 \draw[-dot-=.8] (0,0) to [bend right] (2,4);
\end{tikzpicture}

\end{tikzpicture}
\end{document}

Is it possible to extract x, y coordinate of a point located on the curve? These points can be used for doing some other tasks.

New Answer: Thanks to all

\documentclass{article}
\usepackage{tikz}
\makeatletter
\newcommand{\gettikzxy}[3]{%
  \tikz@scan@one@point\pgfutil@firstofone#1\relax
  \edef#2{\the\pgf@x}%
  \edef#3{\the\pgf@y}%
}
\makeatother

%
\begin{document}
%
\begin{tikzpicture}
\draw (0,0) to [bend left=20]  coordinate[pos=0.7] (A)(2,4);
\draw (0,0) to [bend right=20]  coordinate[pos=0.2] (B)(2,4);
\draw[thick,red] (A) -- (B);
\gettikzxy{(A)}{\ax}{\ay}
\gettikzxy{(B)}{\bx}{\by}
\fill[blue] (\ax, \ay) circle (2pt);
\fill[blue] (\bx, \by) circle (2pt);
\draw[thick,green] (A) -- (\bx,\ay) -- (B);
\draw[thick,yellow] (A) -- (\ax,\by) -- (B);
%
\end{tikzpicture}
%
\end{document}  

enter image description here


Thank you Andrew Stacey...

Now I got a better MWE and using:

\tikzset{pontoncurve/.style={decoration={
  markings,
  mark=at position #1 with {\coordinate (B);}},postaction={decorate}}}

I get the coordinate. But I do not know to change B in \coordinate (B). Should I use \newcommand like \gettikzxy?

\newcommand{\gettikzxy}[3]{%
  \tikz@scan@one@point\pgfutil@firstofone#1\relax
  \edef#2{\the\pgf@x}%
  \edef#3{\the\pgf@y}%
}
\documentclass{article}
%\url{https://tex.stackexchange.com/q/33703/86}
\usepackage{tikz}
\usetikzlibrary{decorations.markings}

\makeatletter
\newcommand{\gettikzxy}[3]{%
  \tikz@scan@one@point\pgfutil@firstofone#1\relax
  \edef#2{\the\pgf@x}%
  \edef#3{\the\pgf@y}%
}
\makeatother
\begin{document}
\begin{tikzpicture}
\coordinate (A) at (2,1);
\gettikzxy{(A)}{\ax}{\ay}
%\fill[red] (\ax,\ay) circle (5pt);

\tikzset{-dot-/.style={decoration={
  markings,
  mark=at position #1 with {\fill circle (2pt);}},postaction={decorate}}} %%% in this line added a ;

\tikzset{pontoncurve/.style={decoration={
  markings,
  mark=at position #1 with {\coordinate (B);}},postaction={decorate}}}

\begin{tikzpicture}
 \draw[pontoncurve=.5] (0,0) to [bend left] (2,4);
\gettikzxy{(B)}{\bx}{\by}
\draw[red] (\bx,\by) -- ++(5,1);

 \draw[-dot-=.8] (0,0) to [bend right] (2,4);
\end{tikzpicture}

\end{tikzpicture}
\end{document}
3
  • 1
    One way would be to create a vertical line (at the x coordinate of where you want to extract the point) with draw=none but name it, and then use the intersections library to find the intersection of this vertical line with the given curve. An example is given in Intersections in PGFplots, and TikZ: Intersection of two lines Commented May 18, 2012 at 8:01
  • 1
    If you want to get the coordinate of the marked point, simply add a coordinate to the \fill command you are using in the marking. More generally, exactly how you extract the coordinates will depend somewhat on how you specify the point. How do you want to do that? Commented May 18, 2012 at 8:14
  • 1
    Regarding your update, in the sample code you don't actually need to get the x and y coordinates of the point: by using coordinate (name) then you have a handle, name, by which you can refer to the point so you can write \draw[red] (B) -- ++(5,1); without needing \gettikzxy at all. Do you want to do anything more complicated with the coordinates than just refer to them? If not, you don't need anything more complicated than the above. Commented May 21, 2012 at 7:19

1 Answer 1

16

You don't need the library decorations.markings and you don't need the macro gettikzxy in your examples; perhaps with other examples, it's useful to work like this.

First \pgfgetlastxy{\ax}{\ay} is enough after

 \coordinate (A) at (2,1); 
  \pgfgetlastxy{\ax}{\ay}  

Instead of

  \draw[-dot-=.5] (0,0) to [bend left]  (2,4);

you have

   \draw (0,0) to [bend left]  coordinate[pos=.5] (B)(2,4);  

Now if you want to get the coordinates, you have several ways like

 \path (B);\pgfgetlastxy{\bx}{\by}  

A complete code

\documentclass{article}
\usepackage{tikz}

\begin{document} 

\begin{tikzpicture}
\coordinate (A) at (2,1);
\pgfgetlastxy{\ax}{\ay}    
\fill[red] (\ax,\ay) circle (5pt);
\end{tikzpicture} 

\begin{tikzpicture}
 \draw (0,0) to [bend left]  coordinate[pos=.5] (B)(2,4);
 \draw (0,0) to [bend right] coordinate[pos=.8] (C)(2,4);
  \path (B);\pgfgetlastxy{\bx}{\by} 
  \path (C);\pgfgetlastxy{\cx}{\cy} 
  \draw[red,thick] (\bx,\by)--(\cx,\cy) ;
\end{tikzpicture}

\end{document}  

Perhaps you need to add another example to get a better answer.

Remark

You can get the coordinates like this with the curveto operation and now with the CVS version, it's possible with the arc operation.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .