 # How to draw a torus in LaTeX using PGF/TikZ

In this LaTeX tutorial, we will learn how to plot a torus using its mathematical equations. To achieve this we will use Pgfplots, a pretty powerful package based on TikZ/PGF. It allows us to draw line plots, bar charts, complex surface plots and much more. A torus (pretty similar to a donut) can be plotted mathematically using the following parametric equation:
\begin{aligned} x & = (R + r\cos{\theta}) \cos{\phi}\\ y & = (R+r \cos{\theta}) \sin{\phi}\\ z & = r \sin{\theta} \end{aligned}
where $R$ and $r$ are external and internal radius of the torus, respectively. Parameters $\theta$ and $\phi$ are angles. They take values from $0$ to $2\pi$.

## How to do it ...

To plot the torus from its parametric equations we will follow these steps:

2. Add tikz environment with axis
3.  plot the torus using \addplot3 command
4.  Add colormap for a touch of style!
Let’s go for details!

## 1. Create tikzpicture environment with axis

Using Pgfplots, we can plot any parametric equation. First we just need to set up the preamble in our LaTeX file as follows:
\documentclass{standalone}

\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}[]
% here comes all the code
\end{axis}
\end{tikzpicture}
\end{document}

The command \pgfplotsset{compat=newest} sets the package to the last version developed. Inside the tikzpicture environment, we have included the axis environment. Hence, all the code we are going to develop is going to be inside it. If you compile the code above you will get an empty coordinate system.

## 2. Create three dimensional plot with \addplot3 command

Next, we have to use the command \addplot3[options](function) in order to get the plot of the parametric equation of the torus. If we set $R=3$ and $r=1$, we can plot the donut. Check the next code to figure out how the parametric equations should be typed.

\documentclass{standalone}

\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}[]
{(3+sin(deg(\x)))*cos(deg(\y))},
{(3+sin(deg(\x)))*sin(deg(\y))},
{cos(deg(\x))}
);
\end{axis}
\end{tikzpicture}
\end{document}

It should be noted that parameter $\theta$ corresponds to $x$ in the TikZ code, and $\phi$ corresponds to $y$. Besides, the functions $\sin$ and $\cos$ read angles in degrees. That’s why we have to use the command deg(\x).

## 3. Plot a surface by changing \addplot3 parameters

By compiling the previous latex code, we get the 3D graphic shown in image a. But it’s far away from what we want. In addition, notice that the options of the axis and the tikzpicture are empty. If we put the correct options we will get the desired shape. Let’s add some options for the \addplot3 command.

\documentclass{standalone}

\usepackage{tikz}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}[]
surf,
samples = 40,
samples y = 80,
domain = 0:2*pi,
domain y = 0:2*pi,
thin,
](
{(3+sin(deg(\x)))*cos(deg(\y))},
{(3+sin(deg(\x)))*sin(deg(\y))},
{cos(deg(\x))}
);
\end{axis}
\end{tikzpicture}
\end{document}

Here we have added the following options:
• surf: Defines the object as a surface.
• shader: Determines the way the package computes the subdivision of the surface.
• samples: It’s the number of subdivisions in the $x$ and $y$ direction.
• domain: Represents the variation of the parameters $s$ and $t$.
• thin: it’s the stroke of the mesh over the surface
Now our torus looks better (see image b), but it’s not enough!

## 4. Rendering a 3D surface in Latex Tikz

We can hide the axis, change the aspect ratio and make it look nicer in general. To achieve this, we have to set up some extra options in the axis environment. Here is an explanation of the options used :

• axis equal image: Sets the aspect ratio of the $x$, $y$ and $z$ as $1:1:1$.
• hide axis: Hides the referential coordinate system.
• z buffer: This command is very important when plotting 3D plots, it makes possible to render the surface with an accurate interpretation of the distribution of the mesh in space.
• view: Sets the angle and orientation of the view.
• scale: Scales the original size of the plot
\documentclass{standalone}

\usepackage{tikz}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis equal image,
hide axis,
z buffer = sort,
view = {122}{30},
scale = 1.5
]
surf,
samples = 40,
samples y = 80,
domain = 0:2*pi,
domain y = 0:2*pi,
thin,
](
{(3+sin(deg(\x)))*cos(deg(\y))},
{(3+sin(deg(\x)))*sin(deg(\y))},
{cos(deg(\x))}
);
\end{axis}
\end{tikzpicture}
\end{document}


Finally the torus looks much better as shown in image c. To put the final touch, let’s give the torus some personality by changing the colour.

## 4. Add a colormap to a 3D surface

By adding the command colormap in the \pgfplotsset , we can change the surface colour. For the example shown in image d, the colours are lightgray and white. Of course it depends on your style, you can change it to the colours you like. And we’re done with our torus.

\documentclass{standalone}

\usepackage{tikz}
\usepackage{pgfplots}
\usepgfplotslibrary{colormaps}
colormap={mycolormap}{color=(lightgray) color=white) color=(lightgray)}
}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis equal image,
hide axis,
z buffer = sort,
view = {122}{30},
scale = 1.5
]
surf,
samples = 40,
samples y= 80,
domain = 0:2*pi,
domain y = 0:2*pi,
colormap name = mycolormap,
thin
] (
{(3+sin(deg(\x)))*cos(deg(\y))},
{(3+sin(deg(\x)))*sin(deg(\y))},
{cos(deg(\x))}
);
\end{axis}
\end{tikzpicture}
\end{document}


### This Post Has 2 Comments

1. Hey man!
Great work.
I love this idea of using commands to draw and am looking forward to using your content to publish my college magazine.

A tiny correction I’d like to bring to your attention is as follows:
x=(R+rcosθ)cosϕ
y=(R+rcosθ)sinϕ
z=rsinϕ

should actually be
x=(R+rcosθ)cosϕ
y=(R+rcosθ)sinϕ
z=rsinθ

1. Hey Dhawal!
Many thanks for your encouraging feedback, I really appreciate it.
I’ve corrected the typo, it is theta instead of phi in Z equation.
Feel free to use it with your college magazine 🙂
All the best,