# Why is % the comment character?

I was reading a book about TeX macros and related features and I came across this rather trivial question...

Can anyone explain to me why `%` is the comment character in LaTeX? Why this one and not another one, is there a particular (historical?) reason for that?

This leads me to another related question: are you using intensively comments in your TeX code? And if yes, for which purpose(s)?

Bonus Question: In the same spirit, why is `\$` the character for the maths mode?

• Matlab also uses it as the comment character, but virtually no other programming language (well, PostScript, but probably as a nod to TeX). This leads me to hypothesise that this might be a convention that has its root in mathematics. Could be completely wrong, of course. – Konrad Rudolph Oct 7 '13 at 0:18
• See also Commenting out large sections. – Pål GD Oct 7 '13 at 6:48
• if you were to ask knuth, i imagine he would say "because they were there". both characters figure more strongly in documents written by financiers (and such like) than in technical documents ... which may be another reason. – wasteofspace Oct 7 '13 at 10:43
• What characters were available? TeX was first implemented with PDP-10 and SAIL. For example C-language still contains trigraphs, so you don't have to have [ etc. in your character code. – Jori Mäntysalo Oct 7 '13 at 13:07
• @KonradRudolph: Prolog does (since 1972); see en.wikipedia.org/wiki/… – Martin Schröder Oct 8 '13 at 22:43

I don't know precisely. The special characters are

``````# \$ % ^ & _ \ { }
``````

The first six are in the upper row of the keyboard, together with `@`. I'm excluding those that are more commonly used in text, that is `! ( ) - + =`. The other non alphabetic ASCII characters (`[];:'"|,<.>/?`) are used in text.

The choice of `\` as the escape character for the commands is almost obvious; also quite obvious is `^` for superscripts and `_` for subscripts.

Some programming or scripting languages use `#` for comments, but it's also commonly used to prefix a number, at least in the US, so it was reserved for prefixing argument placeholders. The `&` became the character for marking alignment points and so the choice for the comment character was between `%` and `@`. The first one won.

OK, there would still be `\$`, but in the TeXbook, page 127, we read

we are using `\$` as the math bracket in this manual, in accord with the plain TeX format defined in Appendix B, because mathematics is supposedly expensive.

• why do you say the choice of `\` is "almost obvious" ? – user4686 Oct 7 '13 at 6:16
• @jfbu It was already used in many languges for "escaping"; say `\n` for denoting an end-of-line. It's a very easy character to type (at least on American keyboards) and has no real usage in text. – egreg Oct 7 '13 at 7:27
• In `errorlog` (the notebook about the development of TeX), I find only a note dated 20 June 1983, when `\catcode`\%` became default in INITEX. But there's no description of what happened before 10 March 1978. `roff`, which can be considered a predecessor of TeX, didn't use `%` for comments. – egreg Oct 7 '13 at 10:22

Why a percent sign? It is obvious: after overusing liquids with percents one forgets everything! ;-)

• Nice try, but I haven't overused those liquids enough tonight to take this answer seriously ;) – Ludovic C. Oct 6 '13 at 23:02
• Do you have any proof? – Sean Allred Oct 7 '13 at 4:05
• @SeanAllred It is certainly a joke, but in the general stream of thinking that TeX is VERY logical. :-) – Przemysław Scherwentke Oct 7 '13 at 5:41
• @PrzemysławScherwentke "Proof" is an antiquated measure of alcoholic content in drinks (though it is another measurement that changes as one traverses the atlantic. I don't remember which way around it is, but 100% alcohol corresponds to 200 proof in one system and 180 proof in the other). (Apologies to Sean for spoiling the joke.) – Andrew Stacey Oct 7 '13 at 8:07
• @Andrew: so when a professor of mine demands proof, I need to pay him a drink? Oh my, mind is blown. `:)` – Paulo Cereda Oct 7 '13 at 15:54

One clue may stem from the fact that postscript also uses % as a comment tag.

It is entirely conceivable, that PS had inherited this syntax from an earlier printer driver language.

I recollect reading (probably in "Digital Typography" by D. Knuth), that development of the early laser printers was one of the motivational factors in development of TeX. Thus, TeX may have inherited the % sign from whatever postscript predecessor that early laser printer installed at Stanford was using.

• It looks like Postscript popped up in '82 whereas TeX showed up in '78. There might yet be some truth to this, albeit not with PS. – Sean Allred Oct 7 '13 at 4:06
• Well, I know this and never implied to the contrary. That's why I was referring to a supposed predecessor (something like Xerox Press - unfortunately I could not find any docs on this one). – oakad Oct 7 '13 at 5:04

The code documentation (i.e., the things commented) is part of the good practices a programmer follows in order to keep track of what he is doing, to leave clear messages for anyone who wants to touch the code and to keep his mind sanity (specially when coming back from long periods of no interaction with the code).

The code documentation should be oriented to demonstrate what your code is doing and where it's doing it, so any modifications can be made as easily as possible.

• I tend to disagree. Code should be self documenting (by the choice of variable names), and using comments to explain intent should be the last resort. Comments should be used to explain high level design of the code. Plain TeX, and to a large extent LaTeX, have some horrible choices of macro names. There seems to be a competition with who can come up with creative use of @ to replace `a`, `o`, `e`; and to remove as many letters as possible (`infinty`, `lssgtr`, etc. LaTeX3 (and to some extent ConTeXt) have made the situation much better now. – Aditya Oct 7 '13 at 19:18
• @Aditya I guess that's a matter of choice. Although I deeply respect your view, I'm very inclined towards having lots of documentation along "self-explanatory" variable names. My head can't get a hold on many things, and because of that I keep a lot of track of what I do. At the same time, this grants me the opportunity to make me code a little more "democratic", due to its natural tendency to explain itself, even though it might seem sometimes redundant. – Mario S. E. Oct 7 '13 at 20:39

`%` is the comment character in LaTeX most probably because it was already the comment character in plain TeX and mentioned in the TeXbook. Note that plain TeX is a format, like LaTeX. The TeX engine sees as comments any character of type category 14. By default, the TeX engine assumes that `%` has category 14. (That does not explain why Knuth chose `%` to have category 14, but that was not the question as written.)

`%` signals a comment in several programming languages, all of which came after TeX (see Wikipedia), except perphaps Prolog, which exists since 1972. Somehow, I doubt that Knuth has been influenced by Prolog, though.

• Surely LaTeX inherited the comment character from Plain TeX: the first LaTeX was simply a set of macros on top of Plain. Consider also that `%` is assigned catcode 14 when INITEX starts. – egreg Oct 7 '13 at 10:24

It was already thus in "TeX and MetaFont", vintage around 1980. Same with @,#,\$,&,^,_, and . There was never any compelling reason to change them. You can, by fiddling with catcodes.