> 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/zh-tw/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/a9ef0136453a1c732ef33032f3204027c4da889d)

## 顯示分數

分數的視覺外觀會因其是行內出現、作為段落的一部分，或是排版成獨立顯示在自己一行上的內容而有所不同。下一個範例示範這些視覺外觀的變化：

```latex
\documentclass{article}
% 使用 geometry 套件來縮減
% 說明文章圖形的寬度
\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/97f4e64aa8459cf106d93677f31fcaaa1bd916bf)

* **注意**：更多關於數學的行內與顯示版本的資訊可參見 Overleaf 文章 [數學模式中的顯示樣式](/latex/zh-tw/shu-xue/10-display-style-in-math-mode.md).

我們的範例分數是使用 `\frac` 命令（`\frac{1}{2}`）排版的，其一般形式為 `\frac{*分子*}{*分母*}`.

## 文字樣式分數

下列範例示範如何使用 `\text{...}` 命令來排版僅含文字的分數，該命令由 `amsmath` 套件。 `\text{...}` 提供。此命令用來避免 LaTeX 將文字排版成一般數學內容。

```latex
\documentclass{article}
% 使用 geometry 套件來縮減
% 說明文章圖形的寬度
\usepackage[textwidth=8cm]{geometry}
\usepackage{amsmath}% 用於 \text{...} 命令
\begin{document}
我們使用 \texttt{amsmath} 套件命令
\verb|\text{...}| 來建立僅含文字的分數
如下：

\[\frac{\text{分子}}{\text{分母}}\]

若沒有 \verb|\text{...}| 命令，結果
看起來會像這樣：

\[\frac{分子}{分母}\]
\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/5ee42a71a8d01c0cab9e1ff467dbb8fa974d0c99)

## 排版數學中的大小與間距

LaTeX 排版的數學內容之大小與間距是 [由演算法決定的](https://tug.org/TUGboat/tb27-1/tb86jackowski.pdf) 這些演算法會套用排版數學所用字型中所含的大小與定位資料。

有時，可能有必要或有需要覆寫 LaTeX 所選的預設數學樣式——數學元素的大小與間距——這個主題在 Overleaf 說明文章中有討論 [數學模式中的顯示樣式](/latex/zh-tw/shu-xue/10-display-style-in-math-mode.md#overriding-default-mathematical-styles).

總之，可透過下列命令變更用來排版數學的預設樣式：

* `\textstyle`：套用用於段落中排版數學的樣式；
* `\displaystyle`：套用用於單獨成行排版數學的樣式；
* `\scriptstyle`：套用用於下標或上標的樣式；
* `\scriptscriptstyle`：套用用於第二層下標或上標的樣式；

下一個範例會示範這些設定。

```latex
\documentclass{article}
% 使用 geometry 套件來縮減
% 說明文章圖形的寬度
\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{和}\quad \textstyle f(x)=\frac{P(x)}{Q(x)}\quad \textrm{和}\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/0396ef07664fe15ffc2bcb0d164db852ff06d442)

## 連分數

可以將分數嵌套以取得更複雜的式子。以下範例中顯示的第二組分數都使用了 `\cfrac` 命令，這個命令是專門設計來產生連分數的。若要使用 `\cfrac` 你必須載入 [`amsmath` 套件](https://ctan.org/pkg/amsmath?lang=en) 於文件導言區。

```latex
\documentclass{article}
% 使用 geometry 套件來縮減
% 說明文章圖形的寬度
\usepackage[textwidth=9.5cm]{geometry}
% 載入 amsmath 以存取 \cfrac{...}{...} 命令
\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/2a579c613b3e013a0f90fbafa76d44aa52ca40a4)

## 最後一個範例

這個範例示範更複雜的連分數：

```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/99e9fea66a5d91cb867d1e0f5bf667695beeb352)

## 延伸閱讀

詳情請見：

* [數學表達式](/latex/zh-tw/shu-xue/01-mathematical-expressions.md)
* [下標與上標](/latex/zh-tw/shu-xue/02-subscripts-and-superscripts.md)
* [中括號與小括號](/latex/zh-tw/shu-xue/03-brackets-and-parentheses.md)
* [使用 amsmath 對齊方程式](/latex/zh-tw/shu-xue/06-aligning-equations-with-amsmath.md)
* [運算子](/latex/zh-tw/shu-xue/07-operators.md)
* [數學字型](/latex/zh-tw/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://overleaf-pro.ayaka.space/latex/zh-tw/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.
