> 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/te-ding-ling-yu/02-chemistry-formulae.md).

# 化学公式

本文重点介绍两个用于排版化学内容和文档的 LaTeX 宏包：

* [chemfig](https://www.ctan.org/pkg/chemfig)：一个用于 [绘制分子的结构式](#using-chemfig-to-draw-molecules-structural-formulae)
* [mhchem](https://ctan.org/pkg/mhchem)：一个用于 [排版化学式和化学方程式](#using-mhchem-to-typeset-chemical-formulae-and-equations)

## 使用 chemfig 绘制分子（结构式）

绘制分子主要是用线连接各原子组。简单的线性结构式可以很容易地使用 `chemfig` 宏包绘制，如下面的示例所示：

```latex
\documentclass{article}
\usepackage{chemfig}
\begin{document}

\section{Introduction}
使用 chemfig 编写化学式很简单。

\chemfig{A=B}
\end{document}
```

[打开此 `chemfig` Overleaf 中的示例](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Chemfig+example+1\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cusepackage%7Bchemfig%7D%0A%5Cbegin%7Bdocument%7D%0A%0A%5Csection%7BIntroduction%7D%0AWriting+chemical+formulae+with+chemfig+is+straightforward.%0A%0A%5Cchemfig%7BA%3DB%7D%0A%5Cend%7Bdocument%7D)

下图显示了上面示例生成的输出：

![一个基本的 chemfig 示例](/files/2d2d6a7edf742006f88bcadb7b4ba9e4285e7502)

该宏包通过 `\usepackage{chemfig}` 在导言区引入。命令 `\chemfig{A=B}` 该命令绘制分子。符号 `=` 决定键的类型。请参见 [参考指南](#reference-guide) 中的键类型列表。

### 角度

定义分子中键之间的角度有几种方法。

```latex
要定义化学式中的角度，你可以使用用于定义角度的单位

\chemfig{A-[1]B-[7]C}

绝对角度

\chemfig{A-[:50]B-[:-25]C}

相对角度

\chemfig{A-[::50]B-[::-25]C}
```

[打开此 `chemfig` Overleaf 中的示例](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Chemfig+example+2\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cusepackage%7Bchemfig%7D%0A%5Cbegin%7Bdocument%7D%0ATo+define+chemical+formulae+you+can+use+units+that+define+the+angles%0A%0A%5Cchemfig%7BA-%5B1%5DB-%5B7%5DC%7D%0A%0AAbsolute+angles%0A%0A%5Cchemfig%7BA-%5B%3A50%5DB-%5B%3A-25%5DC%7D%0A%0ARelative+angles%0A%0A%5Cchemfig%7BA-%5B%3A%3A50%5DB-%5B%3A%3A-25%5DC%7D%0A%5Cend%7Bdocument%7D)

下图显示了上面示例生成的输出：

![Chemfig2OLV2.png](/files/20189c0ae6fee75aabf33ab1b264f74998e0fa80)

上面示例中的三个命令各使用不同的方法来确定键之间的角度。

* `默认单位`：在命令 `\chemfig{A-[1]B-[7]C}` 中，方括号内的参数使用特殊单位设置角度，每个单位等于 45°。因此在这个示例中，角度分别是 45° 和 315°。
* `绝对单位`：角度可以用绝对单位设置，在命令 `\chemfig{A-[:50]B-[:-25]C}` 中，方括号内的参数表示角度，单位为度，并以水平基线为参考。允许使用负角度。
* `相对角度`：在第三个示例中 `\chemfig{A-[::50]B-[::-25]C}` 角度是相对于前一条键来测量的，而不是相对于基线。

### 环

下面的示例展示了绘制正多边形的语法：

```latex
正多边形：

\chemfig{A*5(-B=C-D-E=)}

也可以绘制不完整的环：

\chemfig{A*5(-B=C-D)}
```

[打开此 `chemfig` Overleaf 中的示例](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Chemfig+example+3\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cusepackage%7Bchemfig%7D%0A%5Cbegin%7Bdocument%7D%0ARegular+polygons%3A%0A%0A%5Cchemfig%7BA%2A5%28-B%3DC-D-E%3D%29%7D%0A%0AIncomplete+rings+are+also+possible%3A%0A%0A%5Cchemfig%7BA%2A5%28-B%3DC-D%29%7D%0A%5Cend%7Bdocument%7D)

下图显示了上面示例生成的输出：

![Chemfig3OLV2.png](/files/da653a79507932556cfbeec72ba22eb8734ab5ab)

该命令的语法 `\chemfig{A*5(-B=C-D-E=)}` 如下所述：

* `一个`：这是第一个原子，其余原子都将从这里绘制
* `*5`：多边形的边数
* `(-B=C-D-E=)`：其余的原子和键。如果传入的内容不足以补全多边形，就会绘制一个不完整的环。

此外，还可以向该命令传入一个额外参数：两个星号。例如，如果 `**5` 设置为而不是 `*5` ，则会在多边形内部绘制一个圆。

#### 相连的环

下面的示例绘制了咖啡因的化学结构：

```latex
\documentclass{article}
\usepackage{chemfig}
\begin{document}
\section{I need caffeine.}

\chemfig{*6((=O)-N(-)-(*5(-N=-N(-)-))=-(=O)-N(-)-)}
\end{document}
```

[打开此 `chemfig` Overleaf 中的示例](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Drawing+caffeine+using+chemfig\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cusepackage%7Bchemfig%7D%0A%5Cbegin%7Bdocument%7D%0A%5Csection%7BI+need+caffeine.%7D%0A%0A%5Cchemfig%7B%2A6%28%28%3DO%29-N%28-%29-%28%2A5%28-N%3D-N%28-%29-%29%29%3D-%28%3DO%29-N%28-%29-%29%7D%0A%5Cend%7Bdocument%7D)

下面的图形显示了咖啡因示例生成的输出：

![使用 chemfig LaTeX 宏包绘制的咖啡因分子图](/files/49e77f75093da7848f22f12ae131602f88c962cc)

### 支链

化学式并不总是线性的，事实上，带支链的化学式才是最常见的类型。下面是如何创建它们的示例。

```latex
带支链的分子 \vspace{.5cm}

\chemfig{H-C(-[2]H)(-[6]H)-C(=[1]O)-[7]H}
```

[打开此 `chemfig` Overleaf 中的示例](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Chemfig+example+4\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cusepackage%7Bchemfig%7D%0A%5Cbegin%7Bdocument%7D%0ABranched+molecule+%5Cvspace%7B.5cm%7D%0A%0A%5Cchemfig%7BH-C%28-%5B2%5DH%29%28-%5B6%5DH%29-C%28%3D%5B1%5DO%29-%5B7%5DH%7D%0A%5Cend%7Bdocument%7D)

下图显示了上面示例生成的输出：

![Chemfig4OLV2.png](/files/b36594eb8ccc1182e393895835725ee7a1a04fc5)

每个节点中的支链是通过在括号内添加化学式来创建的。例如，代码

```latex
C(-[2]H)(-[6]H)
```

会在“C”上创建两个支链，一个角度为 2 个单位（90°），另一个角度为 6 个单位（270°）。

支链也可以添加到环上：

```latex
带支链的环
\vspace{.5cm}

\chemfig{A*6(-B=C(-CH_3)-D-E-F(=G)=)}
```

[打开此 `chemfig` Overleaf 中的示例](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Chemfig+example+5\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cusepackage%7Bchemfig%7D%0A%5Cbegin%7Bdocument%7D%0ABranched+ring%0A%5Cvspace%7B.5cm%7D%0A%0A%5Cchemfig%7BA%2A6%28-B%3DC%28-CH_3%29-D-E-F%28%3DG%29%3D%29%7D%0A%5Cend%7Bdocument%7D)

下图显示了上面示例生成的输出：

![Chemfig5OLV2.png](/files/dbfd65e950bade4db9e8bbcb08a5830e7c404a69)

语法类似，可以用括号将支链附加到某个节点（原子）上。例如

```latex
F(=G)
```

会将一个支链附加到节点“F”上。可以使用嵌套支链创建更复杂的示例，甚至可以将环作为支链附加。

### 自定义化学式

可以更改颜色和节点间距等多个参数，还可以添加用于描述该化学式的附加文本。

```latex
{\huge
    \setchemfig{atom sep=2em,bond style={line width=1pt,red,dash pattern=on 2pt off 2pt}}
    \chemname
    {\chemfig{H-C(-[2]H)(-[6]H)-C(=[1]O)-[7]H}}
    {Acetaldehyde}
}
```

[打开此 `chemfig` Overleaf 中的示例](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Chemfig+example+6\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cusepackage%7Bchemfig%7D%0A%5Cbegin%7Bdocument%7D%0A%7B%5Chuge+%0A++++%5Csetchemfig%7Batom+sep%3D2em%2Cbond+style%3D%7Bline+width%3D1pt%2Cred%2Cdash+pattern%3Don+2pt+off+2pt%7D%7D++%0A++++%5Cchemname%0A++++%7B%5Cchemfig%7BH-C%28-%5B2%5DH%29%28-%5B6%5DH%29-C%28%3D%5B1%5DO%29-%5B7%5DH%7D%7D++++%0A++++%7BAcetaldehyde%7D%0A%7D%0A%5Cend%7Bdocument%7D)

下图显示了上面示例生成的输出：

![使用 chemfig 宏包在 LaTeX 中绘制乙醛](/files/51ac6a4fee646b4dc814c54b95b960484e01d364)

这里有两个新命令：

* `\setchemfig{}`：该命令用于配置化学结构绘制的设置。
* `\chem``name{}{}` ：该命令中的第一个参数是一个 `chemfig` 化学式，第二个参数是在化学式下方显示的文本。在这个示例中，文本是“Acetaldehyde”。

还要注意这里使用的字号是 `huge`。你也可以使用其他字号， [字号](/latex/zh-cn/zi-ti/01-font-sizes-families-and-styles.md) 化学式会相应缩放。

### 参考指南（chemfig）

**键类型**

| 键类型    | 代码                  | 输出                                                            |
| ------ | ------------------- | ------------------------------------------------------------- |
| 单键     | `\chemfig{O - H}`   | ![Bond1.png](/files/ac0a93694f41b7f433e385e23eedf9f420b5182e) |
| 双倍     | `\chemfig{O = H}`   | ![Bond2.png](/files/38906f5aff0b5e90a53b3a4ccecfdf4e793d66ea) |
| 三键     | `\chemfig{O ~ H}`   | ![Bond3.png](/files/11d13eeb3744774ae0693556e25d796bce092731) |
| 普通右楔形键 | `\chemfig{O > H}`   | ![Bond4.png](/files/1b4678dfe29066a86d1ef7a2861215936ae16e9f) |
| 普通左楔形键 | `\chemfig{O < H}`   | ![Bond5.png](/files/17c3c5ea016171b2e301fd8575eeec2df1d58755) |
| 虚线右楔形键 | `\chemfig{O >: H}`  | ![Bond6.png](/files/c11506ba4938748836b8d704887cb63897b362f5) |
| 虚线左楔形键 | `\chemfig{O <: H}`  | ![Bond7.png](/files/d2f0d02d48f5d209de70558e00eb302b20aa7e11) |
| 空心右楔形键 | `\chemfig{O >\| H}` | ![Bond8.png](/files/0b4bc6b08da333fb4ce39040171d1984da313c00) |
| 空心左楔形键 | `\chemfig{O <\| H}` | ![Bond9.png](/files/d9da82f148cd90496ff3ebbf0543334e1f026d1b) |

## 使用 mhchem 排版化学式和化学方程式

该 `mhchem` 该宏包的多功能 `\ce` 命令， [可在 Overleaf 的 Visual Editor 预览中使用，](#visual-editors-preview-of-chemical-formulae-and-equations)可用于排版化学式和化学方程式；例如，输入 `\ce{H2O}` 将排版为 $$\text{H}\_2\text{O}$$，即水的化学式。

* **注意**：较新版本的 `mhchem` 宏包可能会 [增加项目编译所需的时间](/latex/zh-cn/zhi-shi-ku/038-fixing-and-preventing-compile-timeouts.md#mhchemtimeout)。切换到 [`chemformula` 宏包](https://ctan.org/pkg/chemformula?lang=en)，并使用其 `\ch` 命令，可以减少编译时间，但 `\ch` 目前 Visual Editor 还无法预览该命令。

### 如何加载 mhchem 宏包

要使用最新版本的 `mhchem`，当前版本为 4，请在文档导言区添加以下行：

```latex
\usepackage[version=4]{mhchem}
```

这将加载 `mhchem` 及其用于格式化化学式和化学方程式的默认样式。要更改这些默认设置，你可以使用 `mhchem` 宏包选项或 `\mhchemoptions` 命令。例如，要将文本模式字体更改为无衬线体，并使用 PGF/Ti*k*Z，你可以使用 `mhchem` 使用

```latex
\usepackage[version=4,arrows=pgf-filled,textfontname=sffamily]{mhchem}
```

或者你也可以使用 `\mhchemoptions` 命令在文档中

```latex
\mhchemoptions{arrows=pgf-filled,textfontname=sffamily}
```

该 [`mhchem` 包文档](http://mirrors.ctan.org/macros/latex/contrib/mhchem/mhchem.pdf) 还提供了关于配置 `mhchem`的进一步指导，并附有大量化学式和化学方程式示例，其中一些我们在此重现，以帮助你入门。请注意，在更复杂的输入中使用空格字符到 `\ce` 命令。这些空格是必需的，以帮助 `mhchem` 正确解析你的化学式和化学方程式；省略它们可能会触发错误 [Unexpected input character](#the-unexpected-input-character-mhchem-error).

### 一些示例

下面的 Overleaf 项目加载了 `mhchem` 以下三个宏包选项：

* `arrows=pgf-filled`：使用 PGF 绘制实心箭头
* `textfontname=sffamily`：在数学模式之外，化学式和方程式使用默认的无衬线字体排版
* `mathfontname=mathsf`：在数学模式中使用无衬线字体

该 `\ce` 该命令用于排版一系列示例。

```latex
\documentclass{article}
%Load mhchem using some package options
\usepackage[version=4,arrows=pgf-filled,
textfontname=sffamily,
mathfontname=mathsf]{mhchem}
\begin{document}
\subsection*{Simple molecules}

\begin{itemize}
\item Water: \verb|\ce{H2O}|, \ce{H2O}
\item Benzene: \verb|\ce{C6H6}|, \ce{C6H6}
\item Hydrogen peroxide: \verb|\ce{H2O2}|, \ce{H2O2}
\item Acetic acid: \verb|\ce{C2H4O2}|, \ce{C2H4O2}
\item Glucose: \verb|\ce{C6H12O6}|, \ce{C6H12O6}
\end{itemize}

\subsection*{Chemical equations}

两个基本示例：

\begin{itemize}
\item \verb|\ce{2H2 + O2 -> 2H2O}| typesets \ce{2H2 + O2 -> 2H2O}
\item \verb|\ce{CO2 + C -> 2 CO}| typesets \ce{CO2 + C -> 2 CO}
\end{itemize}

\subsection*{A more complex example}

输入 \verb|\ce{Hg^2+ ->[I-] HgI2 ->[I-] [Hg^{II}I4]^2-}| 将排版为：\vskip10pt
\noindent \ce{Hg^2+ ->[I-] HgI2 ->[I-] [Hg^{II}I4]^2-}。

\subsection*{A math mode example}

化学表达式可以使用数学模式命令，如 \verb|\frac| 来排版。\vskip10pt

\noindent 输入 \verb|\[K=\frac{[\ce{Hg^2+}][\ce{Hg}]}{[\ce{Hg2^2+}]}\]| 会得到：

\[K=\frac{[\ce{Hg^2+}][\ce{Hg}]}{[\ce{Hg2^2+}]}\]
\end{document}
```

[打开此 `mhchem` Overleaf 中的示例。](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=mhchem+example\&snip=%5Cdocumentclass%7Barticle%7D%0A%25Load+mhchem+using+some+package+options%0A%5Cusepackage%5Bversion%3D4%2Carrows%3Dpgf-filled%2C%0Atextfontname%3Dsffamily%2C%0Amathfontname%3Dmathsf%5D%7Bmhchem%7D%0A%5Cbegin%7Bdocument%7D%0A%5Csubsection%2A%7BSimple+molecules%7D%0A%0A%5Cbegin%7Bitemize%7D%0A%5Citem+Water%3A+%5Cverb%7C%5Cce%7BH2O%7D%7C%2C+%5Cce%7BH2O%7D%0A%5Citem+Benzene%3A+%5Cverb%7C%5Cce%7BC6H6%7D%7C%2C+%5Cce%7BC6H6%7D%0A%5Citem+Hydrogen+peroxide%3A+%5Cverb%7C%5Cce%7BH2O2%7D%7C%2C+%5Cce%7BH2O2%7D%0A%5Citem+Acetic+acid%3A+%5Cverb%7C%5Cce%7BC2H4O2%7D%7C%2C+%5Cce%7BC2H4O2%7D%0A%5Citem+Glucose%3A+%5Cverb%7C%5Cce%7BC6H12O6%7D%7C%2C+%5Cce%7BC6H12O6%7D%0A%5Cend%7Bitemize%7D%0A%0A%5Csubsection%2A%7BChemical+equations%7D%0A%0ATwo+basic+examples%3A%0A%0A%5Cbegin%7Bitemize%7D%0A%5Citem+%5Cverb%7C%5Cce%7B2H2+%2B+O2+-%3E+2H2O%7D%7C+typesets+%5Cce%7B2H2+%2B+O2+-%3E+2H2O%7D%0A%5Citem+%5Cverb%7C%5Cce%7BCO2+%2B+C+-%3E+2+CO%7D%7C+typesets+%5Cce%7BCO2+%2B+C+-%3E+2+CO%7D%0A%5Cend%7Bitemize%7D%0A%0A%5Csubsection%2A%7BA+more+complex+example%7D%0A%0AWriting+%5Cverb%7C%5Cce%7BHg%5E2%2B+-%3E%5BI-%5D+HgI2+-%3E%5BI-%5D+%5BHg%5E%7BII%7DI4%5D%5E2-%7D%7C+typesets+this%3A%5Cvskip10pt%0A%5Cnoindent+%5Cce%7BHg%5E2%2B+-%3E%5BI-%5D+HgI2+-%3E%5BI-%5D+%5BHg%5E%7BII%7DI4%5D%5E2-%7D.%0A%0A%5Csubsection%2A%7BA+math+mode+example%7D%0A%0AChemical+expressions+can+be+typeset+using+math+mode+commands+such+as+%5Cverb%7C%5Cfrac%7C.+%5Cvskip10pt%0A%0A%5Cnoindent+Writing+%5Cverb%7C%5C%5BK%3D%5Cfrac%7B%5B%5Cce%7BHg%5E2%2B%7D%5D%5B%5Cce%7BHg%7D%5D%7D%7B%5B%5Cce%7BHg2%5E2%2B%7D%5D%7D%5C%5D%7C+produces+this%3A%0A%0A%5C%5BK%3D%5Cfrac%7B%5B%5Cce%7BHg%5E2%2B%7D%5D%5B%5Cce%7BHg%7D%5D%7D%7B%5B%5Cce%7BHg2%5E2%2B%7D%5D%7D%5C%5D%0A%5Cend%7Bdocument%7D)

该项目生成以下输出：

![显示 mhchem 示例的图形](/files/c4b1b00f0eafb4f3edd285f293c7a9f6f89e3d0e)

该 [`mhchem` 包文档](http://mirrors.ctan.org/macros/latex/contrib/mhchem/mhchem.pdf) 非常值得一读，因为它除了介绍宏包选项和配置之外，还包含大量化学式和化学方程式示例，我们在此仅重现其中一部分以帮助你入门。

### Visual Editor 对化学式和化学方程式的预览

Overleaf 的 Visual Editor 提供了由 `\ce` 命令排版的化学式和化学方程式预览，如下视频片段所示：

{% embed url="<https://videos.ctfassets.net/nrgyaltdicpt/38RMpDdTIkwBhVo4Jh2s28/aac834f5d7f2676133f884e1c6b3d0cb/VisualEditorchem.mp4>" %}

### mhchem 的“意外输入字符”错误

该 `mhchem` 宏包错误 **`断言失败：意外输入字符`** 通常是由于 `\ce` 命令输入中缺少空格所致——空格对于 `mhchem` 准确解析更复杂的化学表达式至关重要。在从 PDF 文档复制并粘贴示例时，这个错误也可能出现，因为空格可能无法被准确复制。

下面的示例演示了这个错误：

```latex
\ce{CO2+C->2CO}
```

[打开此 **会产生错误的** `mhchem` Overleaf 中的示例。](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=mhchem+errors+example\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cusepackage%5Bversion%3D4%5D%7Bmhchem%7D+%0A%5Cbegin%7Bdocument%7D%0A%5Cce%7BCO2%2BC-%3E2CO%7D%0A%5Cend%7Bdocument%7D)

至少，我们需要在 `C` 和 `->` 之间加一个空格；不过，这样做可能仍不足以排版出我们想要的表达式。补上所有空格后，它看起来像这样：

```latex
\ce{CO2 + C -> 2 CO}
```

[打开这个已修正的 `mhchem` Overleaf 中的示例。](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=mhchem+corrected+example\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cusepackage%5Bversion%3D4%5D%7Bmhchem%7D+%0A%5Cbegin%7Bdocument%7D%0A%5Cce%7BCO2+%2B+C+-%3E+2+CO%7D%0A%5Cend%7Bdocument%7D)

该项目生成以下输出：

![](/files/041bae7fe09d6e92a229c66cb8bc441e191bdd1f)

## 进一步阅读

更多信息请参见

* [分子轨道图](/latex/zh-cn/te-ding-ling-yu/04-molecular-orbital-diagrams.md)
* [费曼图](/latex/zh-cn/te-ding-ling-yu/03-feynman-diagrams.md)
* [TikZ 宏包](/latex/zh-cn/tu-xing-he-biao-ge/05-tikz-package.md)
* [插入图片](/latex/zh-cn/geng-duo-zhu-ti/27-inserting-images.md)
* [希腊字母和数学符号列表](/latex/zh-cn/shu-xue/11-list-of-greek-letters-and-math-symbols.md)
* [该 **chemfig** 包文档](http://www.ctan.org/pkg/chemfig)
* [该 **tikz/pgf** 包文档](http://www.ctan.org/pkg/pgf)


---

# 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/te-ding-ling-yu/02-chemistry-formulae.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.
