## Drawing pictures in LaTeX

Posted: 20th August 2010 by Tim in LaTeX
Tags: , , , , , , , , , , , , , , , , , ,

LaTeX has the ability to draw images out of the box. The drawing functionality is pretty basic — lines, circles, boxes and the like — but perfect for most simple diagrams.

This tutorial will be split into three sections: configuring the environment, different ways of drawing shapes, and a description of the shapes themselves.

###### Configuring the Environment

Before we get started on the environment, we need to think about what we are drawing and how we want to set up the coordinates. We need to define a unit length, which is the length between each coordinate in the picture. It is also used to define the size of the canvas. This is set using \setlength{\unitlength}{length} (see this post for a list of available unit lengths).

Note that the default unit length is 1pt, which is not overly useful for drawing in most cases.

Once the unit length is set, we can create the picture canvas like so:

\begin{picture}(width, height)[(x-offset, y-offset)]

What are all of those parameters? The first parameter, (width, height) defines the height and width of the canvas. This is a number with no units, setting the width according to the unitlength set previously. For example, if the unitlength is set to 5mm, setting the width as 10 will create a canvas of width 50mm. Similarly for height.

Note that the canvas size is not an absolute boundary. It is possible to draw past the boundaries of the canvas – even off the page.

The (x-offset, y-offset) setting is optional, as indicated by the square brackets, and defines the coordinates of the bottom left corner. By default. this corner will have the coordinates (0, 0).

Now that all sounds confusing, so let’s look at an example.

\setlength{\unitlength}{1cm} \begin{picture}(10, 5)(-1, -1) ... \end{picture}

This will create a 10cm x 5cm canvas, with the bottom left corner at (-1, -1) and the top right corner at (9, 4).

The line thickness can also be set, though be aware that it only really works for straight lines. If you set the line thickness to, say, 2pt and draw an oval, then the straight lines of the oval will be 2pt but the curves will be much thinner. Best to avoid line thickness unless using it for straight lines only.

To set the line thickness, use the \linethickness{thickness} command. This can be set many times throughout the document.

###### Different Ways of Drawing Shapes

\put(x, y){shape}
Draws the shape on the picture at position (x, y). eg:

\begin{picture}(2,2) \put(1,1){\circle{1}} \end{picture}

\multiput(x, y)(x-diff, y-diff){num-shapes}{shape}
Draws num-shapes shapes on the picture, starting at (x, y). Each shape is shifted x-diff units to the right and y units up. eg:

\begin{picture}(5,5) \multiput(0,0)(0.5,1){5}{\line(1,0){0.5}} \end{picture}

\shortstack[position]{shape \\ shape \\ …)
Draws a stack of objects. position may be r, l or c to stack the objects to the right, left or center, respectively. eg:

\begin{picture}(5,5) \shortstack{\circle{1}\\\circle{1}\\\circle{1}} \end{picture}

Note: shape in these commands may be any of the shapes below, or another \put, \multiput or \shortstack command.

###### Description of Available Shapes

\begin{picture}(2,2) \put(1,1){\circle{1}} \end{picture}

\begin{picture}(2,2) \put(1,1){\circle*{1}} \end{picture}

\dashbox{dash_len}(width, height)[pos]{text}
Draws a box with a dashed outline, with dashes of length dash_len, around text. The pos argument, if used, is identical to the pos argument in \framebox eg:

\begin{picture}(2,2) \put(0,0){\dashbox{0.2}(2,2)[s]{Boo}} \end{picture}

\frame{object}
Draws a rectangle around object with no spaces between the frame and the object. eg:

\begin{picture}(2,2) \put(1,1){\frame{Boo}} \end{picture}

\framebox(width,height)[pos]{text}
Draws a box around text, with the specified width. pos, if specified, defines where the text is positioned in the box. pos may be l (left), r (right), s (space added between words so that text fills the box) or, if omitted, centered. eg:

\begin{picture}(2,2) \put(0,0){\framebox(2,2){Boo}} \end{picture}

\line(x-slope,y-slope){len}
Draws a line of length len with the specified slope. The slope is defined as two coprime numbers between -6 and 6 inclusive. ie: two numbers with no common factors other than one. len defines the horizontal length, unless the line is vertical where len defines the vertical height. eg:

\begin{picture}{3,1} \put(0,0){\line(3,1){3}} \end{picture}

\makebox[width][pos]{text}
Draws an invisible box with text inside. This is the same as \framebox without the outline. eg:

\begin{picture}(2,2) \put(0,0){\makebox{Boo}} \end{picture}

\oval(width,height)[section]
Draws a rectangle with rounded corners. If section is defined, only part of the oval is drawn. section may be t (top), b (bottom), r (right) or l (left). eg:

\begin{picture}(2,2) \put(1,1){\oval(2,1)} \end{picture}

\vector(x-slope,y-slope){len}
Draws a line with an arrow on the end. This is exactly the same as the \line object with an arrow. eg:

\begin{picture}{3,1} \put(0,0){\vector(3,1){3}} \end{picture}

###### An Example

% set in the preamble - makes changing the image size easier \def\gridspace{20} \def\doublegridspace{40} \def\griddiagonal{10} \def\griddot{\circle*{2}}

...

\setlength{\unitlength}{1mm} \begin{picture}(80,70)         \linethickness{1.5pt}

        % dots         \multiput(20, 10)(5, 5){3}{                 \multiput(0, 0)(0, \gridspace){3}{                         \multiput(0, 0)(\gridspace, 0){3}{\griddot}                 }         }

        % straight lines         \multiput(20, 10)(5, 5){3}{                 \multiput(0, 0)(0, \gridspace){3}{\line(1, 0){\doublegridspace}}                 \multiput(0, 0)(\gridspace, 0){3}{\line(0, 1){\doublegridspace}}         }

        % diagonal lines         \multiput(20, 10)(0, \gridspace){3}{                 \multiput(0, 0)(\gridspace, 0){3}{\line(1, 1){\griddiagonal}}         }

        \linethickness{1pt}

        % arrows         \put(20, 10){\vector(-1, -1){5}}         \put(30, 60){\vector(0, 1){10}}         \put(70, 20){\vector(1, 0){10}}

        % labels         \put(18, 12){\makebox(0, 0){$2$}}         \put(28, 62){\makebox(0, 0){$2$}}         \put(72, 22){\makebox(0, 0){$2$}}         \put(17, 4){\makebox(0, 0){$x$}}         \put(80, 18){\makebox(0, 0){$y$}}         \put(28, 70){\makebox(0, 0){$z$}} \end{picture}

Will produce the following image:

1. phynix says:

ah! You just saved my life with this entry!
Very useful site, thank you!

2. Esteban says:

Hi, thanks. Is there a way to change the line color of a box?

3. Tim says:

Hi Esteban,
You can change the color of the picture elements by using the color package. I wrote this post which explains how to change the text color; this command works from within the picture environment too.

4. Gurusanthosh says:

hi.. your work is super.. i am a newbie to latex.. i am adding captions like this
\begin{figure}
\setlength{\unitlength}{1mm}
\begin{center}
\begin{picture}(width,height)
Insert the commands for drawing the figure here
\end{picture}
\end{center}
\caption[Short Caption]{Full text of the caption.}
\end{figure}

but the caption is coming in between the picture.. how to make it down??

5. Faruq Sandi says:

usefull post!

6. sila says:

What about drawing dashed or dotted lines?

7. Tony says:

Let me join with the other comments and than you for this post. Like others, I too am new to TeX and this has been an eye-opener. Could you point me at a good source for the drawing capabilities of *TeX*? I would like to be able to produce Venn Diagrams are annoted with math text of the form A U B and w E A, etc.
Thanks, Tony

8. Algis says: