Is there an easy way to draw complex images in LaTeX? By this I mean: Have a look at the following question. Some answers provide incredible pictures, very elaborated. In the (rudimentary) way I can create figures with tikz or pstricks, it would be impossible for me to come up with the correspondings codes. This made me think: Perhaps there are some tools that help write down the code.

I am aware of things like tikzcd-editor or LaTeXDraw. I also know about InKscape, although I have never used it myself. Howerver, none of them seem to be appropriate to draw tori or spheres. Yes, with LaTeXDraw one can draw ellipses and create things that resemble those surfaces, but the quality of these draws is as good as the quality of Paint.

I am thinking more of some type of image recognition software that identifies the geometric object (as well as some text the figure might have) the user has drawn and produce the corresponding code. Does anything like that exist?


1 Answer 1


There are essentially two parts to your question:

  1. You need to complete a computer vision task to interpret what user-defined sketches ought to be. There has been some progress in that front, a publicly accessible service in this direction is Google's autodraw, but that only really works for 2D sketches. I can imagine 3D being somewhat harder because you are trying to interpret a 3D image from a 2D projection.
  2. You also then need an AI assistant to generate code based on the computer vision interpretation. This is allegedly one of the things that ChatGPT and other LLMs can do. So let's give it a try.

Below the cut is the ChatGPT response to the prompt: "How do I draw a torus in TikZ".

(For the curious, no, the code included does not build. To start it is missing \usepackage{tikz-3dplot}; and there are several other mistakes. Though not being a TikZ expert I cannot really decide how serious these issues are.)

Using this as an illustration of status-quo, my guess would be that such a tool as you are looking for does not yet exist.

Drawing a torus in TikZ involves a series of steps to define the 3D shape and then projecting it onto a 2D plane. Here's a step-by-step guide to drawing a torus using TikZ:

Step 1: Load the necessary TikZ libraries To draw 3D shapes, we need to load the tikz package with the 3d and calc libraries.

Step 2: Define the torus parameters Decide on the major radius (distance from the center of the torus to the center of the tube) and the minor radius (radius of the tube).

Step 3: Calculate the torus points Calculate the coordinates of the points that form the torus based on its parameters.

Step 4: Draw the torus surface Using loops, connect the calculated points to form the surface of the torus.

Step 5: Optional - Add shading and coloring You can add shading and color to enhance the appearance of the torus.

Here's the TikZ code to draw a torus:

\usetikzlibrary{3d, calc}

\begin{tikzpicture}[scale=1.5, tdplot_main_coords]

  % Torus Parameters
  \def\R{2} % Major radius (distance from center to center of the tube)
  \def\r{0.7} % Minor radius (radius of the tube)

  % Calculate the torus points
  \foreach \i in {0,\StepSize,...,360}
    \foreach \j in {0,\StepSize,...,360}
      \pgfmathsetmacro{\x}{(\R + \r * cos(\j)) * cos(\i)}
      \pgfmathsetmacro{\y}{(\R + \r * cos(\j)) * sin(\i)}
      \pgfmathsetmacro{\z}{\r * sin(\j)}
      \coordinate (P) at (\x,\y,\z);
      \draw[fill=gray!50] (P) circle (0.05); % Optional: Draw points to check the torus points

  % Draw the torus surface
  \foreach \i in {0,...,72}
    \foreach \j in {0,...,72}
      \draw[thick, fill=blue!30] ($ (P\i\j)!0.5!(P\nexti\j) $) --
        ($ (P\nexti\j)!0.5!(P\nexti\nextj) $) --
        ($ (P\nexti\nextj)!0.5!(P\i\nextj) $) --
        ($ (P\i\nextj)!0.5!(P\i\j) $) -- cycle;

  % Optional: Draw the torus center and axes
  \coordinate (Center) at (0,0,0);
  \draw[thick,-latex] (Center) -- (3,0,0) node[anchor=north east]{$x$};
  \draw[thick,-latex] (Center) -- (0,3,0) node[anchor=north west]{$y$};
  \draw[thick,-latex] (Center) -- (0,0,3) node[anchor=south]{$z$};
  \draw[fill=red] (Center) circle (0.05); % Torus center point


This code will create a 3D visualization of a torus. The \R and \r values can be adjusted to change the size of the torus. The resulting TikZ picture will be a 3D representation, and you can view it from different angles by rotating it in your PDF viewer.

Remember to compile the document using a LaTeX compiler like pdflatex, and you should obtain the output containing the TikZ drawing of the torus.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .