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