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.

In one of my post, Jake used the \draw let to determine the arc between 2 lines. Where can I find information to better understand this command? Or what do I look up? Similar, I would like to learn more about atan2. I assume it means arctan but what is the 2 for?

\draw let
      \p0 = (F),
      \p1 = (P1),
      \p2 = (P2),
      \n1 = {atan2(\x1 - \x0, \y1 - \y0)},
      \n2 = {atan2(\x2 - \x0, \y2 - \y0)},
      \n3 = {.3cm}
    in (F) +(\n1:\n3) arc [radius = \n3, start angle = \n1, end angle = \n2]
    node[scale = .75, pos = .5, above = .25cm] {\(\Delta\nu\)};

I would like to understand this code a lot better than my present understanding which is center point, right point, left point, and add the rest to draw and arc where \n3 is the length of the arc.

share|improve this question
3  
The pgfmanual (texdoc pgfmanual) is a good place to start. Syntax for path specifications for let and Syntax for mathematical expressions for atan2. Spoiler: atan2{x}{y} computes atan(y/x), that is the argument of the point (x,y). –  T. Verron Jun 21 '13 at 13:28
    
@T.Verron there is a small section on let in the manual on the bottom of page 53 to page 54. It doesn't have much substances though. –  dustin Jun 21 '13 at 13:37
1  
In my version of the manual, let is described in detail in section 14.15 (part III). That's not longer than 2 pages, but it is more than enough for most uses of that command. atan2 is described in section 63.2.3 (part VI). –  T. Verron Jun 21 '13 at 13:40
add comment

1 Answer

up vote 8 down vote accepted

The let operation in a nutshell:

How does TikZ read stuff?

The path operations in TikZ are based on branching off to different mechanisms based on the characters on the stream as they are read. You will find an abundance of if next character is c do something if not do another thing... An example, after finding e

\pgfutil@ifnextchar l{\tikz@ellipse}{\tikz@@e@char}}%

it checks if it is ellipse depending on the next character is l otherwise checks if it is the plain edge or edge from parent by looking at dge or f follows.

\def\tikz@@e@char dge{%
  \pgfutil@ifnextchar f{\tikz@edgetoparent}{\tikz@edge@plain}}%

So if you write, roughly speaking,

\node (a) at (2,0) {};

TikZ starts reading first \node then branches off to the node creation and the finds a parenthesis which branches off to node name reading and then sees an a which makes TikZ to switch to searching for a parenthesis and coordinate parsing. So long story short every item on the path stream gradually collects the necessary info. So when you want to do extra operations such as math calculations and maybe inserting an additional path etc. Something needs to stop the stream reading, and TikZ has such mechanisms depending on how low-level you want to go. let operation is the highest one of such mechanisms and mainly designed for doing coordinate and math computations (you can't start a new path with it). Other lower ones are \pgfextra and pgfinterruppath environment.

OK, so what?

When TikZ encounters one of these at an arbitrary level, it stores the current path settings somewhere and starts parsing stuff inside these macros' or environments contents. Note that I am actually approximating the reality but you don't need such obscure distinctions now (I guess).

When TikZ encounters a let in the stream, it starts looking for particular macros in the stream separated by commas and finalized with in. Anything between let...in is scanned for whether it's a \p... (a point) or \n... (a number). If it is a p macro it's scanned for coordinate name or expression and for number otherwise. So if we have

\draw let \p1=(3,2),\n1={(3-2)} in (\x1,0) -- (0,\y1);

the first one is scanned for the coordinate(or node) with the name (3-2).

\def\tikz@cc@handle@line{%
  \pgfutil@ifnextchar\p{%
    \tikz@cc@handle@coor%
  }{%
    \pgfutil@ifnextchar\n{%
      \tikz@cc@handle@num%
    }{%
      \PackageError{tikz}{``\string\p'' or ``\string\n'' expected}{}%
    }%
  }%
}

Why or how is not important now. \p and \n accepts the next token whether it is a number like \p1 or a word \p{foo} then the token becomes the point name for later. Similarly, \n{foo} becomes the name of the number as if you have issued

\pgfmathsetmacro{\foo}{<math operations>}

One remaining important detail is that whenever a \p is found then since it is supposed to be a coordinate, TikZ also prepares two macro names \x<p name> and \y<p name> which holds the horizontal and vertical coordinate of the point \p<p name> so they can be used later too without you defining and extracting those coordinates.

Finally when an in is found TikZ stops doing this assignment mechanism and gets back to reading the original path. But this time whenever it finds one of those \p,\n,\x or \y it replaces them with the statements given between let...in.

A naming problem should be emphasized. This operation temporarily changes the meaning of these macros. Here is a simple example.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}

\def\xf{1} %<--- xf is 1 here
\node[draw] (a) at (0,0) {Here!};

\draw let \p{f}=(10 pt,0) in (a.south east) -| ++(\x{f},\xf ); %<-- \xf IS NOT \x{f} 
                                                               % so being treated as 1cm.
\draw[ultra thick ,red] (a.north east) --++(10 pt,0);

\draw let \p{f}=(10 pt,0) in (a.south west) -| ++(-\x{f},\x f ); %<-- \x f IS \x{f}

\end{tikzpicture}
\end{document}

enter image description here

Difference between atan and atan2

This is kind of well-known among programmers since atan is a multivalued function in [0,2π]. Example, atan(1) can be 45 (+number over +number) as well as 225 (-number over -number) so atan2 takes the signs of the fraction into account. atan is only defined on [0,π] (or [-π/2,π/2] or any other combo.

share|improve this answer
add comment

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.