LaTeX/Picture

The environment allows programming pictures directly in LaTeX. On the one hand, there are rather severe constraints, as the slopes of line segments and the radii of circles are restricted to a narrow choice of values. On the other hand, the picture environment of LaTeX2e brings with it the command, "q" meaning quadratic. Many frequently-used curves such as circles, ellipses, and catenaries can be satisfactorily approximated by quadratic Bézier curves, although this may require some mathematical toil. If a programming language like Java is used to generate blocks of LaTeX input files, the picture environment becomes quite powerful.

Although programming pictures directly in LaTeX is severely restricted, and often rather tiresome, there are still reasons for doing so. The documents thus produced are "small" with respect to bytes, and there are no additional graphics files to be dragged along.

Packages like, , or  enhance the original picture environment, and greatly strengthen the graphical power of LaTeX.

Basic commands
A environment is available in any LaTeX distribution, without the need of loading any external package. This environment is created with one of the two commands

or

The first pair, $$(x, y)$$, affects the reservation, within the document, of rectangular space for the picture.

The optional second pair, $$(x_0, y_0)$$, assigns arbitrary coordinates to the bottom left corner of the reserved rectangle.

The numbers x, y, x0, y0 are numbers (lengths) in the units of, which can be reset any time (but not within a picture environment) with a command such as

The default value of is.

Most drawing commands have one of the two forms

or

Bézier curves are an exception. They are drawn with the command

With the package absolute dimension (like 15pt) and expression are allowed, in addition to numbers relative to.

Line segments
Line segments are drawn with the command:

The command has two arguments:
 * 1) a direction vector,
 * 2) a "length" (sort of: this argument is the vertical length in the case of a vertical line segment and in all other cases the horizontal distance of the line, rather than the length of the segment itself).

The components of the direction vector are restricted to the integers (−6, −5, ..., 5, 6) and they have to be coprime (no common divisor except 1). The figure below illustrates all 25 possible slope values in the first quadrant. The length is relative to.

Arrows
Arrows are drawn with the command

For arrows, the components of the direction vector are even more narrowly restricted than for line segments, namely to the integers (−4, −3, ..., 3, 4). Components also have to be coprime (no common divisor except 1). Notice the effect of the command on the two arrows pointing to the upper left.

Circles
The command

draws a circle with center (x, y) and diameter (not radius) specified by diameter. The picture environment only admits diameters up to approximately 14mm, and even below this limit, not all diameters are possible. The command produces disks (filled circles). As in the case of line segments, one may have to resort to additional packages, such as, , or.

There is another possibility within the picture environment. If one is not afraid of doing the necessary calculations (or leaving them to a program), arbitrary circles and ellipses can be patched together from quadratic Bézier curves. See Graphics in LaTeX2e for examples and Java source files.

Text and formulae
As this example shows, text and formulae can be written in the environment with the command in the usual way:

\multiput and \linethickness
The command

has 4 arguments: the starting point, the translation vector from one object to the next, the number of objects, and the object to be drawn. The \linethickness command applies to horizontal and vertical line segments, but neither to oblique line segments nor to circles. It does, however, apply to quadratic Bézier curves!

Ovals
The command

or

produces an oval centered at (x, y) and having width w and height h. The optional position arguments b, t, l, r refer to "top", "bottom", "left", "right", and can be combined, as the example illustrates. Line thickness can be controlled by two kinds of commands: on the one hand,  and  on the other. While applies only to horizontal and vertical lines (and quadratic Bézier curves),  and  apply to oblique line segments as well as to circles and ovals.

Multiple use of predefined picture boxes
A picture box can be declared by the command

then defined by

and finally, arbitrarily often be drawn by

The optional position parameter has the effect of defining the "anchor point" of the savebox. In the example, it is set to "bl" which puts the anchor point into the bottom left corner of the savebox. The other position specifiers are top and right.

The name argument refers to a LaTeX storage bin and therefore is of a command nature (which accounts for the backslashes in the current example). Boxed pictures can be nested: In this example, is used within the definition of. The command had to be used as the  command does not work if the segment length is less than about 3 mm.

Quadratic Bézier curves
The command

draws a quadratic bezier curve where $$P_1 = (x_1, y_1)$$, $$P_2 = (x_2, y_2)$$ denote the end points, and $$S = (x, y)$$ denotes the intermediate control point. The respective tangent slopes, $$m_1$$ and $$m_2$$, can be obtained from the equations

\begin{cases} x= \frac{m_2 x_2 - m_1 x_1 - (y_2 - y_1)}{m_2 - m_1} \\ y= y_i + m_i (x - x_i); \quad (i=1,2\, \hbox{ gives same solution}) \end{cases} $$

See Graphics in LaTeX2e for a Java program which generates the necessary command line.

As this example illustrates, splitting up a circle into 4 quadratic Bézier curves is not satisfactory. At least 8 are needed. The figure again shows the effect of the command on horizontal or vertical lines, and of the  and the  commands on oblique line segments. It also shows that both kinds of commands affect quadratic Bézier curves, each command overriding all previous ones.

Catenary
In this figure, each symmetric half of the catenary $$y= \cosh x - 1$$ is approximated by a quadratic Bézier curve. The right half of the curve ends in the point (2, 2.7622), the slope there having the value m = 3.6269. Using again equation (*), we can calculate the intermediate control points. They turn out to be (1.2384, 0) and (−1.2384, 0). The crosses indicate points of the real catenary. The error is barely noticeable, being less than one percent. This example points out the use of the optional argument of the command. The picture is defined in a convenient "mathematical" coordinates, whereas by the command

its lower-left corner (marked by the black disk) is assigned the coordinates (−2.5,−0.25).

Plotting graphs
The control points of the two Bézier curves were calculated with formulas (*). The positive branch is determined by $$P_1 = (0, 0)$$, $$m_1 = 1$$ and $$P_2 = (2, \tanh 2)$$, $$m_2 = 1/ \cosh^2 2$$. Again, the picture is defined in mathematically convenient coordinates, and the lower left corner is assigned the mathematical coordinates (−3,−2) (black disk).

The picture environment and gnuplot
The powerful scientific plotting package gnuplot has the capability to output directly to a LaTeX environment. It is often far more convenient to plot directly to LaTeX, since this saves having to deal with potentially troublesome postscript files. Plotting scientific data (or, indeed, mathematical figures) this way gives much greater control, and of course typesetting ability, than is available from other means (such as postscript). Such pictures can then be added to a document by an command.

N.B. gnuplot is a powerful piece of software with a vast array of commands. A full discussion of gnuplot lies beyond the scope of this note. See for a tutorial.

LaTeX/Слика