LaTeXを使い始めてみた

📢 この記事は gemini-3-flash-preview によって翻訳されました

参考文献のある文章を書くとき、Wordの使い心地はなんとも言えない感じだったんだけど、LaTeXを試してみたら、前にMarkdownに乗り換えたときみたいに「内容だけに集中できる体験」が最高だって感動しちゃった。生成されるPDFもすごく綺麗で、見てるだけで楽しいんだ。

この記事では、VS Codeにインストールして使う方法を中心に、よく使う構文をまとめてみた。細かい詳細は、最後にある公式サイトのチュートリアルを参考にしてみてね。

インストール

TeX Liveをインストールするのが便利だよ https://www.tug.org/texlive/ 。必要な依存関係が全部入るんだけど、インストールにはめちゃくちゃ時間がかかるから注意してね。

次にVS Codeのプラグインを入れよう。Ctrl+Shift+X で拡張機能を開いて、「LaTeX Workshop」を検索してインストールする。

インストールが終わったら Ctrl+, で設定を開いて、「Latex Recipes」を検索。setting.json で編集して、latex-workshop.latex.recipes の配列の中で latexmk (xelatex) を一番上に持ってくる。ついでに自動折り返しも設定しておくといいかも。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
{
    "latex-workshop.latex.recipes": [
        {
            "name": "latexmk (xelatex)",
            "tools": [
                "xelatexmk"
            ]
        },
        {
            "name": "latexmk",
            "tools": [
                "latexmk"
            ]
        },
        {
            "name": "latexmk (latexmkrc)",
            "tools": [
                "latexmk_rconly"
            ]
        },
        {
            "name": "latexmk (lualatex)",
            "tools": [
                "lualatexmk"
            ]
        },

        {
            "name": "pdflatex -> bibtex -> pdflatex * 2",
            "tools": [
                "pdflatex",
                "bibtex",
                "pdflatex",
                "pdflatex"
            ]
        },
        {
            "name": "Compile Rnw files",
            "tools": [
                "rnw2tex",
                "latexmk"
            ]
        },
        {
            "name": "Compile Jnw files",
            "tools": [
                "jnw2tex",
                "latexmk"
            ]
        },
        {
            "name": "Compile Pnw files",
            "tools": [
                "pnw2tex",
                "latexmk"
            ]
        },
        {
            "name": "tectonic",
            "tools": [
                "tectonic"
            ]
        }
    ],
    "[latex]": {
        "editor.wordWrap": "on"
    },
}

ドキュメント構造

LaTeXのドキュメント構造は大体こんな感じ。ドキュメントは \begin{document} から始まって、その前はインポートとか設定を書く場所(プリアンブル)だよ。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
\documentclass[a4paper,12pt]{article} % The document class with options
% select T1 font encoding: suitable for Western European Latin scripts
\usepackage[T1]{fontenc}
% A comment in the preamble
\begin{document}
% This is a comment
This is   a simple
document\footnote{with a footnote}.

This is a new paragraph.
\end{document}

ドキュメントクラス

LaTeXはいろんなドキュメントクラスをサポートしてて、\documentclass{} で指定できるんだ。よく使う標準的なのはこの5つ。

  • article
  • report
  • book
  • letter
  • slides

1行目のインデント

indentfirst パッケージを読み込むと各段落の最初がインデントされるようになるよ。あとはインデントする幅を設定すればOK。

1
2
\usepackage{indentfirst}    % indent
\setlength{\parindent}{1em} % set indent for 1em

日本語対応(日文支持)

XeCJKを導入して、フォントを設定しよう。

1
2
3
4
5
\usepackage{xeCJK}      % kanji

\setCJKmainfont{MS Mincho}
\setCJKsansfont{MS Gothic}
\setCJKmonofont{MS Gothic}

見出し

\section{} を使って見出しを分けるよ。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
\documentclass{article}
\usepackage[T1]{fontenc}
\begin{document}
Hey world!

This is a first document.

\section{Title of the first section}

Text of material in the first section

Second paragraph.

\subsection{Subsection of the first section}

Text of material in the subsection.

\section{Second section}

Text of the second section.

\end{document}

数式

https://www.latexlive.com/ でプレビューしながら視覚的に編集できるよ。

リスト

リストを作る方法は2種類あるよ。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
\documentclass{article}
\usepackage[T1]{fontenc}
\begin{document}

Ordered
\begin{enumerate}
  \item An entry
  \item Another One
  \item Wow! Three entries
\end{enumerate}

Unordered
\begin{itemize}
  \item An entry
  \item Another One
  \item Wow! Three entries
\end{itemize}

\end{document}

参考文献

ドキュメントの最初で、参考文献のタイトル名を変更できるよ。例えば \renewcommand{\refname}{参考文献} と書けば、タイトルが「参考文献」になるんだ。

同じディレクトリに bib ファイル(例えば citations.bib)を作ってね。

引用するときはドキュメント内で直接 \cite{key} を使う。複数を同時に引用するときは , で区切ればOK。

1
\cite{key1, key2}

ドキュメントの最後に参考文献を生成するコードを書くよ。

1
2
3
4
5
6
\small
%% set small size
\bibliographystyle{IEEEtran}
%% you can change the style into any other styles available, I personally love IEEEtran.
\bibliography{citations}
%% to generate references, input the name of your .bib file and cite anywhere in the document.

参考記事

This post is licensed under CC BY-NC-SA 4.0 by the author.

Visits Since 2025-02-28

Hugo で構築されています。 | テーマ StackJimmy によって設計されています。