跳到内容

Introduction

This article explains how to typeset fractions and binomial coefficients, starting with the following example which uses the amsmath package:

\documentclass{article}
\usepackage{amsmath}
\begin{document}
The binomial coefficient, \(\binom{n}{k}\), is defined by the expression:
\[
    \binom{n}{k} = \frac{n!}{k!(n-k)!}
\]
\end{document}

 Open this example in Overleaf.

The amsmath package is loaded by adding the following line to the document preamble:

\usepackage{amsmath}

Here is the output produced:

Image showing LaTeX typesetting of the binomial coefficient

Displaying fractions

The visual appearance of fractions will change depending on whether they appear inline, as part of a paragraph, or typeset as standalone material displayed on their own line. The next example demonstrates those changes to visual appearance:

\documentclass{article}
% Using the geometry package to reduce
% the width of help article graphics
\usepackage[textwidth=8cm]{geometry}
\begin{document}
Fractions can be used inline within the paragraph text, for 
example \(\frac{1}{2}\), or displayed on their own line, 
such as this:
\[\frac{1}{2}\]
\end{document}

 Open this example in Overleaf.

This example produces the following output:

Demonstrating inline and display math modes in LaTeX

  • Note: More information on inline and display versions of mathematics can be found in the Overleaf article Display style in math mode.

Our example fraction is typeset using the \frac command (\frac{1}{2}) which has the general form \frac{numerator}{denominator}.

Text-style fractions

The following example demonstrates typesetting text-only fractions by using the \text{...} command provided by the amsmath package. The \text{...} command is used to prevent LaTeX typesetting the text as regular mathematical content.

\documentclass{article}
% Using the geometry package to reduce
% the width of help article graphics
\usepackage[textwidth=8cm]{geometry}
\usepackage{amsmath}% For the \text{...} command
\begin{document}
We use the \texttt{amsmath} package command
\verb|\text{...}| to create text-only fractions
like this:

\[\frac{\text{numerator}}{\text{denominator}}\]

Without the \verb|\text{...}| command the result 
looks like this:

\[\frac{numerator}{denominator}\]
\end{document}

 Open this example in Overleaf.

This example produces the following output:

Textual fractions

Size and spacing within typeset mathematics

The size and spacing of mathematical material typeset by LaTeX is determined by algorithms which apply size and positioning data contained inside the fonts used to typeset mathematics.

Occasionally, it may be necessary, or desirable, to override the default mathematical styles—size and spacing of math elements—chosen by LaTeX, a topic discussed in the Overleaf help article Display style in math mode.

To summarize, the default style(s) used to typeset mathematics can be changed by the following commands:

  • \textstyle: apply the style used for mathematics typeset in paragraphs;
  • \displaystyle: apply the style used for mathematics typeset on lines by themselves;
  • \scriptstyle: apply the style used for subscripts or superscripts;
  • \scriptscriptstyle: apply the style used for second-order subscripts or superscripts;

which are demonstrated in the next example.

\documentclass{article}
% Using the geometry package to reduce
% the width of help article graphics
\usepackage[textwidth=9.5cm]{geometry}
\begin{document}

Fractions typeset within a paragraph typically look like this: \(\frac{3x}{2}\). You can force \LaTeX{} to use the larger display style, such as \( \displaystyle \frac{3x}{2} \), which also has an effect on line spacing. The size of maths in a paragraph can also be reduced: \(\scriptstyle \frac{3x}{2}\) or \(\scriptscriptstyle \frac{3x}{2}\). For the \verb|\scriptscriptstyle| example note the reduction in spacing: characters are moved closer to the \textit{vinculum} (the line separating numerator and denominator).

Equally, you can change the style of mathematics normally typeset in display style:

\[f(x)=\frac{P(x)}{Q(x)}\quad \textrm{and}\quad \textstyle f(x)=\frac{P(x)}{Q(x)}\quad \textrm{and}\quad \scriptstyle f(x)=\frac{P(x)}{Q(x)}\]
\end{document}

 Open this example in Overleaf

This example produces the following output:

Image showing changes to LaTeX default math styles

Continued fractions

Fractions can be nested to obtain more complex expressions. The second pair of fractions displayed in the following example both use the \cfrac command, designed specifically to produce continued fractions. To use \cfrac you must load the amsmath package in the document preamble.

\documentclass{article}
% Using the geometry package to reduce
% the width of help article graphics
\usepackage[textwidth=9.5cm]{geometry}
% Load amsmath to access the \cfrac{...}{...} command
\usepackage{amsmath}
\begin{document}
Fractions can be nested but, in this example, note how the default math styles, as used in the denominator, don't produce ideal results...

\[ \frac{1+\frac{a}{b}}{1+\frac{1}{1+\frac{1}{a}}} \]

\noindent ...so we use \verb|\displaystyle| to improve typesetting:

\[ \frac{1+\frac{a}{b}} {\displaystyle 1+\frac{1}{1+\frac{1}{a}}} \]

Here is an example which uses the \texttt{amsmath} \verb|\cfrac| command:

\[
  a_0+\cfrac{1}{a_1+\cfrac{1}{a_2+\cfrac{1}{a_3+\cdots}}}
\]

Here is another example, derived from the \texttt{amsmath} documentation, which demonstrates left
and right placement of the numerator using \verb|\cfrac[l]| and \verb|\cfrac[r]| respectively:
\[
\cfrac[l]{1}{\sqrt{2}+
\cfrac[r]{1}{\sqrt{2}+
\cfrac{1}{\sqrt{2}+\dotsb}}}
\]
\end{document}

 Open this example in Overleaf.

This example produces the following output:

Graphic demonstrating contained fractions typeset in LaTeX

A final example

This example demonstrates a more complex continued fraction:

\documentclass{article}
\usepackage{amsmath}
\begin{document}
\newcommand*{\contfrac}[2]{%
{
  \rlap{$\dfrac{1}{\phantom{#1}}$}%
  \genfrac{}{}{0pt}{0}{}{#1+#2}%
}
}
\[
  a_0 +
  \contfrac{a_1}{
  \contfrac{a_2}{
  \contfrac{a_3}{
  \genfrac{}{}{0pt}{0}{}{\ddots}
  }}}
\]

\end{document}

 Open this example in Overleaf.

This example produces the following output:

Image of advanced continued fraction typeset by LaTeX

Further reading

For more information see:

Overleaf guides

LaTeX Basics

Mathematics

Figures and tables

References and Citations

Languages

Document structure

Formatting

Fonts

Presentations

Commands

Field specific

Class files

Advanced TeX/LaTeX