1. 程式人生 > 其它 >Latex+vscode常見報錯及解決方案

Latex+vscode常見報錯及解決方案

基礎知識

vscode是如何編譯Latex的?

利用CTRL+,開啟vscode的設定介面,搜尋latex,可以進入settings.json

settings.json資料夾中,你只需要關注兩個引數:latex-workshop.latex.recipeslatex-workshop.latex.tools

latex-workshop.latex.tools

這個引數定義了latex需要的各個工具的名稱、命令列指令和設定.對於一個工具,其基本格式為:

{
      "name": "your_tool_name",
      "command": "cmd_name",
      "args": [
        "%DOCFILE%"
      ]
    }

他的含義是your_tool_name在實際呼叫時會在命令列中傳入cmd_name {你的文件名字},當然,通常latex需要多個工具的協作,如下是我的鍵值對

latex-workshop.latex.tools
"latex-workshop.latex.tools": [
    {
      "name": "biber",
      "command": "biber",
      "args": [
        "%DOCFILE%"
      ]
    },
    {
      "name": "latexmk",
      "command": "latexmk",
      "args": [
        "-synctex=1",
        "-interaction=nonstopmode",
        "-file-line-error",
        "-pdf",
        "%DOC%"
      ]
    },
    {
      "name": "xelatex",
      "command": "xelatex",
      "args": [
        "-synctex=1",
        "-interaction=nonstopmode",
        "-file-line-error",
        "%DOC%"
      ]
    },
    {
      "name": "bibtex",
      "command": "bibtex",
      "args": [
        "%DOCFILE%"
      ]
    },
    {
      "name": "pdflatex",
      "command": "pdflatex",
      "args": [
        "-synctex=1",
        "-interaction=nonstopmode",
        "-file-line-error",
        "%DOCFILE%"
      ]
    },
  ]

latex-workshop.latex.recipes

recipes則指定了一個工具鏈。當編譯時,vscode會按tools中的工具順序執行指令。而tools的工具名稱就對應了上一章的name

    {
      "name": "pdflatex -> biber -> pdflatex*2",
      "tools": [
        "pdflatex",
        "biber",
        "pdflatex",
        "pdflatex"
      ]
    }

常見問題

錯誤

Latex: Cannot find ‘xxx.bcf‘!

加入如下包\usepackage{biblatex}

警告

headheight is too small 12pt

原理可見https://tex.stackexchange.com/questions/327285/what-does-this-warning-mean-fancyhdr-and-headheight
有一個簡單的修復方法,就是按提示將長度設定成一個更加大的值\setlength{\headheight}{14pt}

Token not allowed in a PDF string (Unicode): (hyperref) removing `math shift'.

錯誤的原因是在section這類表示文章結構的文字中,由於要相容bookmark的格式,所以儘量不能出現數學公式。解決方式為插入\texorpdfstring{你的公式}{},一定要加入後面的{},不然會吞掉之後1個字

\section{總體均值 \texorpdfstring{$\mu$}{} 的區間估計--總體標準差 \texorpdfstring{$\sigma$}{} 已知}

Underfull \hbox (badness 10000)

當嘗試將文字放置在允許區域之外時,會發生這種常見的格式錯誤。 LaTeX無法很好地拉伸文件中的文字。通常,這是因為類檔案具有關於允許多少個框溢位以及允許它們伸展多少的設定值。如果超過這些值,則會出現錯誤。

解決方法
將段落中的\去掉;
若要進行換行,使用\newline;
若要新增空行,可使用\bigskip、\medskip、\smallskip和\vspace{},其中為自行定義的高度。
其中,
\bigskip : 根據其他因素(文件型別,可用空間等),新增12pt的±4pt的空間。
\medskip : 根據其他因素(文件型別,可用空間等),新增6pt的±2pt的空間。
\smallskip : 根據其他因素(文件型別,可用空間等),新增3pt的±1pt的空間。
\vspace{2mm} : 在垂直方向新增2mm的空間。