# let operation in tikz

Ηow can I separate options within brackets [...] so that is affected only the current command;

For example, I want dashed lines only here (0,0) |- (A) [dashed] .

Could someone give a detailed explanation of the use of let operation. The examples in the pgf manual at 150p. I think is not enough to understand the use of the let operation in-depth, at least for me.

\documentclass[11pt]{book}
\usepackage{tikz}
\usetikzlibrary{decorations.markings}
\usetikzlibrary{calc,intersections,through,backgrounds}

\begin{document}

\begin{center}
\begin{tikzpicture}[scale=1.25,cap=round,>=latex]
\draw[step=.5cm,gray,very thin] (-3.0cm,-3.0cm) grid (3.0cm,3.0cm);
\draw[->] (-2.5cm,0cm) -- (2.5cm,0cm);
\draw[->] (0cm,-2.5cm) -- (0cm,2.5cm);

\coordinate (center) at (0.cm,0.cm) ;
\coordinate (A) at (1.5cm,1.cm);
\filldraw [black] (center) circle (0.04cm) node [left] {$Ο$};

\draw let
\p1 = ($(A)-(center)$),
\n1 = {veclen(\x1,\y1)},
\n2 = {atan2(\x1,\y1)}
in
(center) circle (\n1)
(A) circle  (0.1cm)[fill color = black]
(0,0) |- (A) [dashed]
(center)  -- (A);

\end{tikzpicture}
\end{center}


-
The let command is always used within a \path (or \draw or \fill) command, and you can't change the drawing options within a \path. In your example, there's no reason to have the lines from (A) circle ... to (center) -- (A); in the same \draw command as the let keyword (you're not using \p1, \n1, or \n2 in any of them), so you can just move them to their own \draw commands. –  Jake Mar 19 '13 at 16:50
I'm trying to learn to programing in tikz. I understand what you mean, but the question is can I have separate options on each individual command. From what you say probably not –  karathan Mar 19 '13 at 17:03
You can have separate options only on separate \path commands, but since a let keyword is always part of a \path you can't use separate options for different parts of a let keyword. –  Jake Mar 19 '13 at 17:04
So to do this the only way as saying the manual is ... (from manual: ...Note that the effect of a let operation is local to the body of the let operation. If you wish to access a computed coordinate outside the body, you must use a coordinate path operation.... page 152)...So there is no other way to have separate option, sorry for the insistence just trying to clear up ... –  karathan Mar 19 '13 at 17:20
Using a coordinate in the let operation is probably the clearest way to do it. However, you could also use an edge operation if you absolutely want to avoid this. The (center) -- (A) could be replaced by (center) edge [red, solid] (A), for example, but it gets more unwieldy for the other lines. The orthogonal line would have to be expressed using (0,0) edge [to path=(\tikztostart) |- (\tikztotarget), ultra thick, solid, blue] (A), for instance (though you could simplify this by defining custom styles). –  Jake Mar 19 '13 at 17:27

The let operation can only be used within a \path (or \draw or \fill...) command. Drawing options like line width, dashed, or the color, can not be changed in a single path, therefore they cannot be changed within a let command.

In your concrete example, you don't need to have the two circles and the straight lines in the same path as the let command: Only the large circle actually uses the result of the let calculation. In this case, the easiest thing would be to just move the other parts into their own \paths.

In general, however, there are basically two different approaches for using the results from the let operation for paths with different drawing styles:

• You can name coordinates using the coordinate (<name>) at (<coordinate>) keyword. These can then be used in other \path commands.
• You can use the edge keyword, which tells TikZ to not draw the path segment immediately, but to execute it as a separate path after the main path is finished, which makes it possible to use different drawing styles. For straight lines, its as easy as replacing (A) -- (B) with (A) edge [red] (B). For more complicated constructs, you'll have to adjust the value of the to path key: An orthogonal path (A) -| (B) could be expressed as (A) edge [to path=(\tikztostart) |- (\tikztotarget), red] (B).
-

Jake's answer is perfect. The next answer is for the case that you need to do more complex calculation not easily done by simple expressions

I think it's not very fine to do calculations and drawing operations at the same time. And if you use several calculations and several drawings, you complicate the code.

A) Path and drawing

The path begins with \draw (exactly \path[draw]) and ends with ;. Drawing one path means you take one pencil with one color . You cannot change the pencil inside the path.

So if you want to draw two circles with different colors you need to use two paths, example :

\documentclass[11pt]{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw    [blue] (0,0) circle (1 cm);
\draw    [red]  (2,0) circle (1 cm);
\end{tikzpicture}
\end{document}


But if you write

\draw [red]   (0,0) circle (1 cm) [blue,fill=green!20]
(2,0) circle (2 cm);


then the circles are in blue and the disks are green. Blue because it's the last option the color, and the fill color is applied to the path.

B) Path and calculations

Now if you need to do some calculations to draw the circles

In the pgfmanual there is this example : I used longer names, but I have to use curly braces \p{AB} insted of \p1. The code is more readable. Remark : in the next example you can avoid the let operation with the through library.

    \documentclass[11pt]{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\coordinate [label=left:$A$]  (A) at (0,0);
\coordinate [label=right:$B$] (B) at (1.25,0.25);
\draw let \p{AB}        = ($(B) - (A)$),
in
\end{tikzpicture}
\end{document}


The two circles are in black now if I want the first circle in red and the secon in blue and if I want to calculate once the AB, I need to do this

    \documentclass[11pt]{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\coordinate [label=left:$A$]  (A) at (0,0);
\coordinate [label=right:$B$] (B) at (1.25,0.25);
\draw let
\p{AB} = ($(B) - (A)$),
in
[red]  (A)  circle (\n{radius}) ;
\draw  [blue] (B)  circle (\sameradius) ;
\end{tikzpicture}
\end{document}


Explanation : \p{AB} is local to the path operation and you can use the result outside the first path. You need to pass the tikz register \n{radius} to tex. But you need to use a global macro to jump outside the group. It's why I used \xdef !

In your case (if myradius is the radius of the big circle) you wrote

\draw   (center) circle (\myradius pt)
(A) circle  (0.1cm)
(0,0) |- (A) [dashed]
(center)  -- (A);


Here you have only one path. (the beginning is \drawequivalent to \path[draw] and the end is ;).

All the path is drawn with the "dashed" option. If you want to change the options then you need three paths

\draw   (center) circle (\myradius pt);
\fill   (A) circle  (0.1cm) ; % fill the little circle
\draw [dashed]  (0,0) |- (A) % dash the lines
(center)  -- (A);


D) In the Old Days, before the let operation

Perhaps it's interesting to know what it was necessary to do before the let' operation. This operation now is used to avoid the next TeX code. This kind of code sometimes is useful when you want to create complex operations and if you want a code more flexible and faster

First we need to get the coordinates of the vector $(A)-(center)$. We need to use the macro \pgfpointdiff. The result is inside \pgf@x and \pgf@y. We need to work between \makeatletter and \makeatother because we need to use @ like a simple letter.

Then we calculate the length (A)--(center) with \pgfmathveclen. The result is in \pgfmathresult. (unity is pt). This is raw code and it's preferable to take some precautions when we use \pgf@x} , \pgf@y and \pgfmathresult.

\makeatletter
\pgfpointdiff{\pgfpointanchor{A}{center}}%
{\pgfpointanchor{center}{center}}%
\pgfmathveclen{\pgf@x}{\pgf@y}
\makeatother


This is raw code and it's preferable to take some precautions when we use \pgf@x} , \pgf@y and \pgfmathresult.

Now we can stock the mathresult inside a macro \myradius and it's more easy to use and to read the code

\makeatletter
\pgfpointdiff{\pgfpointanchor{A}{center}}%
{\pgfpointanchor{center}{center}}%
\makeatother


But It's possible to do something better because now we have pgflastxy and we can avoid makeatletter

Firstly we create \xdim and ydim to stock the dimensions and we use \pgfgetlastxy to get the dimensions of the vector.

\newdimen\xdim\newdimen\ydim
\pgfpointdiff{\pgfpointanchor{A}{center}}%
{\pgfpointanchor{center}{center}}%
\pgfgetlastxy{\xdim}{\ydim}


The complete code

\documentclass[11pt]{article}
\usepackage{tikz}

\begin{document}

\begin{center}
\begin{tikzpicture}[scale=1.25,cap=round,>=latex]
\draw[step=.5cm,gray,very thin] (-3.0cm,-3.0cm) grid (3.0cm,3.0cm);
\draw[->] (-2.5cm,0cm) -- (2.5cm,0cm);
\draw[->] (0cm,-2.5cm) -- (0cm,2.5cm);

\coordinate (center) at (0.cm,0.cm) ;
\coordinate (A) at (1.5cm,1.cm);
\filldraw [black] (center) circle (0.04cm) node [left] {$Ο$};
% calculation (the let part)
\newdimen\xdim\newdimen\ydim
\pgfpointdiff{\pgfpointanchor{A}{center}}%
{\pgfpointanchor{center}{center}}%
\pgfgetlastxy{\xdim}{\ydim}

\draw   (center) circle (\myradius pt);
\fill   (A) circle  (0.1cm) ;
\draw [dashed]  (0,0) |- (A)
(center)  -- (A);
\end{tikzpicture}
\end{center}
\end{document}


\documentclass[11pt]{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}

\begin{center}
\begin{tikzpicture}[scale=1.25,cap=round,>=latex]
\draw[step=.5cm,gray,very thin] (-3.0cm,-3.0cm) grid (3.0cm,3.0cm);
\draw[->] (-2.5cm,0cm) -- (2.5cm,0cm);
\draw[->] (0cm,-2.5cm) -- (0cm,2.5cm);

\coordinate (center) at (0.cm,0.cm) ;
\coordinate (A) at (1.5cm,1.cm);
\filldraw [black] (center) circle (0.04cm) node [left] {$Ο$};
\draw
let
\p{Acenter}        = ($(A) - (center)$),
`