レポートや論文を書く上で役立つパッケージを紹介します.英文・和文のどちらでも使えると思います.
使い方はごく簡単にしか説明していません.詳細が気になる方は公式ドキュメントを参照してください.
自分も LaTeX に詳しいわけではないので,間違いがあるかもしれません.コメント等ありましたら Twitter (@opt_cp) までよろしくお願いします.
数式 (amsmath, amssymb, mathtools)
amsmath
: 必須.様々な数式環境が使えるようになる.\begin{alignat}
,\begin{aligned}
などamssymb
: blackboard bold の\mathbb
など(例: $\mathbb R$)mathtools
: 場合分けの\begin{dcases*}
,文字を定義するときに使う\coloneqq
など
日本語記事だと例えば以下が参考になります:
mathtools
はとにかく多機能なんですが,中でもお気に入りの \DeclarePairedDelimiter
を紹介します.例えばプリアンブル(\begin{document}
より前のことです)に
\usepackage{amsmath,amssymb,mathtools}
\DeclarePairedDelimiter\floor{\lfloor}{\rfloor}
\DeclarePairedDelimiter\set{\{}{\}}
\DeclarePairedDelimiterX\Set[2]{\{}{\}}{\mspace{2mu}{#1}\;\delimsize|\;{#2}\mspace{2mu}}
と書いて,本文で
\[
\Set*{x \in \mathbb R \setminus \set{1, 2}}
{\frac{1}{x-1} + \floor*{\frac{1}{x-2}} \leq 3}
\]
とすると,
\[
\left\{ \mspace{2mu} x \in \mathbb R \setminus \{1, 2\} \;\middle|\;
\frac{1}{x-1} + \left\lfloor \frac{1}{x-2} \right\rfloor \leq 3 \mspace{2mu} \right\}
\]
と同等の結果\[ \left\{ \mspace{2mu} x \in \mathbb R \setminus \{1, 2\} \;\middle|\;\frac{1}{x-1} + \left\lfloor \frac{1}{x-2} \right\rfloor \leq 3 \mspace{2mu} \right\} \]が得られます.前者の方がずっと可読性が高いですね!
(ところで,上の \mspace{2mu}
はちょっと邪悪っぽいので,よりよい方法をご存知の方は教えてくださると嬉しいです.)
定理 (amsthm, thmtools)
amsthm
: 下のthmtools
パッケージを使うため読み込むthmtools
: 定理系の環境を自作できる.カスタマイズもしやすい
例えば,プリアンブルに
\usepackage{amsthm,thmtools}
\usepackage{xcolor}
\declaretheoremstyle[
bodyfont=\normalfont, % フォントをイタリックにしない
numberwithin=section, % 番号を 3.1 のような形にする
shaded={bgcolor=gray!15}, % 背景を薄い灰色にする (xcolor パッケージが必要)
]{mystyle}
\declaretheorem[name=定義,style=mystyle]{mydefinition}
\declaretheorem[name=定理,style=mystyle]{mytheorem}
と書きます.
\declaretheoremstyle
で mystyle
という定理環境用の「スタイル」を定義しています.\declaretheorem
で mydefinition
環境と mytheorem
環境を定義しています.
上のように設定し,本文で \begin{mytheorem}
を使うと「定理」が書けます.
リンク (url, hyperref)
url
:\url
で URL が綺麗にかける(アンダースコアなども上手く処理してくれる)hyperref
: 式参照などに自動でリンクを貼ってくれる.\href
でウェブサイトへのリンクも貼れる.hypertexnames=false
というオプションが必要らしい(よく分かっていない)
プリアンブルに
\usepackage{xcolor}
\usepackage{url}
\usepackage[hypertexnames=false]{hyperref}
\hypersetup{
colorlinks,
linkcolor={green!40!black},
citecolor={green!40!black},
urlcolor={blue!70!black},
}
と書くと,式参照や文献参照に自動でリンクが貼られます.また,\href{https://atcoder.jp/}{\url{https://atcoder.jp/}}
や \href{https://atcoder.jp/}{AtCoder}
で URL リンクが貼れます.

\hypersetup
でリンクの色などを設定できます(デフォルトだとちょっとカッコ悪い).
日本語の文書で hyperref
パッケージを使う際は,\documentclass[dvipdfmx]{jsarticle}
などと,document class に dvipdfmx
オプションをつける必要があります.
参照 (cleveref, autonum)
cleveref
:\cref
,\Cref
で clever な参照ができるautonum
: 実際に参照した式にのみ自動で番号を振ってくれる
プリアンブルに
\usepackage{amsmath,mathtools}
\usepackage{amsthm,thmtools}
\declaretheorem{theorem} % theorem 環境を定義
\usepackage{cleveref}
\newcommand{\crefrangeconjunction}{--} % 3 つ以上連番で参照するときの表記を設定
\crefname{equation}{}{} % 数式参照時の表記を設定
\Crefname{equation}{Eq.}{Eqs.}
\crefname{theorem}{Theorem}{Theorems} % theorem 環境参照時の表記を設定
と書き,
\begin{theorem}\label{thm:main}
My awesome statement.
\end{theorem}
\begin{align}
f(x) &\coloneqq \sin x, \label{eq:def-f}\\
g(x) &\coloneqq \cos x, \label{eq:def-g}\\
h(x) &\coloneqq \tan x. \label{eq:def-h}
\end{align}
We prove \cref{thm:main}.
We define $f$, $g$, and $h$ by \cref{eq:def-f,eq:def-g,eq:def-h}.
\Cref{eq:def-f,eq:def-g} are ...
とすると,以下のように表示されます:

文中では \cref
を,文頭では \Cref
を使います.上は「式番号の前には Eq. と書いてほしくないが,文が式番号から始まるのは避けたい」という人向けの設定です.
\crefname{環境名}{単数形}{複数形}
で,\cref
コマンドの表記を設定します.「環境名」には,equation, figure, table, section, algorithm, line
などの他に,thmtools
パッケージで定義した自作環境も入れられます.数式系環境は \crefname{equation}
で一括設定することに注意してください.
上で紹介したパッケージは,amsmath
→ hyperref
→ cleveref
→ autonum
の順で読み込む必要があります.
図・表 (caption, subcaption)
caption
: 図・表のキャプションに関する細かい設定ができるsubcaption
: 図・表を複数並べたいときに使う.サブキャプションの設定ができる
図を一つだけ貼るには,
\begin{figure}
\centering
\includegraphics[width=0.5\linewidth]{dog}
\caption{A dog}
\label{fig:dog}
\end{figure}
とすればよいです(これにはパッケージは必要ありません).
図を複数並べたいときは,プリアンブルに
\usepackage[labelformat=simple]{subcaption}
\renewcommand\thesubfigure{(\alph{subfigure})}
と書き,
\begin{figure}
\centering
\subcaptionbox{A cat\label{fig:cat}}
[0.49\linewidth]{\includegraphics[width=0.95\linewidth]{cat}}\hfill%
\subcaptionbox{An elephant\label{fig:elephant}}
[0.49\linewidth]{\includegraphics[width=0.95\linewidth]{elephant}}%
\caption{Animals}
\label{fig:animals}
\end{figure}
としています.オプションで labelformat=simple
を設定した後,\renewcommand
で上書きするという怪しげなことをしていますが,これは前述の \cref
コマンドで参照が上手く表示されるようにするための苦肉の策です.
プリアンブルで \crefname{figure}{Figure}{Figures}
としておけば,\cref{fig:animals}
で $\text{Figure 1}$ と,\cref{fig:cat}
で $\text{Figure 1(a)}$ と表示してくれます.
表 (booktabs)
booktabs
:\cmidrule
などを使って美しい表が簡単に書ける
プリアンブルで
\usepackage{caption,subcaption}
\usepackage{booktabs}
とし,
\begin{table}
\centering
\caption{A nice table.}
\label{table:nice}
\begin{tabular}{@{}llr@{}} \toprule
\multicolumn{2}{c}{Item} \\ \cmidrule(r){1-2}
Animal & Description & Price (\$)\\ \midrule
Gnat & per gram & 13.65 \\
& each & 0.01 \\
Emu & stuffed & 33.33 \\
Armadillo & frozen & 8.99 \\ \bottomrule
\end{tabular}
\end{table}
とすると,以下のようになります:

\toprule, \bottomrule, \midrule, \cmidrule
による罫線の使い分けが便利です.
余談ですが,特に英語圏では「横線少なめ,縦線は極力無し」の表が美しいとされているようです.
参考: Small Guide to Making Nice Tables (PDF)
擬似コード (algorithm, algpseudocode)
algorithm
:\begin{algorithm}
が使えるようになるalgpseudocode
:\begin{algorithmic}
が使えるようになる.色々カスタマイズできる
プリアンブルで
\usepackage{amsmath,mathtools}
\usepackage{algorithm}
\usepackage[noend]{algpseudocode}
\renewcommand{\algorithmicdo}{\textbf{:}}
\renewcommand{\algorithmicthen}{\textbf{:}}
\renewcommand{\algorithmicrequire}{\textbf{Input:}}
\renewcommand{\algorithmicensure}{\textbf{Output:}}
\algnewcommand{\Break}{\textbf{break}}
とし,
\begin{algorithm}
\caption{Trial division}
\label{alg:trial-division}
\begin{algorithmic}[1]
\Require{Integer $n > 0$}
\Ensure{Prime factorization of $n$}
\State{$S \gets \emptyset$}
\Comment{Initialization}
\For{$p = 2,3,4,\dots$}
\If{$p^2 > n$}
\State{\Break}
\EndIf
\State{$m \gets \text{(the largest integer $k$ such that $p^k$ divides $n$)}$}
\If{$m \neq 0$}
\State{$S \gets S \cup \{(p, m)\}$}
\State{$n \gets n / p^m$}
\EndIf
\EndFor
\If{$n \neq 1$}
\State{$S \gets S \cup \{(n, 1)\}$}
\EndIf
\State{\Return{$S$}}
\end{algorithmic}
\end{algorithm}
と書くと,以下のようになります:

noend
オプションにより $\mathbf{end\ for}$ などが省略されます.上では $\mathbf{do}$ と $\mathbf{then}$ もコロンで置き換えて,Python 風の表記にしています.
以下のように,擬似コード内で関数を定義することもできます.再帰を含むアルゴリズムの記述に便利です:
\begin{algorithm}
\begin{algorithmic}[1]
\Function{MyFunction}{$x$}
\State{Do Something}
\EndFunction
\end{algorithmic}
\end{algorithm}
箇条書き (enumitem)
enumitem
: 箇条書き関連の詳細な設定ができる
パッケージを使わなくても \begin{itemize}
や \begin{enumerate}
で箇条書きができます.enumitem
を使うとこれらの詳細な設定ができます.
例えば,プリアンブルに
\usepackage{enumitem}
\setlist[enumerate]{
label=(\alph*),
ref=(\alph*),
topsep=0.4\baselineskip,
itemsep=0.1\baselineskip,
}
と書くと,enumerate
環境のラベルが $\text{(a), (b),...}$ になり,項目の間隔がデフォルトより狭くなります.
箇条書き環境を自作することもできます.ちょっと高度ですが,これと thmtools
,cleveref
を組み合わせることもできます.例えば,プリアンブルに
\usepackage{amsthm,thmtools,cleveref,enumitem}
\declaretheoremstyle[bodyfont=\normalfont]{mystyle2}
\declaretheorem[style=mystyle2]{assumption}
\newlist{enuminasmp}{enumerate}{1}
\setlist[enuminasmp]{label=(\alph*),ref=\theassumption(\alph*)}
\crefname{assumption}{Assumption}{Assumptions}
\crefalias{enuminasmpi}{assumption}
と書いて,
\begin{assumption}\label{asmp:function}
\leavevmode
\begin{enuminasmp}
\item\label{asmp:convex}
$f$ is convex,
\item\label{asmp:smooth}
$f$ has a Lipschitz continuous gradient.
\end{enuminasmp}
\end{assumption}
\cref{asmp:function} is ...
\cref{asmp:convex} is ...
とすると,以下のようになります:

つまり,Assumption のような自作環境でも,subcaption
と同様のことができます.
参考文献 (natbib, cite)
natbib
:\citep
や\citet
で文献参照が楽になるcite
: 参照時に文献番号を昇順にしてくれる
BibTeX を使うことを前提としています.
デフォルトでも \cite
コマンドがありますが,natbib
のコマンドはより便利です.使い方が以下の PDF にコンパクトにまとまっています.
参考: Reference sheet for natbib usage (PDF)
\usepackage[numbers]{natbib}
と numbers
オプションを渡すと,著者名ではなく文献番号で参照されるようになります.その際に役立つのが cite
パッケージです.これを読み込んでおくと,文献参照時に $[7, 3, 15]$ などとなってしまうところが,$[3, 7, 15]$ と自動で昇順になります.