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

  • LaTeX
  • Thread starter thatboi
  • Start date
  • Tags
    Latex
In summary, the article explains how to use the TikZ package in LaTeX to create a torus and color different sections. It details the necessary code, including the use of 3D coordinates, specifying colors for various sections, and adjusting the torus's parameters for visual clarity. The guide provides examples and tips for customizing the appearance of the torus, ensuring that users can effectively illustrate complex designs in their LaTeX documents.
  • #1
thatboi
133
18
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}  
    {\addplot3[samples y=0,domain=0:361,smooth,samples=71,ultra thin,gray!50]  
        ({torusx(x,#1+x/12,\R,\r)}, 
        {torusy(x,#1+x/12,\R,\r)}, 
        {torusz(x,#1+x/12,\R,\r)});
     \addplot3[samples y=0,domain=0:360,smooth,samples=71,ultra thin,gray!50]  
        ({torusx(#1,x,\R,\r)}, 
        {torusy(#1,x,\R,\r)}, 
        {torusz(#1,x,\R,\r)});
    }
    \pgfplotsinvokeforeach{0,10,...,350}  
    {\addplot3[samples y=0,domain=0:361,smooth,samples=71,visible stretch,very
    thin,gray]  
        ({torusx(x,#1+x/12,\R,\r)}, 
        {torusy(x,#1+x/12,\R,\r)}, 
        {torusz(x,#1+x/12,\R,\r)});
    \addplot3[samples y=0,domain=0:360,smooth,samples=71,visible stretch,very
    thin,gray]  
        ({torusx(#1,x,\R,\r)}, 
        {torusy(#1,x,\R,\r)}, 
        {torusz(#1,x,\R,\r)});}
    \addplot3[samples y=0,domain=0:360,smooth,samples=71,opacity=0.6,
        purple,very thick,dashed]  
        ({torusx(-60,x,\R,\r)}, 
        {torusy(-60,x,\R,\r)}, 
        {torusz(-60,x,\R,\r)}); 
    \addplot3[samples y=0,domain=0:360,smooth,samples=71,visible
    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
    Example_torus (1)-1.png
    42 KB · Views: 65
Last edited by a moderator:
  • Like
Likes fresh_42
Physics news on Phys.org

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

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.

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.

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.

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]
		

	









	
			
Back
Top