Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
    
Welcome to TeX.sx! It sounds like you might want \usetikzlibrary{calc} which allows you to perform calculations on coordinates. –  cmhughes Jan 11 '13 at 20:10
    
Thanks for the welcome cmhughes! I have calc loaded already, but am not sure the syntax appropriate to fix the above problem. Do I need part of what I wrote above in $ $ or is my syntax further from the solution than this? –  gammapoint Jan 11 '13 at 20:19
    
Something like \draw ($(A)+(0,0.6ex)$) -- ($(B)+(0,0.6ex)$); –  cmhughes Jan 11 '13 at 20:23
add comment

1 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);
share|improve this answer
    
Can you also include a let example too? I wouldn't say no to a sans-calc solution with at ([shift={(0,-1)}]B), heheh. –  percusse Jan 11 '13 at 20:31
    
Thanks Peter. This would definitely work for my current problem, but what would you do for a more general situation, say if C=(4*bx,5*by) for example? –  gammapoint Jan 11 '13 at 20:33
    
Maybe in the above case you would use let in combination with a node to define C? –  gammapoint Jan 11 '13 at 20:39
    
@percusse: Added your favorite non-calc option. –  Peter Grill Jan 11 '13 at 20:59
    
@gammapoint: Also added an example of using the let syntax to do more complicated transformations. –  Peter Grill Jan 11 '13 at 21:00
show 4 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.