34

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!!

2

3 Answers 3

33

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''.

enter image description here

3
  • 5
    You can also use \draw (D |- {{(0,0)}}) -- (0.75,1.1);.
    – percusse
    Commented Aug 6, 2012 at 22:30
  • @percusse yes, you're right: I keep forgetting about this (simpler) way; I'll add it to my answer. Commented Aug 6, 2012 at 22:31
  • 3
    \draw (D |- 0,0) -- (0.75,1.1); works too. Commented Oct 10, 2013 at 9:22
23

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:

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

%% https://tex.stackexchange.com/questions/86897/recover-scaling-factor-in-tikz
\newcommand*\getscale[1]{%
  \begingroup
    \pgfgettransformentries{\scaleA}{\scaleB}{\scaleC}{\scaleD}{\whatevs}{\whatevs}%
    \pgfmathsetmacro{#1}{sqrt(abs(\scaleA*\scaleD-\scaleB*\scaleC))}%
    \expandafter
  \endgroup
  \expandafter\def\expandafter#1\expandafter{#1}%
}

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


\begin{document}
\begin{tikzpicture}[scale=2]
   \coordinate (D) at (0.3,0.5);

  \ExtractCoordinate{D}
  \fill [red] (D) circle (1pt);
  \draw[gray] (\XCoord,0) node[below, red] {$c$} -- (0.75,1.1);
\end{tikzpicture}
\end{document}
2
  • Unless I am doing something wrong, this method doesn't seem to work with scaling. I.e. if we pass a parameter 'scale=0.5' to \begin{tikzpicture}, the resulting coordinates \XCoord, \YCoord seem off. I am trying to take the coordinates of a regular polygon defined as \node[draw=none, minimum size=4cm, regular polygon, regular polygon sides = 8] (a) {}; And then taking \ExtractCoordinate{a.corner 3}, for example, produces a wrong coordinate with scaling (but a correct one without scaling).
    – mathreader
    Commented Apr 27, 2022 at 12:26
  • 1
    @mathreader: Solution updated to handle scale= issue. Thanks for pointing that out. Commented Apr 27, 2022 at 21:23
19

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.

You must log in to answer this question.

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