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


基礎知識

vscode是如何編譯Latex的?

利用CTRL+,打開vscode的設置界面,搜索latex,可以進入settings.json
image
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的空間。

Underfull \hbox
通常是因為行中出現了無法斷行的元素,導致某行的末尾超出了限制。首先在PDF中定位到錯誤的地方,看看是否有公式超出了。之后可以嘗試把公式截斷成幾份,或修改語序以調整公式位置。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM