Fractions in LaTeX (frac, dfrac, cfrac)
\frac{numerator}{denominator} writes a fraction in math mode. \dfrac and \tfrac from amsmath set the size, and \cfrac is made for continued fractions.
Preamble
The snippets on this page need these lines before \begin{document}:
\usepackage{amsmath}
\usepackage{nicefrac}Write a fraction
\frac takes the numerator first and the denominator second. It is part of LaTeX and works only in math mode: between $...$ in a line of text, or in a display formula such as \[...\].
The mean is $\frac{a+b}{2}$.
\[
\frac{x^2 + 1}{x - 1}
\]Set the size with dfrac and tfrac
In a line of text LaTeX sets a fraction in a smaller size so the line spacing stays even. \dfrac from amsmath forces the large size of a display formula, and \tfrac forces the small text size inside a display formula.
Small: $\frac{1}{2}$, large: $\dfrac{1}{2}$.
\[
\frac{1}{2}x + \tfrac{1}{2}y
\]Nested and continued fractions
A fraction inside a fraction is set smaller than the outer one. For a continued fraction use \cfrac from amsmath, which keeps every level at full size.
\[
\frac{1}{1 + \frac{1}{x}}
\qquad
x = a_0 + \cfrac{1}{a_1 + \cfrac{1}{a_2 + \cfrac{1}{a_3}}}
\]Change the formula
The preview is drawn in your browser by KaTeX, which knows the common math commands. The editor compiles the formula with pdfTeX.
Slanted fractions in text
\nicefrac{1}{2} from the nicefrac package raises the numerator, lowers the denominator and puts a slash between them. It works in text and in math mode.
Add \nicefrac{1}{2} litre of milk and \nicefrac{3}{4} cup of sugar.Pitfalls
\fracoutside math mode stops with Missing $ inserted.\frac12works without braces because each argument is then a single character. As soon as an argument is longer, as in\frac{1}{12}, it needs braces.- The plain TeX form
{a \over b}still appears in old documents. Withamsmathloaded it produces the warning "Foreign command \over"; write\frac{a}{b}instead. \sfracfromxfracis an alternative to\nicefrac. It needs a font that scales to any size: with the default Computer Modern fonts the log fills with size substitution warnings unless you loadlmodern.
Compiled with pdfLaTeX from TeX Live 2023.
Related pages
- Package
amsmath - Package
nicefrac - Package
xfrac - How-to Binomial coefficient in LaTeX
- Error Missing $ inserted