Draw a Rectangle in TikZ

  • This tutorial is one of post series about predefined TikZ shapes, or more precisely TikZ node shapes. In this post, we will learn how to create a TikZ rectangle node shape, change its size, its color and access to all its anchors.

Node shape rectangle in TikZ

A rectangle shape can be drawn by using the option rectangle to the \node command. Following line creates a rectangle node named (r) at coordinate (0,0).

\documentclass[border=0.2cm]{standalone}

% Required package
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}

	\node[rectangle] (r) at (0,0) {};

\end{tikzpicture}

\end{document}

Compiling this code displays nothing. The rectangle node will be drawn only if we provide draw option to the node command as follows:

\documentclass[border=0.2cm]{standalone}

% Required package
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}

	\node[rectangle,draw] (r) at (0,0) {};

\end{tikzpicture}

\end{document}
Rectangle shape in TikZ LaTeX
  • The rectangle node shape is always defined and no TikZ library is needed to be loaded (The same with circle node shape).

Add text to the rectangle node

The previous code creates a rectangle node with empty content. Text can be added inside the rectangle node by putting it between the curly braces {} as follows.

\documentclass[border=0.2cm]{standalone}

% Required package
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}

	\node[rectangle,draw] (r) at (0,0) {Rectangle};

\end{tikzpicture}

\end{document}

To fit the text inside the shape, this code yields to a rectangle with a size depends on its content:

Rectangle node shape in TikZ LaTeX

Change text color of the rectangle node

In the previous section, we have added content to the rectangle node and now we would like to change the text color. This can be achieved by providing the option text = <color> to the \node command:

\documentclass[border=0.2cm]{standalone}

% Required package
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}

\node[rectangle,
	draw,
	text = olive] (r) at (0,0) {Rectangle};

\end{tikzpicture}

\end{document}
Text color Rectangle node shape in TikZ LaTeX

Add filling color to a node

The rectangle node can be filled with any color using the option fill = <color>. Here is an illustrative example of a dark green filling with our olive text:

\documentclass[border=0.2cm]{standalone}

% Required package
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}

\node[rectangle,
	draw,
	text = olive,
	fill = green!30!black] (r) at (0,0) {Rectangle};

\end{tikzpicture}

\end{document}
Fill node shape with color TikZ

Change the border line color of a node

The rectangle border color can be modified (default is black) by providing the color name to the draw option draw = <color>. Here is an example of a rectangle filled with dark green, has olive text and light gray border:

\documentclass[border=0.2cm]{standalone}

% Required package
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}

\node[rectangle,
	draw = lightgray,
	text = olive,
	fill = green!30!black] (r) at (0,0) {Rectangle};

\end{tikzpicture}

\end{document}
Change border line color in TikZ LaTeX

Change the size of a rectangle node

We mentioned above that the size of a rectangle depends on its content. This is always true and we can set only the minimum size of a rectangle. We can arrange a minimum size for its width with minimum width option, and similarly, we can set its height with minimum height option. Below, we set the minimum width value to 2 cm and the minimum height value to 1 cm.

\documentclass[border=0.2cm]{standalone}

% Required package
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}

\node[rectangle,
	draw = lightgray,
	text = olive,
	fill = green!30!black,
	minimum width = 2cm, 
	minimum height = 1cm] (r) at (0,0) {Rectangle};

\end{tikzpicture}

\end{document}
Rectangle shape in TikZ

If we need both the width and the length sizes to be the same, we can use minimum size option.

Anchors of a rectangle node

The advantage of using a node rectangle is that it defines a set of anchors that we can use to get coordinates of the node borders or to position nodes with accuracy with respect to given coordinates. By default, node center is positioned at the provided coordinates ((0,0) for the previous examples).

Anchors rectangle TikZ LaTeX
Anchors angles rectangle TikZ LaTeX

(r.north) represents the coordinates of the point located at the north of the rectangle node where r is the node name. We can also use angles where (r.180) is the coordinates of the point located at the west of the rectangle node.

Adding anchor = west will place the node point (r.west) at the provided coordinate (0,0). Check this post: how to annotate an image in LaTeX (Positioning Nodes using Anchors section).

Outer separation

The above anchors can be moved outside the node border by adding outer sep = <value> to the node command. Here is an example of anchors moved outside the node border with 10pt.

Push anchors rectangle TikZ
  • We've reached the end of this tutorialIf you have any questions or remarks, leave me a comment below or reach me via e-mail at admin@latexdraw.com, I will be happy to hear from you!