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.

## How to do it ...

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

**Load pgfplots package****Add tikz environment with axis****plot the torus using \addplot3 command****Add colormap for a touch of style!**

## 1. Create tikzpicture environment with axis

\documentclass{standalone} \usepackage{pgfplots} \pgfplotsset{compat=newest} \begin{document} \begin{tikzpicture} \begin{axis}[] % here comes all the code \end{axis} \end{tikzpicture} \end{document}

## 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} \pgfplotsset{compat=newest} \begin{document} \begin{tikzpicture} \begin{axis}[] \addplot3[]( {(3+sin(deg(\x)))*cos(deg(\y))}, {(3+sin(deg(\x)))*sin(deg(\y))}, {cos(deg(\x))} ); \end{axis} \end{tikzpicture} \end{document}

*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} \pgfplotsset{compat=newest} \begin{document} \begin{tikzpicture} \begin{axis}[] \addplot3[ surf, shader = faceted interp, 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}

**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

## 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} \pgfplotsset{compat=newest} \begin{document} \begin{tikzpicture} \begin{axis}[ axis equal image, hide axis, z buffer = sort, view = {122}{30}, scale = 1.5 ] \addplot3[ surf, shader = faceted interp, 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} \pgfplotsset{compat=newest, 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 ] \addplot3[ surf, shader = faceted interp, 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}

## Leave a Reply