美文网首页Mac 使用笔记
macOS 配置 LaTeX(MacTeX + VSCode +

macOS 配置 LaTeX(MacTeX + VSCode +

作者: 风逝FM | 来源:发表于2019-07-27 00:07 被阅读0次

    一、安装准备

    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 插件配置中文环境

    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. 创建 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}
    

    来查看系统环境变量,将displayfile文件移动到返回的一个合适的目录中,比如/usr/local/bin.

    1. 在 Skim 中配置如下


      配置 VS Code 为默认编辑器

      以配置 VS Code 为默认编辑器,并自动更新文件。

    2. 在 TeX 代码中按 cmd+option+j 快捷键,即可跳转到 PDF 文档中对应的位置;在 PDF 文档中按 cmd+shift+鼠标 快捷键,即可跳转到 TeX 代码中对应的位置。

    四、VSCode 相关设置

    1,自动换行

    VSCode 是默认不换行的,在设置中粘贴以下代码即可实现自动换行

    "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

    相关文章

      网友评论

        本文标题:macOS 配置 LaTeX(MacTeX + VSCode +

        本文链接:https://www.haomeiwen.com/subject/jfwjrctx.html