Regular Polygon 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 regular polygon node shape, change its size, its color and access to all its anchors.

Regular polygon shape in TikZ

Regular polygon shape is a part of the shapes.geometric TikZ library, so to work with this shape, we need to declare the library. We need to add the following piece of code after the TikZ package declaration:

 \usetikzlibrary{shapes.geometric}

A polygon shape can be drawn by using the option regular polygon to the \node command. Using this command, we can draw any kind of regular polygon by using a parameter for its number of sides. The following code creates a regular polygon node named (p) at coordinate (0,0).

 \documentclass[border=0.2cm]{standalone}

\usepackage{tikz}
\usetikzlibrary{shapes.geometric}

\begin{document}

\begin{tikzpicture}
	\node[regular polygon] (p) at (0,0) {};
\end{tikzpicture}

\end{document}


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

 \documentclass[border=0.2cm]{standalone}

\usepackage{tikz}
\usetikzlibrary{shapes.geometric}

\begin{document}

\begin{tikzpicture}
	\node[regular polygon,draw] (p) at (0,0) {};
\end{tikzpicture}

\end{document}


Draw polygon TikZ
  • By default, regular polygon command creates a polygon with five sides, which is a pentagon. We can change the number of sides by adding regular polygon sides command with a parameter. Check the following example which draws a polygon with eight sides, an octagon.
 \documentclass[border=0.2cm]{standalone}

\usepackage{tikz}
\usetikzlibrary{shapes.geometric}

\begin{document}

\begin{tikzpicture}
	\node[regular polygon,
	draw,
	regular polygon sides = 8] (p) at (0,0) {};
\end{tikzpicture}

\end{document}


Draw octagon TikZ

Add text to the regular polygon node

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

 \documentclass[border=0.2cm]{standalone}

\usepackage{tikz}
\usetikzlibrary{shapes.geometric}

\begin{document}

\begin{tikzpicture}
	\node[regular polygon,
	draw,
	regular polygon sides = 8] (p) at (0,0) {Polygon};
\end{tikzpicture}

\end{document}


To fit the text inside the shape, this code yields to a polygon, in this case an octagon, with a size depends on its content:

Add text inside polygon shape TikZ

Change text color of the polygon node

In the previous section, we have added content to the regular polygon 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}

\usepackage{tikz}
\usetikzlibrary{shapes.geometric}

\begin{document}

\begin{tikzpicture}
	\node[regular polygon,
	draw,
	regular polygon sides = 8,
	text = blue] (p) at (0,0) {Polygon};
\end{tikzpicture}

\end{document}


Change text color polygon shape tikz

Add filling color to the Polygon shape

The regular polygon node can be filled with any color using the option fill = <color>. Here is an illustrative example of a light lime filling with our blue text:

 \documentclass[border=0.2cm]{standalone}

\usepackage{tikz}
\usetikzlibrary{shapes.geometric}

\begin{document}

\begin{tikzpicture}
	\node[regular polygon,
	draw,
	regular polygon sides = 8,
	text = blue,
	fill = lime!30] (p) at (0,0) {Polygon};
\end{tikzpicture}

\end{document}


Fill color polygon shape TikZ

Change the border line color

The regular polygon 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 polygon filled with light lime, has blue text and teal borders:

 \documentclass[border=0.2cm]{standalone}

\usepackage{tikz}
\usetikzlibrary{shapes.geometric}

\begin{document}

\begin{tikzpicture}
	\node[regular polygon,
	draw = teal,
	regular polygon sides = 8,
	text = blue,
	fill = lime!30] (p) at (0,0) {Polygon};
\end{tikzpicture}

\end{document}


Change border color Polygon in TikZ

Change the size of a regular polygon node

We mentioned above that the size of a regular polygon depends on its content. This is always true and we can set only the minimum size of a polygon using the option minimum size =<value>. Below, we set the minimum size option to 3 cm.

 \documentclass[border=0.2cm]{standalone}

\usepackage{tikz}
\usetikzlibrary{shapes.geometric}

\begin{document}

\begin{tikzpicture}
	\node[regular polygon,
	draw = teal,
	regular polygon sides = 8,
	text = blue,
	fill = lime!30,
	minimum size = 3cm] (p) at (0,0) {Polygon};
\end{tikzpicture}

\end{document}


Anchors of a regular polygon node

The advantage of using a node regular polygon 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 of polygon TikZ

(p.south) represents the coordinates of the point located at the south of the regular polygon node where p is the node name. We can also use angles where (p.90) is the coordinates of the point located at the north of the regular polygon node.

Adding anchor = west will place the node point (p.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 in TikZ polygon
  • We reached the end of this post, If you have any questions or suggestionsleave me a comment or reach me via email at admin@latexdraw.com, I will be happy to hear from you!