[Tex/LaTex] Draw a rectangle and then a circle with TikZ

tikz-pgf

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:

enter image description here

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.

Best Answer

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}

3 ways

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}