> For the complete documentation index, see [llms.txt](https://ayakaleaf-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://ayakaleaf-pro.ayaka.space/latex/zh-cn/shu-xue/05-fractions-and-binomials.md).

# 分数和二项式

## 引言

本文介绍如何排版分数和二项式系数，先从下面这个使用了 [`amsmath` 宏包](https://ctan.org/pkg/amsmath?lang=en):

```latex
\documentclass{article}
\usepackage{amsmath}
\begin{document}
二项式系数，\(\binom{n}{k}\)，由以下表达式定义：
\[
    \binom{n}{k} = \frac{n!}{k!(n-k)!}
\]
\end{document}
```

[在 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)

该 [`amsmath` 宏包](https://ctan.org/pkg/amsmath?lang=en) 通过在文档导言区添加以下一行来加载：

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

以下是生成的输出：

![显示二项式系数的 LaTeX 排版效果的图片](/files/0e307ad057542fa032e579968608ca2b3f517d41)

## 显示分数

分数的视觉外观会根据它们是以内联形式出现、作为段落的一部分出现，还是作为独立内容单独排版在一行中而发生变化。下面的示例演示这些视觉外观上的变化：

```latex
\documentclass{article}
% Using the geometry package to reduce
% the width of help article graphics
\usepackage[textwidth=8cm]{geometry}
\begin{document}
分数可以以内联形式用于段落文本中，例如
例如 \(\frac{1}{2}\)，或者单独显示在自己的一行中，
，如下所示：
\[\frac{1}{2}\]
\end{document}
```

[在 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)

此示例生成如下输出：

![在 LaTeX 中演示行内和显示数学模式](/files/0b973d463386828c71ae37af653f78a3ce5df13d)

* **注意**：关于数学的行内和显示版本的更多信息，可在 Overleaf 文章中找到 [数学模式中的显示样式](/latex/zh-cn/shu-xue/10-display-style-in-math-mode.md).

我们的示例分数使用以下方式排版： `\frac` 命令（`\frac{1}{2}`），其一般形式为 `\frac{*numerator*}{*denominator*}`.

## 文本分数

下面的示例演示如何使用以下内容来排版仅文本分数： `\text{...}` 命令，由……提供 `amsmath` 宏包。 `\text{...}` 该命令用于防止 LaTeX 将文本排版为常规数学内容。

```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}
我们使用 \texttt{amsmath} 宏包中的命令
\verb|\text{...}| 来创建仅文本分数
如下：

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

如果没有 \verb|\text{...}| 命令，结果
看起来像这样：

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

[在 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)

此示例生成如下输出：

![文本分式](/files/66ee29dee0323c365d2320f7bc32c0e5a9e76662)

## 排版数学中的大小和间距

LaTeX 排版的数学内容的大小和间距是 [由算法决定的](https://tug.org/TUGboat/tb27-1/tb86jackowski.pdf) 这些算法会应用用于数学排版的字体中包含的大小和位置信息。

有时，可能需要或希望覆盖 LaTeX 选择的默认数学样式——数学元素的大小和间距——这一主题在 Overleaf 帮助文章中有讨论 [数学模式中的显示样式](/latex/zh-cn/shu-xue/10-display-style-in-math-mode.md#overriding-default-mathematical-styles).

总之，下面这些命令可以更改用于数学排版的默认样式：

* `\textstyle`：应用用于段落中数学排版的样式；
* `\displaystyle`：应用用于单独成行数学排版的样式；
* `\scriptstyle`：应用用于下标或上标的样式；
* `\scriptscriptstyle`：应用用于二级下标或上标的样式；

这些将在下一个示例中演示。

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

段落中排版的分数通常看起来像这样：\(\frac{3x}{2}\)。你可以强制 \LaTeX{} 使用更大的显示样式，例如 \( \displaystyle \frac{3x}{2} \)，这也会影响行距。段落中的数学大小也可以缩小：\(\scriptstyle \frac{3x}{2}\) 或 \(\scriptscriptstyle \frac{3x}{2}\)。对于 \verb|\scriptscriptstyle| 示例，请注意间距的缩小：字符会更靠近 \textit{vinculum}（分子和分母之间的分隔线）放置。

同样，你也可以更改通常以显示样式排版的数学的样式：

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

[在 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)

此示例生成如下输出：

![显示 LaTeX 默认数学样式变化的图片](/files/7550d96983e28a9e0c6ec406617fb868de3d2df3)

## 连分数

分数可以嵌套以得到更复杂的表达式。下面示例中显示的第二对分数都使用了 `\cfrac` 命令，它专门用于生成连分数。要使用 `\cfrac` ，你必须加载 [`amsmath` 宏包](https://ctan.org/pkg/amsmath?lang=en) 到文档导言区中。

```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}
分数可以嵌套，但在这个示例中，请注意分母中使用的默认数学样式并不会产生理想的结果……

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

\noindent ……因此我们使用 \verb|\displaystyle| 来改进排版：

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

下面是一个使用 \texttt{amsmath} \verb|\cfrac| 命令的示例：

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

下面是另一个示例，摘自 \texttt{amsmath} 文档，它演示了左
和右对齐放置分子，分别使用 \verb|\cfrac[l]| 和 \verb|\cfrac[r]|：
\[
\cfrac[l]{1}{\sqrt{2}+
\cfrac[r]{1}{\sqrt{2}+
\cfrac{1}{\sqrt{2}+\dotsb}}}
\]
\end{document}
```

[在 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)

此示例生成如下输出：

![演示 LaTeX 中嵌套分数排版的图示](/files/66ff5049d954d0bcc54ea2d6708df2410281d810)

## 最后一个示例

这个示例演示一个更复杂的连分数：

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

[在 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)

此示例生成如下输出：

![LaTeX 排版的高级连分数图像](/files/74be7718de467e4791f381efc25c0878f6e60bd9)

## 进一步阅读

更多信息请参见：

* [数学表达式](/latex/zh-cn/shu-xue/01-mathematical-expressions.md)
* [下标和上标](/latex/zh-cn/shu-xue/02-subscripts-and-superscripts.md)
* [方括号和圆括号](/latex/zh-cn/shu-xue/03-brackets-and-parentheses.md)
* [使用 amsmath 对齐方程](/latex/zh-cn/shu-xue/06-aligning-equations-with-amsmath.md)
* [运算符](/latex/zh-cn/shu-xue/07-operators.md)
* [数学字体](/latex/zh-cn/shu-xue/12-mathematical-fonts.md)
* [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://ayakaleaf-pro.ayaka.space/latex/zh-cn/shu-xue/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.
