# How to draw a circle with radius from variable?

I want to draw this simple picture:

The shapes of points (blue circles) are not important, they are just for example.

The point is that I want to have smth like variable R inside {tikzpicture} drawing the picture. It will give an opportunity to generate automatically different pictures. I've found some examples of using variables, but they are not, what I want.

A pseudocode of what I expect:

double R = 10;
coordinate (A) at (0, 0);
coordinate (B) at (-R, 0);
coordinate (C) at (-R, R);
draw (A) circle (R);
draw (B) -- (C);
fill[blue] (A) circle (1pt);
fill[blue] (B) circle (1pt);
fill[blue] (C) circle (1pt);


How can I do that in tex?

• you could use tkz-euclide Dec 3 '20 at 14:29
• have a look at the answer below Dec 3 '20 at 15:35

You can use your syntax almost literally except that you have to add backslashes. I also use circle[radius=R] instead of circle (R), but the latter would work, too, and use a loop for the blue circles.

\documentclass[tikz,border=3mm]{standalone}
\begin{document}
\begin{tikzpicture}[declare function={R = 4;}]
\coordinate (A) at (0, 0);
\coordinate (B) at (-R, 0);
\coordinate (C) at (-R, R);
\draw (B) -- (C);
\foreach \X in {A,B,C}
\end{tikzpicture}
\end{document}


• wow, this looks cool! declare function={R = 4;} - what does it do? Why can we use R now like a number?) Dec 3 '20 at 15:48
• @Allok Because TikZ parses these expressions (coordinates, radii, etc), so you can feed them with functions. Such functions can be just constants, as in this case, or more complicated beasts. \draw (1+R,0) -- ({R*sin(30)},2+1); also works because the components get parsed.
– user229669
Dec 3 '20 at 15:53

like this

\documentclass[tikz,border=10pt]{standalone}
\usepackage{tikz}

\begin{document}
\def\R{3cm}
\begin{tikzpicture}
\coordinate (A) at (0, 0);
\coordinate (B) at ({-\R}, 0);
\coordinate (C) at (-\R, \R);
\draw (A) circle ({\R});
\draw (B) -- (C);
\fill[blue] (A) circle (1pt);
\fill[blue] (B) circle (1pt);
\fill[blue] (C) circle (1pt);
\end{tikzpicture}

\end{document}


• that's very close, thanks! but in that case R - smth like global variable, is there a way to make it local, only inside {tikzpicture}? Dec 3 '20 at 14:55

a rough approximation with tkz-euclide

\documentclass{article}
\usepackage{tkz-euclide}
\begin{document}

\begin{tikzpicture}[]
%arbitrary points
\tkzDefPoint(0,3){A}
\tkzDefPoint(0,-3){B}
%tangent to circle
%mid point of tangent line
\tkzDefMidPoint(B,A)\tkzGetPoint{P}