[Tex/LaTex] Using let command in TikZ but want to have reference to new point later as well

calculationstikz-pgf

I'm beginning to use TikZ and am having some issues understanding the proper way to reference my data points without specifying the precise absolute positions for all of them. An example of the simple situation I'm confused with is as follows:

Let's say I have 3 points, A=(ax,ay), B=(bx,by) and C=(bx,by-1). I want to draw lines between these points, but I also want them to be labelled A, B and C so that I may refer to them later. If I simply define my points A and B like so:

\coordinate (A) at (ax,ay);
\coordinate (B) at (bx,by);

then what is the best way to get C? Should I try something like

\coordinate (C) at "some way of referring to B's coordinates and doing math on them"

or do I have to use some (seemingly) clumsy statement like

\coordinate (C) at let \p1 = (B) in (\x1,\y1-1);

It seems like it'd be best if I could do the former option, but many answers on this type of topic online focus on the use of the 'let' command. However, my usage of the let command above is incorrect. I also tried things like

let \p1 =(B) in \coordinate (C) at (\x1,\y1-1);

but this also didn't work. I haven't seen any examples of the let command used in conjunction with \coordinate so not sure what the syntax should be.

So my question is, what is the best way to assign C? I could define it inline when using \draw to draw the line from A to B to C, but then I don't think I'd have access to the C coordinate again without recomputing.

Best Answer

Basic Coordinate Calculations:

You can use the tikz's calc library to compute the coordinate for (C) as follows:

\coordinate (C) at ($(B) + (0,-1)$);

The use of the $ triggers the computation.

enter image description here

You can also compute positions between one coordinate to another. For example ($(A)!0.5!(B)$) represents the amount that is 0.5 along the line from (A) to (B).

The example below draws a red line from (C) to this point which it label the node as (D). This point (D) can then be referred to later, as shown in the case of the brown line. Note that if you don't use shape=coordinate you would need to use (D.center) to access the middle of the node.

References:


As percusse suggested, calculation of ($(B) + (0,-1)$) can also be done without the use of tikz's calc library using the shift option:

\coordinate (C) at ([shift={(0,-1)}]B);

Code:

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

\begin{document}
\begin{tikzpicture}
\coordinate (A) at (0,1);
\coordinate (B) at (2,3);
\coordinate (C) at ($(B) + (0,-1)$);

\draw [thick, blue]
       (A) node [black, left ] {A}
    -- (B) node [black, right] {B}
    -- (C) node [black, right] {C};

\draw [thick, red] (C) -- ($(A)!0.5!(B)$) 
    node [shape=coordinate] (D) {};

\draw [thick, brown] (D) -- ($(A)!0.5!(C)$);

\end{tikzpicture}
\end{document}

Advanced Coordinate Calculations:

The above method works great if all you are doing is adding/subtracting coordinates. If however you want to apply more general transformations, we need to access the individual x and y coordinates. For this the let syntax is useful.

For example, to compute the case indicated in the comments where we desire C to be at some multiple of the x and y coordinates, we can use:

\path let \p1=(B) in coordinate (C) at (4*\x1,5*\y1);