110

What is the difference between {some-expression}\over{another-expression} and \frac{some-expression}{another-expression} ?

Which is preferable?

0

4 Answers 4

117

The command \over is a so-called "primitive" command that's available in Plain TeX and in LaTeX. \frac is a LaTeX-only command that builds on the \over macro to provide something that's much less likely to throw unexpected errors if the user doesn't handle the syntax of the \over command just right.

Assuming you have the amsmath package loaded, the \frac command is defined as follows:

\DeclareRobustCommand{\frac}[2]{{\begingroup#1\endgroup\@@over#2}}

where \@@over is an amsmath-internal version of \over. Note that the first argument of \frac (the numerator) is carefully encased in a \begingroup ... \endgroup construct and that the entire command's "content" is surrounded by a double set of curly braces. The braces serve to remove potential ambiguities that might arise from a less than fully careful use of the \over command by itself.

If you're a user of LaTeX, there's no reason whatsoever for not using the \frac command -- and several good reasons for not using the \over command directly. In short, if you use LaTeX, use \frac.

5
  • 16
    I would go as far as to say that including \over (and friends \atop etc.) were a bad design decision by Knuth. TeX uses different layout style for display mode, text mode, superscripts, and subscript. However, because of macros like \over, when TeX is building the math token list, the current math mode is not known. This causes all sorts of difficulties like having to use \mathchoice in simple macros like \text. If macros like \over are ruled out, the design of a lot of macros will simplify a lot.
    – Aditya
    Commented Sep 24, 2012 at 21:14
  • 2
    Could you give an example of the sort of problems that could occur with \over? Commented Jul 25, 2016 at 7:03
  • @BlueRaja-DannyPflughoeft - Did you see the example given in egreg's answer?
    – Mico
    Commented Jul 25, 2016 at 9:32
  • 13
    I've discussed this with Knuth. His rationale for using \over rather than \frac is that it's the natural vocabulary of a mathematician, and his model for TeX was that of a mathematician, not a computer scientist. (One concludes that there are concepts which are for him more important than ease of programming.) Commented Oct 5, 2019 at 20:17
  • "No reason whatsoever" is very strong. I use LaTeX to write notes in real time in class; \over is essential for that usecase. Curly braces require a chord (2 simultaneous keypressses) to type, and so halving the number of braces I need to write is a major speedup. The note from @barbara-beeton on Kunth's intentons is also relevant. If, however, you are trying to write robust LaTeX code, then \frac is probably the way to go. Commented Apr 8 at 17:09
42

The latter is preferable. It corrects a behavior that in some situations can reveal itself in a bad way. The LaTeX definition is

\def\frac#1#2{{\begingroup #1\endgroup\over #2}}

In some cases an assignment made in the numerator can affect also the denominator. It's difficult to produce it with standard LaTeX commands, but you can compare the result of

\[
\fam0 a\over b
\]

with

\[
\frac{\fam0 a}{b}
\]

In the first case both "a" and "b" will be upright, which is probably not expected.

Another relevant argument against the \over syntax is that it's foreign to LaTeX, which always uses first the command and then its argument and never an "infix" syntax. Moreover the grouping around numerator\over denominator is automatically provided, avoiding mistakes such as

1\over 2 \over 3

Update

Starting from the 2019-10-01 release of LaTeX, the definition of \frac uses \DeclareRobustCommand, as part of a program to remove as many fragile commands as possible:

% latex.ltx, line 4705:
\DeclareRobustCommand\frac[2]{{\begingroup#1\endgroup\over#2}}

In olden times it was not possible to make all commands robust, because of memory restrictions. These considerations are no longer a problem.

The amsmath package has used \DeclareRobustCommand for its modified definition of \frac for many years.

2
  • never an "infix" syntax … except for with \choose Commented Sep 26, 2012 at 22:36
  • 4
    @MikaelVejdemo-Johansson \choose is not documented in the LaTeX manual, as far as I know. :)
    – egreg
    Commented Sep 26, 2012 at 22:39
21

In User’s Guide for the amsmath Package p.14 it is written the following:

The primitive generalized fraction commands \over, \overwithdelims, \atop, \atopwithdelims, \above, \abovewithdelims produce warning messages if used with the amsmath package, for reasons discussed in technote.tex.

And in Technical notes on the amsmath package p.2:

Not only is the unusual syntax of the TeX primitives rather out of place in LaTeX, but furthermore that syntax seems to be responsible for one of the most significant flaws in TeX's mathematical typesetting capabilities: the fact that the current mathstyle at any given point in a math formula cannot be determined until the end of the formula, because of the possibility that a following generalized fraction command will change the mathstyle of the preceding material.

(...)

There are additional bad consequences following from the syntax of those generalized fraction commands that only become evident when you do some writing of nontrivial macros for math use. For example, as things currently stand you cannot measure the size of any object in math without going through \mathchoice and leaving and reentering math mode via \hbox{$ (which then introduces complications regarding \everymath and \mathsurround). And it seems that uncertainty about the current mathstyle is the only barrier to allowing the use of mu units with \vrule, to make vertical struts in constructing compound symbols or notation. And so on and so forth.

3
  • The hyperlinks you provided don't appear to work anymore. Can you provide updates?
    – Mico
    Commented Dec 31, 2020 at 9:15
  • 1
    Links ave been repaired. Commented Jan 11, 2021 at 2:51
  • 1
    To be fair, you avoiding \over will probably not make TeX change to fix that oddity because of the stability requirement. (by the way LuaTeX has \Ustack to fix that issue already)
    – user202729
    Commented Mar 30, 2022 at 7:24
0

In some cases, I found \over doesn't need curly braces whereas \frac does.

\[ P(n,m) = {n! \over (n-m)!} \] <!-- { } for {n! \over (n-m!)} -->
\[ n! \over (n-m)! \]
\[ P(n,m) = \frac{n!}{(n-m)!} \] <!-- { } for \frac {} {} -->
\[ \frac{n!}{(n-m)!} \]
1
  • 1
    Welcome to TeX.SE! Please add an screenshot of your result and explain the difference.
    – Mensch
    Commented Sep 22, 2021 at 9:50

You must log in to answer this question.

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