# extract x value from coordinate in TikZ

maybe I'm stupid, I looked at the many examples in which somehow shows the work with the coordinates, but I don't understand it. Simple example - I have:

\begin{tikzpicture}
\coordinate [label=left:$D$] (D) at (0.3,0.5);
\end{tikzpicture}

Now I would like to use x-part of my coordinate in the next \draw command, but I don't know how to simply extract it.

\draw[name path=my_line, gray] (**x-part of D coordinate**,0) node[below, red]{$c$} -- (0.75,1.1);

I hope that's my question is obvious. Thanks!!

• look up the let keyword Aug 6 '12 at 22:21
• Aug 6 '12 at 22:23

You can use the let syntax (See Section 14.15 The Let Operation of the manual); another option (suggested by percusse in a comment) is to use the |- syntax (page 131 of the manual). An example with both possibilities:

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

\begin{document}

\begin{tikzpicture}
\coordinate [label=left:$D$] (D) at (0.3,0.5);
\draw[name path=my_line, gray] let \p1=(D) in (\x1,0) node[below, red] {$c$} -- (0.75,1.1);
\end{tikzpicture}

\begin{tikzpicture}
\coordinate [label=left:$D$] (D) at (0.3,0.5);
\draw (D |- {{(0,0)}}) node[below, red] {$c$} -- (0.75,1.1);
\end{tikzpicture}

\end{document}

Quoting from the manual:

the meaning of (p-|q) is ''the intersection of a vertical line through p and a horizontal line through q''.

• You can also use \draw (D |- {{(0,0)}}) -- (0.75,1.1);. Aug 6 '12 at 22:30
• \draw (D |- 0,0) -- (0.75,1.1); works too. Oct 10 '13 at 9:22

You can also adapt the solution from Extract x, y coordinate of an arbitrary point in TikZ and invoke \ExtractCoordinate{D} before you need the coordinates of this point and then use \XCoord or \YCoord where you need the value.

The code below produces output identical to Gonzalo Medina's answer.

## Code:

\usepackage{tikz}
\usetikzlibrary{calc}

% 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}
\coordinate [label=left:$D$] (D) at (0.3,0.5);

\ExtractCoordinate{D}
\draw[gray] (\XCoord,0) node[below, red] {$c$} -- (0.75,1.1);
\end{tikzpicture}

For the sake of completeness and future reference, if you have a point (a) defined, you can define a new point (b) which depends on the coordinates of (a) using the let as follows:

\path let \p1 = (a) in coordinate (b) at (2*\x1,\y1/2);

Since \coordinate is an alias for \path coordinate you have to use the above code if you want to use let for the definition of a coordinate.