# How to draw 3 equal circles inside a circle in tikz or other way?

I want to draw equal circles inside a circle using Tikz-pgf or other way as in the below image (please forget the numbers inside the small circles): To draw a circle, we can use:

\begin{tikzpicture}
\end{tikzpicture} But how to create an equal circle inside this unit circle, as in the first picture ?

One way is to find the center of each small circle and its radius. But that is very technical.

Is their any drawing tool that can easily do it? For example, there are drawing tools in Wolfram, but they need a subscription.

Remark: To give some idea about the first image above, it is a number system called the p-adic number system, and the image shows the distribution of 3-adic numbers. It is an interesting number system in mathematics.

• @SebGlav, yes that is from circle packing in a circle, but how would type squre inputs like (1+2/sqrt(3)) in Tikz ? Sep 4 at 13:00
• Please give comments if you are downvoting. Sep 4 at 13:01
• Admittedly it is more of a geometry question than a LaTeX queston, but at least it is interesting. Sep 4 at 13:20
• @learner, you'll need a node for displaying text. You can place it in absolute coordinates, with predefined \coordinate, or relative using positioning. But it's all written in the manuel, i.e. the ... tutorials there. // You may alos look there, how the syntax for polar coordinates look like. Will make your task easier. Sep 4 at 14:35

Here you go! \documentclass[tikz,border=3.14mm]{standalone}
\begin{document}
\newcommand{\RA}{2}
\pgfmathsetmacro{\RB}{\RA*(1+2/sqrt(3)}
\begin{tikzpicture}
\foreach \ang in {90,210,330}
\end{tikzpicture}
\end{document}


Of course, if you want to draw the outer circle first and define the inner circles from it, you may change a bit your code:

\documentclass[tikz,border=3.14mm]{standalone}
\begin{document}
\newcommand{\RA}{2}
\pgfmathsetmacro{\RB}{\RA/(1+2/sqrt(3)}
\begin{tikzpicture}
\foreach \ang in {90,210,330}
\end{tikzpicture}
\end{document}


## BONUS: SECOND ITERATION

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{calc}
\begin{document}
\newcommand{\RA}{2}
\pgfmathsetmacro{\RB}{\RA/(1+2/sqrt(3)}
\begin{tikzpicture}
\foreach \ang [count = \i from 1] in {90,210,330}
\draw (\ang:\RA-\RB) coordinate (center-\i) circle[radius=\RB cm];
\pgfmathsetmacro{\RC}{\RB/(1+2/sqrt(3)}
\foreach \i in {1,2,3}
\foreach \ang in {90,210,330}
\draw ($(center-\i)+(\ang:\RB-\RC)$) circle[radius=\RC cm];
\end{tikzpicture}
\end{document} It was made very roughly if you just need two iterations. If you need more, it will be needed to make the code more legible and useful.

• Great! Now can we use the same code to draw 3 equal circles in each of the 3 small circes you have created ? That is I want divide each 3 small circles into same way Sep 4 at 13:10
• Feel free to do it recursively. Sep 4 at 13:12
• \RA is the radius of the first circle, here 2 cm. Then \pgfmathsetmacro{\RB}{\RA/(1+2/sqrt(3)} computes the radius of the inner circles, which is named \RB. Sep 4 at 13:25
• Just replace \draw by \draw[fill=purple] or even \filldraw[purple]. And please avoid asking multiple questions in the comments in the future. Sep 4 at 14:16
• tikz.dev to begin with. You can find multiple sources out there on the Internet. Sep 4 at 14:18

The mathematical problem was solved by SebGlav already in the comments and they also helped you a big deal with their answer.

I want to provide a recursive solution using circular nodes and labels (i.e. more nodes).

It uses the ext.pgfkeys-plus library of my tikz-ext package for the /utils/TeX/ifnum={<test>}{<true>}{<false>} key to end the recursion when level zero has been reached. (And if you can't use that the definition of it is in the code below.)

The idea behind this is to anchor the labels (i.e. the circles inside another circle) at the angles 90, 210 and 330 and at their parent node. That way, we only need to evalute the radii and PGF/TikZ figures out the trigonometry of it.

The second picture moves the recursion check inside cic/label so we can use level 0 to do something totally different. Furthermore another argument is introduced (#3 in cic/@ which replaced cic which is now only a “starter”) which gets carried down and appears as #2 in cic/node'.

It just contains a list of directions in the form of u (90°), l (210°) and r (330°).

Since this is just a ternary number, we could also replace this with some calculations using the level #1 and the digits 0, 1 and 2 to evaluate the values on-the-fly but here I've opted to hardcode these.

## Code

\documentclass[tikz]{standalone}
%\usepgfkeyslibrary{ext.pgfkeys-plus}
\makeatletter
\pgfkeys{/utils/TeX/ifnum/.code n args={3}{%
\ifnum#1\relax\expandafter\pgfutil@firstoftwo\else\expandafter\pgfutil@secondoftwo\fi
{\pgfkeysalso{#2}}{\pgfkeysalso{#3}}}}
\makeatother
\begin{document}
\tikz[
cic/node/.style={
shape=circle, inner sep=+0pt, outer sep=+0pt, minimum size={#1}},
cic/.style n args={2}{
cic/node={#2}, cic/level #1/.try,
/utils/TeX/ifnum={#1=0}{}{
/utils/exec=%
\pgfmathtruncatemacro\level{#1-1},
cic/label/.style n args={3}{
label={[anchor=#3, label position=#3, cic={#1}{#2}]}},
cic/level setter/.style args={#1=#2}{cic/level #1/.style={fill=#2}},
cic/level setter/.list={3=purple, 2=red!50, 1=green!50!black, 0=blue!50},
] \node[cic={3}{2cm}]{};

\tikz[
cic/node/.style={
shape=circle, inner sep=+0pt, outer sep=+0pt, minimum size={#1}},
cic/node'/.style 2 args={% this node contents is just a debug fall back
cic/node={#1}, path only, node contents={#2}, cic/node #2/.try},
cic/.style 2 args={cic/@={#1}{#2}{cic-}},
cic/@/.style n args={3}{
cic/node={#2}, cic/level #1/.try,
cic/label/.style n args={4}{
/utils/TeX/ifnum={#1=0}{
label={[anchor=#3, label position=#3, cic/node'={#2}{#4}]},
}{
label={[anchor=#3, label position=#3, cic/@={#1}{#2}{#4}]}}},
cic/level setter/.style args={#1=#2}{cic/level #1/.style={fill=#2}},
cic/level setter/.list={2=purple, 1=red!50},
cic/node setter/.style args={#1=#2}{
cic/node cic-#1/.style={node contents={#2}}},
cic/node setter/.list={uu=0, ru=1, lu=2, ur=3, rr=4, lr=5, ul=6, rl=7, ll=8}
] \footnotesize\node[cic={2}{2cm}]{};
\end{document}


## Output  • For a more general approach, see my answer on something that is related to fractals and Sierpinski carpets. (But you will need to do the setup which is a bit verbose.) Sep 4 at 17:12
• Well thanks, can you please suggest how to \node command for text display using polar coordinates (r, theta) ? Because I want to display the numbers in the 1st image of my question Sep 4 at 17:16
• I mean what is the syntax of a node for polar coordinates. I didn't find exactly in the manual Sep 4 at 17:17
• @learner I don't know what you mean. Adding the numbers is not so complicated (see my update). In this case, you just need to relate the values to the positions. Or figure out a formula depending on the position in diagram (see my remarks about ternary numbers). Sep 4 at 17:30
• @Qrrbrbirlbel Wonderful solution, as usual. You definitely master TikZ and your approach is way deeper than mine (I didn't even have the time to look deeper myself, but I wouldn't find anything like you did). Bravo! Sep 4 at 20:49