Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I'm using tex a lot more now, and I'd like to get more efficient by typing less. I've partly been inspired by editing mathjax posts and seeing people write things like \frac12. However I've also had some unexpected errors.

For example, writing \eta_\max gives me the dreaded TeX has become confused error, which is a nightmare to debug in a long piece of maths. But I can get away with \eta_\mathrm{max}, if I was willing to use \mathrm incorrectly. And in general there's no need for curly braces with symbols like \infty in expressions like \int_{-\infty}^\infty.

So my question is, when can I get away with dropping the braces and when can I not? And what's the reason I get the error with \eta_\max?

share|improve this question
6  
Use braces. Somebody says to drop them, but, as you discovered, it's not always obvious when you can. Writing \frac12 gives the expected result, but it's not really clear, is it? –  egreg Feb 15 at 0:54
2  
@egreg no it's not obvious! But I write 1/2 so often that \frac12 is a worthwhile shortcut. And I like to know the geeky shortcuts :) –  TooTone Feb 15 at 0:57
    
May be you can get help from your editor but always use braces to be on safer side. –  Harish Kumar Feb 15 at 1:08
    
I usually type [Compose] + [1] + [2] (i.e. ♫12) to get the Unicode symbol ½. But I'm not sure whether this is possible without Neo. –  canaaerus Feb 15 at 9:31
1  
I'd like to be the one to say to drop them ;-) That is, unless the "argument" begins with a \ , drop the braces. I find it silly seeing people use x^{y}_{z}. –  morbusg Feb 15 at 16:19

2 Answers 2

up vote 34 down vote accepted

When TeX is looking for a mandatory argument for a macro, there are two cases:

  1. The macro is followed by a curly brace {: the argument is everything up to the matching brace };

  2. Otherwise the following token (character or control sequence) is the argument.

Thus \frac12 is equivalent to \frac{1}{2}, since the first token is 1 and the following one is 2.

However, _ and ^ are different from macros and this is a reason to prefer always using braces.

As some other primitives of TeX, _ and ^ look for a brace doing expansion as they go. The trick that allows for $f'(x)$ instead of $f^{\prime}(x)$ is based on this: ' is (in math mode) equivalent to a macro and does quite a few things using the further property that the argument to ^ (the same holds for _) can be enclosed by \bgroup and \egroup, which is not allowed for ordinary macros.

So, what happens when you have \eta_\max? TeX finds _, so it expands the following token (because it's not { or \bgroup) and the expansion of \max is

\mathop {\operator@font max}

Now \mathop is not a brace and is not expandable either, so the scanning stops here and TeX issues an error message, because \eta_\mathop is illegal. If amsmath is loaded, the expansion of \max is

\qopname \relax m{max}

and \qopname expands to \protect\qopname and chaos ensues again: \protect is equivalent to \relax (in normal situations), so it's ignored (by rule of TeX, it would be quite long to explain it completely) and the following token is \qopname (with a trailing space) whose expansion starts again with \mathop. Error.

The same would happen with X^\notin and several other single tokens.

Why does \eta_\mathrm{max} work? Because \mathrm{max} essentially does

{<choose the upright math font>max}

with an additional pair of braces to keep <choose the upright math font> local. Those braces are what _ wants to identify the material to typeset as subscript.

Use braces and you'll not be bitten by strange behaviors.

share|improve this answer
1  
Thankyou that's a great and comprehensive answer. I like to know how things work under the covers and the difference between the behaviour of macros vs _ and ^ was something I was unaware of and is particularly significant. I will be particularly conservative with the latter! –  TooTone Feb 15 at 1:54
    
@TooTone I'd add that using MathJax as a model is not safe: I tried and $a_\max$ works. MathJax uses a different interpretation, not based on TeX way of doing things. –  egreg Feb 15 at 11:17
    
When you say "However, _ and ^ are different from macros and this is a reason to prefer always using braces.", is there any reason I should use $a^{2}$ instead of $a^2$? –  mrc Feb 15 at 21:51
    
@mrc If you get into the habit of always using braces, strange problems will not bite you. There's no problem in doing $a^2$, but this will open the door to forgetting braces when they're necessary. –  egreg Feb 15 at 21:52
    
@egreg re MathJax, good point, I'm finding it inspiring in the sense that I'm seeing other people write in a tex-like form. Previously I've just used tex for assignments, for taking notes, and so on, and it's hard to get better if you never see, admire and copy what your contemporaries are doing. –  TooTone Feb 16 at 23:55

I would not say that you should always use braces, but if you leave them out you should know why they can be left out. Basically leaving out braces in a subscript or superscript is safe whenever it is a single token that either does not expand at all, or expands to a single unexpandable token, or expands to stuff enclosed in braces. So $a_i$ is definitely fine, and $x_\alpha$ is too unless \alpha has been redefined in a unusual way (its standard definition is as an unexpandable math symbol), and if \C is defined to give a symbol for the complex numbers then $f_\C$ probably needs no braces either; however I say the latter only because I think most reasonable definitions would either define it as an unexpandable symbol or make it (ultimately) expand to stuff enclosed in braces. Of course in situations where you cannot be sure how a symbol is defined, it would be prudent to put in braces.

On the other hand one can be almost certain, without looking up the definition, that $\eta_\max$ will not work. This is because \max will not expand to a single symbol (after all its representation contains three letters), and since it is supposed to stand for a math operator, its expansion cannot be brace-enclosed: when enclosed in braces, any math symbol with a specific spacing rules (relation, operator) is demoted to an ordinary math symbol; to get the right spacing \max must avoid having a brace-enclosed expansion.

As a rule of thumb, I would say it is only worth the effort of checking the definition to see whether braces can be omitted in subscripts and superscripts given by a control sequence, when that sequence designates single symbol of the ordinary math category. So I would write $f_{\leq}$ since \leq is a relation symbol (even though its definition in plain TeX is a \mathchardef, allowing braces to be omitted). In the case of a non-active single character one can always omit the braces, so $f_<$ should be OK unless < has been made an active character, but one should be aware of characters like ' that become active in math mode.

share|improve this answer
    
"\max will not expand to a single symbol". Nice way to see why, e.g., \infty or \alpha is likely to work but \max is not. –  TooTone Feb 15 at 18:59
3  
Well, also \neq and \cong could be considered as a single symbol: try $A_\neq$. ;-) The problem is that it's not always obvious what is considered a single symbol for TeX. –  egreg Feb 15 at 21:56
    
@egreg I never said or implied that braces can be omitted in case of control sequences that would seem to correspond to a single symbol (though if you know them to be defined by \mathchardef it wil be OK); I just said that in case of \max one can be pretty sure it is not a single symbol, and one cannot expect it to work. –  Marc van Leeuwen Feb 16 at 9:52

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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