本文主要介绍了TexLive的安装,以及在VSCode和neovim上配置Latex的编译方式,同时支持将TiKZ绘制的图片转为svg格式。

可以通过neovim+vimtex+zathuura进行编写、编译以及查看tex。

TeX

TeX Live‌是TeX语言的完整发行套件,包含LaTeX、底层引擎、宏包、字体及其他辅助工具。主要用来提供编译环境(如pdfLaTeX、XeLaTeX)、多语言支持包、排版调试工具等全流程解决方案

目前有两个主要的 TeX 系统,MiKTeX和TeX Live。两者都可用于Windows、macOS和Linux。MiKTeX在Windows上有很强的背景;在macOS上,TeX Live被捆绑到一个更大的集合中,称为MacTeX。

由于TeX Live支持多平台,并且由于它具有一些性能优势,因此优先选择Tex Live

Latex

基于TeX构建的宏包集合,通过预定义命令简化排版流程(如自动编号、交叉引用等)

LaTex是一种用于高质量排版的文档准备系统。最常用于大中型技术或科学文档,以及任何形式的出版。

LaTex并不是文字处理器。相反,LaTex使用户更专注于写入正确的内容,而不是担心文档的外观。

TeX Live

  1. 下载TeX Live镜像,国内镜像地址:文件-北京大学开源镜像站 TeXLive镜像
  2. 下载完成右键挂载即可。 TeXLive镜像挂载结果
  3. 以管理员权限运行终端,进入挂载盘符,执行install-tl-windows.bat --no-gui,按D修改安装路径,按I开始安装,直至安装完成。

TexStudio

在安装TeXLive时,如果使用no-gui方式那么安装路径会被默认加入到环境变量PATH中,因此接下来安装TeXstudio时不需要进行任何配置。

官网地址:TeXstudio - A LaTeX editor

国内镜像地址:Index of /github-release/texstudio-org/texstudio/4.8.7/ | 清华大学开源软件镜像站 | Tsinghua Open Source Mirror

如果出现命令找不到,则需要修改下面的配置。 TeXStudio配置界面

VSCode配置

将下面的JSON内容合并到VSCode的settings.json中,具体方式是将{}中的内容拷贝到settings.json的根对象中。

下面的配置支持将生成的PDF文件转换为svg,尤其是当使用TiKZ绘图时,非常好用。

转换工具使用的是pdf2svg,该工具的下载地址:jalios/pdf2svg-windows: pdf2SVG for windows (using poppler and cairo)

注意:在转换为svg时,该svg的背景是透明的,如果需要白色背景需要在svg中添加一行代码<rect width="100%" height="100%" fill="white"/>

{
     //------------------------------LaTeX 配置----------------------------------
    // 设置是否自动编译
    "latex-workshop.latex.autoBuild.run": "never",
    //右键菜单
    "latex-workshop.showContextMenu": true,
    //从使用的包中自动补全命令和环境
    "latex-workshop.intellisense.package.enabled": true,
    //编译出错时设置是否弹出气泡设置
    "latex-workshop.message.error.show": false,
    "latex-workshop.message.warning.show": false,
    // 编译工具和命令
    "latex-workshop.latex.tools": [
        {
            "name": "xelatex",
            "command": "xelatex",
            "args": [
                "-synctex=1",
                "-interaction=nonstopmode",
                "-file-line-error",
                "%DOCFILE%"
            ]
        },
        {
            "name": "pdflatex",
            "command": "pdflatex",
            "args": [
                "-synctex=1",
                "-interaction=nonstopmode",
                "-file-line-error",
                "%DOCFILE%"
            ]
        },

        {
            "name": "latexmk",
            "command": "latexmk",
            "args": [
                "-synctex=1",
                "-interaction=nonstopmode",
                "-file-line-error",
                "-pdf",
                "-outdir=%OUTDIR%",
                "%DOC%"
            ],
            "env": {}
        },

        {
            "name": "lualatex",
            "command": "lualatex",
            "args": [
                "-synctex=1",
                "-interaction=nonstopmode",
                "-file-line-error",
                "-shell-escape", //这个命令行在网上的Latex Workshop设置里一般没有,所以直接recipe会报错
                "%DOCFILE%"
            ]
        },

        {
            "name": "bibtex",
            "command": "bibtex",
            "args": ["%DOCFILE%"]
        },
        {
            "name": "pdf2svg",
            "command": "pdf2svg",
            "args": ["%DOCFILE%.pdf", "%DOCFILE%.svg"]
        }
    ],
    // 用于配置编译链
    "latex-workshop.latex.recipes": [
        {
            "name": "PDFLaTeX",
            "tools": ["pdflatex"]
        },
        {
            "name": "LuaLaTeX",
            "tools": ["lualatex"]
        },
        {
            "name": "XeLaTeX",
            "tools": ["xelatex"]
        },
        {
            "name": "BibTeX",
            "tools": ["bibtex"]
        },
        {
            "name": "LaTeXmk",
            "tools": ["latexmk"]
        },
        {
            "name": "xelatex -> bibtex -> xelatex*2",
            "tools": ["xelatex", "bibtex", "xelatex", "xelatex"]
        },
        {
            "name": "pdflatex -> bibtex -> pdflatex*2",
            "tools": ["pdflatex", "bibtex", "pdflatex", "pdflatex"]
        },
        {
            "name":"pdf2svg",
            "tools": ["pdf2svg"]
        }
    ],
    //文件清理。此属性必须是字符串数组
    "latex-workshop.latex.clean.fileTypes": [
        "*.aux",
        "*.bbl",
        "*.blg",
        "*.idx",
        "*.ind",
        "*.lof",
        "*.lot",
        "*.out",
        "*.toc",
        "*.acn",
        "*.acr",
        "*.alg",
        "*.glg",
        "*.glo",
        "*.gls",
        "*.ist",
        "*.fls",
        "*.log",
        "*.fdb_latexmk"
    ],
    //设置为onFaild 在构建失败后清除辅助文件
    "latex-workshop.latex.autoClean.run": "onFailed",
    // 使用上次的recipe编译组合
    "latex-workshop.latex.recipe.default": "lastUsed",
    //使用 SumatraPDF 预览编译好的PDF文件
    // 设置VScode内部查看生成的pdf文件
    "latex-workshop.view.pdf.viewer": "tab",
    // PDF查看器用于在\ref上的[View on PDF]链接
    "latex-workshop.view.pdf.ref.viewer": "auto",
}

示例

下面的示例给出使用TiKZ绘制的IPv4首部结构图的代码

\documentclass[tikz,border=5mm]{standalone}
\usepackage{ctex}
\usepackage{tikz}
 
\usetikzlibrary{positioning, shapes.multipart}
\begin{document}
	\begin{tikzpicture}[
		box/.style={
			draw, 
			minimum width=32cm,
			minimum height=1.5cm,
			align=center,
			inner sep=0
		},
		field/.style={
			draw, 
			minimum height=1.5cm,
			align=center,
			font=\normalsize,
			inner sep=0
		}
	]
		
		% \node [选项] (名称) at (坐标) {内容}; 默认绘制的为矩形	
		% 绘制32位行框架
		%\foreach \i in {0,1,2,3,4} {
		%	\node[box] (row\i) at (0,-\i*1.5) {};
		%	\node[left] at (row\i.west) {\i};
		%}
		% 确定原点位置
		%\draw[fill=red] (0,-3.75) circle (2pt) node[right] {原点};
		
		% 位数表示
		\node[minimum width=4cm] (left0) at (-15.5,1.0) {0};
		\node[minimum width=4cm] (left0) at (-0.5,1.0) {15};
		\node[minimum width=4cm] (left0) at (0.5,1.0) {16};
		\node[minimum width=4cm] (left0) at (15.5,1.0) {31};
		% 绘制右侧指示箭头和中间数字
		\node (A) at (17.0, 0.75) {};
		\node (B) at (17.0,-2.75) {};
		\draw[->,line width=1pt] (B) -- (A);
		\node at (17.0,-3) {20字节}; % 默认显示
		\node (C) at (17.0, -3.25) {};
		\node (D) at (17.0,-6.75) {};
		\draw[->,line width=1pt] (C) -- (D);
		
		\node (E) at (16.25, 0.75) {};
		\node (F) at (17.75, 0.75) {};
		\draw[-,line width=1.2pt] (E) -- (F);
		
		\node (G) at (16.25, -6.75) {};
		\node (H) at (17.75, -6.75) {};
		\draw[-,line width=1.2pt] (G) -- (H);

		% 第一行字段
		\node[field, minimum width=4cm] (version) at (-14,0) {版本\\4位};
		\node[field, minimum width=4cm, right=0pt of version] (ihl) {首部长度\\4位};
		\node[field, minimum width=8cm, right=0pt of ihl] (tos) {服务类型\\8位};
		\node[field, minimum width=16cm, right=0pt of tos] (totlen) {总长度\\16位};
		
		% 第二行字段
		\node[field, minimum width=16cm] (id) at (-8,-1.5) {标识符\\16位};
		\node[field, minimum width=3cm, right=0pt of id] (flags) {标志\\3位};
		\node[field, minimum width=13cm, right=0pt of flags] (frag) {分片偏移\\13位};
		
		% 第三行字段
		\node[field, minimum width=8cm] (ttl) at (-12,-3.0) {生存时间\\8位};
		\node[field, minimum width=8cm, right=0pt of ttl] (proto) {协议\\8位};
		\node[field, minimum width=16cm, right=0pt of proto] (checksum) {首部校验和\\16位};
		
		% 第四行字段
		\node[field, minimum width=32cm] (src) at (0,-4.5) {源IP地址\\32位};
		
		% 第五行字段
		\node[field, minimum width=32cm] (dst) at (0,-6.0) {目的IP地址\\32位};
		
		% 第六行字段
		\node[field, minimum width=32cm, minimum height=2.5cm] (dst) at (0,-8.0) {选项(如果有)};
		
		%% 第七行字段
		\node[field, minimum width=32cm, minimum height=2.5cm] (dst) at (0,-10.5) {数据};
	\end{tikzpicture}
\end{document}

编译结果如图所示(使用命令pdf2svg <input.pdf> <output.pdf>将上面编译生成的pdf转为svg,同时修改svg的背景颜色):

ipv4-header

Linux上搭建Tex环境

下面的命令用于安装Tex Live:

$ sudo apt install texlive
$ sudo apt install texlive-lang-chinese

Tex Live用于后台处理tex文档,在Tex Live安装完成之后需要安装tex的编辑器。同时还需要安装latex的编译器latexmk

$sudo apt install neovim
$sudo apt install latexmk

同时可以安装插件use("lervag/vimtex"),此时可以通过vim命令:VimtexCompile来进行编译,并在pdf阅读器上查看以及同步更新。

附赠一幅latexmk的配置文件如下:

# cat ~/.latexmkrc
$dvi_previewer = 'start xdvi -watchfile 1.5';
$ps_previewer  = 'start gv --watch';
$pdf_previewer = 'start zathura';

问题

Package ctex Error: CTeX fontset `fandol’ is unavailable.

latex的编译工具有多种,有些对中文字体库ctex不支持,比如pdflatex,但是latexmk默认情况下是通过pdflatex进行编译的,所以在引入ctex库时会报上述错误。

解决方法:

vimtex插件的配置文件中将latexmk的默认编译器替换为xelatex即可支持中文