18

Consider the following m(non-)we:

\documentclass{standalone}
\begin{document}
Foo & Bar
\end{document}

The build will fail with

! Misplaced alignment tab character &.

while it will generate a perfectly fine PDF when removing the & character from the code.

Even plain TeX behaves like that and

Foo & Bar
\bye

yields the same error.

I have only ever seen & used in tabular and array environments, never outside of it. Why is that?

Couldn't one define e.g. \tabular and \endtabular so that & is only special inside the environment, and just a normal character outside of these environments?

Why does & outside of tabular and array environments have to break the document?

To rephrase the question: WTF is an alignment tab character and where do we usually put it?

  • 2
    The & is a character with special code and reserved for alignment purposes, it acts a delimiter for table cells and allows for nested alignment groups. If it's not & it must have been another symbol or macro. This is by design of TeX already. If you want the literal &, then use \&. See also chapter 22 of The TeXBook – user31729 Jun 7 '16 at 8:53
  • 4
    On a related note, ConTeXt redefines the catcode of &, so \starttext A & B \stoptext works and prints "A & B". But there you have other macros for separating cells (see e.g. wiki.contextgarden.net/Tables_Overview). – Torbjørn T. Jun 7 '16 at 9:11
  • 9
    If the catcode were different outside tabulars and you would have to change the catcode of & in the code of \begin{tabular} you could no longer use tabulars in the argument of other commands. and it would be difficult to store parts of a tabular row in commands. tabular would be a bit like a verbatim command. That's something one should better avoid. – Ulrike Fischer Jun 7 '16 at 9:23
  • 2
    I find it more consistent to always use \& for a printed ampersand. – egreg Jun 7 '16 at 9:24
  • 1
    Assume you \def\firm{Foo & Bar}, which would be usable outside of tabulars. What would you expect to happen if \firm occurs within a tabular? – Hagen von Eitzen Jun 7 '16 at 13:42
20

Let's assume that the format or the user doesn't set the catcode of & (or any other symbol) globally to 4.

What would be the consequences assuming that you still want to have locally such a character (and not use commands like \nc for "new column" all the time)?

  1. Every tabular like environment would have to make sure to set a char to be the alignment tab -- and they must agree which one to use. This would involve a lot of patching. This is naturally not an argument why it hasn't be done when the format was written.
  2. It wouldn't be possible to use this character if the tabular is used in the argument of some command. E.g. you would run into problems when using tabulars in footers and headers, in addresses of letters, in author lists, in beamer frames. One could naturally define a command as a replacement, but users would have to understand when this command is needed.
  3. It would be rather difficult to disable locally the alignment tab. E.g. currently you can easily type a tabular with lots of & like this:

    \documentclass{article}
    
    \begin{document}
    \begingroup
    \catcode`\&=12
    \catcode`!=4
    \begin{tabular}{ll}
    a & b ! c & d
    \end{tabular}
    \endgroup    
    \end{document}
    

    But if tabular resets the catcode of & it will no longer work. So you would need some hooks to allow such manipulations -- and again in every tabular like environment.

On the whole a lot things would get much more difficult for package writers and for users. It is easer to learn to type \&.

  • I'm not fully buying that argument. It assumes in my opinion that one does have to handle aligment using catecode 4 chars. But this isn't quite the case, is it? If one assumes & to have catcode 13 all points above could be made to work (only with slighty different approaches. I'm not saying that nothing would break (for existing documents) but this is more or less true for any change. However, an active catcode would work equally well and for some scenarios even better. Ony exception is the hidden company name\newcommand\foo{Foo & Friends} that then would result in a surprise inside a table. – Frank Mittelbach Jul 26 '17 at 8:18
  • @FrankMittelbach: using a catcode 13 & which has everywhere the meaning "aligment char" could probably work too (and as a command allow for more tricks) but then you would still not be able to type "friends & foo" directly outside tabulars. As the question was less about the catcode and more about the "why global and not local" my answer is more about this part. (Btw: I'm rather certain that a catcode change would break something, e.g. I just saw a package which used & to separate arguments). – Ulrike Fischer Jul 26 '17 at 8:47
  • I was thinking of & catcode 13 always and expanding to \& when typeset outside halign and expanding to \nc inside halign (with \nc generating a catcode 4 char). So you can typeset foo & friends outside. the package separating args would also work and so would most packages right now. There are of course a few tricks that would need to be replaced with new code but not that much. And your trick in point 3 above could probably be done somewhat cleaner too :-). 'm not saying this wouldn't be a huge change and it will affect some documents and packages but it could make for a better solution. – Frank Mittelbach Jul 26 '17 at 9:31
  • @FrankMittelbach But then one would have to type "friends & foo" inside a tabular differently to outside. How do you explain this to users? tabulars are used in a lot of "hidden places", e.g. in address fields or headers. You only need to look at babel shorthands to see some of the potential difficulties of changing "meaning" depending on the context, e.g. how they clash with tikz syntax. – Ulrike Fischer Jul 26 '17 at 10:32
  • Hidden tabulars should not exist (or rather there is no need for them to use & internally, so they could keep it for text). As for context changes being difficult in places, yes for sure, but that doesn't mean they aren't a viable concept. The reason that babel shorthand are so much of a problem is not so much that they exist but that they "may" exist and that internally we deal with catcode changes rather than with a uniform base concept for context changes. – Frank Mittelbach Jul 26 '17 at 10:40
13

You ask:

Why is & special outside of tabular environments?

I think the question is not well-posed. I would say that & is special everywhere, not just outside tabular environments. The character & has category code 4 -- "alignment tab" -- in all TeX and LaTeX formats I've ever encountered. This catcode setting allows it to be used as the alignment tab character not only in tabular-like environments (e.g., tabular*, tabularx, longtable, etc), but also array environments, the deprecated eqnarray environment, some of the display math environments of the amsmath package, the IEEEeqnarray environment of the IEEEtrantools package, and probably quite a few additional environments.

The virtue of assigning catcode 4 to & by default is that all package writers can (and, frankly, should ) write their code assuming that & has catcode 4. Without this default, you'd have some package writers choosing & as the alignment tab character, whereas others might choose @ or who knows what else. Such an outcome would be distinctly inferior for most users, since they'd have to memorize which character, or characters, serve as the alignment tab function in which environments.

Conversely, if one does not want & to be special -- and doesn't want to modify its catcode -- one must input it as \&.

  • 1
    I asked the question like that, because it is very easy to find out what & does in tabular and array environments, but I don't know about its use outside of these environments. Apparently there is none, we just always have & as a special character for consitency reasons. Ulrike mentioned a few problems that could arise if & was only special inside these environments. – Bananguin Jun 7 '16 at 10:01
  • 1
    I think a positive example would do some justice with \verb<char>...<char> for a nonpersistent catcode allocation to emphasize that this was also a deliberate choice. – percusse Jun 7 '16 at 11:20
  • @percusse - A really good idea. :-) May I suggest you provide an answer that provides such a positive example? – Mico Jun 7 '16 at 13:36
  • I don't think I have the TeXpertise :) It's merely an observation – percusse Jun 7 '16 at 14:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.