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}
\]
Result
The compiled example

Open in the editor

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
\]
Result
The compiled example

Open in the editor

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.

11+1xx=a0+1a1+1a2+1a3\frac{1}{1 + \frac{1}{x}} \qquad x = a_0 + \cfrac{1}{a_1 + \cfrac{1}{a_2 + \cfrac{1}{a_3}}}

Open this formula in the editor

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.
Result
The compiled example

Open in the editor

Pitfalls

Compiled with pdfLaTeX from TeX Live 2023.

Related pages