17

I find LaTeX a little bit inconsistent on this. Do the following experiments:

  1. The code

    a.%
    b
    

    will produce a.b, while the code

    a.
    b
    

    will produce a. b. Looks like the comment symbol % has removed the carriage return at the end of the line. However,

  2. The code

    \ta%
    u
    

    will produce an error (assuming you haven't defined \ta), much like

    \ta
    u
    

    but should the % here also remove the carriage return and combining the two lines into the meaningful \tau?

2
  • 1
    Erh, a. % b produces a. , under normal conditions, everything from % until the end of the line is ignored, including the newline at the end and the %
    – daleif
    Nov 13, 2015 at 20:16
  • Also note that while \ta%⏎u doesn’t work, \tau%⏎dt is one way to produce τdt without a space (like \tau{}dt or {\tau}dt would). Nov 14, 2015 at 15:12

2 Answers 2

26

Like almost all programming languages, comment interpretation happens after tokenization.

\ta% produces the token \ta which will then fail to expand and genenerate an error before the u is seen at all.

this is no different from C or javascrpt etc where you can not use a comment in the middle of a function name, or XML/HTML where you can not use a comment in the middle of an element name.

19

It's consistent with the rules. When TeX finds an end-of-record marker (end of line, if you prefer) as determined by the operating system, it throws it away together with possible trailing spaces and tabs; it then appends the current \endlinechar, usually ASCII 13 (represented in TeX as ^^M). At this stage, tokenization has not yet taken place.

Now tokenization starts. Under normal conditions, % has category code 14 (comment) and ASCII 13 has category code 5. When TeX finds a character of category code 14, it throws away it with everything that follows on the line. A category code 5 character inserts a space, throws away what follows on the line and triggers looking for and removing spaces on the next line; if another category code 5 characters is found after this removal, a \par token is inserted, otherwise processing starts from the found character.

With the input

a%
b

the letters “a” and “b” are typeset without any intervening space, because the one inserted by ^^M follows the comment character.

With the input

a
b

the space inserted by ^^M is seen and inserted in the output.

For understanding what happens with

\ta%
u

one should remember how control sequences are formed. If the character following the backslash is a letter, TeX forms the name from all letters following it, stopping when a non letter (TeXnically, a character with category code different from 11) is found. If the character following the backslash is not a letter, that single character forms the control sequence name.

Since % has category code 14, it stops looking for the control sequence name, so you get \ta followed by u, not \tau.

Note that the rules imply that an input such as

a\
b

makes TeX form the control sequence \^^M and, indeed, plain TeX and LaTeX define \^^M to be the same as (backslash space).

Not the answer you're looking for? Browse other questions tagged .