I need to create a figure like this:
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 tikzpicture
s 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: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
tikzpicture
s. However,(5,1)
is atx=5cm
, well to the left of the leftmost border of the box which is atx=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:But there is not much point in courting disaster when it would be much easier to write
which produces the same result without the attendant risks and much more easily.