[Tex/LaTex] Circles inside a rectangle

circlestikz-pgf

I need to create a figure like this:

Enter image description here

And my poor code is the following.

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw[rounded corners=15pt]
(10,0) rectangle ++(15,3);
\begin{tikzpicture}
\draw (5,1) circle (1cm);
\end{tikzpicture}
\end{tikzpicture}
\end{document}

But this code does not work since the circle will be outside the rectangle, which I don't want.

So my question is:

Why does this code not work? In other programmes when you put a loop inside a loop it works as expected, but here putting a tikzpicture inside a tikzpicture does not work as I'd expect. That is, how do nested tikzpictures work in TikZ in general?

Best Answer

It is perfectly possible to nest loops. However, nesting tikzpicture environments is known to be hazardous. Although it sometimes works, it should be avoided. Basically, it is expected to break.

What I would do is to draw the circles first and the fit the outer box around them. For example:

\documentclass[border=5pt,tikz,multi]{standalone}
\usetikzlibrary{fit,positioning}
\begin{document}
\begin{tikzpicture}[ultra thick]
  \coordinate (c0) at (0,0);
  \foreach \i [count=\j, evaluate=\j as \k using \j-1, evaluate=\j as \m using { int(mod(\j,5))==0 ? "" : "draw" }, evaluate=\j as \n using { \j>10 ? "20mm" : "10mm" }] in {1,...,11}
  \node (c\j) [right=7.5pt of c\k |- c0, circle, anchor=north west, minimum size=\n, \m] {};
  \node [fit=(c1) (c11), draw, rounded corners=15pt, inner xsep=5mm, minimum height=30mm] {};
\end{tikzpicture}
\end{document}

radio

If you prefer, you can simply draw the circles one-by-one and then draw the box in the same way. Just name the leftmost and rightmost so that you can say fit=(<name 1>) (<name 2>) and all should be well.

In this particular case, you could, if you really wanted, nest the tikzpictures. However, (5,1) is at x=5cm, well to the left of the leftmost border of the box which is at x=10cm. So to put it in the box, you'd need to put it in the box.

You also need to put the tikzpicture inside a node. For example:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{fit,positioning}
\begin{document}
\begin{tikzpicture}
  \draw [rounded corners=15pt] (10,0) rectangle ++(15,3);
  \node at (12.5,1.5) {% BREAKAGE EXPECTED !!
    \begin{tikzpicture}% DON"T TRY THIS AT HOME !!
      \draw circle (1cm);
    \end{tikzpicture}
  };
\end{tikzpicture}
\end{document}

circle in box

But there is not much point in courting disaster when it would be much easier to write

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{fit,positioning}
\begin{document}
\begin{tikzpicture}
  \draw [rounded corners=15pt] (10,0) rectangle ++(15,3);
  \draw (12.5,1.5) circle (1cm);
\end{tikzpicture}
\end{document}

which produces the same result without the attendant risks and much more easily.