Section 768: Alignment

It’s sort of a miracle whenever \halign and \valign work, because they cut across so many of the control structures of .

Therefore the present page is probably not the best place for a beginner to start reading this program; it is better to master everything else first.

Let us focus our thoughts on an example of what the input might be, in order to get some idea about how the alignment miracle happens. The example doesn’t do anything useful, but it is sufficiently general to indicate all of the special cases that must be dealt with; please do not be disturbed by its apparent complexity and meaninglessness.

\tabskip 2pt plus 3pt
\halign to 300pt{u1#v1&
        \tabskip 1pt plus 1fil u2#v2&
        u3#v3\cr
    a1&\omit a2&\vrule\cr
    \noalign{\vskip 3pt}
    b1\span b2\cr
    \omit&c2\span\omit\cr}

Here’s what happens:

(0) When ‘\halign to 300pt\{’ is scanned, the scan_spec routine places the 300pt dimension onto the save_stack, and an ALIGN_GROUP code is placed above it. This will make it possible to complete the alignment when the matching ‘}’ is found.

(1) The preamble is scanned next. Macros in the preamble are not expanded, except as part of a tabskip specification. For example, if u2 had been a macro in the preamble above, it would have been expanded, since must look for ‘minus...’ as part of the tabskip glue. A “preamble list” is constructed based on the user’s preamble; in our case it contains the following seven items:

\glue 2pt plus 3pt(the tabskip preceding column 1)
\alignrecord, width (preamble info for column 1)
\glue 2pt plus 3pt(the tabskip between columns 1 and 2)
\alignrecord, width (preamble info for column 2)
\glue 1pt plus 1fil(the tabskip between columns 2 and 3)
\alignrecord, width (preamble info for column 3)
\glue 1pt plus 1fil(the tabskip following column 3)

These “alignrecord” entries have the same size as an UNSET_NODE, since they will later be converted into such nodes. However, at the moment they have no type or subtype fields; they have info fields instead, and these info fields are initially set to the value END_SPAN, for reasons explained below. Furthermore, the alignrecord nodes have no height or depth fields; these are renamed u_part and v_part, and they point to token lists for the templates of the alignment. For example, the u_part field in the first alignrecord points to the token list ‘u1’, i.e., the template preceding the ‘#’ for column 1.

(2) now looks at what follows the \cr that ended the preamble. It is not ‘\noalign’ or ‘\omit’, so this input is put back to be read again, and the template ‘u1’ is fed to the scanner. Just before reading ‘u1’, goes into restricted horizontal mode. Just after reading ‘u1’, will see ‘a1’, and then (when the & is sensed) will see ‘v1’. Then scans an ENDV token, indicating the end of a column. At this point an UNSET_NODE is created, containing the contents of the current hlist (i.e., ‘u1a1v1’). The natural width of this unset node replaces the width field of the alignrecord for column 1; in general, the alignrecords will record the maximum natural width that has occurred so far in a given column.

(3) Since ‘\omit’ follows the ‘&’, the templates for column 2 are now bypassed. Again goes into restricted horizontal mode and makes an UNSET_NODE from the resulting hlist; but this time the hlist contains simply ‘a2’. The natural width of the new unset box is remembered in the width field of the alignrecord for column 2.

(4) A third UNSET_NODE is created for column 3, using essentially the mechanism that worked for column 1; this unset box contains ‘u3\vrule v3’. The vertical rule in this case has running dimensions that will later extend to the height and depth of the whole first row, since each UNSET_NODE in a row will eventually inherit the height and depth of its enclosing box.

(5) The first row has now ended; it is made into a single unset box comprising the following seven items:

\glue 2pt plus 3pt
\unsetbox for 1 column: u1a1v1
\glue 2pt plus 3pt
\unsetbox for 1 column: a2
\glue 1pt plus 1fil
\unsetbox for 1 column: u3\vrule v3
\glue 1pt plus 1fil

The width of this unset row is unimportant, but it has the correct height and depth, so the correct baselineskip glue will be computed as the row is inserted into a vertical list.

(6) Since ‘\noalign’ follows the current \cr, appends additional material (in this case \vskip 3pt) to the vertical list. While processing this material, will be in internal vertical mode, and NO_ALIGN_GROUP will be on save_stack.

(7) The next row produces an unset box that looks like this:

\glue 2pt plus 3pt
\unsetbox for 2 columns: u1b1v1u2b2v2
\glue 1pt plus 1fil
\unsetbox for 1 column: (empty)
\glue 1pt plus 1fil

The natural width of the unset box that spans columns 1 and 2 is stored in a “span node,” which we will explain later; the info field of the alignrecord for column 1 now points to the new span node, and the info of the span node points to END_SPAN.

(8) The final row produces the unset box

\glue 2pt plus 3pt
\unsetbox for 1 column: (empty)
\glue 2pt plus 3pt
\unsetbox for 2 columns: u2c2v2
\glue 1pt plus 1fil

A new span node is attached to the alignrecord for column 2.

(9) The last step is to compute the true column widths and to change all the unset boxes to hboxes, appending the whole works to the vertical list that encloses the \halign. The rules for deciding on the final widths of each unset column box will be explained below.

Note that as \halign is being processed, we fearlessly give up control to the rest of . At critical junctures, an alignment routine is called upon to step in and do some little action, but most of the time these routines just lurk in the background. It’s something like post-hypnotic suggestion.

Section 769

We have mentioned that alignrecords contain no height or depth fields. Their glue_sign and glue_order are pre-empted as well, since it is necessary to store information about what to do when a template ends. This information is called the extra_info field.

alignment.h
// << Start file |alignment.h|, 1381 >>

#define u_part(X)     mem[(X) + HEIGHT_OFFSET].integer // pointer to <u_j> token list
#define v_part(X)     mem[(X) + DEPTH_OFFSET].integer  // pointer to <v_j> token list
#define extra_info(X) info(X + LIST_OFFSET)            // info to remember during template

Section 770

Alignments can occur within alignments, so a small stack is used to access the alignrecord information. At each level we have a preamble pointer, indicating the beginning of the preamble list; a cur_align pointer, indicating the current position in the preamble list; a cur_span pointer, indicating the value of cur_align at the beginning of a sequence of spanned columns; a cur_loop pointer, indicating the tabskip glue before an alignrecord that should be copied next if the current list is extended; and the align_state variable, which indicates the nesting of braces so that \cr and \span and tab marks are properly intercepted. There also are pointers cur_head and cur_tail to the head and tail of a list of adjustments being moved out from horizontal mode to vertical mode.

The current values of these seven quantities appear in global variables; when they have to be pushed down, they are stored in 5-word nodes, and align_ptr points to the topmost such node.

constants.h
#define ALIGN_STACK_NODE_SIZE 5 // number of |mem| words to save alignment states
alignment.h
#define preamble link(ALIGN_HEAD) // the current preamble list

⟨ Global variables 13 ⟩+≡

pointer cur_align;          // current position in preamble list
pointer cur_span;           // start of currently spanned columns in preamble list
pointer cur_loop;           // place to copy when extending a periodic preamble
pointer align_ptr;          // most recently pushed-down alignment stack node
pointer cur_head, cur_tail; // adjustment list pointers

Section 771

The align_state and preamble variables are initialized elsewhere.

⟨ Set initial values of key variables 21 ⟩+≡

align_ptr = null;
cur_align = null;
cur_span = null;
cur_loop = null;
cur_head = null;
cur_tail = null;

Section 772

Alignment stack maintenance is handled by a pair of trivial routines called push_alignment and pop_alignment.

alignment.c
// << Start file |alignment.c|, 1382 >>

void push_alignment() {
    pointer p; // the new alignment stack node
    p = get_node(ALIGN_STACK_NODE_SIZE);
    link(p) = align_ptr;
    info(p) = cur_align;
    llink(p) = preamble;
    rlink(p) = cur_span;
    mem[p + 2].integer = cur_loop;
    mem[p + 3].integer = align_state;
    info(p + 4) = cur_head;
    link(p + 4) = cur_tail;
    align_ptr = p;
    cur_head = get_avail();
}

void pop_alignment() {
    pointer p; // the top alignment stack node
    free_avail(cur_head);
    p = align_ptr;
    cur_tail = link(p + 4);
    cur_head = info(p + 4);
    align_state = mem[p + 3].integer;
    cur_loop = mem[p + 2].integer;
    cur_span = rlink(p);
    preamble = llink(p);
    cur_align = info(p);
    align_ptr = link(p);
    free_node(p, ALIGN_STACK_NODE_SIZE);
}

Section 773

has eight procedures that govern alignments: init_align and fin_align are used at the very beginning and the very end; init_row and fin_row are used at the beginning and end of individual rows; init_span is used at the beginning of a sequence of spanned columns (possibly involving only one column); init_col and fin_col are used at the beginning and end of individual columns; and align_peek is used after \cr to see whether the next item is \noalign.

We shall consider these routines in the order they are first used during the course of a complete \halign, namely init_align, align_peek, init_row, init_span, init_col, fin_col, fin_row, fin_align.

Section 774

When \halign or \valign has been scanned in an appropriate mode, calls init_align, whose task is to get everything off to a good start. This mostly involves scanning the preamble and putting its information into the preamble list.

alignment.c
// << Declare the procedure called |get_preamble_token|, 782 >>

void init_align() {
    pointer save_cs_ptr; // |warning_index| value for error messages
    pointer p;           // for short-term temporary use

    save_cs_ptr = cur_cs; // \halign or \valign, usually
    push_alignment();
    align_state = -1000000; // enter a new alignment level

    // << Check for improper alignment in displayed math, 776 >>
    
    push_nest(); // enter a new semantic level
    
    // << Change current mode to |-VMODE| for \halign, |-HMODE| for \valign, 775 >>
    
    scan_spec(ALIGN_GROUP, false);
    
    // << Scan the preamble and record it in the |preamble| list, 777 >>
    
    new_save_level(ALIGN_GROUP);
    if (every_cr != null) {
        begin_token_list(every_cr, EVERY_CR_TEXT);
    }
    align_peek(); // look for \noalign or \omit
}

Section 775

In vertical modes, prev_depth already has the correct value. But if we are in MMODE (displayed formula mode), we reach out to the enclosing vertical mode for the prev_depth value that produces the correct baseline calculations.

⟨ Change current mode to -VMODE for \halign, -HMODE for \valign 775 ⟩≡

if (mode == MMODE) {
    mode = -VMODE;
    prev_depth = nest[nest_ptr - 2].aux_field.sc;
}
else if (mode > 0) {
    negate(mode);
}

Section 776

When \halign is used as a displayed formula, there should be no other pieces of mlists present.

⟨ Check for improper alignment in displayed math 776 ⟩≡

if (mode == MMODE
    && (tail != head || incompleat_noad != null))
{
    print_err("Improper ");
    print_esc("halign");
    print(" inside $$'s");
    help3("Displays can use special alignments (like \\eqalignno)")
        ("only if nothing but the alignment itself is between $$'s.")
        ("So I've deleted the formulas that preceded this alignment.");
    error();
    flush_math();
}

Section 777

⟨ Scan the preamble and record it in the preamble list 777 ⟩≡

preamble = null;
cur_align = ALIGN_HEAD;
cur_loop = null;
scanner_status = ALIGNING;
warning_index = save_cs_ptr;
align_state = -1000000; // at this point, |cur_cmd = LEFT_BRACE|
while(true) {
    // << Append the current tabskip glue to the preamble list, 778 >>
    if (cur_cmd == CAR_RET) {
        break; // goto done, \cr ends the preamble
    }
    // << Scan preamble text until |cur_cmd| is |TAB_MARK| or |CAR_RET|, looking for changes in the tabskip glue; append an alignrecord to the preamble list, 779 >>
}
// done:
scanner_status = NORMAL;

Section 778

⟨ Append the current tabskip glue to the preamble list 778 ⟩≡

link(cur_align) = new_param_glue(TAB_SKIP_CODE);
cur_align = link(cur_align);

Section 779

⟨ Scan preamble text until cur_cmd is TAB_MARK or CAR_RET, looking for changes in the tabskip glue; append an alignrecord to the preamble list 779 ⟩≡

// << Scan the template <u_j>, putting the resulting token list in |HOLD_HEAD|, 783 >>
link(cur_align) = new_null_box();
cur_align = link(cur_align); // a new alignrecord
info(cur_align) = END_SPAN;
width(cur_align) = NULL_FLAG;
u_part(cur_align) = link(HOLD_HEAD);
// << Scan the template <v_j>, putting the resulting token list in |HOLD_HEAD|, 784 >>
v_part(cur_align) = link(HOLD_HEAD);

Section 780

We enter ‘\span’ into eqtb with TAB_MARK as its command code, and with SPAN_CODE as the command modifier. This makes interpret it essentially the same as an alignment delimiter like ‘&’, yet it is recognizably different when we need to distinguish it from a normal delimiter. It also turns out to be useful to give a special CR_CODE to ‘\cr’, and an even larger CR_CR_CODE to ‘\crcr’.

The end of a template is represented by two “frozen” control sequences called \endtemplate. The first has the command code END_TEMPLATE, which is OUTER_CALL, so it will not easily disappear in the presence of errors. The get_x_token routine converts the first into the second, which has ENDV as its command code.

NOTE

String "endemplate" is added to the pool.

⟨ Read the other strings 51 ⟩+≡

put_string("endtemplate"); // ENDTEMPLATE_STRING: 267

⟨ Internal strings numbers in the pool 51 ⟩+≡

#define ENDTEMPLATE_STRING 267
constants.h
#define SPAN_CODE          256           // distinct from any character
#define CR_CODE            257           // distinct from |SPAN_CODE| and from any character
#define CR_CR_CODE         (CR_CODE + 1) // this distinguishes \crcr from \cr
#define END_TEMPLATE_TOKEN (CS_TOKEN_FLAG + FROZEN_END_TEMPLATE)

⟨ Put each of TeX’s primitives into the hash table 226 ⟩+≡

primitive("span", TAB_MARK, SPAN_CODE);
primitive("cr", CAR_RET, CR_CODE);
text(FROZEN_CR) = str_ptr - 1; // "cr"
eqtb[FROZEN_CR] = eqtb[cur_val];
primitive("crcr", CAR_RET, CR_CR_CODE);
text(FROZEN_END_TEMPLATE) = ENDTEMPLATE_STRING;
text(FROZEN_ENDV) = ENDTEMPLATE_STRING;
eq_type(FROZEN_ENDV) = ENDV;
equiv(FROZEN_ENDV) = NULL_LIST;
eq_level(FROZEN_ENDV) = LEVEL_ONE;
eqtb[FROZEN_END_TEMPLATE] = eqtb[FROZEN_ENDV];
eq_type(FROZEN_END_TEMPLATE) = END_TEMPLATE;

Section 781

⟨ Cases of print_cmd_chr for symbolic printing of primitives 227 ⟩+≡

case TAB_MARK:
    if (chr_code == SPAN_CODE) {
        print_esc("span");
    }
    else {
        chr_cmd("alignment tab character ");
    }
    break;

case CAR_RET:
    if (chr_code == CR_CODE) {
        print_esc("cr");
    }
    else {
        print_esc("crcr");
    }
    break;

Section 782

The preamble is copied directly, except that \tabskip causes a change to the tabskip glue, thereby possibly expanding macros that immediately follow it. An appearance of \span also causes such an expansion.

Note that if the preamble contains ‘\global\tabskip’, the ‘\global’ token survives in the preamble and the ‘\tabskip’ defines new tabskip glue (locally).

⟨ Declare the procedure called get_preamble_token 782 ⟩≡

void get_preamble_token() {
restart:
    get_token();
    while (cur_chr == SPAN_CODE && cur_cmd == TAB_MARK) {
        get_token(); // this token will be expanded once
        if (cur_cmd > MAX_COMMAND) {
            expand();
            get_token();
        }
    }
    if (cur_cmd == ENDV) {
        fatal_error("(interwoven alignment preambles are not allowed)");
    }
    if (cur_cmd == ASSIGN_GLUE
        && cur_chr == GLUE_BASE + TAB_SKIP_CODE)
    {
        scan_optional_equals();
        scan_glue(GLUE_VAL);
        if (global_defs > 0) {
            geq_define(GLUE_BASE + TAB_SKIP_CODE, GLUE_REF, cur_val);
        }
        else {
            eq_define(GLUE_BASE + TAB_SKIP_CODE, GLUE_REF, cur_val);
        }
        goto restart;
    }
}

Section 783

Spaces are eliminated from the beginning of a template.

⟨ Scan the template <u_j>, putting the resulting token list in HOLD_HEAD 783 ⟩≡

p = HOLD_HEAD;
link(p) = null;
while(true) {
    get_preamble_token();
    if (cur_cmd == MAC_PARAM) {
        break; // goto done1
    }
    if (cur_cmd <= CAR_RET
        && cur_cmd >= TAB_MARK
        && align_state == -1000000)
    {
        if (p == HOLD_HEAD
            && cur_loop == null
            && cur_cmd == TAB_MARK)
        {
            cur_loop = cur_align;
        }
        else {
            print_err("Missing # inserted in alignment preamble");
            help3("There should be exactly one # between &'s, when an")
                ("\\halign or \\valign is being set up. In this case you had")
                ("none, so I've put one in; maybe that will work.");
            back_error();
            break; // goto done1
        }
    }
    else if (cur_cmd != SPACER || p != HOLD_HEAD) {
        link(p) = get_avail();
        p = link(p);
        info(p) = cur_tok;
    }
}
// done1:

Section 784

⟨ Scan the template <v_j>, putting the resulting token list in HOLD_HEAD 784 ⟩≡

p = HOLD_HEAD;
link(p) = null;
while(true) {
    get_preamble_token();
    if (cur_cmd <= CAR_RET
        && cur_cmd >= TAB_MARK
        && align_state == -1000000)
    {
        break; // goto done2
    }
    if (cur_cmd == MAC_PARAM) {
        print_err("Only one # is allowed per tab");
        help3("There should be exactly one # between &'s, when an")
            ("\\halign or \\valign is being set up. In this case you had")
            ("more than one, so I'm ignoring all but the first.");
        error();
        continue;
    }
    link(p) = get_avail();
    p = link(p);
    info(p) = cur_tok;
}
// done2:
link(p) = get_avail();
p = link(p);
info(p) = END_TEMPLATE_TOKEN; // put \endtemplate at the end

Section 785

The tricky part about alignments is getting the templates into the scanner at the right time, and recovering control when a row or column is finished.

We usually begin a row after each \cr has been sensed, unless that \cr is followed by \noalign or by the right brace that terminates the alignment. The align_peek routine is used to look ahead and do the right thing; it either gets a new row started, or gets a \noalign started, or finishes off the alignment.

alignment.c
void align_peek() {
restart:
    align_state = 1000000;
    // << Get the next non-blank non-call token, 406 >>
    if (cur_cmd == NO_ALIGN) {
        scan_left_brace();
        new_save_level(NO_ALIGN_GROUP);
        if (mode == -VMODE) {
            normal_paragraph();
        }
    }
    else if (cur_cmd == RIGHT_BRACE) {
        fin_align();
    }
    else if (cur_cmd == CAR_RET && cur_chr == CR_CR_CODE) {
        goto restart; // ignore \crcr
    }
    else {
        init_row(); // start a new row
        init_col(); // start a new column and replace what we peeked at
    }
}

Section 786

To start a row (i.e., a ‘row’ that rhymes with ‘dough’ but not with ‘bough’), we enter a new semantic level, copy the first tabskip glue, and change from internal vertical mode to restricted horizontal mode or vice versa. The space_factor and prev_depth are not used on this semantic level, but we clear them to zero just to be tidy.

alignment.c
void init_row() {
    push_nest();
    mode = (-HMODE - VMODE) - mode;
    if (mode == -HMODE) {
        space_factor = 0;
    } 
    else {
        prev_depth = 0;
    }
    tail_append(new_glue(glue_ptr(preamble)));
    subtype(tail) = TAB_SKIP_CODE + 1;
    cur_align = link(preamble);
    cur_tail = cur_head;
    init_span(cur_align);
}

Section 787

The parameter to init_span is a pointer to the alignrecord where the next column or group of columns will begin. A new semantic level is entered, so that the columns will generate a list for subsequent packaging.

alignment.c
void init_span(pointer p) {
    push_nest();
    if (mode == -HMODE) {
        space_factor = 1000;
    }
    else {
        prev_depth = IGNORE_DEPTH;
        normal_paragraph();
    }
    cur_span = p;
}

Section 788

When a column begins, we assume that cur_cmd is either OMIT or else the current token should be put back into the input until the template has been scanned. (Note that cur_cmd might be TAB_MARK or CAR_RET.) We also assume that align_state is approximately 1000000 at this time. We remain in the same mode, and start the template if it is called for.

alignment.c
void init_col() {
    extra_info(cur_align) = cur_cmd;
    if (cur_cmd == OMIT) {
        align_state = 0;
    }
    else {
        back_input();
        begin_token_list(u_part(cur_align), U_TEMPLATE);
    } // now |align_state = 1000000|
}

Section 789

The scanner sets align_state to zero when the template ends. When a subsequent \cr or \span or tab mark occurs with align_state = 0, the scanner activates the following code, which fires up the template. We need to remember the cur_chr, which is either CR_CR_CODE, CR_CODE, SPAN_CODE, or a character code, depending on how the column text has ended.

This part of the program had better not be activated when the preamble to another alignment is being scanned, or when no alignment preamble is active.

⟨ Insert the <v_j> template and goto restart 789 ⟩≡

if (scanner_status == ALIGNING || cur_align == null) {
    fatal_error("(interwoven alignment preambles are not allowed)");
}
cur_cmd = extra_info(cur_align);
extra_info(cur_align) = cur_chr;
if (cur_cmd == OMIT) {
    begin_token_list(OMIT_TEMPLATE, V_TEMPLATE);
}
else {
    begin_token_list(v_part(cur_align), V_TEMPLATE);
}
align_state = 1000000;
goto restart;

Section 790

The token list OMIT_TEMPLATE just referred to is a constant token list that contains the special control sequence \endtemplate only.

⟨ Initialize the special list heads and constant nodes 790 ⟩≡

info(OMIT_TEMPLATE) = END_TEMPLATE_TOKEN; // |link(OMIT_TEMPLATE) = null|

Section 791

When the ENDV command at the end of a template comes through the scanner, things really start to happen; and it is the fin_col routine that makes them happen. This routine returns true if a row as well as a column has been finished.

alignment.c
bool fin_col() {
    pointer p;    // the alignrecord after the current one
    pointer q, r; // temporary pointers for list manipulation
    pointer s;    // a new span node
    pointer u;    // a new unset box
    scaled w;     // natural width
    int o;        // order of infinity
    halfword n;   // span counter
    
    if (cur_align == null) {
        confusion("endv");
    }
    q = link(cur_align);
    if (q == null) {
        confusion("endv");
    }
    
    if (align_state < 500000) {
        fatal_error("(interwoven alignment preambles are not allowed)");
    }
    p = link(q);
    // << If the preamble list has been traversed, check that the row has ended, 792 >>
    if (extra_info(cur_align) != SPAN_CODE) {
        unsave();
        new_save_level(ALIGN_GROUP);
        // << Package an unset box for the current column and record its width, 796 >>
        // << Copy the tabskip glue between columns, 795 >>
        if (extra_info(cur_align) >= CR_CODE) {
            return true;
        }
        init_span(p);
    }
    align_state = 1000000;
    // << Get the next non-blank non-call token, 406 >>
    cur_align = p;
    init_col();
    return false;
}

Section 792

⟨ If the preamble list has been traversed, check that the row has ended 792 ⟩≡

if (p == null && extra_info(cur_align) < CR_CODE) {
    if (cur_loop != null) {
        // << Lengthen the preamble periodically, 793 >>
    }
    else {
        print_err("Extra alignment tab has been changed to ");
        print_esc("cr");
        help3("You have given more \\span or & marks than there were")
            ("in the preamble to the \\halign or \\valign now in progress.")
            ("So I'll assume that you meant to type \\cr instead.");
        extra_info(cur_align) = CR_CODE;
        error();
    }
}

Section 793

⟨ Lengthen the preamble periodically 793 ⟩≡

link(q) = new_null_box();
p = link(q); // a new alignrecord
info(p) = END_SPAN;
width(p) = NULL_FLAG;
cur_loop = link(cur_loop);
// << Copy the templates from node |cur_loop| into node |p|, 794 >>
cur_loop = link(cur_loop);
link(p) = new_glue(glue_ptr(cur_loop));
subtype(link(p)) = TAB_SKIP_CODE + 1;

Section 794

⟨ Copy the templates from node cur_loop into node p 794 ⟩≡

q = HOLD_HEAD;
r = u_part(cur_loop);
while (r != null) {
    link(q) = get_avail();
    q = link(q);
    info(q) = info(r);
    r = link(r);
}
link(q) = null;
u_part(p) = link(HOLD_HEAD);
q = HOLD_HEAD;
r = v_part(cur_loop);
while (r != null) {
    link(q) = get_avail();
    q = link(q);
    info(q) = info(r);
    r = link(r);
}
link(q) = null;
v_part(p) = link(HOLD_HEAD);

Section 795

⟨ Copy the tabskip glue between columns 795 ⟩≡

tail_append(new_glue(glue_ptr(link(cur_align))));
subtype(tail) = TAB_SKIP_CODE + 1;

Section 796

⟨ Package an unset box for the current column and record its width 796 ⟩≡

if (mode == -HMODE) {
    adjust_tail = cur_tail;
    u = hpack(link(head), NATURAL);
    w = width(u);
    cur_tail = adjust_tail;
    adjust_tail = null;
}
else {
    u = vpackage(link(head), NATURAL, 0);
    w = height(u);
}
n = MIN_QUARTERWORD; // this represents a span count of 1
if (cur_span != cur_align) {
    // << Update width entry for spanned columns, 798 >>
}
else if (w > width(cur_align)) {
    width(cur_align) = w;
}
type(u) = UNSET_NODE;
span_count(u) = n;
// << Determine the stretch order, 659 >>
glue_order(u) = o;
glue_stretch(u) = total_stretch[o];
// << Determine the shrink order, 665 >>
glue_sign(u) = o;
glue_shrink(u) = total_shrink[o];
pop_nest();
link(tail) = u;
tail = u;

Section 797

A span node is a 2-word record containing width, info, and link fields. The link field is not really a link, it indicates the number of spanned columns; the info field points to a span node for the same starting column, having a greater extent of spanning, or to END_SPAN, which has the largest possible link field; the width field holds the largest natural width corresponding to a particular set of spanned columns.

A list of the maximum widths so far, for spanned columns starting at a given column, begins with the info field of the alignrecord for that column.

constants.h
#define SPAN_NODE_SIZE 2 // number of |mem| words for a span node

⟨ Initialize the special list heads and constant nodes 790 ⟩+≡

link(END_SPAN) = MAX_QUARTERWORD + 1;
info(END_SPAN) = null;

Section 798

⟨ Update width entry for spanned columns 798 ⟩≡

q = cur_span;
do {
    incr(n);
    q = link(link(q));
} while (q != cur_align);
if (n > MAX_QUARTERWORD) {
    confusion("256 spans"); // this can happen, but won't
}
q = cur_span;
while (link(info(q)) < n) {
    q = info(q);
}
if (link(info(q)) > n) {
    s = get_node(SPAN_NODE_SIZE);
    info(s) = info(q);
    link(s) = n;
    info(q) = s;
    width(s) = w;
}
else if (width(info(q)) < w) {
    width(info(q)) = w;
}

Section 799

At the end of a row, we append an unset box to the current vlist (for \halign) or the current hlist (for \valign). This unset box contains the unset boxes for the columns, separated by the tabskip glue. Everything will be set later.

alignment.c
void fin_row() {
    pointer p; // the new unset box
    if (mode == -HMODE) {
        p = hpack(link(head), NATURAL);
        pop_nest();
        append_to_vlist(p);
        if (cur_head != cur_tail) {
            link(tail) = link(cur_head);
            tail = cur_tail;
        }
    }
    else {
        p = vpack(link(head), NATURAL);
        pop_nest();
        link(tail) = p;
        tail = p;
        space_factor = 1000;
    }
    type(p) = UNSET_NODE;
    glue_stretch(p) = 0;
    if (every_cr != null) {
        begin_token_list(every_cr, EVERY_CR_TEXT);
    }
    align_peek();
} // note that |glue_shrink(p) = 0| since |glue_shrink = shift_amount|

Section 800

Finally, we will reach the end of the alignment, and we can breathe a sigh of relief that memory hasn’t overflowed. All the unset boxes will now be set so that the columns line up, taking due account of spanned columns.

alignment.c
void fin_align() {
    pointer p, q, r, s, u, v; // registers for the list operations
    scaled t, w;              // width of column
    scaled o;                 // shift offset for unset boxes
    halfword n;               // matching span amount
    scaled rule_save;         // temporary storage for |overfull_rule|
    memory_word aux_save;     // temporary storage for |aux|
    
    if (cur_group != ALIGN_GROUP) {
        confusion("align1");
    }
    unsave(); // that |ALIGN_GROUP| was for individual entries
    if (cur_group != ALIGN_GROUP) {
        confusion("align0");
    }
    unsave(); // that |ALIGN_GROUP| was for the whole alignment
    if (nest[nest_ptr - 1].mode_field == MMODE) {
        o = display_indent;
    }
    else {
        o = 0;
    }

    // << Go through the preamble list, determining the column widths and changing the alignrecords to dummy unset boxes, 801 >>

    // << Package the preamble list, to determine the actual tabskip glue amounts, and let |p| point to this prototype box, 804 >>

    // << Set the glue in all the unset boxes of the current list, 805 >>
    
    flush_node_list(p);
    pop_alignment();
    // << Insert the current list into its environment, 812 >>
}

Section 801

It’s time now to dismantle the preamble list and to compute the column widths. Let be the maximum of the natural widths of all entries that span columns i through j, inclusive. The alignrecord for column i contains in its width field, and there is also a linked list of the nonzero for increasing j, accessible via the info field; these span nodes contain the value j - i + MIN_QUARTERWORD in their link fields. The values of were initialized to NULL_FLAG, which we regard as .

The final column widths are defined by the formula

where is the natural width of the tabskip glue between columns k and k + 1. However, if for all i in the range 1 i j (i.e., if every entry that involved column j also involved column j + 1), we let , and we zero out the tabskip glue after column j.

computes these values by using the following scheme: First . Then replace by , for all j 1. Then . Then replace by for all j 2; and so on. If any turns out to be , its value is changed to zero and so is the next tabskip.

⟨ Go through the preamble list, determining the column widths and changing the alignrecords to dummy unset boxes 801 ⟩≡

q = link(preamble);
do {
    flush_list(u_part(q));
    flush_list(v_part(q));
    p = link(link(q));
    if (width(q) == NULL_FLAG) {
        // << Nullify |width(q)| and the tabskip glue following this column, 802 >>
    }
    if (info(q) != END_SPAN) {
        // << Merge the widths in the span nodes of |q| with those of |p|, destroying the span nodes of |q|, 803 >>
    }
    type(q) = UNSET_NODE;
    span_count(q) = MIN_QUARTERWORD;
    height(q) = 0;
    depth(q) = 0;
    glue_order(q) = NORMAL;
    glue_sign(q) = NORMAL;
    glue_stretch(q) = 0;
    glue_shrink(q) = 0;
    q = p;
} while (q != null);

Section 802

⟨ Nullify width(q) and the tabskip glue following this column 802 ⟩≡

width(q) = 0;
r = link(q);
s = glue_ptr(r);
if (s != ZERO_GLUE) {
    add_glue_ref(ZERO_GLUE);
    delete_glue_ref(s);
    glue_ptr(r) = ZERO_GLUE;
}

Section 803

Merging of two span-node lists is a typical exercise in the manipulation of linearly linked data structures. The essential invariant in the following repeat loop is that we want to dispense with node r, in q’s list, and u is its successor; all nodes of p’s list up to and including s have been processed, and the successor of s matches r or precedes r or follows r, according as link(r) = n or link(r) n or link(r)  n.

⟨ Merge the widths in the span nodes of q with those of p, destroying the span nodes of q 803 ⟩≡

t = width(q) + width(glue_ptr(link(q)));
r = info(q);
s = END_SPAN;
info(s) = p;
n = MIN_QUARTERWORD + 1;
do {
    width(r) -= t;
    u = info(r);
    while (link(r) > n) {
        s = info(s);
        n = link(info(s)) + 1;
    }
    if (link(r) < n) {
        info(r) = info(s);
        info(s) = r;
        decr(link(r));
        s = r;
    }
    else {
        if (width(r) > width(info(s))) {
            width(info(s)) = width(r);
        }
        free_node(r, SPAN_NODE_SIZE);
    }
    r = u;
} while (r != END_SPAN);

Section 804

Now the preamble list has been converted to a list of alternating unset boxes and tabskip glue, where the box widths are equal to the final column sizes. In case of \valign, we change the widths to heights, so that a correct error message will be produced if the alignment is overfull or underfull.

⟨ Package the preamble list, to determine the actual tabskip glue amounts, and let p point to this prototype box 804 ⟩≡

save_ptr -= 2;
pack_begin_line = -mode_line;
if (mode == -VMODE) {
    rule_save = overfull_rule;
    overfull_rule = 0; // prevent rule from being packaged
    p = hpack(preamble, saved(1), saved(0));
    overfull_rule = rule_save;
}
else {
    q = link(preamble);
    do {
        height(q) = width(q);
        width(q) = 0;
        q = link(link(q));
    } while (q != null);
    p = vpack(preamble, saved(1), saved(0));
    q = link(preamble);
    do {
        width(q) = height(q);
        height(q) = 0;
        q = link(link(q));
    } while (q != null);
}
pack_begin_line = 0;

Section 805

⟨ Set the glue in all the unset boxes of the current list 805 ⟩≡

q = link(head);
s = head;
while (q != null) {
    if (!is_char_node(q)) {
        if (type(q) == UNSET_NODE) {
            // << Set the unset box |q| and the unset boxes in it, 807 >>
        }
        else if (type(q) == RULE_NODE) {
            // << Make the running dimensions in rule |q| extend to the boundaries of the alignment, 806 >>
        }
    }
    s = q;
    q = link(q);
}

Section 806

⟨ Make the running dimensions in rule q extend to the boundaries of the alignment 806 ⟩≡

if (is_running(width(q))) {
    width(q) = width(p);
}
if (is_running(height(q))) {
    height(q) = height(p);
}
if (is_running(depth(q))) {
    depth(q) = depth(p);
}
if (o != 0) {
    r = link(q);
    link(q) = null;
    q = hpack(q, NATURAL);
    shift_amount(q) = o;
    link(q) = r;
    link(s) = q;
}

Section 807

The unset box q represents a row that contains one or more unset boxes, depending on how soon \cr occurred in that row.

⟨ Set the unset box q and the unset boxes in it 807 ⟩≡

if (mode == -VMODE) {
    type(q) = HLIST_NODE;
    width(q) = width(p);
}
else {
    type(q) = VLIST_NODE;
    height(q) = height(p);
}
glue_order(q) = glue_order(p);
glue_sign(q) = glue_sign(p);
glue_set(q) = glue_set(p);
shift_amount(q) = o;
r = link(list_ptr(q));
s = link(list_ptr(p));
do {
    // << Set the glue in node |r| and change it from an unset node, 808 >>
    r = link(link(r));
    s = link(link(s));
} while (r != null);

Section 808

A box made from spanned columns will be followed by tabskip glue nodes and by empty boxes as if there were no spanning. This permits perfect alignment of subsequent entries, and it prevents values that depend on floating point arithmetic from entering into the dimensions of any boxes.

⟨ Set the glue in node r and change it from an unset node 808 ⟩≡

n = span_count(r);
t = width(s);
w = t;
u = HOLD_HEAD;
while (n > MIN_QUARTERWORD) {
    decr(n);
    // << Append tabskip glue and an empty box to list |u|, and update |s| and |t| as the prototype nodes are passed, 809 >>
}
if (mode == -VMODE) {
    // << Make the unset node |r| into an |HLIST_NODE| of width |w|, setting the glue as if the width were |t|, 810 >>
}
else {
    // << Make the unset node |r| into a |VLIST_NODE| of height |w|, setting the glue as if the height were |t|, 811 >>
}
shift_amount(r) = 0;
if (u != HOLD_HEAD) {
    // append blank boxes to account for spanned nodes
    link(u) = link(r);
    link(r) = link(HOLD_HEAD);
    r = u;
}

Section 809

⟨ Append tabskip glue and an empty box to list u, and update s and t as the prototype nodes are passed 809 ⟩≡

s = link(s);
v = glue_ptr(s);
link(u) = new_glue(v);
u = link(u);
subtype(u) = TAB_SKIP_CODE + 1;
t += width(v);
if (glue_sign(p) == STRETCHING) {
    if (stretch_order(v) == glue_order(p)) {
        t += (scaled)round(glue_set(p)*stretch(v));
    }
}
else if (glue_sign(p) == SHRINKING) {
    if (shrink_order(v) == glue_order(p)) {
        t -= (scaled)round(glue_set(p)*shrink(v));
    }
}
s = link(s);
link(u) = new_null_box();
u = link(u);
t += width(s);
if (mode == -VMODE) {
    width(u) = width(s);
}
else {
    type(u) = VLIST_NODE;
    height(u) = width(s);
}

Section 810

⟨ Make the unset node r into an HLIST_NODE of width w, setting the glue as if the width were t 810 ⟩≡

height(r) = height(q);
depth(r) = depth(q);
if (t == width(r)) {
    glue_sign(r) = NORMAL;
    glue_order(r) = NORMAL;
    set_glue_ratio_zero(glue_set(r));
}
else if (t > width(r)) {
    glue_sign(r) = STRETCHING;
    if (glue_stretch(r) == 0) {
        set_glue_ratio_zero(glue_set(r));
    }
    else {
        glue_set(r) = (double)(t - width(r)) / glue_stretch(r);
    }
}
else {
    glue_order(r) = glue_sign(r);
    glue_sign(r) = SHRINKING;
    if (glue_shrink(r) == 0) {
        set_glue_ratio_zero(glue_set(r));
    }
    else if (glue_order(r) == NORMAL && width(r) - t > glue_shrink(r)) {
        set_glue_ratio_one(glue_set(r));
    }
    else {
        glue_set(r) = (double)(width(r) - t) / glue_shrink(r);
    }    
}
width(r) = w;
type(r) = HLIST_NODE;

Section 811

⟨ Make the unset node r into a VLIST_NODE of height w, setting the glue as if the height were t 811 ⟩≡

width(r) = width(q);
if (t == height(r)) {
    glue_sign(r) = NORMAL;
    glue_order(r) = NORMAL;
    set_glue_ratio_zero(glue_set(r));
}
else if (t > height(r)) {
    glue_sign(r) = STRETCHING;
    if (glue_stretch(r) == 0) {
        set_glue_ratio_zero(glue_set(r));
    }
    else {
        glue_set(r) = (double)(t - height(r)) / glue_stretch(r);
    }
}
else {
    glue_order(r) = glue_sign(r);
    glue_sign(r) = SHRINKING;
    if (glue_shrink(r) == 0) {
        set_glue_ratio_zero(glue_set(r));
    }
    else if (glue_order(r) == NORMAL && height(r) - t > glue_shrink(r)) {
        set_glue_ratio_one(glue_set(r));
    }
    else {
        glue_set(r) = (double)(height(r) - t) / glue_shrink(r);
    }
}
height(r) = w;
type(r) = VLIST_NODE;

Section 812

We now have a completed alignment, in the list that starts at head and ends at tail. This list will be merged with the one that encloses it. (In case the enclosing mode is MMODE, for displayed formulas, we will need to insert glue before and after the display; that part of the program will be deferred until we’re more familiar with such operations.)

In restricted horizontal mode, the clang part of aux is undefined; an over-cautious Pascal runtime system may complain about this.

⟨ Insert the current list into its environment 812 ⟩≡

aux_save = aux;
p = link(head);
q = tail;
pop_nest();
if (mode == MMODE) {
    // << Finish an alignment in a display, 1206 >>
}
else {
    aux = aux_save;
    link(tail) = p;
    if (p != null) {
        tail = q;
    }
    if (mode == VMODE) {
        build_page();
    }
}