macOS 配置 LaTeX(MacTeX + VSCode + Skim)

一、安裝準備

1,安裝 MacTeX

Downloading MacTeX 2019 中下載最新版本的 MacTeX 并安裝。

2,安裝 VSCode 及相關插件

Download Visual Studio Code 中下載最新版本的 VSCode 并安裝。打開 VSCode,在 Extensions 搜索框中輸入 LaTeX, 選擇 LaTeX WorkShop 插件并安裝。如下圖:

安裝 LaTeX WorkShop 插件

安裝完成后,即可編譯英文 Tex 文件。如新建HelloWorld.tex文件

\documentclass{article}

\begin{document}
    Hello World.
\end{document}

選擇Build Latex Project 即可得到如下圖結果:

英文 TeX 文件編譯

二、 LaTeX WorkShop 插件配置中文環(huán)境

LaTeX WorkShop 插件默認只提供PDFLaTeX,而中文編譯需要XeFLaTeX,所以還需另行配置。依次選擇Code > Preferences > Settings,點擊下圖所示花括號圖標進入 Settings 的 JSON 文件:

進入 Settings 的 JSON 文件

粘貼以下設置代碼

"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"
          ]
        },
    ],

注意,不同設置要以逗號隔開。保存,即可編譯中文 Tex 文件,如新建Hello.tex文件

\documentclass{ctexart}

\begin{document}
     你好
\end{document}

選擇Build Latex Project 即可得到如下圖結果:

中文 TeX 文件編譯

三、配置外部 PDF 閱讀器及正反向跳轉

  1. 安裝 Skim PDF 閱讀器。
  2. 在 json 配置文件中粘貼如下代碼:
"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%"
],
  1. 創(chuàng)建 displayfile.txt文件,粘貼以下代碼
#!/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 u+x displayfile.txt

其中displayfile.txt文件的位置要對應,然后去掉.txt后綴。

  1. 在終端運行
echo ${PATH}

來查看系統(tǒng)環(huán)境變量,將displayfile文件移動到返回的一個合適的目錄中,比如/usr/local/bin.

  1. 在 Skim 中配置如下


    配置 VS Code 為默認編輯器

    以配置 VS Code 為默認編輯器,并自動更新文件。

  2. 在 TeX 代碼中按 cmd+option+j 快捷鍵,即可跳轉到 PDF 文檔中對應的位置;在 PDF 文檔中按 cmd+shift+鼠標 快捷鍵,即可跳轉到 TeX 代碼中對應的位置。

四、VSCode 相關設置

1,自動換行

VSCode 是默認不換行的,在設置中粘貼以下代碼即可實現(xiàn)自動換行

"editor.wordWrap": "on"

2,自定義 Snippets

依次選擇Code > Preferences > User Snippets,選擇 LaTeX,即打開latex.json文件,在里面可定義自己的 Snippets,如新建 Section
的 Snippets 可定義如下:

"LaTeX - sec": {
        "prefix": "sec",
        "body": ["\\section{$1}$0"],
        "description": "New Section"
    },

保存后,即可在編輯 TeX 文件時使用該 Snippets,如下圖


使用 Snippets

參考資料

[1] Configure Visual Stuido Code as LaTeX IDE
[2] 設置VsCode自動換行
[3] 在 macOS 上配置 VSCode 與 Skim 的 LaTeX 正反跳轉
[4] 使用VSCode編寫LaTeX

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內容