Go to the first, previous, next, last section, table of contents.


3. Drawing Segments and Scaling

TeXdraw provides individually scaled segments which can be used to create relocatable drawing modules.

3.1 Drawing segments

A TeXdraw drawing segment allows for local modifications of parameters and relative positioning. A TeXdraw segment is delimited by a \bsegment command and an \esegment command. Inside the segment, the initial current position is (0 0). Any changes to parameters such as the gray level and the line width, remain local to the segment. Segments are implemented in TeX using a \begingroup and \endgroup. Segments can be nested.

\bsegment
Start a drawing segment. The coordinate system is shifted such that the current position corresponds to the coordinate (0 0). Changes to scaling, position and line parameters stay local to the drawing segment.
\esegment
End a drawing segment. The current position in effect before the corresponding \bsegment command is restored. The scaling and line parameter values revert to those in effect before the corresponding \bsegment command was invoked.

3.2 Drawing paths

Certain subtle interactions occur between drawing segments and fill operations. In PostScript, lines are drawn by first defining a path, then later stroking the path to draw the line. In TeXdraw, this stroking occurs when the line is terminated, say by a \move command. PostScript paths are interrupted by, but continue after a drawing segment. This means that a path started before a segment may not be stroked (drawn) until after the segment ends. Consider the following example.

\move (0 0)
\lvec (1 1)
\bsegment
  \move (-0.25 -0.25)
  \fcir f:0.8 r:0.5
\esegment
\move (0 0)

A PostScript path is started at (0 0) and continues with a line to (1 1). This path is interrupted by the segment. The filled circle is drawn next. After the segment, the path continues and is not stroked until the \move (0 0) command after the end of the segment. This means that the line appears on top of the filled region.

If the fill operation is to cover the line, the path must be stroked before the fill operation. From TeXdraw, the move commands \move and \rmove, and the end TeXdraw command \etexdraw terminate a path and cause it to be stroked. Within a segment, the end segment command \esegment also terminates and strokes a path. In the example above, the line can be stroked by inserting a move command (such as a \rmove (0 0) which does not affect the position), before the start of the segment.

3.3 Saving positions

The \savecurrpos command saves the current position. The saved position is an absolute position, not one relative to a segment. The position saving mechanism is global; the position can be saved within a nested segment and then used outside of the segment. The x and y coordinates of the position are saved separately as named coordinates. The names are of the form *name, with the leading * being obligatory. A companion command, \savepos, saves a given coordinate (relative to the current segment) as an absolute symbolic position.

\savecurrpos (*px *py)
Save the current position as the absolute position referenced by (*px *py).
\savepos (x y)(*px *py)
Save the coordinate position (x y) as the absolute position referenced by (*px *py). The coordinate (x y) is interpreted in the normal fashion as a coordinate relative to the current segment, using the current scaling factors and drawing unit.

The symbolic names used to specify a saved position can consist of any characters that are not special to TeX, but must start with a * character. The symbolic names can be used as the x and/or y coordinate in any command that needs a coordinate. Symbolic coordinates are not normally used with relative motion commands such as \rlvec or \rmove. If used with relative motion, the corresponding displacement is equal to the symbolic coordinate value.

On exit from a segment, the position and graphics state on entry is restored. Any changes to line types, scaling and position are discarded. However, it is sometimes useful alter the position on exit from a segment. The \savepos command allows for the saving of a position within the segment. This position can be restored after the \esegment with a \move command using the saved symbolic position. This approach can be used to build modules which operate in a manner analogous to the basic relative motion line vector commands.

The following example defines a macro which draws a box 0.75 inches wide by 0.5 inches high containing centered text. On leaving the macro the position will be set at a point on the righthand side of the box.

\def\tbox #1{\bsegment
               \lvec (0 +0.25)    \lvec (0.75 +0.25)
               \lvec (0.75 -0.25) \lvec (0 -0.25) \lvec (0 0)
               \textref h:C v:C \htext (0.375 0){#1}
               \savepos (0.75 0)(*ex *ey)
             \esegment
             \move (*ex *ey)}

With this definition, we can treat \tbox in the same way as the basic vector commands, stringing them together to form a block diagram as in this example.

\centertexdraw{
  \ravec (1 0) \tbox{$H(z)$} \ravec (1 0)
}

3.4 Scaling coordinates

There are two scale factors available, the unit scale factor and the segment scale factor. The overall scale factor is the product of these two. There are absolute and relative versions of commands to change these scale factors.

The unit scale factor is normally used to affect global scale changes. Changes to the unit scale factor remains local to a segment, but propagate to inferior segments. The default value is unity.

The segment scale factor is used for local scale changes. It remains local to a segment. The segment scale factor is reset to unity on entry into each segment. This means that changes to the segment scale factor do not propagate to inferior segments.

\setunitscale scale
Set the unit scaling to scale. The argument scale is a real number which is used to scale coordinate values. The overall scaling factor is the product of the unit scale factor and the segment scale factor.
\relunitscale value
Adjust the unit scale factor by multiplying by value. This has the effect of multiplying the overall scale factor by the same factor. The overall scaling factor is the product of the unit scale factor and the segment scale factor.
\setsegscale scale
Set the segment scale factor. The argument scale is a real number which is used to scale coordinate values. The overall scale factor is the product of the unit scale factor and the segment scale factor.
\relsegscale value
Adjust the segment scale factor by multiplying by value. This has the effect of multiplying the current overall scale factor by the same factor. The overall scaling factor is the product of the unit scale factor and the segment scale factor.

In addition to the unit scale factor and the segment scale factor, the scaling can be controlled by the choice of drawing units with the command \drawdim (see section 2.4 Coordinate specification).

\drawdim cm \setunitscale 2.54
Set the units to centimetres scaled by 2.54. Together these commands are effectively the same as \drawdim in.

The segment scale can be used to allow scale changes in segments so that values are in more convenient units. For example suppose dimensions in a segment are multiples of one third of an inch. The segment scale can be set once to make 1 drawing unit equal 0.3333 inches. From that point on, coordinates can be specified with integer values.

The following example defines a macro to draw a rectangular box which is twice as wide as it is high. The width is specified as an argument.

\def\mybox #1{\bsegment
                \setsegscale #1
                \lvec (0 +0.25) \lvec (1 +0.25) \lvec (1 -0.25)
                \lvec (0 -0.25) \lvec (0 0)
              \esegment}

3.5 Drawing size

The effective size of the drawing is determined by the maximum excursions of the coordinates supplied to TeXdraw commands. The minimum and maximum scaled x and y coordinates are tallied. Note that \move commands contribute to the determination of the calculated size of the drawing, even though they do not generate visible lines. The circle and ellipse commands add a compensation for the radii of circles and ellipses. The final TeXdraw drawing is placed in a TeX box with lower lefthand corner corresponding to (x-min y-min) and upper righthand corner at (x-max y-max).

Text generated by \htext, \vtext or \rtext can spill outside the box as determined above. Only the text reference point is guaranteed to be in the drawing box. Arcs can also spill outside the drawing box. Note also that the widths of lines, and the sizes of arrowheads do not affect the size of the drawing. The calculated size of the drawing will never be larger than the actual size of the drawing. In extreme cases in which text or lines extend far outside the drawing, extra \move commands should be used to establish the size of the drawing so that the TeXdraw box includes all of the drawing.

TeXdraw provides the \drawbb command to draw a box which indicates the effective size of the drawing. Whenever \drawbb is invoked, a ruled box is drawn around the drawing as it has been sized up to that point. Normally \drawbb is invoked just before the end of a drawing to indicate the effective size of the final drawing.

\drawbb
Draw a ruled box around the effective size of a drawing produced by TeXdraw commands.

3.6 Initial current position

The first operation in a drawing should be a move to establish the current position. The current position can be established explicitly through a \move command or a text positioning command such as \htext with a coordinate. However, if an attempt is made to use a drawing command which needs a current position and none has been established, TeXdraw implicitly sets the initial current position to (0 0). The size of the TeXdraw figure is normally determined from the sequence of coordinates specified, but will include the implicit initial position in case another initial position has not been explicitly specified.


Go to the first, previous, next, last section, table of contents.