3

I am trying to define something like $\frac\mathtt{A\_B}\mathtt{C\_D}$ to get a fraction of two typewriter-font variables (inline math preferred), but I keep getting errors such as

"missing } inserted"

and the result is definitely wrong. What am I doing wrong?

  • 4
    $\frac{\mathtt{A_B}}{\mathtt{C_D}}$ ... you need some extra braces. – Steven B. Segletes Jan 7 '15 at 18:04
  • Even when you recall the proper syntax, it is easy to forget braces when typing. Often, when doing something complicated, I will type in empty braces for the arguments and later fill them in. Thus, here I might type \frac{}{}, and then go in and fill in \frac{\mathtt{}}{\mathtt{}}. Finally, I would insert A\_B and C\_D as the final step. It helps to avoid getting confused. – Steven B. Segletes Jan 7 '15 at 18:46
  • 1
    Surprisingly, it helped! I used to type things such as \frac 1 2, and assumed a LaTeX command would be also treated as a single element, but I guess there is more to it. – Leo Jan 7 '15 at 18:50
  • There are some macros that if you omit the braces, it will take the next [single] token as the argument. And there are other macros where it will protest the omission of braces. Unless you know that a macro is happy with the braces omitted, it is safer to retain them by way of habit. – Steven B. Segletes Jan 7 '15 at 18:52
  • 2
    Whoever maintains that \frac 1 2 is good input should look at this question. – egreg Jan 7 '15 at 18:54
4

No, \frac can't know where the numerator ends and the denominator begins nor when the denominator ends, unless you tell it precisely.

The macro \frac has two arguments, so its syntax is

\frac{<numerator>}{<denominator>}

and it's better to keep this in mind.

It's true that \mathtt needs an argument, so it's clear to a human reader what the numerator and denominator should be in

\frac\mathtt{A\_B}\mathtt{C\_D}

However, TeX is a computer program, not a human being, and it need precise instructions.

Speaking generally, macros are processed from left to right, so what TeX sees is \frac and it knows it should receive two arguments.

The rule how an argument is determined, when looked for is quite simple:

  1. spaces are ignored until finding something which is not a space;

  2. if the next token is a {, the argument is whatever goes from { up to the matching };

  3. otherwise the next token is the argument.

When rule 2 is used, the braces are stripped off after the argument has been determined.

It's by rules 1 and 3 that an input such as \frac 1 2 leads to a properly formatted fraction. In fact, TeX sees 1 and determines it's the first argument to \frac, then it ignores the space and determnes that 2 is the second argument.

Similarly, with your input, \mathtt is determined to be the first argument and {A\_B} is the second one! So your input is completely equivalent to

\frac{\mathtt}{A\_B}\mathtt{C\_D}

which will lead to a malformed numerator and so to weird error messages.

Conversely, with

\frac{\mathtt{A\_B}}{\mathtt{C\_D}}

the fraction will be typeset correctly.

Moral of the story: always use braces around arguments. When you're an experienced TeX user, you may know when it's safe to omit the braces. But be careful:

\frac 11 2

will possibly surprise you! Read again rules 1 to 3 and you'll discover that typing

\frac{11}{2}

is the best strategy. And \frac{1}{2} too.

  • missed a pair. fixed. – barbara beeton Jan 7 '15 at 20:37
  • @barbarabeeton I was answering while sipping a good glass of wine on the train, so I got distracted. ;-) – egreg Jan 7 '15 at 20:37

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.