> For the complete documentation index, see [llms.txt](https://overleaf-pro.ayaka.space/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://overleaf-pro.ayaka.space/latex/mathematics/05-fractions-and-binomials.md).

# Fractions and Binomials

## Introduction

This article explains how to typeset fractions and binomial coefficients, starting with the following example which uses the [`amsmath` package](https://ctan.org/pkg/amsmath?lang=en):

```latex
\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.](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Fractions+and+Binomials\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cusepackage%7Bamsmath%7D%0A%5Cbegin%7Bdocument%7D%0AThe+binomial+coefficient%2C+%5C%28%5Cbinom%7Bn%7D%7Bk%7D%5C%29%2C+is+defined+by+the+expression%3A%0A%5C%5B%0A++++%5Cbinom%7Bn%7D%7Bk%7D+%3D+%5Cfrac%7Bn%21%7D%7Bk%21%28n-k%29%21%7D%0A%5C%5D%0A%5Cend%7Bdocument%7D)

The [`amsmath` package](https://ctan.org/pkg/amsmath?lang=en) is loaded by adding the following line to the document preamble:

```latex
\usepackage{amsmath}
```

Here is the output produced:

![Image showing LaTeX typesetting of the binomial coefficient](/files/4yGRTi1O8eP2vWUG91hm)

## 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:

```latex
\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.](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Inline+and+display+of+fractions+and+binomials\&snip=%5Cdocumentclass%7Barticle%7D%0A%25+Using+the+geometry+package+to+reduce%0A%25+the+width+of+help+article+graphics%0A%5Cusepackage%5Btextwidth%3D8cm%5D%7Bgeometry%7D%0A%5Cbegin%7Bdocument%7D%0AFractions+can+be+used+inline+within+the+paragraph+text%2C+for+%0Aexample+%5C%28%5Cfrac%7B1%7D%7B2%7D%5C%29%2C+or+displayed+on+their+own+line%2C+%0Asuch+as+this%3A%0A%5C%5B%5Cfrac%7B1%7D%7B2%7D%5C%5D%0A%5Cend%7Bdocument%7D)

This example produces the following output:

![Demonstrating inline and display math modes in LaTeX](/files/eBK8GcfhlQTGzOV9PbMV)

* **Note**: More information on inline and display versions of mathematics can be found in the Overleaf article [Display style in math mode](/latex/mathematics/10-display-style-in-math-mode.md).

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.

```latex
\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.](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Text+fractions\&snip=%5Cdocumentclass%7Barticle%7D%0A%25+Using+the+geometry+package+to+reduce%0A%25+the+width+of+help+article+graphics%0A%5Cusepackage%5Btextwidth%3D8cm%5D%7Bgeometry%7D%0A%5Cusepackage%7Bamsmath%7D%25+For+the+%5Ctext%7B...%7D+command%0A%5Cbegin%7Bdocument%7D%0AWe+use+the+%5Ctexttt%7Bamsmath%7D+package+command%0A%5Cverb%7C%5Ctext%7B...%7D%7C+to+create+text-only+fractions%0Alike+this%3A%0A%0A%5C%5B%5Cfrac%7B%5Ctext%7Bnumerator%7D%7D%7B%5Ctext%7Bdenominator%7D%7D%5C%5D%0A%0AWithout+the+%5Cverb%7C%5Ctext%7B...%7D%7C+command+the+result+%0Alooks+like+this%3A%0A%0A%5C%5B%5Cfrac%7Bnumerator%7D%7Bdenominator%7D%5C%5D%0A%5Cend%7Bdocument%7D)

This example produces the following output:

![Textual fractions](/files/XpxrW9MddtAAsnFWTa5k)

## Size and spacing within typeset mathematics

The size and spacing of mathematical material typeset by LaTeX is [determined by algorithms](https://tug.org/TUGboat/tb27-1/tb86jackowski.pdf) 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](/latex/mathematics/10-display-style-in-math-mode.md#overriding-default-mathematical-styles).

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.

```latex
\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](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Changing+default+math+styles\&snip=%5Cdocumentclass%7Barticle%7D%0A%25+Using+the+geometry+package+to+reduce%0A%25+the+width+of+help+article+graphics%0A%5Cusepackage%5Btextwidth%3D9.5cm%5D%7Bgeometry%7D%0A%5Cbegin%7Bdocument%7D%0A%0AFractions+typeset+within+a+paragraph+typically+look+like+this%3A+%5C%28%5Cfrac%7B3x%7D%7B2%7D%5C%29.+You+can+force+%5CLaTeX%7B%7D+to+use+the+larger+display+style%2C+such+as+%5C%28+%5Cdisplaystyle+%5Cfrac%7B3x%7D%7B2%7D+%5C%29%2C+which+also+has+an+effect+on+line+spacing.+The+size+of+maths+in+a+paragraph+can+also+be+reduced%3A+%5C%28%5Cscriptstyle+%5Cfrac%7B3x%7D%7B2%7D%5C%29+or+%5C%28%5Cscriptscriptstyle+%5Cfrac%7B3x%7D%7B2%7D%5C%29.+For+the+%5Cverb%7C%5Cscriptscriptstyle%7C+example+note+the+reduction+in+spacing%3A+characters+are+moved+closer+to+the+%5Ctextit%7Bvinculum%7D+%28the+line+separating+numerator+and+denominator%29.%0A%0AEqually%2C+you+can+change+the+style+of+mathematics+normally+typeset+in+display+style%3A%0A%0A%5C%5Bf%28x%29%3D%5Cfrac%7BP%28x%29%7D%7BQ%28x%29%7D%5Cquad+%5Ctextrm%7Band%7D%5Cquad+%5Ctextstyle+f%28x%29%3D%5Cfrac%7BP%28x%29%7D%7BQ%28x%29%7D%5Cquad+%5Ctextrm%7Band%7D%5Cquad+%5Cscriptstyle+f%28x%29%3D%5Cfrac%7BP%28x%29%7D%7BQ%28x%29%7D%5C%5D%0A%5Cend%7Bdocument%7D)

This example produces the following output:

![Image showing changes to LaTeX default math styles](/files/j5PO0gOUvZazktsklkak)

## 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](https://ctan.org/pkg/amsmath?lang=en) in the document preamble.

```latex
\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.](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Example+of+continued+fractions\&snip=%5Cdocumentclass%7Barticle%7D%0A%25+Using+the+geometry+package+to+reduce%0A%25+the+width+of+help+article+graphics%0A%5Cusepackage%5Btextwidth%3D9.5cm%5D%7Bgeometry%7D%0A%25+Load+amsmath+to+access+the+%5Ccfrac%7B...%7D%7B...%7D+command%0A%5Cusepackage%7Bamsmath%7D%0A%5Cbegin%7Bdocument%7D%0AFractions+can+be+nested+but%2C+in+this+example%2C+note+how+the+default+math+styles%2C+as+used+in+the+denominator%2C+don%27t+produce+ideal+results...%0A%0A%5C%5B+%5Cfrac%7B1%2B%5Cfrac%7Ba%7D%7Bb%7D%7D%7B1%2B%5Cfrac%7B1%7D%7B1%2B%5Cfrac%7B1%7D%7Ba%7D%7D%7D+%5C%5D%0A%0A%5Cnoindent+...so+we+use+%5Cverb%7C%5Cdisplaystyle%7C+to+improve+typesetting%3A%0A%0A%5C%5B+%5Cfrac%7B1%2B%5Cfrac%7Ba%7D%7Bb%7D%7D+%7B%5Cdisplaystyle+1%2B%5Cfrac%7B1%7D%7B1%2B%5Cfrac%7B1%7D%7Ba%7D%7D%7D+%5C%5D%0A%0AHere+is+an+example+which+uses+the+%5Ctexttt%7Bamsmath%7D+%5Cverb%7C%5Ccfrac%7C+command%3A%0A%0A%5C%5B%0A++a_0%2B%5Ccfrac%7B1%7D%7Ba_1%2B%5Ccfrac%7B1%7D%7Ba_2%2B%5Ccfrac%7B1%7D%7Ba_3%2B%5Ccdots%7D%7D%7D%0A%5C%5D%0A%0AHere+is+another+example%2C+derived+from+the+%5Ctexttt%7Bamsmath%7D+documentation%2C+which+demonstrates+left%0Aand+right+placement+of+the+numerator+using+%5Cverb%7C%5Ccfrac%5Bl%5D%7C+and+%5Cverb%7C%5Ccfrac%5Br%5D%7C+respectively%3A%0A%5C%5B%0A%5Ccfrac%5Bl%5D%7B1%7D%7B%5Csqrt%7B2%7D%2B%0A%5Ccfrac%5Br%5D%7B1%7D%7B%5Csqrt%7B2%7D%2B%0A%5Ccfrac%7B1%7D%7B%5Csqrt%7B2%7D%2B%5Cdotsb%7D%7D%7D%0A%5C%5D%0A%5Cend%7Bdocument%7D)

This example produces the following output:

![Graphic demonstrating contained fractions typeset in LaTeX](/files/fnvTV7rnUiCtbtxBNusG)

## A final example

This example demonstrates a more complex continued fraction:

```latex
\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.](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=A+more+complex+continued+fraction\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cusepackage%7Bamsmath%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cnewcommand%2A%7B%5Ccontfrac%7D%5B2%5D%7B%25%0A%7B%0A++%5Crlap%7B%24%5Cdfrac%7B1%7D%7B%5Cphantom%7B%231%7D%7D%24%7D%25%0A++%5Cgenfrac%7B%7D%7B%7D%7B0pt%7D%7B0%7D%7B%7D%7B%231%2B%232%7D%25%0A%7D%0A%7D%0A%5C%5B%0A++a_0+%2B%0A++%5Ccontfrac%7Ba_1%7D%7B%0A++%5Ccontfrac%7Ba_2%7D%7B%0A++%5Ccontfrac%7Ba_3%7D%7B%0A++%5Cgenfrac%7B%7D%7B%7D%7B0pt%7D%7B0%7D%7B%7D%7B%5Cddots%7D%0A++%7D%7D%7D%0A%5C%5D%0A%0A%5Cend%7Bdocument%7D)

This example produces the following output:

![Image of advanced continued fraction typeset by LaTeX](/files/GBAbLq5eWTjir01NVLzz)

## Further reading

For more information see:

* [Mathematical expressions](/latex/mathematics/01-mathematical-expressions.md)
* [Subscripts and superscripts](/latex/mathematics/02-subscripts-and-superscripts.md)
* [Brackets and Parentheses](/latex/mathematics/03-brackets-and-parentheses.md)
* [Aligning equations with amsmath](/latex/mathematics/06-aligning-equations-with-amsmath.md)
* [Operators](/latex/mathematics/07-operators.md)
* [Mathematical fonts](/latex/mathematics/12-mathematical-fonts.md)
* [The not so short introduction to LaTeX2ε](http://www.ctan.org/tex-archive/info/lshort/)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://overleaf-pro.ayaka.space/latex/mathematics/05-fractions-and-binomials.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
