gauss.sty – A Package for Typesetting Matrix Operations

Manuel Kauers

October 11, 2002

Abstract This package provides L^{A}TEX-macros for typesetting operations on a

matrix. By an “operation on a matrix” we understand a row operation or a column operation.

The user interface of the package is very straightforward and easy to understand while the results look quite pretty.

Contents

1 Usage 1 1.1 How to typeset matrix operations . . . . . . . . . . . . . . . . 2 1.2 Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 1.3 Adapting the package . . . . . . . . . . . . . . . . . . . . . . 5

1.3.1 Distances and dimensions . . . . . . . . . . . . . . . . 5 1.3.2 Labels . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 1.3.3 Matrix delimiters . . . . . . . . . . . . . . . . . . . . . 8

1.4 Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 1.5 Trap doors and hints . . . . . . . . . . . . . . . . . . . . . . . 9 1.6 Bug parade . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

2 Implementation 10 2.1 Allocation of registers and definition of common macros … 10 2.2 Converting floasts and lengths to each other . . . . . . . . . . 14 2.3 Macros for calculus on floats . . . . . . . . . . . . . . . . . . . 15 2.4 Macros for measurements . . . . . . . . . . . . . . . . . . . . 18 2.5 Macros for drawing purposes . . . . . . . . . . . . . . . . . . 21 2.6 Generic operation commands . . . . . . . . . . . . . . . . . . 24 2.7 The gmatrix environment . . . . . . . . . . . . . . . . . . . . 29 2.8 Public tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31

1 Usage

If you find yourself in search of a package that enables you to easily typeset constructions like

|^{↓ ↓  \underline{· 1} ↓^{0}  ^{+}} ; \begin{matrix}1 & 0 & 5 & 7 & 2 \\ 3 & 1 & 1 & 5 & 1 \\ 1 & 0 & − 7 & 1 & 4 \\ 4 & 3 & 6 & 5 & 4 \\ 1 & 7 & 9 & 4 & 3 \\ 0 & 0 & 8 & 0 & − 1 \end{matrix}| ⟵^{−3}^{+} ⟵^{−1}^{+} ⟵ ⟵ | · 0⟵_{x^{2}−1}^{+}  = |\begin{matrix}0 & 1 & 5 & 7 & 2 \\ 1 & 0 & − 14 & − 16 & − 5 \\ 0 & 0 & − 12 & − 6 & 2 \\ 7 & 1 & 9 & 4 & 3 \\ 3 & 4 & 6 & 5 & 4 \\ 0 & 0 & 0 & 0 & 0 \end{matrix}| ,

then this package is what you need. It defines a new matrix environment which is extended by comprehensive macros for typesetting so-called “operations” on the matrix. An operation is either a row operation or a column operation, and may involve one or two lines. Examples of such operations arise in the context of Gaussian elimination for solving systems of linear equations in linear algebra: swaping rows, adding the multiple of one row to another, and multiply a row by a constant factor.

1.1 How to typeset matrix operations

gmatrix The package defines a new matrix environment gmatrix which behaves just like L^{A}TEX’s and AMSL^{A}TEX’s matrix. It takes an optional parameter ⟨delimtype⟩ to select the matrix delimiters. So, gmatrix[p] corresponds

to pmatrix, gmatrix[v] to vmatrix, and so on.

The gmatrix environment can be used exaclty like its brothers and sisters defined by L^{A}TEX and AMSL^{A}TEX, for instance:

   \begin{gmatrix}[p]
    a & b \\
    c & d
   \end{gmatrix}

(\begin{matrix}a & b \\ c & d \end{matrix})

The content of the gmatrix environment consists of three parts: matrix, row operations, and column operations. The latter two are optional parts, and the ordering of them is arbitrary (i.e. row operations may be stated before column operations and vice versa). The matrix part is required, and it must be the first one.

\rowops \colops To skip to the next section, there are two comands \rowops which swiches to the row operation section, and \colops which switches to the column operation section.

\mult \add \swap Within the operation sections, you have to state the sequence of operations which are to be typeset. There are the three commands \mult, \add, and \swap to do this. These commands are specified as follows:

1. \mult{i}{\cdot b} typesets the operation “multiply the ith row (or column) by b”,

2. \swap[a][b]{i}{j} typesets the operation “swap the ith and the jth row (or column)”. a and b are labels to typeset at the end of the arrows, similar to the ·b of the \mult command. The command does nothing if i = j.

3. \add[a][b]{i}{j} typesets the operation “add the a-fold of row (or column) i to row (or column) j. b is a label for the jth line. The command does nothing if i = j.

In the standard implementation, optional arguments of \swap and the second optional argument of \add are ignored. See Section 1.3 for how to enable them.

Rows are counted top-down, and columns are counted from left to right. The uppermost row and the leftmost column have the index 0. There is also the posibility to use * as index which causes the typesetting of several operations where * runs over all indices. For example, \mult{*}{5} in the \rowops section of a n × n matrix is equivalent to

state \mult{0}{5},. . . ,\mult{n − 1}{5}.

1.2 Examples

• A matrix with row operations

     \begin{gmatrix}[p]
      1 & 2 & 3 \\
      4 & 5 & 6 \\
      7 & 8 & 9
     \rowops
      \swap{0}{1}
      \mult{0}{\cdot 7}
      \add[5]{1}{2}
     \end{gmatrix}

(\begin{matrix}1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \end{matrix}) ⟵ ⟵ | · 7 ⟵^{5}^{+}

• The same operations in an other ordering

\begin{gmatrix}[p]
1 & 2 & 3 \\
4 & 5 & 6 \\
7 & 8 & 9
\rowops
\add[5]{1}{2}
\swap{0}{1}
\mult{0}{\cdot 7}
\end{gmatrix}

(\begin{matrix}1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \end{matrix}) ⟵^{5}^{+} ⟵ ⟵ | · 7

• A matrix with column operations

\begin{gmatrix}[p]
1 & 2 & 3 \\
4 & 5 & 6 \\
7 & 8 & 9
\colops
\swap{0}{1}
\mult{0}{\cdot 7}
\add[5]{1}{2}
\end{gmatrix}

(^{↓ ↓ \underline{· 7} ↓^{5} ^{+}} ; 1 2 3 4 5 6 7 8 9)

• A matrix with both row and column operations

\begin{gmatrix}[v]
1 & 2 & 3 \\
4 & 5 & 6 \\
7 & 8 & 9
\rowops
\swap{1}{2}
\mult{2}{\cdot 3}
\add[-5]{1}{0}
\add[-3]{1}{2}
\colops
\swap{0}{1}
\mult{0}{\cdot 7}
\add[5]{1}{2}
\end{gmatrix}

|^{↓ ↓ \underline{· 7} ↓^{5} ^{+}} ; 1 2 3 4 5 6 7 8 9| ⟵ ⟵ | · 3 ⟵^{−5}^{+} ⟵^{−3}^{+}

• Multiple operations using the * index

\begin{gmatrix}[p]
 1&2&3&4\\
 5&6&7&8\\
 9&10&11&12\\
 13&14&15&16
\rowops
 \add[x]{0}{*}
\end{gmatrix}

(\begin{matrix}1 & 2 & 3 & 4 \\ 5 & 6 & 7 & 8 \\ 9 & 10 & 11 & 12 \\ 13 & 14 & 15 & 16 \end{matrix}) ⟵^{x}^{+} ⟵^{x}^{+} ⟵^{x}^{+}

Note that the first row is not added to itself, because \add[x]{0}{0} has no effect. You can also use two stars:

\begin{gmatrix}[p]
 1&2&3\\
 4&5&6\\
 7&8&9
\rowops
 \add{*}{*}
\end{gmatrix}

(\begin{matrix}1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \end{matrix}) ⟵^{+} ⟵^{+} ⟵^{+} ⟵^{+} ⟵^{+} ⟵^{+}

• The package clearly also handels a matrix with larger entries correctly:

(\begin{matrix}a & b & c & d & e \\ 0 & 0 & ∫_{a}^{b} f(x) dx & 0 & 0 \\ a & b & c & d & e \end{matrix}) | : ∫_{a}^{b} f(x) dx ⟵^{−c}^{+} ⟵^{−1}^{+}

Even nested gmatrixes are possible:

| \begin{matrix}(\begin{matrix}2 & 2 \\ 3 & 3 \end{matrix}) ⟵^{−3/2}^{+} & (\begin{matrix}1 & 2 \\ 3 & 4 \end{matrix}) \overleftarrow{⟵} & (\begin{matrix}a & b \\ c & d \end{matrix}) ⟵^{−c/a}^{+} \\ (\begin{matrix}0 & 1 \\ 1 & 0 \end{matrix}) | · π & (\begin{matrix}v & w \\ x & y \end{matrix}) \overleftarrow{⟵} & (\begin{matrix}1 & x \\ x & x^{2} \end{matrix}) \overleftarrow{⟵} \\ (\begin{matrix}1 & 2 \\ 3 & 4 \end{matrix}) | · 5 & (\begin{matrix}· & · \\ · & · \end{matrix} ) ⟵^{−·/·}^{+} & (\begin{matrix}5 & 4 \\ 3 & 2 \end{matrix}) | · 1 \end{matrix} | ⟵^{π^{2}/6}^{+} | · 42 ⟵ ⟵

1.3 Adapting the package

1.3.1 Distances and dimensions

The appearance of the operation lines and arrows depends strongly on the values of the dimension parameters described in this section.

\rowarrowsep \colarrowsep \rowarrowsep denotes the distance from the matrix to the operations. For

example, \rowarrowsep=10pt yields

(\begin{matrix}a & b & c \\ d & e & f \\ g & h & i \end{matrix}) ⟵^{x}^{+} ⟵^{y}^{+} | · y ⟵ ⟵

and \rowarrowsep=50pt yiels

(\begin{matrix}a & b & c \\ d & e & f \\ g & h & i \end{matrix})  ⟵^{x}^{+} ⟵^{y}^{+} | · y ⟵ ⟵

The corresponding dimension for column operations is \colarrowsep.

\opskip \opskip is the distance between two consecutive operations. For example,

\opskip=6pt yields

(\begin{matrix}a & b & c \\ d & e & f \\ g & h & i \end{matrix}) ⟵^{x}^{+} ⟵^{y}^{+} | · y ⟵ ⟵

and \opskip=30pt yields

(\begin{matrix}a & b & c \\ d & e & f \\ g & h & i \end{matrix}) ⟵^{x}^{+} ⟵^{y}^{+}  | · y ⟵ ⟵

The \opskip length is responsible for both row and column operations.

\labelskip \labelskip is the distance between an operation arrow and its labels. For

example, \labelskip=3pt yields

(\begin{matrix}a & b & c \\ d & e & f \\ g & h & i \end{matrix}) ⟵^{x}^{+} ⟵^{y}^{+} | · y ⟵ ⟵

and \labelskip=15pt yields

(\begin{matrix}a & b & c \\ d & e & f \\ g & h & i \end{matrix}) ⟵ ^{x}^{+} ⟵ ^{y}^{+} | · y ⟵ ⟵

The \labelskip length is responsible for both row and column operations.

\rowopminsize \colopminsize The length \rowopminsize is the minimum amount of a horizontal operation segment to go to the right. For example, \rowopminsize=3pt yields

(\begin{matrix}a & b & c \\ d & e & f \\ g & h & i \end{matrix}) ⟵^{x}^{+} ⟵^{y}^{+} | · y ⟵ ⟵

If the horizontal segment ends with an arrow tip and \rowopminsize is less than the width of \leftarrow, then the width of \leftarrow is taken. In the above example, this is the case in the \add[x]{0}{1} operation. An example for an exact use of a small value of \rowopminsize is the upper horizontal line of \add[y]{1}{2}. For comparation, \rowopminsize=30pt yields

(\begin{matrix}a & b & c \\ d & e & f \\ g & h & i \end{matrix}) ⟵^{x}^{+} ⟵^{y}^{+} | · y ⟵ ⟵

The corresponding value for column operations is \colopminsize.

1.3.2 Labels

The typesetting of a label can be changed by redefining the macros which are responsible for label typesetting. Each label parameter of \mult, \add, and \swap is passed to special “fontifier” macros which take one argument and fontify it according to the semantical requirements. Here is a list of those fontifier macros and their default definitions:

\rowmultlabel  \rowmultlabel is the label of a \mult operation in the \rowops section. Its

default definition is {|\,#1}.

\colmultlabel \colmultlabel is the respective macro for the \colops section. It is defined to

   \underline{\hbox to 1.2em{$\hss\mathstrut{}#1\hss$}}

by default.

\rowswapfromlabel  \rowswapfromlabel is the label of a \swap operation in the \rowops section

which is to place at the first of the two rows. It is defaultly defined to {}, i.e. the label parameter is ignored.

\colswapfromlabel  \colswapfromlabel is the respective macro for the \colops section which

is also empty by default.

\rowswaptolabel  \rowswaptolabel is like \rowswapfromlabel, but for the other row. It is

empy by default.

\colswaptolabel  \colswaptolabel is \rowswaptolabel’s brother for the \colops section.

\rowaddfromlabel \rowaddfromlabel is the macro for the label of the from-line of an \add command. It is defined to {\scriptstyle#1} by default.

\coladdfromlabel \coladdfromlabel is respective macro for the column operations.

\rowaddtolabel \rowaddtolabel fontifies the label of the to-line of an \add command. This macro is defined to {\scriptscriptstyle +} by default, i.e. it ignores the parameter.

\coladdtolabel \coladdtolabel is the respective command for the column operation. It behaves likewise.

For the following example, all of the above labels were defined to {#1}, i.e. to identity.

   \begin{gmatrix}[p]
    a & b & c \\
    d & e & f \\
    g & h & i
   \colops
    \mult0{m}
    \add[af][at]01
    \swap[sf][st]02
   \rowops
    \mult0{m}
    \add[af][at]01
    \swap[sf][st]02
   \end{gmatrix}

(^{m ↓ af at ↓  ↓ sf  st} ; \begin{matrix}a & b & c \\ d & e & f \\ g & h & i \end{matrix}) m ⟵af at ⟵ ⟵sf st

1.3.3 Matrix delimiters

\newmatrix It is possible to define new delimiter specifiers to gmatrix, say gmatrix[X], by defining a matrix environment Xmatrix. A definition of Xmatrix which fulfills the requirements needed for compatibility with gmatrix is provided automatically by the call of

\newmatrix{⟨left-delim⟩}{⟨right-delim⟩}{X},

which defines the environment Xmatrix. The arguments ⟨left-delim⟩ and ⟨right-delim⟩ need to be compatible to the \left-\right mechanism of TEX. As soon as Xmatrix exists, it is also possible to use X as optional argument

to gmatrix.

By convention, the suffix is one single character. If you try to enter g@ or the empty string as suffix, nothing is done, otherwise, the definition works as well.

1.4 Features

• You need not care about the width or height of some macro cells, operations are always aligned well, i.e. centered to the column or row.

• Operation elements will not intersect each other, unless you give some very huge labels.

• There is no restriction to the order of operation commands, so you can choose an arbitrary order to achive the best typographic result.

• If no operations are given, the result is exactly the result of the AMS-TEX matrix environment.

• Unlike AMS’s matrix environment, there is no limit to the matrix’ size in our reimplementation gmatrix.

• Nested gmatrix’s are possible.

1.5 Trap doors and hints

• The last row must not end with an \\, but each other line should end with \\.

• The last row is used internally to measure the column’s widths. Therefore, if you want to point to a column i, then the last row must have at least i + 1 entries.

• In row operations, the package considers the width of labels (that is, the width of factors in \mult and \add). But you have to take care that your labels are not higher than the corresponding line, otherwise they may intersect with other arrows or labels.

• analogously for column operations.

• The package should also run without the amsmath package, but if you use that package (which is assumed to be the usual situation), you have to load gauss after amsmath.

1.6 Bug parade

A list of submitted bugs and suggested work-arounds or fixes. If you face any bug that is not in the list below, feel free to contact me at

manuel@kauers.de.

• Hans Frederik Nordhaug faced problems with versions of AMS-L^{A}TEX that don’t define *matrix environments as expected (e.g. version 2.13). The current version of gauss therefore redefines all those environments using our \newmatrix tool, and requires amsmath to be loaded prior to the gauss package.

• Morten Høgholm suggested the introduction of fontifying macros and the use of changeable lengths as discussed in Section 1.3. He also suggested some very fine typographic tunings.

• Herbert Voss found that a \unitlength=1pt was missing to make the behaviour of the package independent of redefinitions of \unitlength outside gmatrix.

2 Implementation

1 \ProvidesPackage{gauss}[2002/10/11]
2 \RequirePackage{amsmath}
3 \makeatletter

To avoid naming conflicts with other packages, our private control sequences all start with \g@. Permanently public are only the gmatrix environment, the fontifying macros (like \rowaddfromlabel), and the dimensions (like \opskip).

The amsmath package is not necessarily needed, but if used, it has to be loaded prior to the gauss package. This is forced by the \RequirePackage command.

2.1 Allocation of registers and definition of common macros

Boxes,…

4 \newbox\g@trash
5 \newbox\g@matrixbox

 6 \newbox\g@eastbox
 7 \newbox\g@northbox
 8 \newbox\g@label
 9 \newbox\g@b@tmp
10 \newbox\g@b@tmpa
11 \newbox\g@b@tmpb

… counters,…

12 \newcount\g@maxrow
13 \newcount\g@maxcol
14 \newcount\g@maxrow@old
15 \newcount\g@maxcol@old
16 \newcount\g@c@tmp
17 \newcount\g@c@tmpa

… and dimensions …

18 \newdimen\g@axisHeight
19 \newdimen\g@linethickness
20 \newdimen\g@tab
21 \newdimen\g@arrowht
22 \newdimen\g@arrowwd
23 \newdimen\g@d@tmp
24 \newdimen\g@d@tmpa
25 \newdimen\g@d@tmpb
26 \newdimen\g@d@tmpc
27 \newdimen\g@d@tmpd
28 \newdimen\g@d@tmpe

are allocated.

\g@for For frequent use, we define a special loop mechanism, which allowes to iterate over a given interval from a lower bound to a higher one, or reversely. The code to execute is given as the third argument of \g@for, using #1 for the iteration variable that is substituted by \the\loopCount for each value in the given bounds.

Each of the bounds is also visited. Example: The following code prints out the numbers from 1 to 37, inclusively:

   \g@for1\to37\do{#1 }

We first need some more control sequences: \g@loopContent is defined to the loop’s body when the loop is entered. \g@loopCount is the variable to increment or decrement with each iteration. \g@loopEnd marks the value at which to stop the loop, and \g@loopStep contains the direction, i.e.

\g@loopStep = −1 iff \g@loopEnd < ⟨start value⟩.
29 \def\g@loopContent#1{}

30 \newcount\g@loopCount\g@loopCount=0
31 \newcount\g@loopEnd\g@loopEnd=1
32 \newcount\g@loopStep\g@loopStep=1

The \g@loop command executes the loop initialized by \g@for. Each iteration is executed in its own group to avoid side effects and expecially to provide nested loops.

33 \def\g@loop{%
34 % base case?
35 \ifnum\g@loopCount=\g@loopEnd\else
36 % no: execute loop body
37 {\expandafter\g@loopContent\expandafter{\the\g@loopCount}}%
38 % increment or decrement the loop variable
39 \advance\g@loopCount\g@loopStep
40 % call \g@loop recursivly.
41 \g@loop
42 \fi

43 }

Finally, here is the definition of \g@for. Each value in the interval from #1 to #2, including #1 and #2 is visited exactly one time.

44 \def\g@for#1\to#2\do#3{%
45 \def\g@loopContent##1{#3}%
46 \g@loopCount=#1
47 \g@loopEnd=#2
48 \ifnum\g@loopEnd>\g@loopCount%
49 \g@loopStep=1
50 \else\g@loopStep=-1
51 \fi
52 \advance\g@loopEnd\g@loopStep % inclusive upper bound
53 \g@loop

54 }

\g@checkBounds The next tool is used by the generic operation commands to check whether or not a given index is valid. If #2 ≤ #3 ≤ #4 does not hold, a package error is thrown that tells the user what happened.

Parameter #1 contains ‘r’ or ‘c’ to denote “rows” or “columns”, respectively. This piece of information is only used within the construction of the error message.

\ifg@indexCorrect The result of \g@checkBounds is returned via \ifg@indexCorrect.
55 \newif\ifg@indexCorrect
56 \def\g@checkBounds#1#2#3#4{%
57 \g@indexCorrectfalse
58 \ifnum#2>#3%

59 \PackageError{gauss}{\g@shorterror{#1} #3<#2}{\g@longerror{#1}}
60 \else
61 \ifnum#3>#4%
62 \PackageError{gauss}{\g@shorterror{#1} #3>#4}{\g@longerror{#1}}
63 \else
64 \g@indexCorrecttrue
65 \fi
66 \fi

67 }

We skip the definitions of \g@shorterror and \g@longerror which serve to produce error messages.

\g@downarrow For drawing horizontal arrows of arbitrary length, we use the construction

   \hbox to⟨width⟩{$\leftarrowfill$}

which uses Plain-TEX’s \leftarrowfill. Unfortunately, there is no vertical correspondence to that mechanism and thus, we have construct something like this by ourselves. We will do so by reimplementing a mechanism that is used by \left and \right to construct delimiters of arbitrary height.

68 \DeclareMathSymbol{\g@downarrowSymb}{\mathord}{largesymbols}{`\y}
69 \DeclareMathSymbol{\g@vertlineSymb}{\mathord}{largesymbols}{`\?}
70 \def\g@vertline{\hbox{$\g@vertlineSymb$}\kern-\lineskip}%

After allocating the basic symbols, we define \g@downarrow by a recursion which fills up a vbox with the necessary number of \g@vertline’s and

a final \g@downarrowSymb.

The resulting vbox has exactly the height given in #1 (as TEX-length), and no depth. If #1 is less than a minimum height, then it is set to that minimum height.

71 \def\g@downarrow#1{\vbox{%
72 \vfill
73 \baselineskip\z@\relax
74 \g@d@tmpc=#1\relax
75 \ifdim \g@d@tmpc<\g@arrowht
76 \g@d@tmpc\g@arrowht\relax
77 \fi
78 \g@vlineRec
79 \kern\g@d@tmpc
80 \setbox\g@trash=\hbox{$\g@downarrowSymb$}%
81 \hbox{\raise\dp\g@trash\box\g@trash}%
82 }}
83 \def\g@vlineRec{%
84 \advance\g@d@tmpc-\g@arrowht

85 \ifdim \g@d@tmpc<\z@ \else
86 \g@vertline
87 \g@vlineRec
88 \fi

89 }

2.2 Converting floasts and lengths to each other

\g@defdim \g@defdouble \g@dim \g@double The typesetting of matrix operations is done by use of the picture environment of L^{A}TEX. The macros of that environment require plain numbers, possibly containing a decimal point. Though it is not clearly correct, we will call that data format float or double.

picture’s macros do not work if you give them dimensions as input. And since the results of measuring a matrix are necessarily dimensions, we need a mechanism to convert dimensions to floats and vice versa.

This mechanism is the topic of the current section.

In fact, we almost provide our own data structure whose values can be shown as TEX dimensions or as floats. You can “construct a new instance” of that structure either by a dimension (using \g@defdim) or by a double (using \g@defdouble). In both cases, a macro is defined to be the corresponding double value.

Given an instance of our data structure, i.e. given a double, you can get its double representation using \g@double (this just typesets the double representation), and you can store its value into a TEX dimension using

\g@dim.

Macros for manipulation on floats are defined in the following section.

We first need a macro that cuts away the “pt”. This is rather tricky because the “pt” that arises in the result of some \the⟨counter⟩ has not the catcodes as expected. We can redefine them temporarily but we have to note that constructions like \g@defdim{⟨identifier⟩}{12pt} (i.e. giving the length directly) are no longer possible, since the “pt” of a directly given length has the “normal” catcodes.

90 \edef\redo#1{\catcode`p=#1\catcode`t=#1\relax}
91 \redo{12}
92 \def\g@del#1pt{#1}
93 \redo{11}

Defining a float by a dimension. The first argument expects an idetifier (identifiers are arbitrary strings), and the second argument expects a TEX dimension register, i.e. some control sequence \cs that evaluates to “… pt” if you say \the\cs.

It is not possible to specify a double by directly give a length. Use \g@defdouble below in that case.

94 \def\g@defdim#1#2{%
95 \edef\g@defdim@arg{\the #2}%
96 \edef\g@defdim@arg{\expandafter\g@del\g@defdim@arg}%
97 \g@defdouble{#1}{\g@defdim@arg}%

98 }

And here is \g@defdouble. #1 should be an identifier and #2 should be the value to store in float #1. To avoid naming conflics with other macros, #2 is stored into a macro based on g@@ and the content of #1.

99 \def\g@defdouble#1#2{%
100 \expandafter\expandafter\expandafter\global
101 \expandafter\edef\csname g@@#1 \endcsname{#2}%

102 }

We now come to the macros for “reading” a float. These are \g@dim (to read the dimensional representation) and \g@double (for the double representation).

An error will occur if you try to read the value of a float that was not previously defined. (“Missing number, treated as zero.”)

First \g@dim: Let #1 be the identifier and #2 the TEX dimension registern to store the value of #1 in.

103 \def\g@dim#1#2{%
104 \edef\g@dim@arg{\g@double{#1}}%
105 #2=\g@dim@arg\p@\relax

106 }

And \g@double is even simpler:

107 \def\g@double#1{%
108 \csname g@@#1 \endcsname

109 }

2.3 Macros for calculus on floats

We need some macros that provide simple arithmetic calculation on floats. Those are defined now.

\g@advance Given a float f_{1}, the following macro performs f_{1} := f_{1} +f_{2} where f_{2} may be either a TEX dimension or a float: If \csnamef_{2}\encsname does not evaluate to some control sequence, it is assumed to denote a TEX dimension (e.g. 5pt,

or \the\something)
110 \def\g@advance#1#2{%
111 \g@dim{#1}{\g@d@tmpa}% f_1 := #1

112 \expandafter\ifx\csname g@@#2 \endcsname\relax
113 \g@d@tmpb=#2% f_2 := #2 (TeX dimension)
114 \else
115 \g@dim{#2}{\g@d@tmpb}% f_2 := #2 (float)
116 \fi
117 \advance\g@d@tmpa\g@d@tmpb\relax% f_1 += f_2
118 \g@defdim{#1}{\g@d@tmpa}% #1 := f_1

119 }

\g@min \g@minD Given two floats f_{1}, f_{2} and a TEX dimension d_{3}, the following macro performs d_{3} := min{f_{1}, f_{2}}.

120 \def\g@min#1#2#3{%
121 \g@dim{#1}{\g@d@tmpa}% f_1 := #1
122 \g@dim{#2}{\g@d@tmpb}% f_2 := #2
123 \ifdim \g@d@tmpa<\g@d@tmpb
124 #3=\g@d@tmpa
125 \else
126 #3=\g@d@tmpb
127 \fi
128 \relax

129 }

There is a so called D-version of the latter macro. By use of \g@min, this macro also calculates min{f_{1}, f_{2}}, but its result is translated into a double representation which is then stored in control sequence #3.

130 \def\g@minD#1#2#3{%
131 \g@min{#1}{#2}{\g@d@tmpc}%
132 \edef\g@minD@arg{\the\g@d@tmpc}%
133 \edef\g@minD@arg{\expandafter\g@del\g@minD@arg}%
134 \edef#3{\g@minD@arg}%

135 }

\g@max \g@maxD And here is are the two opposite macros of the preceeding two.

136 \def\g@max#1#2#3{%
137 \g@dim{#1}{\g@d@tmpa}%
138 \g@dim{#2}{\g@d@tmpb}%
139 \ifdim \g@d@tmpa<\g@d@tmpb
140 #3=\g@d@tmpb
141 \else
142 #3=\g@d@tmpa
143 \fi
144 \relax

145 }

146 \def\g@maxD#1#2#3{%
147 \g@max{#1}{#2}{\g@d@tmpc}%

148 \edef\g@maxD@arg{\the\g@d@tmpc}%
149 \edef\g@maxD@arg{\expandafter\g@del\g@maxD@arg}%
150 \edef#3{\g@maxD@arg}%

151 }

\g@dist \g@distD Given two floats f_{1}, f_{2} and a TEX dimension d_{3}, the following macro performs d_{3} := f_{1} − f_{2}.

152 \def\g@dist#1#2#3{%
153 \g@dim{#1}{\g@d@tmpa}% f_1 := #1
154 \g@dim{#2}{\g@d@tmpb}% f_2 := #2
155 \ifdim \g@d@tmpa<\g@d@tmpb
156 #3=\g@d@tmpb
157 \advance#3 by-\g@d@tmpa
158 \else
159 #3=\g@d@tmpa
160 \advance#3 by-\g@d@tmpb
161 \fi
162 \relax

163 }

Again, we have a D-version, where the result is given in double representation.

164 \def\g@distD#1#2#3{%
165 \g@dist{#1}{#2}{\g@d@tmpc}%
166 \edef\g@distD@arg{\the\g@d@tmpc}%
167 \edef\g@distD@arg{\expandafter\g@del\g@distD@arg}%
168 \edef#3{\g@distD@arg}%

169 }

\g@updateArea \g@update While the macros that we have seen in this section so far are mainly used for elementary drawing purposes, we now define a slightly more sophisticated macro. It is needed to update the leftmost x-values of the so-far matrix operation set (in terms of row operations). It is invoked after adding a new operation to the set.

To update a float f_{1} with respect to f_{2} is defined to execute f_{1} := max{f_{1}, f_{2}}. This is what the following macro does.

170 \def\g@update#1#2{%
171 \g@dim{#2}{\g@d@tmpe}
172 \g@dim{#1}{\g@d@tmpb}
173 \ifdim \g@d@tmpe>\g@d@tmpb
174 \g@defdim{#1}{\g@d@tmpe}%
175 \fi

176 }

The matrix dimensions are stored in floats named ⟨name⟩ + ⟨index⟩ where ⟨name⟩ spcifies the dimension (e.g. “cy” for the current y values of a column) and ⟨index⟩ is the index of the row/column to which the float’s value belongs.

Now, the following macro iterates over i ∈ {#3, …, #4} and updates all the floats with name #2 + i with respect to float #1.

177 \def\g@updateArea#1#2#3#4{\g@for#3\to#4\do{\g@update{#2##1}{#1}}}

2.4 Macros for measurements

The macros defined in this section are used to measure the dimensions of a given matrix and store the measured values into floats.

For each row i of the matrix, the y-position of the center of row i with respect to the bottom of the matrix is stored in a float named ry+i. Another float rx + i is initialized to 0. This latter value will always contain the leftmost position at which a new row operation can start without intersecting previous operations that crossed row i.

For each row j of the matrix we similarly define the values cx + i and cy + i. Note that cx + i corresponds to ry + i and cy + i corresponds to rx+i, since column operations grow vertically whereas row operations grow horizontally.

\g@measureRows We first consider row measuring. The following macro assumes that the current box is a \vbox that only contains a copy of the matrix, i.e. one \hbox per row including all the intermediate glues and kerns and whatever. You can initialize what we assume to have by saying

\vbox{\halign{… }} (typeset the matrix)

\box0=\lastbox (save the matrix)

   \vbox{\unhcopy0\g@measureRows} (measure the row’s heights)

\box0 (restore the matrix)

Caution: The following macros will not work if the matrix was not constructed with an \halign because special knowledge about the structure of \halign’s result is used.

It is assumed that \g@d@tmp initially contains the y-position of the matrix’s bottom. It is further assumed that \g@maxrow contains the total number of rows. These two counters will be modified during the recursion.

178 \def\g@measureRows{%
179 \setbox\g@trash\lastbox
180 \ifnum\g@maxrow<0% base case: this box is not part of the matrix
181 \else

182 \ifdim\ht\g@trash=0pt%
183 \advance\g@d@tmp\lastskip\unskip
184 \advance\g@d@tmp\lastkern\unkern
185 \unpenalty
186 \else
187 \advance\g@d@tmp\dp\g@trash
188 \advance\g@d@tmp\g@axisHeight
189 \g@defdim{ry\the\g@maxrow}{\g@d@tmp}%
190 \g@defdim{rx\the\g@maxrow}{\z@}%
191 \advance\g@d@tmp-\g@axisHeight
192 \advance\g@d@tmp\ht\g@trash
193 \advance\g@maxrow-1%
194 \fi
195 \g@measureRows
196 \fi

197 }

\g@measureCols In fact, the row measurement is the easier case. The measurement of column widths is more complicated by two reasons: 1. The number of columns is unknown, and 2. we will meet the cells in reverse order.

This is why column measurement is implemented in two main steps. First the width of each cell and the distance between two preceeding cells is measured and stored into temporary floats ct + ⟨index⟩ (distance) and cy + ⟨index⟩ (width), where ⟨index⟩ is counted from back to front. By the way, we count the number of columns.

In the base case of the recursion we start a second recursion that will calculate the final results out of the intermediate results and that will arange the indexing properly.

What input do we expect? It is assumed that the current box is an \hbox whose first item is an \hbox of width 100cm (to detect the base case), followed by a copy of the last row of the matrix to measure. See the definition of g@matrix to see how such a situation can be constructed.

We further assume that g@d@tmp is initialized to 0.0pt.

198 \def\g@measureCols{%
199 \setbox\g@trash\lastbox
200 \ifdim \wd\g@trash=100cm%
201 % base case. Invert the ordering and sum the dimensions.
202 \g@defdouble{ct\the\g@maxcol}{0}%
203 \g@defdouble{cy\the\g@maxcol}{0}%
204 \global\g@maxcol\g@maxcol
205 \g@c@tmp\g@maxcol
206 \advance\g@c@tmp-1%
207 \g@measureColsSucc

208 \global\advance\g@maxcol-1%
209 \else
210 \ifdim \ht\g@trash=0pt%
211 \advance\g@d@tmp\lastskip\unskip
212 \advance\g@d@tmp\lastkern\unkern
213 \unpenalty
214 \else
215 % use ct temporaryly to store the skip between
216 % colnr + 1 and colnr.
217 \g@defdim{ct\the\g@maxcol}{\g@d@tmp}%
218 \g@d@tmp\z@
219 % use cy temporaryly to store the cell's width.
220 \g@defdim{cy\the\g@maxcol}{\wd\g@trash}%
221 \advance\g@maxcol1%
222 \fi
223 \g@measureCols
224 \fi

225 }

Now, the macro for the second step of the measurement algorithm is defined. This is easier, since we now already know the total number of columns that have been measured. Roughly speaking, we sum their width’s from left to right to obtain the x-values of the horizontal center of each column. Furthermore, the y-values are now initialized to 0, and the order is inverted.

Knowledge about the implementation of g@matrix is used!

226 \def\g@measureColsSucc{%
227 \ifnum \g@c@tmp<0\else
228 \g@c@tmpa=\g@maxcol
229 \advance\g@c@tmpa-\g@c@tmp
230 \advance\g@c@tmpa-1
231 \g@dim{cy\the\g@c@tmp}{\g@d@tmpa}% width of this cell
232 \g@dim{ct\the\g@c@tmp}{\g@d@tmpb}% glue right to this cell
233 \advance\g@d@tmp.5\g@d@tmpa%
234 \g@defdouble{cy\the\g@c@tmp}{0}%
235 \g@defdim{cx\the\g@c@tmpa}{\g@d@tmp}%
236 \advance\g@d@tmp.5\g@d@tmpa
237 \advance\g@d@tmp\g@d@tmpb
238 \ifnum \g@c@tmpa=0%
239 \advance\g@d@tmp.5\g@tab
240 \fi
241 \advance\g@c@tmp-1
242 \g@measureColsSucc
243 \fi

244 }

\g@measureAxis This is an easier macro. It measures and defines some common lengths, e.g. the distance between bottom line and math axis, and the dimensions of math arrows which are used for drawing arrows in operations.

245 \def\g@measureAxis{%
246 % 1. Where is the math axis relative to the ground line?
247 \setbox\g@trash=\hbox{$\vcenter{\hbox to 5pt{}}$}%
248 \global\g@axisHeight=\ht\g@trash
249 % 2. What is the minimum width of a horizontal arrow?
250 \setbox\g@trash=\hbox{$\leftarrow$}%
251 \global\g@arrowwd\wd\g@trash
252 % 3. What is the minimum height of a vertical arrow?
253 \setbox\g@trash=\vbox{\g@vertline}
254 \global\g@arrowht=\ht\g@trash
255 \global\advance\g@arrowht\dp\g@trash
256 \global\advance\g@arrowht\lineskip
257 % 4. What should be the thickness of ordinary lines?
258 \global\g@linethickness=\fboxrule\relax

259 }

\g@measureArea The last marco of this subsection provides the measurement of a set of floats.

(Therefore, it is rather a calculus macro.)

Assuming that #4 is a float identifier and for all i ∈ I := {#2, …, #3} #1 + i is a float identifier, the macro does

#4 := max_{i∈I}{#1 + i}

260 \def\g@measureArea#1#2#3#4{%
261 \g@defdim{#4}{\z@}%
262 \g@for#2\to#3\do{%
263 \g@max{#1##1}{#4}{\g@d@tmpe}%
264 \g@defdim{#4}{\g@d@tmpe}%

265 }% 266 }

2.5 Macros for drawing purposes

This Section defines low level macros for drawing purposes within a picture environment by use of floats.

\g@vline Let f_{1}, f_{2} and f_{3} be floats. Then,

    \g@vline{f_{1}}{f_{2}}{f_{3}}

draws a vertical line from (f_{1}, f_{2}) to (f_{2}, f_{3}).

267 \def\g@vline#1#2#3{%
268 \g@minD{#2}{#3}{\min}
269 \g@distD{#2}{#3}{\dist}
270 \put(\g@double{#1},\min){\line(0,1){\dist}}

271 }

\g@vvline Let f_{1}, f_{2} and f_{3} be floats. Then,

    \g@vvline{f_{1}}{f_{2}}{f_{3}}

draws a vertical line of length f_3, starting at (f_{1}, f_{2}), i.e. a line from (f_{1}, f_{2}) to (f_{1}, f_{2} + f_{3}).

272 \def\g@vvline#1#2#3{%
273 \put(\g@double{#1},\g@double{#2}){\line(0,1){\g@double{#3}}}

274 }

\g@varrow Let f_{1}, f_{2} and f_{3} be floats. Then,

    \g@varrow{f_{1}}{f_{2}}{f_{3}}

draws an arrow from (f_{1}, max{f_{2}, f_{3}}) to (f_{1}, min{f_{2}, f_{3}}).

275 \def\g@varrow#1#2#3{%
276 \g@dim{#2}{\g@d@tmpa}%
277 \g@dim{#3}{\g@d@tmpb}%
278 \advance\g@d@tmpb-\g@d@tmpa
279 \g@cbox{#1}{#2}{\g@downarrow{\g@d@tmpb}}%

280 }

\g@hline Let f_{1}, f_{2} and f_{3} be floats. Then,

    \g@hline{f_{1}}{f_{2}}{f_{3}}

draws a horizontal line from (f_{1}, f_{2}) to (f_{3}, f_{2}).

281 \def\g@hline#1#2#3{%
282 \g@minD{#1}{#3}{\min}%
283 \g@distD{#1}{#3}{\dist}%
284 \put(\min,\g@double{#2}){\line(1,0){\dist}}%

285 }

\g@hhline Let f_{1}, f_{2} and f_{3} be floats. Then,

    \g@hhline{f_{1}}{f_{2}}{f_{3}}

draws a horizontal line of length f_3, starting at (f_{1}, f_{2}), i.e. a line from (f_{1}, f_{2}) to (f_{1} + f_{3}, f_{2}).

286 \def\g@hhline#1#2#3{%
287 \put(\g@double{#1},\g@double{#2}){\line(1,0){\g@double{#3}}}%

288 }

\g@harrow Let f_{1}, f_{2} and f_{3} be floats. Then,

   \g@harrow{f_{1}}{f_{2}}{f_{3}}

draws an arrow from (max{f_{1}, f_{3}}, f_{2}) to (min{f_{1}, f_{3}}, f_{2}).

289 \def\g@harrow#1#2#3{%
290 \g@dim{#1}{\g@d@tmpa}%
291 \g@dim{#3}{\g@d@tmpb}%
292 \advance\g@d@tmpb-\g@d@tmpa
293 \advance\g@d@tmpb2\p@
294 \g@rbox{#1}{#2}{\hbox to\g@d@tmpb{\leftarrowfill}}%

295 }

The remaining two macros allow to put arbitrary math material to a specified position. Those are used for typesetting so called labels within matrix operations, for example, the factor at an \add arrow.

\g@rbox The first one is intended to use for row operations. Assuming that #1, #2 are float identifiers and #3 is math material, we put #3 into an \hbox and put that box to point (#1, #2).

The box will be vertically aligned to #2 (i.e., the math axis of #3 will be at height #2), and horizontally start at #1.

The macro puts the math material of #3 into \g@label and just copies its content when using, so you can reuse \g@label (e.g. for measuring purposes).

296 \def\g@rbox#1#2#3{%
297 \setbox\g@label=\hbox{$\relax#3\relax$}%
298 \ht\g@label\z@\dp\g@label\z@
299 \setbox\g@label=\hbox{$\mathstrut\box\g@label$}%
300 \put(\g@double{#1},\g@double{#2})%
301 {\makebox(0,0)[l]{\kern-\p@\copy\g@label}}%

302 }

\g@cbox The last macro of this section does the corresponding job for columns.

Here, #3 will be centered horizontally to #1, whereas #2 denotes the height of the label’s bottom.

Again, you can reuse the constructed box, it remains in register

\g@label.

303 \def\g@cbox#1#2#3{%
304 \setbox\g@label=\hbox{$\relax#3\relax$}%
305 \setbox\g@label=\hbox{\raise\dp\g@label\box\g@label}%
306 \put(\g@double{#1},\g@double{#2})%
307 {\makebox(0,0)[b]{\copy\g@label}}%

308 }

2.6 Generic operation commands

Before \halign begins, the matrix construction macro defines, what to do if the matrix is finished. This is defined in \g@endregion (see the next section for further information).

The \rowops and \colops commands are temporarily set to \g@east or \g@north, respectively. Thus, when entering an operation part, the first thing to do is to invoke \g@endregion to do the things that have to be done when the matrix input finishes. After that, \g@endregion has to be redefined to avoid doing the same process two times. Fortunately, \g@north and \g@east can easily reuse \g@endregion and store there those things that have to be done at the end of a region.

Hence, each switching to another part of the matrix input consists of three parts:

1. Invoke \g@endregion to finish the current input part.

2. Redefine \g@endregion to do the stuff that has to be done at the end of the region that is now starting. The result of the region is stored into a special box register which is used in the gmatrix environment.

3. Initialize the new region.

You can imagine that it is easy to define further regions (e.g. for operations to the right or below the matrix).

The two predefined regions \rowops and \colops are very similar, so we will show just one of them in this documentation.

\g@north The \g@north macro is the generic version of \colops, its corresponding

part is \g@east.
309 \def\g@north{%

1. Finish the current region

310 \g@endregion

2. Redefine \g@endregion and prevent \colops from being called again.

311 \gdef\colops{\PackageError{gauss}
312 {Two sets of column operations are specified in %
313  just one matrix. This is not allowed.}}%
314 \gdef\g@endregion{%
315 \end{picture}\egroup
316 \g@measureArea{cy}{0}{\the\g@maxcol}{sum}%
317 \g@dim{sum}{\ht\g@northbox}%
318 \global\setbox\g@northbox=\hbox{%
319  \raise\colarrowsep\box\g@northbox}%

320 }%

3. Initialization of the \colops region: Define the operation macros to be the corresponding private versions of this region (see below), set sum := 0 and start the picture environment where the operations are painted in.

321 \def\swap{\g@north@arrow11\colswapfromlabel\colswaptolabel}%
322 \def\add{\g@north@arrow01\coladdfromlabel\coladdtolabel}%
323 \let\mult\g@north@mult
324 \g@defdim{sum}{\z@}%
325 \global\setbox\g@northbox=\hbox\bgroup
326 \begin{picture}(\g@double{w},0)(0,0)
327  \linethickness{\g@linethickness}%

328 }

\g@north@mult The multiplication macro is the simplest one because it affects only one single column.

329 \def\g@north@mult#1#2{%
330 \ifx *#1

Reduce * to a set of numbers.

331 \g@for0\to\g@maxcol\do{\g@north@mult{##1}{#2}}%
332 \else

Now #1 is a number. Is it an index?

333 \g@checkBounds{c}{0}{#1}{\the\g@maxcol}%
334 \ifg@indexCorrect

Yes, it is. Typeset the operation.

335 \g@cbox{cx#1}{cy#1}{\colmultlabel{#2}}%
336 \g@dim{cy#1}{\g@d@tmpc}%
337 \advance\g@d@tmpc\ht\g@label
338 \g@defdim{cy#1}{\g@d@tmpc}%
339 \g@advance{cy#1}{\the\opskip}%
340 \g@update{sum}{cx#1}%
341 \fi
342 \fi

\g@north@arrow The \g@north@arrow macro is a generalisation of \swap and \add. It takes the following eight parameters:

• #1: 0 to make the ‘from’ line non-arrowed, 1 to get an arrow tip

• #2: 0 to make the ‘to’ line non-arrowed, 1 to get an arrow tip

• #3: macro for ‘from’ label rendering

• #4: macro for ‘to’ label rendering

• #5: [opt] label of the ‘from’ row

• #6: [opt] label of the ‘to’ row

• #7: index of the ‘from’ row

• #8: index of the ‘to’ row

If only one of the two optional arguments is given, then it is taken as #5 and #6 is taken empty. If both are missing, both are taken empty.

In \g@north above, \add is defined to

\g@north@arrow01\coladdfromlabel\coladdtolabel

and \swap is defined as

\g@north@arrow11\colswapfromlabel\colswaptolabel
344 \def\g@north@arrow#1#2#3#4{%
345 \@ifnextchar[%
346 {\g@north@arrow@a{#1}{#2}{#3}{#4}}%
347 {\g@north@arrow@b{#1}{#2}{#3}{#4}{}[]}%

348 }

349 \def\g@north@arrow@a#1#2#3#4[#5]{%
350 \@ifnextchar[%
351 {\g@north@arrow@b{#1}{#2}{#3}{#4}{#5}}%
352 {\g@north@arrow@b{#1}{#2}{#3}{#4}{#5}[]}%

353 }

354 \def\g@north@arrow@b#1#2#3#4#5[#6]#7#8{%
355 \ifx *#7

Reduce star indices to loops of number indices. ** needs a special handling.

356 \ifx *#8
357 \g@for0\to\g@maxcol\do{%
358  \g@north@arrow@b{#1}{#2}{#3}{#4}{#5}[#6]{##1}{*}}%
359 \else

Two loops rather than one because going from #8 down to 0 looks better than going from 0 to #8

360 \g@for#8\to0\do{%
361  \g@north@arrow@b{#1}{#2}{#3}{#4}{#5}[#6]{##1}{#8}}%
362 \g@for#8\to\g@maxcol\do{%
363  \g@north@arrow@b{#1}{#2}{#3}{#4}{#5}[#6]{##1}{#8}}%
364 \fi
365 \else
366 \ifx *#8

Reduce star indices to loops of number indices.

367 \g@for#7\to0\do{%
368  \g@north@arrow@b{#1}{#2}{#3}{#4}{#5}[#6]{#7}{##1}}%
369 \g@for#7\to\g@maxcol\do{%
370  \g@north@arrow@b{#1}{#2}{#3}{#4}{#5}[#6]{#7}{##1}}%
371 \else

Now, #7 and #8 are numbers.

372 \ifnum #7=#8\else
373  \g@checkBounds{c}{0}{#7}{\the\g@maxcol}%
374  \ifg@indexCorrect
375  \g@checkBounds{c}{0}{#8}{\the\g@maxcol}%
376  \ifg@indexCorrect

Now, #7 and #8 are different from each other, and both of them are legal indices. Store the current hights of the operations tower above column #7 and #8 into tmp1 and tmp2, respectively.

377  \g@defdouble{tmp1}{\g@double{cy#7}}%
378  \g@defdouble{tmp2}{\g@double{cy#8}}%

Find out the height of the horizontal connection line. First increment #7 and #8 by the minimum amounts.

379  \ifx0#1
380  \g@advance{cy#7}{\the\colopminsize}%
381  \else
382  \g@advance{cy#7}{\the\g@arrowht}%
383  \fi
384  \ifx0#2
385  \g@advance{cy#8}{\the\colopminsize}%
386  \else
387  \g@advance{cy#8}{\the\g@arrowht}%
388  \fi

Incorporate the columns between #7 and #8 into the height. Then sum denotes the level of the horizontal line.

389  \g@measureArea{cy}{#7}{#8}{sum}%

Draw arrows and/or vertical lines from #7’s and #8’s height up to sum.

390  \ifx0#1
391  \g@vline{cx#7}{tmp1}{sum}%
392  \else
393  \g@varrow{cx#7}{tmp1}{sum}%
394  \fi
395  \ifx0#2
396  \g@vline{cx#8}{tmp2}{sum}%
397  \else
398  \g@varrow{cx#8}{tmp2}{sum}%
399  \fi

Draw the horizontal line.

400  \g@hline{cx#7}{sum}{cx#8}%

Insert space between the horizontal line and the labels if at least one of the labels #5 and #6 is not empty. Typeset the labels into boxes and measure them.

401  \setbox\g@b@tmpa=\hbox{$#3{#5}$}%
402  \setbox\g@b@tmpb=\hbox{$#4{#6}$}%
403  \ifdim\ht\g@b@tmpa>\z@
404  \g@advance{sum}{\the\labelskip}%
405  \else
406  \ifdim\ht\g@b@tmpb>\z@
407  \g@advance{sum}{\the\labelskip}%
408  \fi
409  \fi

Draw the ‘from’ label if there is one

410  \g@d@tmpc\z@
411  \ifdim\ht\g@b@tmpa>\z@
412  \g@cbox{cx#7}{sum}{\kern-\p@\vcenter{\box\g@b@tmpa}}%
413  \g@d@tmpc=\ht\g@label
414  \fi

Draw the ‘to’ label if there is one

415  \ifdim\ht\g@b@tmpb>\z@
416  \g@cbox{cx#8}{sum}{\kern-\p@\vcenter{\box\g@b@tmpb}}%
417  \ifdim \ht\g@label>\g@d@tmpc
418  \g@d@tmpc=\ht\g@label
419  \fi
420  \fi

Advance the sum by the maximum height of the two labels and the desired space between two consecutive operations

421  \g@advance{sum}{\the\g@d@tmpc}%
422  \g@advance{sum}{\the\opskip}%

Update all column tower heights between #7 and #8 to sum.

423  \g@updateArea{sum}{cy}{#7}{#8}%

That’s it.

424  \fi
425  \fi
426 \fi
427 \fi
428 \fi

429 }

\g@east \g@east@mult The corresponding eastern macros are very similar to the above defined northern versions. Maybe there is a way to define generic operation commands once for all regions, but this would at least lead to less comprehesive definitions.

   We skip the definitions of \g@east, \g@east@mult and \g@east@arrow

in this documentation.

2.7 The gmatrix environment

gmatrix calls #1matrix where matrix is redefined to g@matrix. g@matrix

typesets the matrix using \halign and stores the operations into box registers \g@northbox and \g@eastbox, respectively. The matrix itself is stored

into \g@matrixbox.

The “real” typesetting is done at the end of gmatrix.

gmatrix . . . and here is gmatrix:
430 \newenvironment{gmatrix}[1][]
431 {\unitlength=1pt\def\g@environment{#1matrix}%
432 \begin{g@matrix}%
433 }{%
434 \end{g@matrix}%

Delete temporarily the definition of matrix.

435 \let\matrix\@empty
436 \let\endmatrix\@empty

Find out sizes of the matrix. Set \g@d@tmp to the height of the matrix.

437 \g@d@tmpa\ht\g@matrixbox \advance\g@d@tmpa\p@
438 \g@d@tmpb\dp\g@matrixbox \advance\g@d@tmpb\p@
439 \g@d@tmp\ht\g@northbox \ht\g@northbox\z@
440 \dp\g@northbox\z@
441 \ifdim \g@d@tmp>\z@
442 \advance\g@d@tmp-\opskip

443 \fi
444 \advance\g@d@tmp.5\ht\g@matrixbox
445 \advance\g@d@tmp.5\dp\g@matrixbox

Start the matrix environment to get the left delimiter.

446 \begin{\g@environment}%

Typeset the column operations to the north of the matrix, and the matrix itself.

447 \vcenter{\hbox{%
448 \rlap{\raise\ht\g@matrixbox\box\g@northbox}% north
449 % 1 additional pt above and below the matrix
450 \rule\z@\g@d@tmpa\lower\g@d@tmpb\null
451 \box\g@matrixbox% the matrix itself
452 }}%

Close the matrix environment to get now the right delimiter.

453 \end{\g@environment}%

Finally typeset the eastern operations. Insert vertical space of \g@d@tmp (the height of the matrix) and horizontal space of \rowarrowsep before.

454 \rule\rowarrowsep\z@
455 \rule\z@\g@d@tmp
456 \g@dim{d}{\g@d@tmpa}%
457 \vcenter{\hbox{\lower\g@d@tmpa\box\g@eastbox}}%

458 }

Here is the definition of \g@endmatrix. This is the initial \g@endregion which is defined within \begin{gmatrix} to finish the matrix input.

459 \def\g@endmatrix{%
460 \mathstrut\crcr
461 \egroup % end of \halign
462 \egroup % end of \vbox, this contains the matrix

Save the matrix into matrixbox.

463 \global\setbox\g@matrixbox\lastbox

Measure the matrix’ dimensions.

464 \g@measureAxis
465 \setbox\g@trash=\vbox{%
466 \unvcopy\g@matrixbox

Copy the last row of the matrix into \g@eastbox and reinsert it to the vbox.

467 \global\setbox\g@eastbox=\lastbox
468 \copy\g@eastbox
469 \g@d@tmp\z@ {\g@measureRows}% measure rows

470 }%

471 \setbox\g@trash=\hbox{%

Insert a box of width 100cm to recognize the beginning of the hbox within the measurement recursion.

472 \hbox to 100cm{.\hfill.}%
473 \unhbox\g@eastbox
474 \g@d@tmp\z@ {\g@measureCols}% measure columns

475 }%

Determine global dimensions of the matrix (total height, etc.).

476 \g@d@tmpa=\ht\g@matrixbox\advance\g@d@tmpa\dp\g@matrixbox
477 \g@defdim{h}{\g@d@tmpa}%
478 \g@defdim{w}{\wd\g@matrixbox}%
479 \g@defdim{d}{\dp\g@matrixbox}%

480 }%

g@matrix Finally, we have the following definition of g@matrix:

481 \edef\g@prae{\hfil$\relax\noexpand\mathstrut}
482 \edef\g@post{\relax$\hfil}
483 \newenvironment{g@matrix}
484 {\setbox\g@trash=\hbox\bgroup
485 \global\g@maxrow@old\g@maxrow
486 \global\g@maxcol@old\g@maxcol
487 \global\g@maxrow0%
488 \global\g@maxcol0%
489 \let\rowops\g@east
490 \let\colops\g@north
491 \vbox\bgroup
492 % count rows while typesetting
493 \def\\{\mathstrut\cr\global\advance\g@maxrow1\relax}%
494 \global\let\g@endregion\g@endmatrix
495 \global\g@tab=2\arraycolsep
496 \ialign\bgroup\g@prae##\g@post&&\kern\g@tab\g@prae##\g@post\cr
497 }{%
498 \g@endregion
499 \egroup % end of \hbox
500 % enable nested gmatrixes (for DniQ :-)
501 \global\g@maxrow\g@maxrow@old
502 \global\g@maxcol\g@maxcol@old
503 \global\let\g@endregion\g@endmatrix
504 \global\let\rowops\g@east
505 \global\let\colops\g@north

506 }

2.8 Public tools

\newmatrix The \newmatrix command allows to define new matrix environments with special delimiters as described in Section 1.

507 \def\newmatrix#1#2#3{%
508 \ifx g#3 \else
509 \ifx {#3}{g@} \else
510 \expandafter\ifx\csname#3matrix\endcsname\relax
511  \newenvironment{#3matrix}%
512  {\left#1\begin{matrix}}{\end{matrix}\right#2}%
513 \else
514  \renewenvironment{#3matrix}%
515  {\left#1\begin{matrix}}{\end{matrix}\right#2}%
516 \fi
517 \fi
518 \fi

519 }

For compatibility reasons, we redefine predefined matrix environments such as pmatrix. This is necessary to avoid problems that arise when dealing with earlier AMSTEX versions.

520 \newmatrix()p
521 \newmatrix[]b
522 \newmatrix\lbrace\rbrace B
523 \newmatrix\lvert\rvert v
524 \newmatrix\lVert\rVert V
\rowmultlabel \colmultlabel \rowaddfromlabel \coladdfromlabel \rowaddtolabel \coladdtolabel \rowswapfromlabel \colswapfromlabel \rowswaptolabel \colswaptolabel Labels of operations are typeset using the so-called fontifying macros de-

scribed in Section 1.3. All of them take exaclty one argument, and they are called within math mode. The user may redefine them to adjust the appearence of operations according to his needs. The following is the standard definition:

525 \def\rowmultlabel#1{|\,#1}
526 \def\rowswapfromlabel#1{}
527 \def\rowswaptolabel#1{}
528 \def\rowaddfromlabel#1{\scriptstyle #1}
529 \def\rowaddtolabel#1{\scriptscriptstyle +}
530 \def\colmultlabel#1{%
531 \underline{\hbox to 1.2em{$\hss\mathstrut{}#1\hss$}}%

532 }

533 \def\colswapfromlabel#1{}
534 \def\colswaptolabel#1{}
535 \def\coladdfromlabel#1{\scriptstyle #1}
536 \def\coladdtolabel#1{\scriptscriptstyle +}

\colarrowsep \rowarrowsep \labelskip \opskip \colopminsize \rowopminsize Finally, we define the public lengths of Section 1.3:
537 \newdimen\colarrowsep\colarrowsep=.5em
538 \newdimen\rowarrowsep\rowarrowsep=.5em
539 \newdimen\opskip\opskip=5pt
540 \newdimen\labelskip\labelskip=4pt
541 \newdimen\colopminsize\colopminsize=3pt
542 \newdimen\rowopminsize\rowopminsize=3pt

And that’s all.

543 \makeatother