Draw Torus with Tikz in LaTeX – with Equation

In this LaTeX tutorial, I will show you how to plot torus in LaTeX using Tikz. We will also learn to write the torus equation in LaTeX.

I will cover these in this tutorial:

  • Parametic equation of a torus so that you can understand what I am doing clearly.
  • Drawing torus using Tikz and pgfplots.
  • Changing the color using colormap.
  • Adding annotations on the Plotted torus.

Before plotting a Torus, we should know the math behind it. But as this is a LaTeX tutorial, I will not be going deep in the math.

If you wish you can check: Draw Double Torus using Tikz and pgfplots in LaTeX – An attempt

Math behind Torus

Here is the parametric equation of a Torus:

Torus Mathematical Equation

Here R = External Radius of our torus or major radius.

r = inner radius or you can say minor radius.

θ (Theta):

  • Description: This angle typically varies around the circular cross-section of the tube of the torus (the minor radius).
  • It can range between 0 to 2π.

(Phi):

  • Description: This angle varies around the central axis of the torus (the major radius).
  •  The range is the same as the 0 to 2π.

These two angles can describe any point on the torus surface.

Math is really awesome.

Draw torus using Tikz

We need to use pgfplotspackage with Tikz package to deal with 3d points.

\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}

\begin{document}

\begin{tikzpicture}
    \begin{axis}[
        view={180}{50}, % Viewing angle
        hide axis, % Hide the axes
        samples=40, % Number of samples in the x direction
        samples y=40, % Number of samples in the y direction
        domain=0:360,
        y domain=0:360,
        z buffer=sort, % Improve 3D rendering
    ]
        \addplot3[
            thin,
            surf,
            shader=flat,
            opacity=0.8, % Add some transparency
            mesh/rows=40, % Match the number of samples
            mesh/cols=40 % Match the number of samples y
        ]
        (
            {(2 + cos(x)) * cos(y)}, 
            {(2 + cos(x)) * sin(y)}, 
            {sin(x)}
        );
    \end{axis}
\end{tikzpicture}

\end{document}

Output:

Draw torus in LaTeX using Tikz

If you want to change the viewing angle you can change the value in line number 10: view={x}{y}, put different values of x and y and have fun.

Change the color of our torus

We can change the color of the torus that we just drew using colormap. There are lot of pre-defined color maps available. For example: viridis, violtet, blackwhite, hot, jet etc.

I am using viridis colormap here:

\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}

\begin{document}

\begin{tikzpicture}
    \begin{axis}[
        view={180}{50}, % Viewing angle
        hide axis, % Hide the axes
        samples=40, % Number of samples in the x direction
        samples y=40, % Number of samples in the y direction
        domain=0:360,
        y domain=0:360,
        z buffer=sort, % Improve 3D rendering
        colormap/viridis % Set the colormap
    ]
        \addplot3[
            thin,
            surf,
            shader=flat,
            opacity=0.8, % Add some transparency
            mesh/rows=40, % Match the number of samples
            mesh/cols=40 % Match the number of samples y
        ]
        (
            {(2 + cos(x)) * cos(y)}, 
            {(2 + cos(x)) * sin(y)}, 
            {sin(x)}
        );
    \end{axis}
\end{tikzpicture}

\end{document}

Output:

Viridis colormap torus

You can see on line number 17 I have used viridis colormap.

You can set colormap cool using this: colormap/cool

The output will be like this:

Torus colormap latex tikz

I have tried a lot of colormaps and among those I like violet.

If you need any kind of further customization you can let me know in the comment section.

Add annotations on the Torus – Tikz

We can add annotations on any point of the torus to add label texts.

\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}

\begin{document}

\begin{tikzpicture}
    \begin{axis}[
        view={180}{50}, 
        hide axis,
        samples=40, 
        samples y=40,
        domain=0:360,
        y domain=0:360,
        z buffer=sort, 
        colormap/viridis
    ]
        \addplot3[
            thin,
            surf,
            shader=flat,
            opacity=0.8, 
            mesh/rows=40, 
            mesh/cols=40 
        ]
        (
            {(2 + cos(x)) * cos(y)}, 
            {(2 + cos(x)) * sin(y)}, 
            {sin(x)}
        );

        % Adding labels
        \node[anchor=south] at (axis cs:2, 2, 1) {Point A};
        \node[anchor=west] at (axis cs:2.5, -2, 0.5) {Point B};
        \node[anchor=north] at (axis cs:-2, 2, 0.5) {Point C};

        % Adding axis labels
        \node[anchor=east] at (axis cs:2, 0, 0) {X-axis};
        \node[anchor=north] at (axis cs:0, 2, 0) {Y-axis};
        \node[anchor=west] at (axis cs:0, 0, 1) {Z-axis};
    \end{axis}
\end{tikzpicture}

\end{document}

Output:

Label or annotations on Torus - Tikz

You can also check: How to draw Octagon in LaTeX using Tikz

Leave a Reply

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