在使用Latex的过程中遇到了一些令人不太愉悦的问题:

前一阵发现了一个新兴的排版语言:typst。该语言很好地解决了上述问题,并且轻便易用,由于极快的编译速度,所见即所得,排版效果很好。

在typst的语法不同于Latex,更加符合现代编程语言的风格,例如:

它的编译速度很快、包管理很不错,从Latex转到typst也是非常容易上手的。

本地部署

typst是一个在线的排版工具,甚至支持多人协作。

但是也可以本地部署,可以加载自己的包。

github仓库上下载最新的release,解压后可以看到一个typst的可执行文件,将其加入环境变量,即可在命令行中使用typst命令。

在命令行中使用typst可以看到一些帮助信息

The Typst compiler

Usage: typst.exe [OPTIONS] <COMMAND>

Commands:
compile  Compiles an input file into a supported output format [aliases: c]
watch    Watches an input file and recompiles on changes [aliases: w]
init     Initializes a new project from a template
query    Processes an input file to extract provided metadata
fonts    Lists all discovered fonts in system and custom font paths
update   Self update the Typst CLI
help     Print this message or the help of the given subcommand(s)

Options:
    --color[=<WHEN>]  Set when to use color. auto = use color if a capable terminal is detected [default: auto] [possible values: auto, always, never]
    --cert <CERT>     Path to a custom CA certificate to use when making network requests [env: TYPST_CERT=]
-h, --help            Print help
-V, --version         Print version

可以使用typst compile命令来编译文件,使用typst update命令来更新typst。

Vscode插件

在Vscode中搜索typst,可以找到下面的插件:

安装这些插件,可以在Vscode中使用typst的语法高亮和自动补全,并且可以实时预览,和一键编译。

另外还推荐一个插件可以实现图片的高效粘贴:

这样加入图片只需要一个快捷键即可。

Hello world

下面是一个简单的例子:

// main.typ
= Hello world
你好,世界!

你可以点击插件提供的按钮(在当前文档右上角的预览和编译按钮)编译后得到一个pdf文件。你可以可以在命令行中使用typst compile main.typ命令来编译。

现在你已经学会了如何使用typst,可以开始你的排版之旅了。

Guide for LaTex Users

如果你是LaTex的用户,你可以参考这篇文章来快速上手typst。

语法对比

元素 LaTeX Typst
加粗 \textbf{strong} *strong*
强调 \emph{emphasis} _emphasis_
等宽字体/代码 \texttt{print(1)} print(1)
链接 \url{https://typst.app} https://typst.app/
标签 \label{intro} <intro>
引用 \ref{intro} @intro
引用 \cite{humphrey97} @humphrey97
列表 itemize环境 - List
编号列表 enumerate环境 + List
术语列表 description环境 / Term: List
图片 figure环境 figure函数
表格 table环境 table函数
公式 $x$align/equation环境 $x$$ x = y $

对于一些latex常用的环境,这里给出详细的说明:

itemize环境与enumerate环境

在typst中,使用-来表示itemize环境,使用+来表示enumerate环境。

- 你好
- 世界

的效果是

+ 你好
+ 世界

的效果是

  1. 你好
  2. 世界

figure环境

在typst中,使用figure函数来表示figure环境。

#figure(
  image("我是路径", width: 80%),
  caption: [
    我是图片
  ],
)

table环境

在typst中,使用table函数来表示table环境。

#figure(
  table(
  columns: (auto, auto, auto),
  inset: 10pt,
  align: horizon,
  [1], [2], [3],
  [4], [5], [6],
  [7], [8], [9],
  ),
  caption: [你是一个表格]
)

表格的呈现是:

1 2 3
4 5 6
7 8 9

并且居中且有caption。

align环境

公式环境是更容易使用的$x$$ x = y $分别是行内公式和行间公式。

行间公式就是多两个空格,也可以写成:

$
x = y
$

额外的话

更重要的是,最好通过阅读文档来学习typst的语法,文档中有很多例子,能够帮助你更快地自定义自己的块。

所有的这些特殊语法都对应一个函数,例如$对应math.equation函数,可以通过#set math.equation来定义样式。

Vscode自动补全

我们可以把一些固定的常用的块设置成自动补全。在Vscode中,ctrl+shift+p打开命令面板,输入Snippets: Configure User Snippets,选择typst,在其中添加:

{
	"Figure Template": {
		"prefix": "fig",
		"body": [
			"#figure(",
			"  image(\"\", width: 80%),",
			"  caption: [",
			"    ",
			"  ],",
			")"
		],
		"description": "Insert a figure template"
	},
	"Table Template": {
		"prefix": "tab",
		"body": [
			"#table(",
			"  columns: (auto, auto, auto),",
			"  inset: 10pt,",
			"  align: horizon,",
			"  [],[],[],",
			")"
		],
		"description": "Insert a table template"
	}
}

这样在编写的时候,只需要输入fig或者tab,就可以得到一个模板。可以根据自己的需求添加更多的模板。

模板与库的使用

参见包管理,可以通过

#import "@preview/example:0.1.0": *

来在线引入包。或者

#import "@local/example:0.1.0": *

来引入本地包。

可以参见我的typst模板库,来进行模板的使用。

你也可以针对我的模板写一个自动补全

"My Template": {
    "prefix": "temp",
    "body": [
        "#import \"@local/mytemplate:1.0.0\": *",
        "",
        "#show: project.with(",
        "   title: [这是一个标题],",
        "   info: [这是一个副标题],",
        "   authors: ( ),",
        "   time: \"这是一个时间\",",
        "   abstract: [],",
        "   keywords: (),",
        "   contents:true,",
        ")"
    ],
    "description": "Insert a template"
},

这样你可以快速引入模板,并且开始编辑了。

结语

typst是一个非常好的排版工具,它的语法简单易懂,排版效果很好,编译速度很快,包管理很方便,本地部署也很简单,非常适合用来写一些平时的文档。

但正如官方文档所写,这还是一个新兴的排版工具,可能还有一些不够完善的地方:

参考

https://typst.app/docs