\chapter{特色工具和功能}\label{chap:spec}
\addtocontents{los}{\protect\addvspace{10pt}}

\begin{intro}
本章介绍一些特色的 \LaTeX{} 辅助功能。前两个功能 \hologo{BibTeX} 和 makeindex 依靠一些辅助程序自动生成参考文献、索引等；
之后的使用颜色、超链接等则令我们生成美观易用的电子文档。
\end{intro}

\section{参考文献和 \hologo{BibTeX} 工具}\label{sec:bib}

\subsection{基本的参考文献和引用}\label{subsec:bib-basics}

\pinyinindex{cankaowenxian}{参考文献}
\LaTeX{} 提供的参考文献和引用方式比较原始，需要用户自行书写参考文献列表（包括格式），
因此较难直接使用。相关的命令我们只作最简单的介绍。

\cmdindex{cite}
\LaTeX{} 提供了最基本的 \cmd{cite} 命令用于在正文中引用参考文献：
\begin{command}
\cmd{cite}\marg{citation}
\end{command}

\Arg{citation} 为引用的参考文献的标签，类似 \cmd{ref} 里的参数；\cmd{cite} 带一个可选参数，为引用的编号后加上额外的内容，
如 \cmd{cite}\oarg*{page 22}\marg*{Paper2013} 可能得到形如 [13, page 22] 这样的引用。

\envindex{thebibliography}
\cmdindex{bibitem}
参考文献由 \env{thebibliography} 环境包裹。每条参考文献由 \cmd{bibitem} 开头，其后是参考文献本身的内容：
\begin{command}
\cmd{begin}\marg*{thebibliography}\marg{widest label} \\
\quad \cmd{bibitem}\oarg{item number}\marg{citation} ...\\
\cmd{end}\marg*{thebibliography}
\end{command}
其中 \Arg{citation} 是 \cmd{cite} 使用的文献标签，
\Arg{item number} 自定义参考文献的序号，如果省略，则按自然排序给定序号。
\Arg{widest label} 用以限制参考文献序号的宽度，如 99 意味着不超过两位数字。通常设定为与参考文献的数目一致。

在 \cls{article} 文档类，\env{thebibliography} 环境自动生成不带编号的一节，标题默认为“References”；
而在 \cls{report} 或 \cls{book} 文档类中，则会生成不带编号的一章，标题默认为“Bibliography”。
用户可通过 \ref{sec:latex-settings} 节给出的方法定制参考文献的标题。

以下为一个使用 \env{the\-biblio\-graphy} 排版参考文献的例子：
\begin{verbatim}
\documentclass{article}
\begin{document}
\section{Introduction}
Partl~\cite{germenTeX} has proposed that \ldots

\begin{thebibliography}{99}
\bibitem{germenTeX} H.~Partl: \emph{German \TeX},
  TUGboat Volume~9, Issue~1 (1988)
\end{thebibliography}
\end{document}
\end{verbatim}

\subsection{\hologo{BibTeX} 数据库}\label{subsec:bibtex-data}

\index{bibtexdb@\protect\hologo{BibTeX} 数据库}
\hologo{BibTeX} 是最为流行的参考文献数据组织格式之一。它的出现让我们摆脱手写参考文献条目的麻烦。我们还可以通过参考文献样式的支持，
让同一份 \hologo{BibTeX} 数据库生成不同样式的参考文献列表。

\hologo{BibTeX} 数据库以 \texttt{.bib} 作为扩展名，其内容是若干个文献条目，每个条目的格式为：
\begin{command}
\texttt @\Arg{type}\texttt\{\Arg{citation}, \\
\qquad\Arg{key1} = \marg{value1}, \\
\qquad\Arg{key2} = \marg{value2}, \\
\qquad\ldots\\
\texttt\}
\end{command}

其中 \Arg{type} 为文献的类别，如 \texttt{article} 为学术论文，\texttt{book} 为书籍，\texttt{in\-collection} 为论文集中的某一篇，等等。
\Arg{citation} 为 \cmd{cite} 命令使用的文献标签。在 \Arg{citation} 之后为条目里的各个字段，以 \Arg{key} = \marg{value} 的形式组织。

我们在此简单列举学术论文里使用较多的 \hologo{BibTeX} 文献条目类别：
\begin{description}
  \item[\texttt{article}] 学术论文，必需字段有 author, title, journal, year; 可选字段包括 volume, number, pages, doi 等；
  \item[\texttt{book}] 书籍，必需字段有 author/editor, title, publisher, year; 可选字段包括 volume/number, series, address 等；
  \item[\texttt{incollection}] 论文集中的一篇，必需字段有 author, title, booktitle, publisher, year; 可选字段包括 editor, volume/number, chapter, pages, address 等；
  \item[\texttt{inbook}] 书中的一章，必需字段有 author/editor, title, chapter/pages, publisher, year; 可选字段包括 volume/number, series, address 等。
\end{description}

例如 \texttt{article} 类别的参考文献数据条目写法如下：
\begin{verbatim}
@article{Alice13,
  title = {Demostration of bibliography items},
  author = {Alice Axford and Bob Birkin and Charlie Copper and Danny Dannford},
  year = {2013},
  month = {Mar},
  journal = {Journal of \TeX perts},
  volume = {36},
  number = {7},
  pages = {114-120}}
\end{verbatim}
所有类别的文献条目格式请参考 \CTAN|biblio/bibtex/base/btxdoc.pdf|。

多数时候，我们无需自己手写 \hologo{BibTeX} 文献条目。从 Google Scholar 或者期刊/数据库的网站上都能够导出 \hologo{BibTeX} 文献条目，
老牌的文献管理软件 EndNote 也支持生成 \hologo{BibTeX} 格式的数据库。开源软件 JabRef 甚至支持 \hologo{BibTeX} 文献条目的导入、导出和管理。

\subsection{\hologo{BibTeX} 样式}\label{subsec:bibtex-style}

参考文献的写法在不同文献里千差万别，包括作者、标题、年份等各项的顺序和字体样式、文献在列表中的排序规则等。
\hologo{BibTeX} 用样式（style）来管理参考文献的写法。\hologo{BibTeX} 提供了几个预定义的样式，
如 \texttt{plain}, \texttt{unsrt}, \texttt{alpha} 等。
如果使用期刊模板的话，可能会提供自用的样式。样式文件以 \texttt{.bst} 为扩展名。

\cmdindex{bibliographystyle}
使用样式文件的方法是在源代码内（一般在导言区）使用 \cmd{biblio\-graphy\-style} 命令：
\begin{command}
\cmd{bibliographystyle}\marg{bst-name}
\end{command}
这里 \Arg{bst-name} 为 \texttt{.bst} 样式文件的名称，\textbf{不要带 \texttt{.bst} 扩展名}。

我们以 \ref{subsec:bibtex-data} 小节给出的数据条目为例，使用 \cmd{biblio\-graphy\-style} 命令选择不同的参考文献样式，
效果大致如表 \ref{tbl:bibtex-style} 所示。

\begin{table}[htp]
\caption{\hologo{BibTeX} 样式的排版效果}\label{tbl:bibtex-style}
\hrule
\begin{trivlist}\item\relax
\textbf{plain}\\{}
[1] Alice Axford, Bob Birkin, Charlie Copper, and Danny Dannford.
\newblock Demostration of bibliography items.
\newblock {\em Journal of \TeX perts}, 36(7):114--120, Mar 2013.

\medskip
\textbf{alpha}\\{}
[ABCD13] Alice Axford, Bob Birkin, Charlie Copper, and Danny Dannford.
\newblock Demostration of bibliography items.
\newblock {\em Journal of \TeX perts}, 36(7):114--120, Mar 2013.

\medskip
\textbf{abbrv}\\{}
[1] A.~Axford, B.~Birkin, C.~Copper, and D.~Dannford.
\newblock Demostration of bibliography items.
\newblock {\em Journal of \TeX perts}, 36(7):114--120, Mar 2013.

\medskip
\textbf{amsplain}（\AmS{} 文档类 \textsf{amsart} 等配套的样式）\\{}
[1] Alice Axford, Bob Birkin, Charlie Copper, and Danny Dannford, \emph{Demostration of bibliography
  items}, Journal of \TeX perts \textbf{36} (2013), no.~7, 114--120.

\medskip
\textbf{elsarticle-num}（Elsevier 提供的 \textsf{elsarticle} 文档类配套的样式）\\{}
[1] A.~Axford, B.~Birkin, C.~Copper, D.~Dannford, Demostration of bibliography items,
  Journal of \TeX perts 36~(7) (2013) 114--120.

\medskip
\textbf{IEEEtran}（\textsf{IEEEtran} 模板文档类配套的样式）\\{}
[1] A.~Axford, B.~Birkin, C.~Copper, and D.~Dannford, ``Demostration of
  bibliography items,'' \emph{Journal of \TeX perts}, vol.~36, no.~7, pp.
  114--120, Mar 2013.

\medskip
\textbf{gbt7714-numerical}（GB/T 7714---2015 样式，由 \textsf{gbt7714} 宏包提供）\\{}
[1] 陈登原. 国史旧闻: 第 1 卷[M]. 北京: 中华书局, 2000: 29.
\end{trivlist}
\hrule
\end{table}

\subsection{使用 \hologo{BibTeX} 排版参考文献}\label{subsec:bibtex-use}

\index{bibtex@\protect\hologo{BibTeX} 工具}
现在我们来看如何利用 \hologo{BibTeX} 数据库生成参考文献和引用。

第一步：准备一份 \hologo{BibTeX} 数据库，假设数据库文件名为 \texttt{books.bib}，
和 \LaTeX{} 源代码\textbf{一般位于同一个目录下}。

第二步：在源代码中添加必要的命令。假设源代码名为 \texttt{demo.tex}（见源代码 \ref{code:bibtex-demo}）。
\begin{enumerate}
\item \cmdindex{bibliographystyle}
首先需要使用命令 \cmd{bibliographystyle} 设定参考文献的格式。

\item \cmdindex{cite,nocite}
其次，在正文中引用参考文献。\hologo{BibTeX} 程序在生成参考文献列表的时候，通常只列出用了 \cmd{cite} 命令引用的那些。
如果需要列出未被引用的文献，则需要 \cmd{nocite}\marg{citation} 命令；而 \cmd{nocite}\marg*{*} 则让所有未被引用的文献都列出。

\item \cmdindex{bibliography}
再次，在需要列出参考文献的位置，使用 \cmd{biblio\-graphy} 命令代替 \env{the\-biblio\-graphy} 环境：
\begin{command}
\cmd{bibliography}\marg{bib-name}
\end{command}

其中 \Arg{bib-name} 是 \hologo{BibTeX} 数据库的文件名，\textbf{不要带 \texttt{.bib} 扩展名}。
\end{enumerate}

\begin{sourcecode}[htp]
\begin{Verbatim}
\documentclass{article}
\bibliographystyle{plain}
\begin{document}
\section{Some words}
Some excellent books, for example, \cite{citation1}
and \cite{citation2} \ldots

\bibliography{books}
\end{document}
\end{Verbatim}
\caption{利用 \texttt{books.bib} 生成参考文献的源代码 \texttt{demo.tex}。}\label{code:bibtex-demo}
\end{sourcecode}

注意：\cmd{bibliographystyle} 和 \cmd{bibliography} 命令缺一不可，没有这两个命令，使用 \hologo{BibTeX} 生成参考文献列表的时候会报错。

第三步：写好了以上两个文件之后，我们就可以开始编译了。
\begin{enumerate}
  \item 首先使用 \texttt{pdflatex} 或 \texttt{xelatex} 等命令编译 \LaTeX{} 源代码 \texttt{demo.tex}；
  \item 接下来用 \texttt{bibtex} 命令处理 \texttt{demo.aux} 辅助文件记录的参考文献格式、引用条目等信息。
  \texttt{bibtex} 命令处理完毕后会生成 \texttt{demo.bbl} 文件，内容就是一个 \env{thebibliography} 环境；
  \item 再使用 \texttt{pdflatex} 或 \texttt{xelatex} 等命令把源代码 \texttt{demo.tex} 编译\textbf{两遍}，读入参考文献并正确生成引用。
\end{enumerate}

整个过程使用的命令如下（可以略去扩展名）：
\begin{verbatim}
xelatex demo
bibtex demo
xelatex demo
xelatex demo
\end{verbatim}

使用 \texttt{latex + dvipdfmx} 命令编译时，则 \texttt{dvipdfmx} 命令放在最后，相当于先后使用
\texttt{latex}, \texttt{bibtex}, \texttt{latex}, \texttt{latex}, \texttt{dvipdfmx}。

\subsection{\pkg{natbib} 宏包}\label{subsec:natbib}

\pkgindex{natbib}
时下许多学术期刊比较喜欢使用人名——年份的引用方式，形如(\emph{Axford et~al.}, 2013)。
\pkg{natbib} 宏包提供了对这种“自然”引用方式的处理。

\cmdindex[natbib]{citep,citet}
除了 \cmd{cite} 之外，\pkg{natbib} 宏包在正文中支持两种引用方式：
\begin{command}
\cmd{citep}\marg{citation} \\
\cmd{citet}\marg{citation}
\end{command}

它们分别生成形如(\emph{Axford et~al.}, 2013) 和 \emph{Axford et~al.} (2013) 的人名——年份引用。
正确排版人名——年份引用还依赖于特定的 \hologo{BibTeX} 样式。\pkg{natbib} 提供了与 \LaTeX{} 预定义样式相对应的几个样式，
包括 \texttt{plainnat}、\texttt{abbrvnat} 和 \texttt{unsrtnat}。学术论文模板是否支持 \pkg{natbib}，需要参考其帮助文档。

\pkg{natbib} 宏包同样也支持数字引用，并且支持将引用的序号压缩，例如：
\begin{verbatim}
\usepackage[numbers,sort&compress]{natbib}
\end{verbatim}
调用 \pkg{natbib} 宏包时指定以上选项后，连续引用多篇文献时，会生成形如 (3--7) 的引用而不是 (3, 4, 5, 6, 7)。

\pkg{natbib} 宏包还有更多选项和用法，比如默认的引用是用小括号包裹的，可指定 \texttt{square} 选项改为中括号；
再比如 \cmd{citep} 命令也支持可选参数，为引用前后都添加额外内容。这里不再赘述，请参考 \pkg{natbib} 宏包的帮助文档。


\subsection{\pkg{biblatex} 宏包}\label{subsec:biblatex}

\pkgindex{biblatex}
本节的末尾简单介绍一下基于 \pkg{biblatex} 宏包排版参考文献的方式。
\pkg{biblatex} 宏包是一套基于 \LaTeX{} 宏命令的参考文献解决方案，提供了便捷的格式控制和强大的排序、分类、筛选、多文献表等功能。
\pkg{biblatex} 宏包也因其对 UTF-8 和中文参考文献的良好支持，被国内较多 \LaTeX{} 模板采用。

基于 \pkg{biblatex} 宏包的方式与基于 \hologo{BibTeX} 的传统方式有一定区别，下面从文档结构和命令、编译方式、样式选择等方面逐一介绍：

\subsubsection{文档结构和 \pkg{biblatex} 相关命令}

\cmdindex[biblatex]{addbibresource,printbibliography}
\begin{enumerate}
  \item 首先是在导言区调用 \pkg{biblatex} 宏包。宏包支持以 \Arg{key}=\Arg{value} 形式指定选项，包括参考文献样式 style、参考文献著录排序的规则 sorting 等。
  \item 接着在导言区使用 \cmd{addbibresource} 命令为 \pkg{biblatex} 引入参考文献数据库。与基于 \hologo{BibTeX} 的传统方式不同的是，这里需要写完整的文件名。
  \item 在正文中使用 \cmd{cite} 命令引用参考文献。除此之外还可以使用丰富的命令达到不同的引用效果，如 \cmd{citeauthor} 和 \cmd{citeyear} 分别单独引用作者和年份，\cmd{textcite} 和 \cmd{parencite} 分别类似 \pkg{natbib} 宏包提供的 \cmd{citet} 和 \cmd{citep} 命令，以及脚注式引用 \cmd{footcite} 等。
  \item 最后在需要排版参考文献的位置使用命令 \cmd{printbibliography}。
\end{enumerate}

\begin{sourcecode}[htp]
\begin{Verbatim}
% File: egbibdata.bib
@book{caimin2006,
  title      = {UML基础和Rose建模教程},
  address    = {北京},
  author     = {蔡敏 and 徐慧慧 and 黄柄强},
  publisher  = {人民邮电出版社},
  year       = {2006},
  month      = {1}
}

% File: demo.tex
\documentclass{ctexart}
% 使用符合 GB/T 7714-2015 规范的参考文献样式
\usepackage[style=gb7714-2015]{biblatex}
% 注意加 .bib 扩展名
\addbibresource{egbibdata.bib}

\begin{document}

见文献\cite{caimin2006}。

\printbibliography
\end{document}
\end{Verbatim}
\caption{应用 \pkg{biblatex} 的示例 egbibdata.bib 和 demo.tex。}
\end{sourcecode}

\subsubsection{编译方式}

\index{biber}
与基于 \hologo{BibTeX} 的传统方式不同的是，\pkg{biblatex} 宏包使用 \texttt{biber} 程序处理参考文献。因此上述文档的编译步骤为：

\begin{verbatim}
xelatex demo
biber demo
xelatex demo
xelatex demo
\end{verbatim}

\subsubsection{\pkg{biblatex} 样式和其它选项}

\pkg{biblatex} 使用的参考文献样式分为著录样式（bibliography style）和引用样式（citation style），分别以 \texttt{.bbx} 和 \texttt{.cbx} 为扩展名。参考文献的样式在调用宏包时使用 \texttt{style} 选项指定，或者使用 \texttt{bibstyle} 或 \texttt{citestyle} 分别指定：
\begin{verbatim}
% 同时调用 gb7714-2015.bbx 和 gb7714-2015.cbx
\usepackage[style=gb7714-2015]{biblatex}
% 著录样式调用 gb7714-2015.bbx，引用样式调用 biblatex 宏包自带的 authoryear
\usepackage[bibstyle=gb7714-2015,citestyle=authoryear]{biblatex}
\end{verbatim}

以下总结一些常用的参考文献样式，除 \pkg{biblatex} 宏包自带的样式外，许多样式以单独的宏包在发行版内发布。

\begin{description}
  \item[\texttt{authoryear}]
  \pkg{biblatex} 自带样式，类似 \pkg{natbib} 默认的引用样式效果。
  \item[\texttt{authortitle}]
  \pkg{biblatex} 自带样式，采用作者-题名（shorttitle 字段）的引用样式。
  \item[\texttt{verbose}]
  \pkg{biblatex} 自带样式，引用样式中包含作者、题名、书名、页码等字段的信息。
  \item[\texttt{alphabetic}]
  \pkg{biblatex} 自带样式，著录样式与 \hologo{BibTeX} 的 \texttt{alpha} 样式类似。
  \item[\texttt{trad-alpha}]
  \pkg{biblatex-trad} 样式包，移植自 \hologo{BibTeX} 默认的 \texttt{alpha} 样式。另外还包括 \texttt{trad-abbrv}、\texttt{trad-plain} 和 \texttt{trad-unsrt}。
  \item[\texttt{gb7714-2015}]
  符合中文文献著录标准 GB/T 7714---2015 的样式，著录按顺序编码排版。另外还包括按作者—年份顺序排版著录的样式 \texttt{gb7714-2015ay}。
  \item[\texttt{caspervector}]
  以中文文献著录标准 GB/T 7714---2015 为基础的一个样式。
  \item[\texttt{ieee}]
  兼容 \pkg{IEEEtran} 风格的样式，著录按顺序编码排版。另外还包括按作者—年份顺序排版著录的样式 \texttt{ieee-alphabetic}。
\end{description}

\section{索引和 makeindex 工具}\label{sec:index}

\pinyinindex{suoyin}{索引}
\index{makeindex@makeindex 工具}
书籍和大文档通常用索引来归纳关键词，方便用户查阅。\LaTeX{} 借助配套的 makeindex 程序完成对索引的排版。

\subsection{使用 makeindex 工具的方法}\label{subsec:makeidx}

要使用索引，须经过这么几个步骤（仍设源代码名为 \texttt{demo.tex}）：

\pkgindex{makeidx}
\cmdindex[makeidx]{makeindex}
第一步，在 \LaTeX{} 源代码的导言区调用 \pkg{makeidx} 宏包，并使用 \cmd{makeindex} 命令开启索引的收集：
\begin{verbatim}
\usepackage{makeidx}
\makeindex
\end{verbatim}

\cmdindex{index}
\cmdindex[makeidx]{printindex}
第二步，在正文中需要索引的地方使用 \cmd{index} 命令。\cmd{index} 命令的参数写法详见下一小节；
并在需要输出索引的地方（如所有章节之后）使用 \cmd{printindex} 命令。

第三步，编译过程：

\begin{enumerate}
  \item 首先用 \texttt{xelatex} 等命令编译源代码 \texttt{demo.tex}。编译过程中产生索引记录文件 \texttt{demo.idx}；
  \item 用 makeindex 程序处理 \texttt{demo.idx}，生成用于排版的索引列表文件 \texttt{demo.ind}；
  \item 再次编译源代码 \texttt{demo.tex}，正确生成索引列表。
\end{enumerate}

\subsection{索引项的写法}\label{subsec:index-entry}

\cmdindex{index}
添加索引项的命令为：
\begin{command}
\cmd{index}\marg{index entry}
\end{command}

其中 \Arg{index entry} 为索引项，写法由表 \ref{tbl:index-entry} 汇总。其中 \texttt!、\texttt @ 和 \texttt|
为特殊符号，如果要向索引项直接输出这些符号，需要加前缀 \texttt"；而 \texttt" 需要输入两个引号 \texttt{""} 才能输出到索引项。

\begin{table}[htp]
\centering
\caption{索引项的写法列表}\label{tbl:index-entry}
\begin{tabular}{lll}
  \hline
  \textbf{举例} &\textbf{索引项} &\textbf{备注}\\
  \hline
  \multicolumn{3}{l}{普通索引} \\[.8ex]
  \verb+hello+              & hello, 1             & 普通索引 \\
  \hline
  \multicolumn{3}{l}{分级索引，以 \texttt! 分隔，最多支持三级} \\[.8ex]
  \verb+hello+              & hello, 1             & 一级索引 \\
  \verb+hello!Peter+        &\quad Peter, 3 & 二级索引 \\
  \verb+hello!Peter!Jack+   &\qquad Jack,  3 & 三级索引 \\
  \hline
  \multicolumn{3}{l}{格式化索引，形式为 \Arg{alpha}\texttt @\Arg{format}} \\
  \multicolumn{3}{l}{\Arg{alpha}为纯字母，用来排序} \\
  \multicolumn{3}{l}{\Arg{format}为索引的格式，可以包括 \LaTeX 代码和简单的公式} \\[.8ex]
  \verb+Mobius@M\""obius+   & M\"obius, 2          & 输出重音 \\
  \verb+alpha@$\alpha$+     & $\alpha$, 7          & 输出公式 \\
  \verb+bold@\textbf{bold}+ & \textbf{bold}, 12    & 输出粗体 \\
  \hline
  \multicolumn{3}{l}{页码范围} \\[.8ex]
  \verb+morning|(+          & morning, 6--7        & 范围索引的开头 \\
  \verb+morning|)+          &                      & 范围索引的结尾 \\
  \hline
  \multicolumn{3}{l}{格式化索引页码} \\[.8ex]
  \verb+Jenny|textbf+       & Jenny, \textbf{3}       & 调用 \cmd{textbf} 加粗页码 \\
  \verb+Joe|see{Jenny}+     & Joe, \textit{see} Jenny & 调用 \cmd{see} 生成特殊形式 \\
  \verb+Joe|seealso{Jenny}+ & Joe, \textit{see also} Jenny & 调用 \cmd{seealso} 生成特殊形式 \\
  \hline
\end{tabular}
\end{table}

读者可以钻研一下以下给出的一个较为复杂的，结合多级索引、索引格式、页码格式等的用法示例。但在自己使用时，
最好还是遵循“简单的就是最好的”原则，尽量使用表 \ref{tbl:index-entry} 中的写法。
\begin{verbatim}
Test index.
\index{Test@\textsf{""Test}|(textbf}
\index{Test@\textsf{""Test}!sub@"|sub"||see{Test}}
\newpage
Test index.
\index{Test@\textsf{""Test}|)textbf}
\end{verbatim}

\section{使用颜色}\label{sec:color}

\pinyinindex{yanse}{颜色}
原始的 \LaTeX{} 不支持使用各种颜色。\pkg{color} 宏包或者 \pkg{xcolor} 宏包提供了对颜色的支持，给 PDF 输出生成颜色的特殊指令。

\subsection{颜色的表达方式}\label{subsec:color-code}

\pkgindex{color}
\cmdindex[color/xcolor]{color}
调用 \pkg{color} 或 \pkg{xcolor} 宏包后，我们就可以用如下命令切换颜色：
\begin{command}
\cmd{color}\oarg{color-mode}\marg{code} \\
\cmd{color}\marg{color-name}
\end{command}

颜色的表达方式有两种，其一是使用色彩模型和色彩代码，代码用 $0\sim1$ 的数字代表成分的比例。
\pkg{color} 宏包支持 \texttt{rgb}、\texttt{cmyk} 和 \texttt{gray} 模型，\pkg{xcolor} 支持更多的模型如 \texttt{hsb} 等。
\begin{example}
\large\sffamily
{\color[gray]{0.6}
  60\% 灰色} \\
{\color[rgb]{0,1,1}
  青色}
\end{example}

其二是直接使用名称代表颜色，前提是已经定义了颜色名称（没定义的话会报错）：
\begin{example}
\large\sffamily
{\color{red} 红色} \\
{\color{blue} 蓝色}
\end{example}

\pkgindex{xcolor}
\pkg{color} 宏包仅定义了 8 种颜色名称，\pkg{xcolor} 补充了一些，总共有 19 种，见表 \ref{tbl:colors}。

\def\showcolor#1{%
  \texttt{#1}\index{yanse@颜色!#1@\texttt{#1}}%
  \ \begingroup\fboxsep=0pt\fbox{{\color{#1}\vrule width 1.2em height 1.4ex}}\endgroup}
\def\showxcolor#1{%
  \texttt{#1}\index{yanse@颜色!#1@\texttt{#1} (\pkg{xcolor})}%
  \ \begingroup\fboxsep=0pt\fbox{{\color{#1}\vrule width 1.2em height 1.4ex}}\endgroup}

\begin{table}[htp]
\centering
\caption{\pkg{color} 和 \pkg{xcolor} 宏包可用的颜色名称}\label{tbl:colors}
\renewcommand\arraystretch{1.1}
\begin{tabularx}{0.8\textwidth}{*{4}{>{\raggedleft\arraybackslash}X}}
 \hline
 \multicolumn{4}{c}{基本的 8 种颜色名称（后三种颜色为 CMYK 模型）} \\
 \showcolor{black} & \showcolor{red} & \showcolor{green} & \showcolor{blue} \\
 \showcolor{white} & \showcolor{cyan} & \showcolor{magenta} & \showcolor{yellow} \\
 \hline
 \multicolumn{4}{c}{\pkg{xcolor} 额外可用的颜色名称：} \\
 \showxcolor{darkgray} & \showxcolor{gray} & \showxcolor{lightgray} &    \\
 \showxcolor{brown}  & \showxcolor{olive} & \showxcolor{orange} & \showxcolor{lime}\\
 \showxcolor{purple} & \showxcolor{teal} & \showxcolor{violet} & \showxcolor{pink} \\
 \hline
\end{tabularx}
\end{table}

\pkg{xcolor} 还支持将颜色通过表达式混合或互补：
\begin{example}
\large\sffamily
{\color{red!40} 40\% 红色}\\
{\color{blue}蓝色
\color{blue!50!black}蓝黑
\color{black}黑色}\\
{\color{-red}红色的互补色}
\end{example}

我们还可以通过命令自定义颜色名称，注意这里的 \Arg{color-mode} 是必选参数：
\begin{command}
\cmd{definecolor}\marg{color-name}\marg{color-mode}\marg{code}
\end{command}

如果调用 \pkg{color} 或 \pkg{xcolor} 宏包时指定 \texttt{dvipsnames} 选项，就有额外的 68 种颜色名称可用%
\footnote{\pkg{color} 宏包实际要指定 \texttt{dvipsnames} 和 \texttt{usenames} 选项。}。
\pkg{xcolor} 宏包还支持通过指定其它选项载入更多颜色名称。限于篇幅不展开介绍，详情请参考 \pkg{xcolor} 宏包的手册。

\subsection{带颜色的文本和盒子}\label{subsec:colorbox}

原始的 \cmd{color} 命令类似于字体命令 \cmd{bfseries}，它使之后排版的内容全部变成指定的颜色，
所以直接使用时通常要加花括号分组。\pkg{color} 和 \pkg{xcolor} 宏包都定义了一些方便用户使用的带颜色元素。

\cmdindex[color/xcolor]{textcolor}
输入带颜色的文本可以用类似 \cmd{textbf} 的命令：
\begin{command}
\cmd{textcolor}\oarg{color-mode}\marg{code}\marg{text} \\
\cmd{textcolor}\marg{color-name}\marg{text}
\end{command}

\cmdindex[color/xcolor]{colorbox}
以下命令构造一个带背景色的盒子，\Arg{material} 为盒子中的内容：
\begin{command}
\cmd{colorbox}\oarg{color-mode}\marg{code}\marg{material} \\
\cmd{colorbox}\marg{color-name}\marg{material}
\end{command}

\cmdindex[color/xcolor]{fcolorbox}
以下命令构造一个带背景色和有色边框的盒子，\Arg{fcode} 或 \Arg{fcolor-name} 用于设置边框颜色：
\begin{command}
\cmd{fcolorbox}\oarg{color-mode}\marg{fcode}\marg{code}\marg{material} \\
\cmd{fcolorbox}\marg{fcolor-name}\marg{color-name}\marg{material}
\end{command}

\begin{example}
\sffamily
文字用\textcolor{red}{红色}强调\\
\colorbox[gray]{0.95}{浅灰色背景} \\
\fcolorbox{blue}{yellow}{%
 \textcolor{blue}{蓝色边框+文字，%
  黄色背景}
}
\end{example}

\cmdindex{fboxrule,fboxsep}
\cmd{fcolorbox} 也可以像 \ref{subsec:fbox} 小节里的 \cmd{fbox} 那样调节 \cmd{fbox\-rule} 和 \cmd{fbox\-sep}；对于 \cmd{color\-box}，
调整 \cmd{fbox\-sep} 是有效的。这里不再给出具体的示例。

\section{使用超链接}\label{sec:hyperlinks}

\pinyinindex{chaolianjie}{超链接}
PDF 文档格式是现今最流行的电子文档格式，而电子文档最实用的需求之一就是超链接功能。
\LaTeX{} 中实现这一功能的是 \pkg{hyperref} 宏包。

\subsection{\pkg{hyperref} 宏包}\label{subsec:hyperref}

\pkgindex{hyperref}
\pkg{hyperref} 宏包涉及的链接遍布 \LaTeX{} 的每一个角落——目录、引用、脚注、索引、参考文献等等都被封装成超链接。
但这也使得它与其它宏包发生冲突的可能性大大增加，虽然宏包已经尽力解决各方面的兼容性，但仍不能面面俱到。
为减少可能的冲突，习惯上将 \pkg{hyperref} 宏包\textbf{放在其它宏包之后调用}。

\pinyinindex{xuanxiang}{选项（宏包/文档类）}
与 \pkg{graphicx} 宏包类似，\texttt{latex} + \texttt{dvipdfmx} 命令下调用 \pkg{hyperref} 宏包时，需要指定选项 \texttt{dvipdfmx}；
而 \texttt{pdflatex} 或 \texttt{xelatex} 命令下不需要。

\cmdindex[hyperref]{hypersetup}
\pkg{hyperref} 宏包提供了命令 \cmd{hypersetup} 配置各种参数。参数也可以作为宏包选项，在调用宏包时指定：
\begin{command}
\cmd{hypersetup}\marg*{\Arg{option1},\Arg{option2}=\Arg{value},\ldots} \\
\cmd{usepackage}\oarg*{\Arg{option1},\Arg{option2}=\Arg{value},\ldots}\marg*{hyperref}
\end{command}
当选项值为 \texttt{true} 时，可以省略“\texttt{=true}”不写。可用的参数见表 \ref{tbl:hyperref-settings}。

\begin{table}[htp]
\centering
\caption{\pkg{hyperref} 宏包提供的参数设置}\label{tbl:hyperref-settings}
\def\TF{\Arg{true\textnormal|false}}
\begin{tabular}{llp{19.5em}}
 \hline
 \textbf{参数}                     & \textbf{默认值} & \textbf{含义} \\
 \hline
 \texttt{draft=}\TF                & \textit{false}  & 关闭所有超链接、书签等功能（也可以通过文档类选项指定） \\
 \texttt{final=}\TF                & \textit{true}   & 开启所有超链接、书签等功能（也可以通过文档类选项指定） \\
 \hline
 \texttt{colorlinks=}\TF           & \textit{false}  & 设置为 \textit{true} 为链接文字带颜色，反之加上带颜色的边框 \\
 \texttt{hidelinks}                &                 & 取消链接的颜色和边框 \\
 \texttt{pdfborder=}\marg*{\Arg{n} \Arg{n} \Arg{n}}
                                   &  0 0 1          & 超链接边框设置，设为 0 0 0 可取消边框 \\
 \hline
 \texttt{bookmarks=}\TF\textsuperscript{\dag}
                                   & \textit{true}   & 是否生成书签 \\
 \texttt{bookmarksopen=}\TF        & \textit{false}  & 是否展开书签 \\
 \texttt{bookmarksnumbered=}\TF    & \textit{false}  & 书签是否带章节编号 \\
 \hline
 \texttt{pdftitle=}\Arg{string}    & 空              & 标题 \\
 \texttt{pdfauthor=}\Arg{string}   & 空              & 作者 \\
 \texttt{pdfsubject=}\Arg{string}  & 空              & 主题 \\
 \texttt{pdfkeywords=}\Arg{string} & 空              & 关键词 \\
 \texttt{pdfstartview=}\Arg{Fit\textnormal|FitH\textnormal|FitV}
                                   & \textit{Fit}    & 设置 PDF 页面以适合页面/适合宽度/适合高度等方式显示，默认为适合页面 \\
 \hline
\end{tabular}
\begin{quotation}
\small
\textsuperscript{\dag} 该选项只能作为宏包选项，在调用宏包时指定。
\end{quotation}
\end{table}

\subsection{超链接}\label{subsec:url-href}

\cmdindex[hyperref]{url,nolinkurl}
\pkg{hyperref} 宏包提供了直接书写超链接的命令，用于在 PDF 中生成 URL：
\begin{command}
\cmd{url}\marg{url} \\
\cmd{nolinkurl}\marg{url}
\end{command}

\cmd{url} 和 \cmd{nolinkurl} 都像抄录命令 \cmd{verb} 一样输出一个 URL，区别是前者还为 URL 加上了超链接，后者没有。%
\footnote{注意，一些 PDF 阅读器会为 URL 文本自动加上超链接，这些超链接不是 \LaTeX{} 生成的。}%
在 \cmd{url} 等命令的参数 \Arg{url} 里，
\textbf{可直接输入如 \texttt\%、\texttt\& 这样的特殊符号}。

\cmdindex[hyperref]{href}
我们也可以像 HTML 中的超链接一样，把一段文字作为超链接：
\begin{command}
\cmd{href}\marg{url}\marg{text}
\end{command}

\begin{example}
\url{https://wikipedia.org} \\
\nolinkurl{https://wikipedia.org} \\
\href{https://wikipedia.org}{Wiki}
\end{example}

\cmdindex[hyperref]{hyperref}
使用 \pkg{hyperref} 宏包后，文档中所有的引用、参考文献、索引等等都转换为超链接。
用户也可对某个 \cmd{label} 命令定义的标签 \Arg{label} 作超链接（注意这里的 \Arg{label} 虽然是可选参数的形式，但通常是\textbf{必填的}%
\footnote{不带可选参数的 \cmd{hyperref} 命令带四个必选参数，前三个组成特殊格式的超链接，最后一个同 \Arg{text}。}）：
\begin{command}
\cmd{hyperref}\oarg{label}\marg{text}
\end{command}

默认的超链接在文字外边加上一个带颜色的边框（在打印 PDF 时边框不会打印），可指定 \texttt{color\-links} 参数修改为将文字本身加上颜色，
或修改 \texttt{pdf\-border} 参数调整边框宽度以“去掉”边框；\texttt{hide\-links} 参数则令超链接既不变色也不加边框。
\begin{verbatim}
\hypersetup{hidelinks}
% or:
\hypersetup{pdfborder={0 0 0}}
\end{verbatim}

\subsection{PDF 书签}\label{subsec:pdf-bookmark}

\pinyinindex{PDFshuqian}{PDF 书签}
\pkg{hyperref} 宏包另一个强大的功能是为 PDF 生成书签。对于章节命令 \cmd{chapter}、\cmd{section} 等，
默认情况下会为 PDF 自动生成书签。和交叉引用、索引等类似，生成书签也需要多次编译源代码，第一次编译将书签记录写入 \texttt{.out} 文件，
第二次编译才正确生成书签。

书签的一些属性见表 \ref{tbl:hyperref-settings}。使用 \pkg{CJK} 宏包时，为了防止中文书签出现乱码，
需要进行繁琐的设置；但在使用 \pkg{ctex} 宏包和文档类、且使用 \texttt{xelatex} 或 \texttt{lualatex} 编译的情况下，
无需用户额外干预，即可正确生成中文书签。

\cmdindex[hyperref]{pdfbookmark}
\pkg{hyperref} 还提供了手动生成书签的命令：
\begin{command}
\cmd{pdfbookmark}\oarg{level}\marg{bookmark}\marg{anchor}
\end{command}
\Arg{bookmark} 为书签名称，\Arg{anchor} 为书签项使用的锚点（类似交叉引用的标签）。可选参数 \Arg{level} 为书签的层级，默认为 0。

\cmdindex[hyperref]{texorpdfstring}
章节命令里往往有 \LaTeX{} 命令甚至数学公式，而 PDF 书签是纯文本，对命令和公式的处理很困难，有出错的风险。
\pkg{hyperref} 宏包已经为我们处理了许多常见命令，如 \cmd{LaTeX} 和字体命令 \cmd{textbf} 等，
对于未被处理的命令或数学公式，就要在章节标题中使用如下命令，分别提供 \LaTeX{} 代码和 PDF 书签可用的纯文本：
\begin{command}
\cmd{texorpdfstring}\marg{\LaTeX{} code}\marg{PDF bookmark text}
\end{command}
比如在章节名称里使用公式 $E=mc^2$，而书签则使用纯文本形式的 \verb|E=mc^2|：
\begin{verbatim}
\section{质能公式 \texorpdfstring{$E=mc^2$}{E=mc\textasciicircum 2}}
\end{verbatim}

\subsection{PDF 文档属性}\label{subsec:pdf-settings}

\pkg{hyperref} 宏包还提供了一些参数用于改变 PDF 文档的属性，部分见表 \ref{tbl:hyperref-settings}。


\endinput
