开始使用 LaTeX

在写带参考文献的文章的时候,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{} 指定,常见的有五个标准类别

  • 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.