【生产力工具】VScode + LaTeX + Skim:配置基于 macOS 的科研写作环境

背景

工欲善其事,必先利其器。缺少一个好的科研写作环境怎么行呢?😎

查了查 LaTeX 写作的相关软件,发现大家主要推荐 TeXsudio 或者 VSCode+latex-workshop。作为一个忠实的 VSCode 粉,自然就选择了 VSCode+latex-workshop 环境。

环境配置流程

主要包含软件安装、VSCode 配置以及正反向查找设置。

安装

  1. 安装 MacTeX

  2. 在 VSCode 中安装 LaTeX Workshop 插件

  3. 安装 Skim

    1
    brew cask install skim

    安装时遇到了 brew 连接 github 的问题

VSCode 设置

  1. 使用 XeFLaTeX 编译中文

    setting.json 文件中添加如下代码:

    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
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    {
    // other rules...

    // latex-workshop
    "latex-workshop.latex.tools": [
    {
    "name": "xelatex",
    "command": "xelatex",
    "args": [
    "-synctex=1",
    "-interaction=nonstopmode",
    "-file-line-error",
    "%DOC%"
    ]
    },
    {
    "name": "pdflatex",
    "command": "pdflatex",
    "args": [
    "-synctex=1",
    "-interaction=nonstopmode",
    "-file-line-error",
    "%DOC%"
    ]
    },
    {
    "name": "latexmk",
    "command": "latexmk",
    "args": [
    "-synctex=1",
    "-interaction=nonstopmode",
    "-file-line-error",
    "-pdf",
    "%DOC%"
    ]
    },
    {
    "name": "bibtex",
    "command": "bibtex",
    "args": [
    "%DOCFILE%"
    ]
    }
    ],
    "latex-workshop.latex.recipes": [
    {
    "name": "XeLaTeX",
    "tools": [
    "xelatex"
    ]
    },
    {
    "name": "PDFLaTeX",
    "tools": [
    "pdflatex"
    ]
    },
    {
    "name": "latexmk",
    "tools": [
    "latexmk"
    ]
    },
    {
    "name": "BibTeX",
    "tools": [
    "bibtex"
    ]
    },
    {
    "name": "xelatex -> bibtex -> xelatex*2",
    "tools": [
    "xelatex",
    "bibtex",
    "xelatex",
    "xelatex"
    ]
    },
    {
    "name": "pdflatex -> bibtex -> pdflatex*2",
    "tools": [
    "pdflatex",
    "bibtex",
    "pdflatex",
    "pdflatex"
    ]
    },
    ],
    }
  2. 配置外部 pdf 预览工具:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    {
    // other rules...

    // external pdf viewer
    "latex-workshop.view.pdf.viewer": "external",
    "latex-workshop.view.pdf.external.synctex.command": "/Applications/Skim.app/Contents/SharedSupport/displayline",
    "latex-workshop.view.pdf.external.synctex.args": [
    "-r",
    "%LINE%",
    "%PDF%",
    "%TEX%"
    ],
    "latex-workshop.view.pdf.external.viewer.command": "displayfile",
    "latex-workshop.view.pdf.external.viewer.args": [
    "-r",
    "%PDF%"
    ],
    "editor.wordWrap": "on",
    }

外部 pdf 预览与正反向跳转

  1. displayfile 可执行文件

    创建 displayfile 文件,输入下列代码:

    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
    #!/bin/bash

    # displayfile (Skim)
    #
    # Usage: displayfile [-r] [-g] PDFFILE
    #
    # Modified from "displayline" to only revert the file, not jump to a given line
    #

    if [ $# == 0 -o "$1" == "-h" -o "$1" == "-help" ]; then
    echo "Usage: displayfile [-r] [-g] PDFFILE
    Options:
    -r, -revert Revert the file from disk if it was open
    -g, -background Do not bring Skim to the foreground"
    exit 0
    fi

    # get arguments
    revert=false
    activate=true
    while [ "${1:0:1}" == "-" ]; do
    if [ "$1" == "-r" -o "$1" == "-revert" ]; then
    revert=true
    elif [ "$1" == "-g" -o "$1" == "-background" ]; then
    activate=false
    fi
    shift
    done
    file="$1"
    #shopt -s extglob
    #[ $# -gt 2 ] && source="$3" || source="${file%.@(pdf|dvi|xdv)}.tex"

    # expand relative paths
    [ "${file:0:1}" == "/" ] || file="${PWD}/${file}"

    # pass file arguments as NULL-separated string to osascript
    # pass through cat to get them as raw bytes to preserve non-ASCII characters
    /usr/bin/osascript \
    -e "set theFile to POSIX file \"$file\"" \
    -e "set thePath to POSIX path of (theFile as alias)" \
    -e "tell application \"Skim\"" \
    -e " if $activate then activate" \
    -e " if $revert then" \
    -e " try" \
    -e " set theDocs to get documents whose path is thePath" \
    -e " if (count of theDocs) > 0 then revert theDocs" \
    -e " end try" \
    -e " end if" \
    -e " open theFile" \
    -e "end tell"

    更改为可执行文件:

    1
    chmod 744 displayfile

    放置此文件至某环境变量路径中:

    1
    mv displayfile /usr/local/bin/
  2. 配置 Skim

    在 设置 -> 同步 中,勾选 “检查文件更新” 和 “重新加载”;

    同时更改 “PDF-TeX 同步支持” 为 “Visual Studio Code”;

使用指南

基本操作:

  1. 正反向查找快捷键

    • 在 TeX 中:command + option + j 转跳至 PDF 预览文件的对应位置;

    • 在 PDF 预览文件中:command + shift + mouse 转跳至 TeX 代码对应位置;

  2. 编译 TeX 代码

    在编辑 TeX 文件时,点击右方侧栏的 TeX 按钮 -> 点击 Build LaTeX project

    如果存在参考文献,需要点击 xelatex -> bibtex -> xelatex*2

    • 注:VSCode 在保存 TeX 文件时自动编译。如果希望关闭此功能,在 setting.json 中添加 "latex-workshop.latex.autoBuild.run": "never"