Specialized Documents
Creating 3D Graphics
Learn how to create three-dimensional graphics in LaTeX documents using TikZ, pst-solides3d, and Asymptote.
While LaTeX excels at 2D graphics, creating 3D graphics requires specialized packages. Each approach has different strengths for different use cases.
PGF/TikZ Basic 3D
TikZ provides basic 3D drawing capabilities through coordinate transformations. While not true 3D rendering, it works well for simple 3D diagrams.
\usepackage{tikz}
\usetikzlibrary{3d}
\begin{tikzpicture}[x={(1cm,0cm)}, y={(0cm,1cm)}, z={(0cm,0.5cm)}]
% Draw a cube
\draw[fill=blue!20] (0,0,0) -- (1,0,0) -- (1,1,0) -- (0,1,0) -- cycle;
\draw[fill=blue!40] (1,0,0) -- (1,0,1) -- (1,1,1) -- (1,1,0) -- cycle;
\draw[fill=blue!30] (0,0,0) -- (1,0,0) -- (1,0,1) -- (0,0,1) -- cycle;
% Draw axes
\draw[->] (0,0,0) -- (1.5,0,0) node[below] {$x$};
\draw[->] (0,0,0) -- (0,1.5,0) node[left] {$y$};
\draw[->] (0,0,0) -- (0,0,1.5) node[above] {$z$};
\end{tikzpicture}
TikZ 3D Coordinate System
\begin{tikzpicture}[x={(1cm,0cm)}, y={(0cm,1cm)}, z={(0.5cm,-0.3cm)}]
% Different perspective angles
\draw[thick] (0,0,0) -- (2,0,0) -- (2,2,0) -- (0,2,0) -- cycle;
\draw[thick] (0,0,0) -- (0,0,2);
\end{tikzpicture}
pst-solides3d (True 3D Rendering)
The pst-solides3d package provides true 3D rendering with surface visibility, hidden line removal, and lighting.
\usepackage{pst-solides3d}
\begin{document}
\begin{pspicture}(-2,-2)(2,2)
\psSolid[object=cylinder, r=1, h=2, hue=0 1]
\end{pspicture}
\end{document}
3D Objects
\begin{pspicture}(-3,-3)(3,3)
% Sphere
\psSolid[object=sphere, r=1, hue=0 1](0,0,0)
% Cube
\psSolid[object=cube, a=1.5, hue=0 1](2,0,0)
% Cone
\psSolid[object=cone, r=1, h=2, hue=0 1](-2,0,0)
\end{pspicture}
Custom Surfaces
\psSolid[object=function,
x/y/z F 2 index cos mul 3 index sin mul,
r 1,
hue 0 1]
Compile with: dvips filename.tex && ps2pdf filename.ps
Or use auto-pst-pdf for pdflatex compatibility:
\usepackage{auto-pst-pdf}
Asymptote
Asymptote is a powerful vector graphics language with true 3D support, designed to work seamlessly with LaTeX.
\documentclass{article}
\usepackage{asymptote}
\begin{document}
\begin{asy}
import three;
size(200);
currentprojection=perspective(6,3,2);
draw(unitsphere,blue+opacity(0.5));
draw(O--X,red,Arrow3);
draw(O--Y,green,Arrow3);
draw(O--Z,blue,Arrow3);
\end{asy}
\end{document}
Compile with:
pdflatex filename.tex
asy filename
pdflatex filename.tex
Asymptote 3D Surface
\begin{asy}
import three;
size(200,0);
real f(pair z) {return cos(z.x)*sin(z.y);}
draw(surface(f,(-pi,-pi),(pi,pi),nx=20),blue+opacity(0.7));
\end{asy}
Comparison Table
| Feature | TikZ 3D | pst-solides3d | Asymptote |
|---|---|---|---|
| True 3D rendering | No | Yes | Yes |
| Hidden surface removal | No | Yes | Yes |
| Lighting/shading | Limited | Yes | Yes |
| Ease of use | Easy | Medium | Medium |
| Compilation | pdflatex | dvips/ps2pdf | pdflatex+asy |
| Best for | Diagrams | CAD-like objects | Scientific visualization |
Best Practices
- Choose the right tool: Use TikZ for simple 3D diagrams, pst-solides3d for CAD-like objects, Asymptote for complex scientific visualizations.
- Consider compilation: PSTricks-based packages need dvips; Asymptote needs two pdflatex passes with asy in between.
- Optimize file size: Complex 3D surfaces can produce large files. Reduce resolution where possible.
- Test rendering: Always verify that hidden surfaces are correctly handled in your final output.