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

Doug McKenna doug at mathemaesthetics.com
Tue Jun 28 01:03:24 CEST 2011

David Arseneau wrote:

>> \halign{}#{\cr A\cr}

>You are misreading the error message.  The message says that there
>is a missing \cr before the (template-inserted) brace, but TeX has
>already read as far as the A before the error was detected.  Of
>course that has to be the case because the u-template isn't inserted
>until TeX sees the A.

I understand why the error message issues when it does, and that there's
still misleading, because a missing \cr (one of which occurs on either
side of the 'A' in the source) isn't really the cause of the error, which
is more due to reversed (but still nominally balanced) braces in the
column's template declared in the preamble.

Error messages in such computer language situations are always dicey (a
single missing or extra brace or semicolon in a C compilation can cause
hundreds of error messages, not one of which tells you what or where the
problem is).  What I was interested in is whether the lack of an error
message about reversed or unbalanced braces during preamble template
collection for the column was a bad idea in some weird situation I can't
think of.  Because otherwise, it seems like poor design or
implementation, at least from the user's (my) point of view.  But such
conclusions are often made naively without a thorough understanding of
the system, so I would love to be proven wrong with a counter-example.

Uwe Lueck wrote:

>Doug, please present that minimal file that produces
>the error you are reporting.

Here's some examples (TexLive 2010, via TeXShop using standard LaTeX
format):

--------------------------------------------
\documentclass[11pt]{article}
\begin{document}
\title{Test}
\author{\TeX}
\maketitle

Several successful-because-they-are-empty alignments follow:
\tabskip 32pt
\halign{#\cr}
\halign{{#}\cr}
\halign{}#{\cr}
\halign{}}}}}}}}}#{{{{{{{{{\cr}

%\halign{}}#{\cr} % Uncommenting makes the next, otherwise legal,
alignment, illegal

Some non-empty and legal alignments, omitting some strange template
material in column 2, follow:
\halign{#&}#{\cr A&\omit B\cr}
\halign{#&}}}}Foo}}}}#{{{{{Bar{{{\cr A&\omit B\cr}

Alignments that, when uncommented, issue a confusing error message:
%\halign{#&}#{\cr A&B\cr}
%\halign{}#{\cr A\cr}

Alignments that, when uncommented, lead to a runaway preamble message:
%\halign{}#\cr A\cr}
%\halign{#&}}}}}}}#{{{{{{\cr A&\omit B\cr}
%\halign{}}#{\cr}

\end{document}
--------------------------------------------

The above demonstrates another weird situation with \halign.  The line in
the above code:

%\halign{}}#{\cr} % Uncommenting makes the next, otherwise legal,
alignment, illegal

When left commented, the document as shown above has no errors.  But if I
uncomment that line, I get the error message:

./Alignment.tex:17: Only one # is allowed per tab.
l.17 \halign{#
&}#{\cr A&\omit B\cr}

At first, I didn't look closely, and thought the error was for the
just-uncommented \halign.  But later, I realized that the error message
is for the next (formerly legal) \halign, not the one I uncommented and
for which no error was posted.

Now that's pretty confusing, but again, has to do with weirdly matched
braces that for some reason aren't complained about when they occur in
the earlier \halign's preamble.

Anyway, I appreciate the feedback on my puzzle, and perhaps this
discussion in the archives will help some future poor soul understand the
\halign command's somewhat weird and/or ad hoc syntax vis-a-vis
brace-counting and/or matching and/or reverse-ordering in column
templates in the preambles of alignments.

Doug McKenna