LaTeX/Picture

From testwiki
Jump to navigation Jump to search

Template:LaTeX/Top

The Template:LaTeX/Environment 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 Template:LaTeX/LaTeX 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 Template:LaTeX/LaTeX 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 Template:LaTeX/Package, Template:LaTeX/Package, Template:LaTeX/Package or Template:LaTeX/Package enhance the original picture environment, and greatly strengthen the graphical power of LaTeX.

Basic commands

A Template:LaTeX/Environment 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

Template:LaTeX/Usage

or

Template:LaTeX/Usage

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

The optional second pair, (x0,y0), 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 Template:LaTeX/LaTeX, which can be reset any time (but not within a picture environment) with a command such as

Template:LaTeX/Usage

The default value of Template:LaTeX/LaTeX is Template:LaTeX/Parameter.

Most drawing commands have one of the two forms

Template:LaTeX/Usage

or

Template:LaTeX/Usage

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

Template:LaTeX/Usage

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

Line segments

Line segments are drawn with the command:

Template:LaTeX/Usage

The Template:LaTeX/LaTeX 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 Template:LaTeX/LaTeX.

\setlength{\unitlength}{5cm}
\begin{picture}(1,1)
\put(0,0){\line(0,1){1}}
\put(0,0){\line(1,0){1}}
\put(0,0){\line(1,1){1}}
\put(0,0){\line(1,2){.5}}
\put(0,0){\line(1,3){.3333}}
\put(0,0){\line(1,4){.25}}
\put(0,0){\line(1,5){.2}}
\put(0,0){\line(1,6){.1667}}
\put(0,0){\line(2,1){1}}
\put(0,0){\line(2,3){.6667}}
\put(0,0){\line(2,5){.4}}
\put(0,0){\line(3,1){1}}
\put(0,0){\line(3,2){1}}
\put(0,0){\line(3,4){.75}}
\put(0,0){\line(3,5){.6}}
\put(0,0){\line(4,1){1}}
\put(0,0){\line(4,3){1}}
\put(0,0){\line(4,5){.8}}
\put(0,0){\line(5,1){1}}
\put(0,0){\line(5,2){1}}
\put(0,0){\line(5,3){1}}
\put(0,0){\line(5,4){1}}
\put(0,0){\line(5,6){.8333}}
\put(0,0){\line(6,1){1}}
\put(0,0){\line(6,5){1}}
\end{picture}

Template:BookCat

Arrows

Arrows are drawn with the command Template:LaTeX/Usage

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 Template:LaTeX/LaTeX command on the two arrows pointing to the upper left.

\setlength{\unitlength}{0.75mm}
\begin{picture}(60,40)
\put(30,20){\vector(1,0){30}}
\put(30,20){\vector(4,1){20}}
\put(30,20){\vector(3,1){25}}
\put(30,20){\vector(2,1){30}}
\put(30,20){\vector(1,2){10}}
\thicklines
\put(30,20){\vector(-4,1){30}}
\put(30,20){\vector(-1,4){5}}
\thinlines
\put(30,20){\vector(-1,-1){5}}
\put(30,20){\vector(-1,-4){5}}
\end{picture}

Template:BookCat

Circles

The command Template:LaTeX/Usage

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 Template:LaTeX/LaTeX command produces disks (filled circles). As in the case of line segments, one may have to resort to additional packages, such as Template:LaTeX/Package, Template:LaTeX/Package, or Template:LaTeX/Package.

\setlength{\unitlength}{1mm}
\begin{picture}(60, 40)
\put(20,30){\circle{1}}
\put(20,30){\circle{2}}
\put(20,30){\circle{4}}
\put(20,30){\circle{8}}
\put(20,30){\circle{16}}
\put(20,30){\circle{32}}
\put(40,30){\circle{1}}
\put(40,30){\circle{2}}
\put(40,30){\circle{3}}
\put(40,30){\circle{4}}
\put(40,30){\circle{5}}
\put(40,30){\circle{6}}
\put(40,30){\circle{7}}
\put(40,30){\circle{8}}
\put(40,30){\circle{9}}
\put(40,30){\circle{10}}
\put(40,30){\circle{11}}
\put(40,30){\circle{12}}
\put(40,30){\circle{13}}
\put(40,30){\circle{14}}
\put(15,10){\circle*{1}}
\put(20,10){\circle*{2}}
\put(25,10){\circle*{3}}
\put(30,10){\circle*{4}}
\put(35,10){\circle*{5}}
\end{picture}

Template:BookCat

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 Template:LaTeX/LaTeX command in the usual way:

\setlength{\unitlength}{0.8cm}
\begin{picture}(6,5)
\thicklines
\put(1,0.5){\line(2,1){3}}
\put(4,2){\line(-2,1){2}}
\put(2,3){\line(-2,-5){1}}
\put(0.7,0.3){$A$}
\put(4.05,1.9){$B$}
\put(1.7,2.95){$C$}
\put(3.1,2.5){$a$}
\put(1.3,1.7){$b$}
\put(2.5,1.05){$c$}
\put(0.3,4){$F=\sqrt{s(s-a)(s-b)(s-c)}$}
\put(3.5,0.4){$\displaystyle s:=\frac{a+b+c}{2}$}
\end{picture}

Template:BookCat

\multiput and \linethickness

The command Template:LaTeX/Usage

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!

\setlength{\unitlength}{2mm}
\begin{picture}(30,20)
\linethickness{0.075mm}
\multiput(0,0)(1,0){26}%
{\line(0,1){20}}
\multiput(0,0)(0,1){21}%
{\line(1,0){25}}
\linethickness{0.15mm}
\multiput(0,0)(5,0){6}%
{\line(0,1){20}}
\multiput(0,0)(0,5){5}%
{\line(1,0){25}}
\linethickness{0.3mm}
\multiput(5,0)(10,0){2}%
{\line(0,1){20}}
\multiput(0,5)(0,10){2}%
{\line(1,0){25}}
\end{picture}

Template:BookCat

Ovals

The command Template:LaTeX/Usage

or Template:LaTeX/Usage

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: Template:LaTeX/LaTeX on the one hand, Template:LaTeX/LaTeX and Template:LaTeX/LaTeX on the other. While Template:LaTeX/LaTeX applies only to horizontal and vertical lines (and quadratic Bézier curves), Template:LaTeX/LaTeX and Template:LaTeX/LaTeX apply to oblique line segments as well as to circles and ovals.

\setlength{\unitlength}{0.75cm}
\begin{picture}(6,4)
\linethickness{0.075mm}
\multiput(0,0)(1,0){7}%
{\line(0,1){4}}
\multiput(0,0)(0,1){5}%
{\line(1,0){6}}
\thicklines
\put(2,3){\oval(3,1.8)}
\thinlines
\put(3,2){\oval(3,1.8)}
\thicklines
\put(2,1){\oval(3,1.8)[tl]}
\put(4,1){\oval(3,1.8)[b]}
\put(4,3){\oval(3,1.8)[r]}
\put(3,1.5){\oval(1.8,0.4)}
\end{picture}

Template:BookCat

Multiple use of predefined picture boxes

A picture box can be declared by the command

Template:LaTeX/Usage

then defined by

Template:LaTeX/Usage

and finally, arbitrarily often be drawn by

Template:LaTeX/Usage

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, Template:LaTeX/LaTeX is used within the definition of Template:LaTeX/LaTeX. The Template:LaTeX/LaTeX command had to be used as the Template:LaTeX/LaTeX command does not work if the segment length is less than about 3 mm.

\setlength{\unitlength}{0.5mm}
\begin{picture}(120,168)
\newsavebox{\foldera}
\savebox{\foldera}
  (40,32)[bl]{% definition
  \multiput(0,0)(0,28){2}
    {\line(1,0){40}}
  \multiput(0,0)(40,0){2}
    {\line(0,1){28}}
  \put(1,28){\oval(2,2)[tl]}
  \put(1,29){\line(1,0){5}}
  \put(9,29){\oval(6,6)[tl]}
  \put(9,32){\line(1,0){8}}
  \put(17,29){\oval(6,6)[tr]}
  \put(20,29){\line(1,0){19}}
  \put(39,28){\oval(2,2)[tr]}
}

\newsavebox{\folderb}
\savebox{\folderb}
  (40,32)[l]{% definition
  \put(0,14){\line(1,0){8}}
  \put(8,0){\usebox{\foldera}}
}

\put(34,26){\line(0,1){102}}
\put(14,128){\usebox{\foldera}}
\multiput(34,86)(0,-37){3}
{\usebox{\folderb}}
\end{picture}

Template:BookCat

Quadratic Bézier curves

The command

Template:LaTeX/Usage

draws a quadratic bezier curve where P1=(x1,y1), P2=(x2,y2) denote the end points, and S=(x,y) denotes the intermediate control point. The respective tangent slopes, m1 and m2, can be obtained from the equations

{x=m2x2m1x1(y2y1)m2m1y=yi+mi(xxi);(i=1,2 gives same solution)

See Graphics in LaTeX2e for a Java program which generates the necessary Template:LaTeX/LaTeX command line.

\setlength{\unitlength}{0.8cm}
\begin{picture}(6,4)
\linethickness{0.075mm}
\multiput(0,0)(1,0){7}
{\line(0,1){4}}
\multiput(0,0)(0,1){5}
{\line(1,0){6}}
\thicklines
\put(0.5,0.5){\line(1,5){0.5}}
\put(1,3){\line(4,1){2}}
\qbezier(0.5,0.5)(1,3)(3,3.5)
\thinlines
\put(2.5,2){\line(2,-1){3}}
\put(5.5,0.5){\line(-1,5){0.5}}
\linethickness{1mm}
\qbezier(2.5,2)(5.5,0.5)(5,3)
\thinlines
\qbezier(4,2)(4,3)(3,3)
\qbezier(3,3)(2,3)(2,2)
\qbezier(2,2)(2,1)(3,1)
\qbezier(3,1)(4,1)(4,2)
\end{picture}

Template:BookCat

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 Template:LaTeX/LaTeX command on horizontal or vertical lines, and of the Template:LaTeX/LaTeX and the Template:LaTeX/LaTeX 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

\setlength{\unitlength}{1cm}
\begin{picture}(4.3,3.6)(-2.5,-0.25)
\put(-2,0){\vector(1,0){4.4}}
\put(2.45,-.05){$x$}
\put(0,0){\vector(0,1){3.2}}
\put(0,3.35){\makebox(0,0){$y$}}
\qbezier(0.0,0.0)(1.2384,0.0)
(2.0,2.7622)
\qbezier(0.0,0.0)(-1.2384,0.0)
(-2.0,2.7622)
\linethickness{.075mm}
\multiput(-2,0)(1,0){5}
{\line(0,1){3}}
\multiput(-2,0)(0,1){4}
{\line(1,0){4}}
\linethickness{.2mm}
\put( .3,.12763){\line(1,0){.4}}
\put(.5,-.07237){\line(0,1){.4}}
\put(-.7,.12763){\line(1,0){.4}}
\put(-.5,-.07237){\line(0,1){.4}}
\put(.8,.54308){\line(1,0){.4}}
\put(1,.34308){\line(0,1){.4}}
\put(-1.2,.54308){\line(1,0){.4}}
\put(-1,.34308){\line(0,1){.4}}
\put(1.3,1.35241){\line(1,0){.4}}
\put(1.5,1.15241){\line(0,1){.4}}
\put(-1.7,1.35241){\line(1,0){.4}}
\put(-1.5,1.15241){\line(0,1){.4}}
\put(-2.5,-0.25){\circle*{0.2}}
\end{picture}

Template:BookCat

In this figure, each symmetric half of the catenary y=coshx1 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 Template:LaTeX/LaTeX command. The picture is defined in a convenient "mathematical" coordinates, whereas by the command

Template:LaTeX/Usage

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

Plotting graphs

\setlength{\unitlength}{1cm}
\begin{picture}(6,6)(-3,-3)
\put(-1.5,0){\vector(1,0){3}}
\put(2.7,-0.1){$\chi$}
\put(0,-1.5){\vector(0,1){3}}
\multiput(-2.5,1)(0.4,0){13}
{\line(1,0){0.2}}
\multiput(-2.5,-1)(0.4,0){13}
{\line(1,0){0.2}}
\put(0.2,1.4)
{$\beta=v/c=\tanh\chi$}
\qbezier(0,0)(0.8853,0.8853)
(2,0.9640)
\qbezier(0,0)(-0.8853,-0.8853)
(-2,-0.9640)
\put(-3,-2){\circle*{0.2}}
\end{picture}

Template:BookCat


The control points of the two Bézier curves were calculated with formulas (*). The positive branch is determined by P1=(0,0), m1=1 and P2=(2,tanh2), m2=1/cosh22. 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 Template:LaTeX/Environment 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 Template:LaTeX/LaTeX 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 [1] for a tutorial.


Template:LaTeX/Bottom


sr:LaTeX/Слика