[texhax] What's legal about \halign and backward braces?

Doug McKenna doug at mathemaesthetics.com
Thu Jun 23 00:12:31 CEST 2011


Greetings.

I'm trying to understand the design behind why TeX's \halign command 
behaves in a certain way with respect to braces in the preamble.

I thought I'd see what TeX does when presented with (what I naively 
thought was) the simplest, do-nothing form of the command:

\halign{}

This unexpectedly (to those versed in usual computer language syntax) 
leads to a runawy preamble error, because the '}' doesn't end the 
command's parsing; a # and then a \cr must happen first, and they're 
missing.

I've read the Alignment chapter in The TeX book, including its discussion 
of templates of the form "{#}".  I've read the chapter in TeX by Topic.  
I've searched and read this list's archives on \halign.  I've poked at 
the interpreter (TexLive 2010) to see how it actually behaves.  And I've 
tried to understand the source code, which doesn't answer the question 
either (well, except perhaps deeply implicitly, given enough time).

Which is to say, I think I understand how \halign all works in the usual 
ways.  But the question remains: under what circumstances (if any) is it 
legal to include in a column template a '}' that is unmatched by some 
previous '{'?  The following, for example, appears upon testing to be a 
perfectly legal alignment command:

\halign{}#{\cr\omit A\cr}

It merely places "A" in a single column in a single row.  The u part of 
the column's template in the preamble is "}" and the v part is a 
nominally matching "{", so the braces conform to the rule that they be 
matched across the entire template, even though they are in reverse 
order.  The \omit suppresses their introduction later, so their reverse 
order has no effect, and hence the alignment executes fine.  But if one 
omits the \omit, things go wrong: TeX's error message says there's a 
missing \cr right after the "A", which is pretty confusing, since there 
IS a \cr right after the "A", both in one's source code and in the line 
shown in the error message.  And it thereby appears that the u part's 
'}', executed just before the "A", was legal, and ended some kind of 
group level, so that the error occurred executing the v part.

The fact that TeX didn't complain about the first '}' when scanning the u 
part in the first place, or when executing it in the second place, kind 
of implies (to me) that there are such constructions that don't always 
cause trouble later on after the preamble is recorded.

I thought perhaps a "}#{" template was a way to turn off the implicit 
group that TeX places around each column's processing, thereby allowing 
side effects to leak out to subsequent columns if desired.  But 
apparently that's not the case, because TeX still complains, and it's not 
clear how the second brace re-instates (balances) that implicit group 
anyway.

Can any cogniscenti here give an example of an alignment of the form

\halign{}...#...{\cr}

that (without suppressing the column's template with \omit) remains error 
free?  What am I missing?  On the other hand, if there are no such legal 
constructions, what is TeX's purpose in not flagging the unmatched brace 
error at the time it is syntactically committed scanning the template, or 
later on when when the u part is inserted into the input and executed?

Many thanks in advance for any elucidation.


Doug McKenna



More information about the texhax mailing list