# Draw a rectangle and then a circle with TikZ

I am trying to learn TikZ from the Offical TikZ Documentation.

And I am stuck at few places. I want to draw a box and then a circle in one line.

In the first method, I can give the coordinates, but cannot set the text in the node properly.

In the second method, I could not find a way to give the coordinates simiiar to the first method.

In the third approach I can give the coordinates, but cannot set the width.

1st approach

\begin{tikzpicture}
\draw[top color=blue,bottom color=blue]  (0,0) rectangle +(3,2) node {A} (4,0) circle (4mm) node {B};
\end{tikzpicture}


2nd approach

\begin{tikzpicture}[node distance=20mm,
boxx/.style={
rectangle,
minimum size=8mm,
very thick,draw=red!50!red!50,
top color=blue,
bottom color=red!50!black!20,
font=\ttfamily
},
circlee/.style={circle,
minimum size=8mm,
very thick,draw=red!50!red!50,
top color=blue,
bottom color=red!50!black!20,
font=\ttfamily}]
\node (A) [boxx] {A};
\node (B) [circlee, right=of A] {B};
\end{tikzpicture}


AND

\begin{tikzpicture}
[node distance = 20mm,
every rectangle node/.style={rectangle,
minimum size=8mm,
very thick,draw=red!50!red!50,
top color=blue,
bottom color=red!50!black!20,
font=\ttfamily},
every circle node/.style={circle,
minimum size=8mm,
very thick,draw=red!50!red!50,
top color=blue,
bottom color=red!50!black!20,
font=\ttfamily}
]
\draw (0,0) node[rectangle] {A};
\draw (1,0) node[circle] {B};
\end{tikzpicture}


The output of the diagram is the following:

Can all the three methods be used to get a rectangle of size 3 cm length and 2 cm height and then a circle of radius 4 mm. Both of them are placed at a distance of 20 mm from each other. The rectangle A has a text "A" in the center and the circle B has the text "B" in the cetner.

• 20mm border to border? or 20mm what to what? – cfr May 5 '17 at 21:01
• node distance=20mm. I think its border to border? – infoclogged May 5 '17 at 21:02
• Could you make your post compilable? – cfr May 5 '17 at 21:04
• @cfr Yes, very much. What makes you think it would not? – infoclogged May 6 '17 at 10:07
• No document class. No document environment. No packages and no needed TikZ libraries. So it can't compile as is. – cfr May 6 '17 at 13:00

Do you mean you want something like this?

The first method requires manual hackery because the node drawn first is not the size of the rectangle. (And the calculation may be off, but you need something like this.)

\begin{tikzpicture}
\draw [draw=red, very thick, top color=blue, bottom color=red!50!black!20]  (0,0) rectangle +(3,2) node (A) [midway] {A} (A) +(37mm,0) circle (4mm) node {B};
\end{tikzpicture}


The second and third just require taking account of the fact that nodes have anchors and that placement is relative to those anchors, together will settings for minimum width and height.

\begin{tikzpicture}[
node distance=20mm,
boxx/.style={
rectangle,
minimum size=8mm,
minimum width=30mm,
minimum height=20mm,
very thick,
draw=red!50!red!50,
top color=blue,
bottom color=red!50!black!20,
font=\ttfamily,
anchor=east,
},
circlee/.style={circle,
minimum size=8mm,
very thick,draw=red!50!red!50,
top color=blue,
bottom color=red!50!black!20,
anchor=west,
font=\ttfamily}
]
\node (A) [boxx] {A};
\node (B) [circlee, right=of A] {B};
\end{tikzpicture}
\begin{tikzpicture}
[x = 20mm,
every rectangle node/.style={rectangle,
minimum size=8mm,
minimum width=30mm,
minimum height=20mm,
very thick,
draw=red!50!red!50,
top color=blue,
bottom color=red!50!black!20,
anchor=east,
font=\ttfamily},
every circle node/.style={circle,
minimum size=8mm,
very thick,draw=red!50!red!50,
top color=blue,
bottom color=red!50!black!20,
anchor=west,
font=\ttfamily}
]
\draw (0,0) node [rectangle] {A};
\draw (1,0) node [circle] {B};
\end{tikzpicture}


Complete code:

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}
\draw [draw=red, very thick, top color=blue, bottom color=red!50!black!20]  (0,0) rectangle +(3,2) node (A) [midway] {A} (A) +(37mm,0) circle (4mm) node {B};
\end{tikzpicture}
\begin{tikzpicture}[
node distance=20mm,
boxx/.style={
rectangle,
minimum size=8mm,
minimum width=30mm,
minimum height=20mm,
very thick,
draw=red!50!red!50,
top color=blue,
bottom color=red!50!black!20,
font=\ttfamily,
anchor=east,
},
circlee/.style={circle,
minimum size=8mm,
very thick,draw=red!50!red!50,
top color=blue,
bottom color=red!50!black!20,
anchor=west,
font=\ttfamily}
]
\node (A) [boxx] {A};
\node (B) [circlee, right=of A] {B};
\end{tikzpicture}
\begin{tikzpicture}
[x = 20mm,
every rectangle node/.style={rectangle,
minimum size=8mm,
minimum width=30mm,
minimum height=20mm,
very thick,
draw=red!50!red!50,
top color=blue,
bottom color=red!50!black!20,
anchor=east,
font=\ttfamily},
every circle node/.style={circle,
minimum size=8mm,
very thick,draw=red!50!red!50,
top color=blue,
bottom color=red!50!black!20,
anchor=west,
font=\ttfamily}
]
\draw (0,0) node [rectangle] {A};
\draw (1,0) node [circle] {B};
\end{tikzpicture}
\end{document}

• "_...the fact that nodes have anchors..." ? – Paul Gaborit May 5 '17 at 22:09
• @PaulGaborit Yeses, they do? Thank you :-) – cfr May 5 '17 at 22:42
• @cfr Thank your for your answer. I have few quick questions. 1. you have set minimum width and minimum height in the style template. Is there any way to give the width and height parameters directly to the nodes for example via overriding? 2. What is x = 20mm. The varialbe x hasnt been used anywhere. 3. What is the reference of anchor. West tends to place the circle node more right, but west should normally tend to pull towards left ( acc to geography ) ? 4. In the second approach, the anchor has got not effect. I removed it and it still works. Is it because of the "right of" keyword? – infoclogged May 6 '17 at 10:04
• @infoclogged (1) Yes. I just put it into the style because that's the only size needed for the example. Just add it when calling the style. Or put it after to override a default. (2) It sets the value of each horizontal unit to 20mm. So (1,0) means (20mm,0). 10mm is default. (3) It doesn't place the node west. It uses the node's .west anchor to place it. (4) Yes. – cfr May 6 '17 at 12:59
• @cfr Okay , this works \node (A) [boxx, minimum width=80mm, minimum height=40mm] {Second}; but this does not \draw (0,0) node [rectangle, minimum width=80mm, minimum height=40mm ] {Third}; Any idea ? I have changed the labels for better understanding. – infoclogged May 6 '17 at 14:30