# How Can I Color Different Sections of a Torus in LaTeX Using TikZ?

• LaTeX
• thatboi
thatboi
Hey all,
I have been looking into drawing a torus in LaTeX and came across the post Torus with lines winding along different axis, where the response by user194703 involved the following block of code (which I have very slightly edited)
Code:
\documentclass[tikz,border=3.14mm]{standalone}
% based on
% [URL]https://tex.stackexchange.com/a/199715[/URL]
% [URL]https://tex.stackexchange.com/a/485833[/URL]
% [URL]https://tex.stackexchange.com/a/485494[/URL]
\usepackage{pgfplots}
\pgfplotsset{compat=1.16,width=16cm,
}
\tikzset{declare function={torusx(\u,\v,\R,\r)=cos(\u)*(\R + \r*cos(\v));
torusy(\u,\v,\R,\r)=(\R + \r*cos(\v))*sin(\u);
torusz(\u,\v,\R,\r)=\r*sin(\v);
vcrit1(\u,\th)=atan(tan(\th)*sin(\u));% first critical v value
vcrit2(\u,\th)=180+atan(tan(\th)*sin(\u));% second critical v value
vtest(\u,\v,\az,\el)=sin(-vcrit1(\u-\az,\el)+\v);
disc(\th,\R,\r)=((pow(\r,2)-pow(\R,2))*pow(cot(\th),2)+%
pow(\r,2)*(2+pow(tan(\th),2)))/pow(\R,2);% discriminant
umax(\th,\R,\r)=ifthenelse(disc(\th,\R,\r)>0,asin(sqrt(abs(disc(\th,\R,\r)))),0);
}}
\pgfplotsset{visible stretch/.style={restrict expr to domain={vtest(atan2(rawy,rawx),%
ifthenelse(veclen(rawx,rawy)>\R,asin(rawz/\r),180-asin(rawz/\r)),\pgfkeysvalueof{/pgfplots/view/az},\pgfkeysvalueof{/pgfplots/view/el})}{-0.05:1.1}},
hidden stretch/.style={restrict expr to
domain={vtest(atan2(rawy,rawx),%
ifthenelse(veclen(rawx,rawy)>\R,asin(rawz/\r),180-asin(rawz/\r)),\pgfkeysvalueof{/pgfplots/view/az},\pgfkeysvalueof{/pgfplots/view/el})}{-1.1:0.05}}}
\begin{document}
\begin{tikzpicture}
\pgfmathsetmacro{\R}{4}
\pgfmathsetmacro{\r}{1.2}
\pgfplotsset{view={35}{60},axis lines=none,}
\matrix{
\begin{axis}[]
\pgfplotsinvokeforeach{0,10,...,350}
({torusx(x,#1+x/12,\R,\r)},
{torusy(x,#1+x/12,\R,\r)},
{torusz(x,#1+x/12,\R,\r)});
({torusx(#1,x,\R,\r)},
{torusy(#1,x,\R,\r)},
{torusz(#1,x,\R,\r)});
}
\pgfplotsinvokeforeach{0,10,...,350}
thin,gray]
({torusx(x,#1+x/12,\R,\r)},
{torusy(x,#1+x/12,\R,\r)},
{torusz(x,#1+x/12,\R,\r)});
thin,gray]
({torusx(#1,x,\R,\r)},
{torusy(#1,x,\R,\r)},
{torusz(#1,x,\R,\r)});}
purple,very thick,dashed]
({torusx(-60,x,\R,\r)},
{torusy(-60,x,\R,\r)},
{torusz(-60,x,\R,\r)});
stretch,purple,very thick,dashed]
({torusx(-10,x,\R,\r)},
{torusy(-10,x,\R,\r)},
{torusz(-10,x,\R,\r)});
\end{axis} \\ };
\end{tikzpicture}
\end{document}
The image produced I have also attached. The 2 dashed circles effectively create 2 sections along the torus and I wish to fill the surface of these sections with 2 different colors. If anyone could teach me how to do so I would greatly appreciate it as I am relatively new to drawing 3d graphics via tikz.

#### Attachments

• Example_torus (1)-1.png
42 KB · Views: 55
Last edited by a moderator:
fresh_42

## 1. How do I install the necessary packages to color a torus in LaTeX?

To color a torus in LaTeX, you typically need the tikz and pgfplots packages. You can install them by including the following lines in the preamble of your LaTeX document:

\usepackage{tikz}\usepackage{pgfplots}\pgfplotsset{compat=newest}\usetikzlibrary{3d}
These packages provide the necessary tools to create and color 3D shapes like a torus.

## 2. What is the basic code to draw a torus in LaTeX?

Here is a basic example of how to draw a torus using TikZ:

\documentclass{standalone}\usepackage{tikz}\usetikzlibrary{3d}\begin{document}\begin{tikzpicture}  \draw[thick] (0,0) circle (2);  \foreach \i in {0,30,...,330}    \draw[thick] (\i:2) circle (0.5);\end{tikzpicture}\end{document}
This code creates a simple 2D representation of a torus. For a more realistic 3D torus, additional code and packages are required.

## 3. How can I add color to the torus?

To add color to the torus, you can use the fill option in TikZ. Here is an example:

\documentclass{standalone}\usepackage{tikz}\usetikzlibrary{3d}\begin{document}\begin{tikzpicture}  \fill[blue!50] (0,0) circle (2);  \foreach \i in {0,30,...,330}    \fill[red!50] (\i:2) circle (0.5);\end{tikzpicture}\end{document}
This code colors the main circle blue and the smaller circles red. You can adjust the colors using different color names and percentages.

## 4. How can I create a 3D torus with shading in LaTeX?

Creating a 3D torus with shading involves more complex code. Here is an example using pgfplots:

\documentclass{standalone}\usepackage{pgfplots}\pgfplotsset{compat=newest}\usetikzlibrary{3d}\begin{document}\begin{tikzpicture}  \begin{axis}[view={60}{30}, hide axis]

How Can I Draw Electric Field Lines Over a Quiver Plot in LaTeX?

Jan 5, 2019

MATLAB, Maple, Mathematica, LaTeX

Replies
1

Views
2K

MHB

MHB Introduces: Creating TikZ Pictures

Aug 22, 2017

MATLAB, Maple, Mathematica, LaTeX

Replies
1

Views
5K

How can I determine if a change of variables using the Jacobian is one to one?

Dec 16, 2005

Calculus and Beyond Homework Help

Replies
4

Views
8K

Share:

Share

Forums

Mathematics

MATLAB, Maple, Mathematica, LaTeX