5

I want to draw two arcs in TikZ. Right now I’m using \path let to calculate the radius, but I’d like to store this value and reuse it later, so I don’t have to write the starting points every time.

In particular, I want to calculate the distance 𝐴𝑆1 once, save it, and then use it as the radius when defining other points on the arc.

I guess I should use \pgfmathsetmacro for this, but I’m not sure how.

What is the best way to save a value (for example, the distance between two points) and reuse it in multiple \draw commands?

I would like use for example \coordinate (K1) at (140:\n3).

enter image description here

\documentclass[11pt,a4paper]{article} \usepackage[utf8]{inputenc} \usepackage[czech]{babel} \usepackage[T1]{fontenc} \usepackage{amsmath,amsfonts,amssymb,mathrsfs} \usepackage[cmyk]{xcolor} \usepackage{pgfplots} %grafy s použitím \begin{axis} ... \end{axis} \pgfplotsset{compat=newest} % 1.15 pro geogebru, potřeba pro vykrelsení grafů \usepackage[left=2cm,right=2cm,top=2cm,bottom=2cm]{geometry} \usetikzlibrary{babel,arrows,intersections,patterns,calc,angles,through} \pagestyle{empty} % % % \begin{document} % %%%DEFINE COLOR \definecolor{modra}{cmyk}{1,0,0,0} \definecolor{cerna}{cmyk}{0,0,0,1} \definecolor{bila}{cmyk}{0,0,0,0} % \begin{tikzpicture}[scale=1] \def\delka{2.1} \def\uhel{72} \coordinate (A) at (0,0); \coordinate (B) at (\delka,0); \coordinate (S) at ($(A)!1/2!(B)$); % \draw[-] (A) -- (B); \path[name path=rr] (A) -- (\uhel-90:\delka); \draw[dash pattern=on 6pt off 2pt on 1pt off 2pt,name path=osa,cerna!80] ($(S)!1.25cm!270:(A)$) -- ($(S)!1.25cm!90:(A)$) coordinate[very near end] (o); \path[name intersections={of=rr and osa,by=S1}]; \draw (S1) node {$+$}; % %%%arcs \draw[thick,shift={(S1)}] let \p1 = (A), \p2 = (S1), \n3 = {veclen(\x1-\x2,\y1-\y2)} in (180-15:\n3) arc (180-15:360+15:\n3); \draw[thick,shift={(S1)}] (140:\n3) arc (140:400:\n3); % %%%I use \path let \p2 = (S1) in coordinate (S2) at (\x2, {- \y2}); \draw (S2) node {$+$}; \draw[thick,shift={(S2)}] let \p4 = (A), \p5 = (S2), \n6 = {veclen(\x4-\x5,\y4-\y5)} in (180+15:\n6) arc (180+15:-15:\n6); \draw[fill=blue!25] (A) circle (2pt); \draw[fill=blue!25] (B) circle (2pt); % % \end{tikzpicture} % \end{document} 
7
  • 1
    There is quite some trouble with your code, like missing \begin{document} and \end{document}. Can you please EDIT your question and describe the construction you want to do like you'd write down for students? I.e. in a solution-free formulation, what is given, what is to find out etc.? There may be better ways to do it. E.g. IF the task is to intersect two circles around S1 and S2 to obtain AB, that's quite simple to do. Thank you Commented Sep 16 at 6:56
  • 2
    @MS-SPO Sorry, I didn’t notice! Edited it. :) Commented Sep 16 at 6:58
  • 1
    Use the calc library so you can write \pgfmathsetmacro{\radius}{veclen(\x1-\x2,\y1-\y2)}. Commented Sep 16 at 7:08
  • 3
    Once you have a solution, consider posting it by answering your own question. This way future readers can benefit from the problem-solution (question -answer) structure of TEX.SE . Thank you Commented Sep 16 at 7:22
  • 1
    You may have a look at tex.stackexchange.com/a/103752/322482. Commented Sep 16 at 7:53

1 Answer 1

6

You can use \pgfextra to get the value in let...in scope.

This could be called "smuggle":

\documentclass[tikz,border=5pt]{standalone} \usetikzlibrary{calc} \begin{document} \begin{tikzpicture} \coordinate (A) at (0,0); \coordinate (B) at (2,3); \draw[thick] let \p1 = (A), \p2 = (B), \n3 = {veclen(\x1-\x2,\y1-\y2)} in \pgfextra{\xdef\radius{\n3}} (180-15:\n3) arc (180-15:360+15:\n3); % now you can use \radius outside the \path scope \node at (6,-2) {Radius = \radius}; \end{tikzpicture} \end{document} 

result

1
  • Thank you, it works. :) Commented Sep 16 at 10:37

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.