22

WSL2 和 LaTex 的火花 - Scvoet

 3 years ago
source link: https://scvoet.me/article/use-latex-in-wsl2-with-vscode
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

$LaTeX$ 是基于 $TeX$ 的一种排版系统。

$LaTeX$ 和 markdown 的设计十分相似,都是为了杀死以 Word 为代表的所见即所得模式。

「All in plain text.」

但相较之,$LaTeX$ 因为其专注文本和排版高定制性等特点,多被用于学术界,不难发现几乎每个理科教师都会使用 $LaTeX$(哪怕仅在输入公式时使用),而许多高校也将其为论文的可选格式之一。

在 WSL2 中安装 TeX Live展开目录

你可以选择通过你所使用的 Linux 发行版的包管理器进行一键安装,需要注意的是 deb 系和 rpm 系的源普遍较旧,如果需要安装最新版 Tex Live,推荐使用在线安装或挂载 iso 进行安装。

Arch 系展开目录

pacman -S texlive-core

Debian 系展开目录

apt install texlive-full

在线安装展开目录

在线安装对网络环境有所要求,你可以通过 CTAN mirror 查看临近你设备所在地的镜像。

以下为方便行文使用 tuna 镜像安装作为示例。

wget https://mirrors.tuna.tsinghua.edu.cn/CTAN/systems/texlive/tlnet/install-tl-unx.tar.gz
tar -xzf install-tl-unx.tar.gz
cd install-tl-20210513
./install-tl -repository https://mirrors.tuna.tsinghua.edu.cn/CTAN/systems/texlive/tlnet/

跟随指示配置并安装好后,需要在终端配置文件(如 .bashrc.zshrc)中设置环境变量。

export PATH=/usr/local/texlive/2021/bin/x86_64-linux:$PATH 

虚拟光驱挂载展开目录

如果你已有一个 iso 文件,先将其加载为虚拟光驱,并记录虚拟光驱的对应盘符,随后将其挂载至 WSL 中。

以下命令将虚拟光驱的盘符假定为 E:,你应该根据实际情况进行修改。

mkdir /mnt/img
mount -t drvfs E: /mnt/img
mnt/img/install-tl

跟随指示配置并安装好后,和在线安装相同的是也需要在终端配置文件(如 .bashrc.zshrc)中设置环境变量。

export PATH=/usr/local/texlive/2021/bin/x86_64-linux:$PATH 

VSCode 中使用 $LaTeX$展开目录

安装好 Remote-WSL 扩展LaTex Workshop 扩展后,点击 VSCode 左下角高亮的 Remote 按钮连接至 WSL 环境。

配置 LaTeX Workshop展开目录

VSCode 配置文件一般在用户目录的 .vscode-server/data/Machine/settings.json 中,可参考以下示例。

{
    "latex-workshop.latex.tools": [
        {
            "name": "lualatex",
            "command": "lualatex",
            "args": [
                "-synctex=1",
                "-interaction=nonstopmode",
                "-file-line-error",
                "%DOCFILE%.tex"
            ]
        }
    ],
    "latex-workshop.latex.recipes": [
        {
            "name": "lualatex",
            "tools": [
                "lualatex",
            ]
        }
    ]
}

现在创建一个 .tex 扩展名的文件,并输入 $TeX$ 语句进行测试。

\documentclass[UTF8]{ctexart}
\begin{document}
你好,world!
\end{document}

格式化错误展开目录

如果你在保存时,VSCode 弹出「Formatting failed. Please refer to LaTeX Workshop Output detail.」错误。

在终端中运行

cpan

回车完成自动配置后,执行

latexindent

查看所缺少的模块,例如缺少 Log::Log4perl 模块,则执行

cpan Log::Log4perl

并再次执行 latexindent 检查是否仍有缺失,直至不再输出报错。现在你可以正常使用格式化功能了。

文件保存后,你将看到与该 .tex 文件同级的目录下出现了同名的 .pdf 文件。

参见展开目录


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK