開始使用 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},在此之前的地方都是匯入或者說是設定(Preamble)。

 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{} 中指定,常見的有五個標準類別:

  • article
  • report
  • book
  • letter
  • slides

首行縮排

匯入 indentfirst 套件以確保每個段落都縮排,然後設定縮排字元數。

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/ 進行視覺化編輯。

列表

有兩種方式實現列表:

 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},多個則使用 , 隔開:

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.