理解基本

latex 是一个文档准备系统 (document preparing system),它非常适用于生成高印 刷质 量的科技类和数学类文档。它也能够生成所有其他种类的文档,小到简单的信件,大 到完整的书 籍。latex 使用 tex 作为它的排版引擎。

latex 本质上是利用各种个这样的宏包提供的标记命令来标记文本,从而编译出对应的 格 式,如果学习过 html 标签文本的同学会更加的了解这一书写规范。现代的文本编辑格 式 从普不同文本,到简单的 markdown 以及其他的 markdown 变体,到复杂的 html 以及 latex, 这样的纯文本加上标记的文档,实际上是让人更加的注重文章的内容,而不必去限 定于某种软件才能编辑,无论是跨平台还是版本管理(vcs)上都是十分具有优势的。

另外在学术领域上 latex 上应用比较广泛,markdown 则在程序开发者应用比较多。

工具

language server protocol (lsp) 和 文本编辑器

texlab 为 latex 提供了 语言服务器协议(lsp)。

texlab 和 tectonic 一起使用 https://github.com/latex-lsp/texlab/wiki/tectonic .

tectonic

推荐使用 tentonic , 这是使用 rust 编写的更加现代化 tex 引擎,内部使用的 xetex 和 texlive 驱动。 它会自动下载支持的文件,也就是不用自动管理资源文件了,编译文件十分的简单。

比如编译单文件

user@host $ tectonic paper.tex
running tex ...
note: warnings were issued by the tex engine; use --print and/or --keep-logs for details.
running bibtex ...
rerunning tex because bibtex was run ...
rerunning tex because "paper.aux" changed ...
rerunning tex because "paper.aux" changed ...
running xdvipdfmx ...
writing paper.pdf (383521 bytes)
skipped writing 3 intermediate files (use --keep-intermediates to keep them)

使用tectonic v2 管理项目

tectonic v2文档

tectonic -x 是 v2 版本,提供了更加现代化的命令工具

tectonic -x 0.12.0
process (la)tex documents

usage:
    tectonic -x [options] <subcommand>

flags:
    -h, --help       prints help information
    -v, --version    prints version information

options:
    -c, --chatter <level>    how much chatter to print when running [default: default]  [possible values: default, minimal]
        --color <when>       control colorization of output [default: auto]  [possible values: always, auto, never]

subcommands:
    build      build a document 编译项目
    bundle     commands relating to this document’s tex file bundle
    compile    run a standalone (la)tex compilation
    dump       run a partial compilation and output an intermediate file
    help       prints this message or the help of the given subcommand(s)
    new        create a new document 创建新的项目
    show       display various useful pieces of information
    watch      watch input files and execute commands on change

简单的 tex 项目

创建项目

tectonic -x new tex-demo

可以看到创建了文件项目 tex-demo

tex-demo
├── src
│  ├── _postamble.tex
│  ├── _preamble.tex
│  └── index.tex
└── tectonic.toml

查看配置文件 tectonic.toml 可以看到 output部分标记了输出的类型。
src文件夹内为文件内容,其中构建命令处理文件的顺序为:

  1. src/_preamble.tex
  2. src/index.tex
  3. src/_postamble.tex

它这里处理主要是为了隔离内容与 latex 样板, 实际的项目中可以按照 \include{xxxx}语法自行构建处理顺序。

编译个pdf

进入 tex-demo 文件夹的目录

> tectonic -x build

note: "version 2" tectonic command-line interface activated
note: connecting to https://data1.fullyjustified.net/tlextras-2022.0r0.tar
note: downloading index https://data1.fullyjustified.net/tlextras-2022.0r0.tar.index.gz
note: downloading sha256sum
running tex ...
note: downloading ts1cmr.fd
rerunning tex because "default.aux" changed ...
running xdvipdfmx ...
writing `/...path.../tex-demo/build/default/default.pdf` (2.96 kib)
skipped writing 1 intermediate files (use --keep-intermediates to keep them)

可以看到输出的日志文件中显示已保存文件到 build/default/default.pdf 了。打开pdf文件, 应该可以看到和 src/index.tex 中相同的文件。

这里还无法编译中文,需要使用引入中文包才可以。

常见的语法

命令是以反斜杠`'开头

\命令名称[配置]{参数1,参数...} 

注释

注释使用 % 开头开始标记

基础模板

% 文档类型指定(ctexart中文支持)
\documentclass[utf8]{ctexart}

% 前言区
% 一般用于加载宏包以及设置,索引等
\usepackage{multicol} % 分栏
\usepackage{indentfirst} % 首行缩进
\usepackage{graphicx} % 图片引用
\usepackage{makeidx} % 调用 makeidx 宏包,用来处理索引 % 开启索引的收集
\makeindex % 开启索引
\bibliographystyle{plain} % 指定参考文献样式为 plain
\graphicspath{{../images}} % 设定图片目录
\pagestyle{plain} % 设定页码样式

\begin{document}

% 正文区

\frontmatter % 前言部分, 页码使用小写罗马数字
\maketitle % 标题页

\include{preface} % 前言章节 preface.tex

\tableofcontents % 生成目录

\mainmatter  % 正文部分, 页码使用阿拉伯数字从1开始
\include{chapter1} % 第一章 chapter1.tex 
\include{chapter2} % 第二章 chapter2.tex
...

\appendix % 附录
\include{appendixa} % 附录 a appendixa.tex

...

\backmatter % 后记部分
\include{epilogue} % 后记 epilogue.tex

\bibliography{books} % 利用 bibtex 工具从数据库文件 books.bib 生成参考文献 
\printindex % 利用 makeindex 工具生成索引

\end{document}

可以看出前言区域一般定义宏包以及配置,正文则调用宏包以及排版,而实际的内容则用 \include{} 来调用和组织

\begin{环境} ... \end{环境} 来标记特殊环境

preamble 前言部分

meta 信息

\title{文章标题}
\author{作者}
\date{\today}

这里的 \today 自动生成当天的日期.

在正文中显示这些信息,需要追加 \maketitle 命令追加到正文。

正文部分

章节

\part{部分}
\chapter{大章}
\section{章节}
\subsection{子章节}
\subsubsection{子章节}

% 段落不会生成编号
\paragraph{段落标题}
\subparagraph{子段落标题}

% 使用标记包 hyperref
% 标记以供引用
\label{标记内容}
% 对应使用
\ref{标记内容}

article 文档类带编号的层级为 \section / \subsection / \subsubsection 三级;
report / book 文档类带编号的层级为 \chapter / \section / \subsection 三级。

字体装饰

\ldots % 省略号
\underline{underlined} % 为参数部分追加下划线
\textbf{粗体}
\textit{斜体}

% 这里使用 `{ }` 来限定范围,否则宏命令后的部分都会改变,对于其他的宏命令也是可以使用
% 类似的方式来限定范围
{\color{red} 红色}
{\color{blue} 蓝色}

排版

\\[1] % 断行
\newline % 断行
\newpage % 断页
\clearpage % 断页
\- % 断词

\centering % 居中
\raggedleft % 左对齐
\raggedright % 右对齐

列表环境

  • enumerate 有序列表
  • itemize 无序列表
  • description 描述
\begin{enumerate}
\item ... 
    % 可嵌套
    \begin{itemize}
    \item ... 
    \end{itemize}
\end{enumerate}

常用环境

\begin{center} ... \end{center} % 居中
\begin{flushleft} ... \end{flushleft} % 左对齐
\begin{flushright} ... \end{flushright} % 右对齐

\begin{quote} 引用较短的文字 \end{quote}
\begin{quotation} 引用若干段落的文字,首行缩进 \end{quotation}
\begin{verse} 引用若干段落的文字, 首行悬挂 \end{verse}

\begin{verbatim} 引用代码 \end{verbatim}

graphicx 图片包命令

% 引入图片, 可省略后缀
% width=0.5\textwidth
% height=
% scale = 缩放
% angle = 旋转
\includegraphics[scale=0.5]{cat.png}

% 居中 + 标题
\begin{figure}
\centering
\includegraphics[scale=0.5]{cat.png}
\caption{图片标题}
\end{figure}

表格

tabular 配置

  • l/c/r 对齐方式 左中右
  • p 为单元格宽度
  • | 表示绘制竖线,可以使用 @{内容} 来自定义
% tabular 环境
\begin{tabular}{l|c|r| p{6em}} 

\hline % 画线 --- 横线
% 内容 列 使用 `&` 来分割, `\\` 用于换行

列1 & 列2 & 列3 
\end{tabular}

测试

\begin{table}
\begin{center}
\begin{tabular}{l|c|r}
t1111 & t2 & t3 \\ \hline
c1 & cccc2 & c3 \\
c1111 & c2 & c3 \\ \cline{2-3}
c1 & c2 & c33333 \\ \hline
\end{tabular}
\caption{标题}
\end{center}
\end{table}

数学

% 行间公式 \( ... \)
行间公式 \(x^2 + y^2 = z^2\) ....
% 或者使用 $ ... $
公式 $a^2 + b^2 = c^2$ 使用...

% 段落居中 \[ ... \]
\[ x^n + y^n = z^n \]

% 使用公式环境
% 这个等效与 \[...\]
\begin{equation}  ... \end{equation}

% 比如多行环境
\begin{align} 
a & = b + c \\ 
&=d+e
\end{align}

模板

中文支持

% 图书排版
\documentclass[utf8]{ctexbook}
% 文章排版
\documentclass[utf8]{ctexart}

站点 latex 工作室 提供了不少模板可供使用。

生成pdf

tectonic 会自动下载需要包进行编译处理

# 使用 v1 直接编译 tex 文件
tectonic xxx.tex
# 使用 v2 编译项目
tectonic -x build

结语

latex 是一套非常先进的排版系统,但是也是一个非常复杂的系统(84年),编译速度也 在现代也 不是十分具有优势。在国外已经有一些组织想要重写,以及移植或者在想着替代 它了,虽然发展很缓慢,以后被替 代或者被迫改变的可能性还是很大的。

  • https://typst.app 这是一个想要替代 latex 的系统,主打易用性和编译速度,画的 饼很大,但是github中也在迭代,非常早期的一个项目。

    其中数学部分的解析器非常的棒

  • https://news.ycombinator.com/item?id=25328874 讨论使用 xyz 重写 latex,多数 人都有提到速度的问题