Stanford Lecture: The Internal Details of TeX82 - Session 11 (July 30, 1982)

Unknown AuthorAbout 9 min readNov 27, 2025Watch original
THE SUMMARYAI-generated

Key Concepts

  • DVI File: Device Independent file, a format for representing typeset documents that can be used on various output devices.
  • Opcode: Operation code, a command within the DVI file that instructs the interpreter on what action to perform.
  • Operand: Data that follows an opcode, providing specific values or parameters for the operation.
  • RSU (Ridiculously Small Unit): An early unit of measurement in DVI files, designed to minimize rounding errors.
  • Scaled Points: The primary unit of measurement used internally by TeX and in DVI files, with a defined conversion to metric units.
  • Font Register (f): Stores the current font being used for typesetting.
  • Current Position (h, v): Horizontal and vertical coordinates on the page, with (0,0) at the upper-left corner.
  • Stack Registers (w, x, y, z): Used for storing and recalling spacing values to save space in the DVI file.
  • Stack: A data structure that stores the values of h, v, w, x, y, and z, allowing for saving and restoring states.
  • Set Character Commands: Opcodes that typeset a specific character from the current font and advance the horizontal position.
  • Set Rule Command: An opcode that draws a rectangular rule of specified height and width.
  • Put Commands: Similar to set commands but do not advance the horizontal position.
  • No Op (NOP): An opcode that does nothing, used for padding or future expansion.
  • Beginning of Page (BOP): An opcode that marks the start of a new page, initializing registers and providing page identification.
  • End of Page (EOP): An opcode that marks the end of a page, often involving outputting accumulated data.
  • Push/Pop: Opcodes that save and restore the state of the current registers (h, v, w, x, y, z) to the stack.
  • Motion Commands (right, down): Opcodes that move the current position horizontally or vertically by a specified amount.
  • Font Change Commands (fntnum, fnt1-fnt4): Opcodes that change the current font.
  • XXX Command: An opcode for extensions, allowing arbitrary ASCII text to be embedded in the DVI file.
  • Postamble: A section at the end of the DVI file containing crucial metadata like the pointer to the final BOP, unit conversion factors, magnification, page dimensions, stack depth, and font definitions.
  • Font Definitions: Information about each font used in the document, including its internal number, checksum, scale factor, area, and name.
  • Signature Bytes: A sequence of bytes (223 in hex) at the end of the DVI file used for identification and locating the postamble.
  • Magnification: A parameter that scales the entire document, applied globally.
  • DVI Type Program: A utility to list and interpret the contents of a DVI file.

DVI Files: Device Independent Output

This session focuses on DVI (Device Independent) files, which are the output of TeX and represent the core of its typesetting capabilities. While TeX considers its job done upon generating a DVI file, this file is the starting point for subsequent processing, such as conversion for printing or display on various devices. The primary goal of DVI is to enable TeX output to be usable across a wide range of devices.

Evolution of DVI Format

The DVI format has undergone several revisions:

  • Version 0: The initial proposal.
  • Version 1: Introduced changes in byte packing within words, particularly to accommodate different machine architectures (e.g., 36-bit machines) and file transfer protocols.
  • Version 2 (Current Convention): Similar to Version 1 but with added opcodes and renumbered existing ones. This version aims for a cleaner, more complete, and symmetrical structure.

Structure and Content of a DVI File

A DVI file is a packed sequence of 8-bit bytes (0-255). It functions as an interpretive routine, defining a hypothetical computer whose central processing unit is the DVI interpreter. This interpreter executes a series of opcodes and their associated operands.

Key Registers in the DVI Machine:

  • Font Register (f): An integer storing the current font number. Font numbers can be up to 32-bit, allowing for a vast number of fonts, though TeX itself typically uses a smaller subset.
  • Current Position (h, v): Horizontal and vertical coordinates on the page, analogous to Cartesian coordinates but with the y-axis inverted (increasing 'v' moves down). The origin (0,0) is at the upper-left corner.
  • Spacing Registers (w, x, y, z): Four registers used to store spacing values. 'w' and 'x' are for horizontal spacing, and 'y' and 'z' for vertical spacing. These are used to save space by avoiding repeated explicit spacing values.
  • Stack: A built-in data structure that stores the values of h, v, w, x, y, and z. The push command saves these values onto the stack, and pop restores them, allowing for efficient backtracking and state management. The current font (f) is not pushed or popped.

Units of Measurement:

  • RSU (Ridiculously Small Unit): An early unit designed to be so small that rounding errors would be negligible.
  • Scaled Points: The current standard unit. The DVI file includes a conversion factor (numerator and denominator) at the end to translate these units into a metric system (e.g., 10⁻⁷ or 10⁻⁸ meters). TeX sets these values so that its internal scaled points correspond to the DVI units.
  • Pixel Conversion: DVI units need to be converted to pixels for display on a device. The recommended approach is to perform most arithmetic in DVI units and round to pixels only at critical points.

DVI Opcodes and Commands

The DVI file is a sequence of opcodes, each followed by a specific number of operands.

Character Setting Commands:

  • set_char (0-127): Typesets characters 0 through 127 from the current font. This command also advances the horizontal position 'h' by the width of the character.
  • set1 (128): Sets character 128, followed by a 1-byte character code.
  • set2 (129): Sets character 129, followed by a 2-byte character code.
  • set3 (130): Sets character 130, followed by a 3-byte character code.
  • set4 (131): Sets character 131, followed by a 4-byte character code.
    • These set commands advance 'h' by the character's width. DVI readers need access to character width information, often provided in font definition files or pixel files.

Rule Drawing Command:

  • set_rule (132): Draws a rectangular rule. It is followed by two 4-byte signed operands: 'a' (height) and 'b' (width).
    • If height or width is zero or negative, the rule is effectively invisible, but 'h' is still advanced by 'b'.
    • This command is crucial for ensuring perfect alignment of graphical elements, such as the top of a radical sign with a rule.

Character Placement Commands:

  • put1 (133) to put4: Similar to set commands but do not advance the horizontal position 'h'. This is useful for stacking characters or assembling extensible characters.

Miscellaneous Commands:

  • nop (138): No operation. Does nothing and is not generated by TeX but can be inserted between commands.
  • bop (Beginning of Page): Marks the start of a page. It's a long command followed by 11 four-byte parameters (C0-C9, and 'p').
    • C0-C9 are cross-reference values for page identification. TeX outputs the values of counters 0-9 for this purpose.
    • 'p' is a pointer to the previous bop in the file, allowing backward traversal.
    • A bop initializes all registers to 0, sets the current font to undefined, and empties the stack.
  • eop (End of Page): Marks the end of a page. This is where processing for the page typically concludes, and data might be outputted or sorted. The stack should be empty at eop.
  • push: Saves the current values of h, v, w, x, y, and z onto the stack.
  • pop: Restores values from the stack into h, v, w, x, y, and z. This is a fast way to return to a previous state.

Motion Commands:

  • right1 to right4: Move the horizontal position 'h' by a signed amount 'b'. These commands also set the 'w' register to 'b'.
    • right1 uses a 1-byte signed operand (-128 to 127).
    • right2, right3, right4 use 2, 3, and 4-byte operands respectively.
  • w0 to w4: Similar to right commands but use the value in the 'w' register. w0 sets 'h' to 'h + w'. TeX aims to use these one-byte commands efficiently by learning common spacing values.
  • x0 to x4: Similar to w commands but use the 'x' register. TeX 82 has an algorithm to assign spacing to 'w' and 'x' registers.
  • down1 to down4: Move the vertical position 'v' by a signed amount.
  • y0 to y4: Similar to down commands but use the 'y' register.
  • z0 to z4: Similar to down commands but use the 'z' register.

Font Change Commands:

  • fntnum (171-234): Sets the font register 'f' to a value from 0 to 63.
  • fnt1 to fnt4 (235-238): Set the font register 'f' using 1, 2, 3, or 4-byte font numbers. fnt1 is followed by a 1-byte font specification. Font number -1 is disallowed.

Extension Command:

  • xxx (239): Used for extensions to TeX. When xsend is used in TeX, it's expanded and the resulting characters are placed after an xxx command in the DVI file. It's followed by a 1-byte length indicator and then the ASCII text. Currently limited to 255 bytes, with discussions about extending this.

Postamble (240):

  • This section appears at the very end of the DVI file and contains critical metadata. It is read first by DVI processing software.
  • Pointer to Final BOP: A 4-byte pointer to the last bop command in the file.
  • Numerator and Denominator: Define the DVI units in relation to a metric system.
  • Magnification Parameter (m): A global scaling factor for the entire document. The effective scaling is m * numerator / denominator.
  • Tallest Page Dimensions (l, u): The height ('l') and width ('u') of the largest page box. These are used for layout purposes, though they don't necessarily indicate the exact position of all characters.
  • Maximum Stack Depth (s): The maximum depth the stack reaches in the file.
  • Total Number of Pages (t): The count of bop commands.
  • Font Definitions: For each font used, its internal number, checksum, extra scale factor, font area, and font name (as an ASCII string) are provided. A -1 indicates the end of font definitions.
  • Pointer to Postamble: A 4-byte pointer to the postamble command itself.
  • Identification Byte (2): Indicates DVI Version 2.
  • Signature Bytes (223): A sequence of bytes (hex df) that act as a signature for the DVI file, helping to locate the end of the file.

Example: Generating and Interpreting a DVI File

The speaker demonstrates creating a simple DVI file named TeXput.dvi by running TeX with a minimal input: defining a font (CMR 10) and printing "hello".

The DVI type program is then used to interpret TeXput.dvi. The output shows:

  • The selected options, including magnification (2000).
  • The postamble starting at byte 68.
  • The numerator, denominator, and magnification.
  • The start of page 0.0.
  • Vertical movement (down 3) and a warning about exceeding maximum 'v'.
  • A push operation and the initial state of registers.
  • Font selection (fontnum3 for CMR10).
  • Setting character 72 ('H') for "hello".
  • A set_rule command for an overflow box, resulting in a 14x10 point rule (magnified twice).
  • A pop operation and the end of the page.

The example highlights how DVI commands translate into visual output and how the DVI file contains detailed instructions for rendering the document. The discussion also touches upon the importance of consistency between TeX and MetaFont for precise rendering, especially for rules and character alignment.

AI summaries can miss context or contain errors. Check important details against the original video.

MAKE IT YOURS

Read. Remember. Reuse.

Free tools

Go a little deeper.

Have a question about this video? Load its transcript to open the video chat.