How to draw Octagon in LaTeX – Tikz

In this tutorial, I will show you how easily we can draw an octagon in LaTeX using Tikz.

Draw a Simple octagon in Latex

Look at the below code:

\documentclass{standalone}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
    % Draw an octagon
    \draw[thick] (0:2) \foreach \x in {45,90,...,315} { -- (\x:2) } -- cycle;
\end{tikzpicture}

\end{document}

Output:

Latex octagon Tikz

This code involves mathematical logic.

Let me explain it so that you can understand what I did in that tricky code.

\draw[thick]by using this command I made the lines thick.

If you want to increase the thickness more, you can use ultra thick instead of thick. Or if you want to add a custom thickness you can use:  \draw[line width=2mm]and you can easily change the line width.

(0:2) This is the polar coordinate system notation –  (angle:radius)

  • 0: This is the angle, measured in degrees, from the positive x-axis.
  • 2: This is the radius, which determines the distance from the origin (center of the coordinate system).

So (0:2): Refers to a point that is 2 units away from the origin, at an angle of 0 degrees. We can also say, this point lies on the positive x-axis, 2 units to the right of the origin.

  • \foreach \x in {45,90,…,315}: Iterates over angles from 45 to 315 degrees and draws lines between these points.
  • cycle: It closes the shape by connecting the last point back to the first.

Rotate the octagon so that the flat surface stays at the top

This can be done using this code:

\documentclass{standalone}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
    % Draw a rotated octagon (top flat)
    \draw[ultra thick] (22.5:2) \foreach \x in {67.5,112.5,...,337.5} { -- (\x:2) } -- cycle;
\end{tikzpicture}

\end{document}

Output:

Top flat octagon latex

 

Draw octagon in LaTeX using angle concept

\documentclass{standalone}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
    \draw[thick] 
        (0:1) -- (45:1) -- (90:1) -- (135:1) -- (180:1) -- (225:1) -- (270:1) -- (315:1) -- cycle;
\end{tikzpicture}

\end{document}

It will also give you the same output as the first code.

Octagon in Latex using Tikz and shapes.geometric

Now I am going to use shapes.geometric package and this will make it a lot easier to draw octagon.

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

\begin{document}

\begin{tikzpicture}
    \node[draw, 
          regular polygon, 
          regular polygon sides=8, 
          minimum size=3cm] {};
\end{tikzpicture}

\end{document}

Output:

How to draw octagon in LaTeX - Tikz

How to write text inside octagon and also customize color

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

\begin{document}

\begin{tikzpicture}
    \node[draw=red, 
          regular polygon, 
          regular polygon sides=8, 
          minimum size=3cm,
          text = blue] {Hi there};
\end{tikzpicture}

\end{document}

Output:

add text inside polygon latex

You can see I have used draw=red to make the border of the octagon red.

Using text = blue, I have set the color of the text inside the octagon.

And in line number 12, inside these {} curly braces I have entered the text that I want to show inside the octagon.

If you wish to fill the octagon with a specific color you can add this parameter: fill = color inside the \node command.

Let’s see this example:

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

\begin{document}

\begin{tikzpicture}
    \node[draw=red, 
          regular polygon, 
          regular polygon sides=8, 
          minimum size=3cm,
          fill = lime,
          text = blue] {Hi there};
\end{tikzpicture}

\end{document}

Output:

Fill octagon with color in LaTeX

If you need more info or a specific task to do, then let me know in the comment section. I will provide the solution.

Leave a Reply

Your email address will not be published. Required fields are marked *