Skip to main content

Fractions_and_Binomials

Fractions and Binomials

This article explains how to typeset fractions and binomial coefficients, starting with the following example using 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}

Load the amsmath package by adding the following line to the document preamble.

\usepackage{amsmath}

The compilation result is as follows:

Displaying Fractions

The visual appearance of fractions changes depending on whether they appear inline as part of a paragraph or as independent material displayed on their own line. The next example shows these visual appearance changes.

\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}

Text-Style Fractions

The following example demonstrates typesetting plain text fractions by using the \text{...} command provided by the amsmath package. The \text{...} command is used to prevent LaTeX from typesetting the text as ordinary 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
`\text{...}` to create text-only fractions
like this:

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

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

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

Size and Spacing in Mathematical Typesetting

The size and spacing of mathematical material typeset by LaTeX are determined by algorithms that apply size and positioning data from the fonts used for typesetting mathematics.

Sometimes, we may need or want to override the default mathematical style chosen by LaTeX—the size and spacing of mathematical elements, a topic discussed in the Overleaf help article "Display Style in Math Mode."

In general, the default style of mathematical typesetting can be changed with the following commands.

\textstyle: Style applied to mathematical typesetting within paragraphs. \displaystyle: Style applied to mathematics typeset in display style. \scriptstyle: Style applied to subscripts or superscripts. \scriptscriptstyle: Style applied to second-order subscripts or superscripts. This is 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 `\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}

Continued Fractions

\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}` `\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 `\cfrac[l]` and `\cfrac[r]` respectively:
\[
\cfrac[l]{1}{\sqrt{2}+
\cfrac[r]{1}{\sqrt{2}+
\cfrac{1}{\sqrt{2}+\dotsb}}}
\]
\end{document}


Example:

\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}