Latex在VScode中的安装

发布时间 2023-08-03 18:26:14作者: ydky

Latex在VScode中的安装和使用

VScode+texlive+SumatraPDF

1.texlive下载安装

华为镜像

https://mirrors.huaweicloud.com/CTAN/systems/texlive/Images/

阿里镜像

https://mirrors.aliyun.com/CTAN/systems/texlive/Images/

下载后对其进行解压得到以下文件

右键install-tl-windows.bat,单击以管理员身份运行,进入安装界面。

点击Advanced可以进行安装路径等功能的修改

进行相关设置

设置完成后选择安装,等待安装结束即可。

2.VScode中安装插件

在扩展搜索栏中搜索Latex,选择Latex Workshop进行安装

3.下载安装SumatraPDF

(可选安装,如果不安装SumatraPDF可以选择在VScode内部的PDFviewer,外部查看器能够看到 pdf 文件在查看器中的目录,可以实时进行跳转)

SumatraPDF下载

https://www.sumatrapdfreader.org/download-free-pdf-viewer

4.设置文件配置

按F1输入setting.json打开用户设置

4.1不选择使用SumatraPDF

将下面代码放到{}中即可

"latex-workshop.latex.autoBuild.run": "never",
    "latex-workshop.showContextMenu": true,
    "latex-workshop.intellisense.package.enabled": true,
    "latex-workshop.message.error.show": false,
    "latex-workshop.message.warning.show": false,
    "latex-workshop.latex.tools": [
        {
            "name": "xelatex",
            "command": "xelatex",
            "args": [
                "-synctex=1",
                "-interaction=nonstopmode",
                "-file-line-error",
                "%DOCFILE%"
            ]
        },
        {
            "name": "pdflatex",
            "command": "pdflatex",
            "args": [
                "-synctex=1",
                "-interaction=nonstopmode",
                "-file-line-error",
                "%DOCFILE%"
            ]
        },
        {
            "name": "latexmk",
            "command": "latexmk",
            "args": [
                "-synctex=1",
                "-interaction=nonstopmode",
                "-file-line-error",
                "-pdf",
                "-outdir=%OUTDIR%",
                "%DOCFILE%"
            ]
        },
        {
            "name": "bibtex",
            "command": "bibtex",
            "args": [
                "%DOCFILE%"
            ]
        }
    ],
    "latex-workshop.latex.recipes": [
        {
            "name": "XeLaTeX",
            "tools": [
                "xelatex"
            ]
        },
        {
            "name": "PDFLaTeX",
            "tools": [
                "pdflatex"
            ]
        },
        {
            "name": "BibTeX",
            "tools": [
                "bibtex"
            ]
        },
        {
            "name": "LaTeXmk",
            "tools": [
                "latexmk"
            ]
        },
        {
            "name": "xelatex -> bibtex -> xelatex*2",
            "tools": [
                "xelatex",
                "bibtex",
                "xelatex",
                "xelatex"
            ]
        },
        {
            "name": "pdflatex -> bibtex -> pdflatex*2",
            "tools": [
                "pdflatex",
                "bibtex",
                "pdflatex",
                "pdflatex"
            ]
        },
    ],
    "latex-workshop.latex.clean.fileTypes": [
        "*.aux",
        "*.bbl",
        "*.blg",
        "*.idx",
        "*.ind",
        "*.lof",
        "*.lot",
        "*.out",
        "*.toc",
        "*.acn",
        "*.acr",
        "*.alg",
        "*.glg",
        "*.glo",
        "*.gls",
        "*.ist",
        "*.fls",
        "*.log",
        "*.fdb_latexmk"
    ],
    "latex-workshop.latex.autoClean.run": "onFailed",
    "latex-workshop.latex.recipe.default": "lastUsed",
    "latex-workshop.view.pdf.internal.synctex.keybinding": "double-click"

4.2选择使用SumatraPDF

将下面代码放到{}中(注意修改路径为自己的路径,两处)

"latex-workshop.latex.autoBuild.run": "never",
    "latex-workshop.showContextMenu": true,
    "latex-workshop.intellisense.package.enabled": true,
    "latex-workshop.message.error.show": false,
    "latex-workshop.message.warning.show": false,
    "latex-workshop.latex.tools": [
        {
            "name": "xelatex",
            "command": "xelatex",
            "args": [
                "-synctex=1",
                "-interaction=nonstopmode",
                "-file-line-error",
                "%DOCFILE%"
            ]
        },
        {
            "name": "pdflatex",
            "command": "pdflatex",
            "args": [
                "-synctex=1",
                "-interaction=nonstopmode",
                "-file-line-error",
                "%DOCFILE%"
            ]
        },
        {
            "name": "latexmk",
            "command": "latexmk",
            "args": [
                "-synctex=1",
                "-interaction=nonstopmode",
                "-file-line-error",
                "-pdf",
                "-outdir=%OUTDIR%",
                "%DOCFILE%"
            ]
        },
        {
            "name": "bibtex",
            "command": "bibtex",
            "args": [
                "%DOCFILE%"
            ]
        }
    ],
    "latex-workshop.latex.recipes": [
        {
            "name": "XeLaTeX",
            "tools": [
                "xelatex"
            ]
        },
        {
            "name": "PDFLaTeX",
            "tools": [
                "pdflatex"
            ]
        },
        {
            "name": "BibTeX",
            "tools": [
                "bibtex"
            ]
        },
        {
            "name": "LaTeXmk",
            "tools": [
                "latexmk"
            ]
        },
        {
            "name": "xelatex -> bibtex -> xelatex*2",
            "tools": [
                "xelatex",
                "bibtex",
                "xelatex",
                "xelatex"
            ]
        },
        {
            "name": "pdflatex -> bibtex -> pdflatex*2",
            "tools": [
                "pdflatex",
                "bibtex",
                "pdflatex",
                "pdflatex"
            ]
        },
    ],
    "latex-workshop.latex.clean.fileTypes": [
        "*.aux",
        "*.bbl",
        "*.blg",
        "*.idx",
        "*.ind",
        "*.lof",
        "*.lot",
        "*.out",
        "*.toc",
        "*.acn",
        "*.acr",
        "*.alg",
        "*.glg",
        "*.glo",
        "*.gls",
        "*.ist",
        "*.fls",
        "*.log",
        "*.fdb_latexmk"
    ],
    "latex-workshop.latex.autoClean.run": "onFailed",
    "latex-workshop.latex.recipe.default": "lastUsed",
    "latex-workshop.view.pdf.internal.synctex.keybinding": "double-click",

    "latex-workshop.view.pdf.viewer": "external",
    "latex-workshop.view.pdf.ref.viewer":"auto",
    "latex-workshop.view.pdf.external.viewer.command": ".../SumatraPDF.exe", // 注意修改路径
    "latex-workshop.view.pdf.external.viewer.args": [
        "%PDF%"
    ],
    "latex-workshop.view.pdf.external.synctex.command": ".../SumatraPDF.exe", // 注意修改路径
    "latex-workshop.view.pdf.external.synctex.args": [
        "-forward-search",
        "%TEX%",
        "%LINE%",
        "%PDF%"
    ],
    "editor.wordWrap": "on"

打开SumatraPDF,点击左上角三横-设置-高级选项,以txt格式打开,将下列添加到文件中(两个路径改为自己的路径):

InverseSearchCmdLine = "...\Code.exe" "...\resources\app\out\cli.js" --ms-enable-electron-run-as-node -r -g "%f:%l"
EnableTeXEnhancements = true

4.3内部PDF和外部PDF的转换

内部->外部

直接将文件配置时的内容进行替换,注意修改路径!!!

外部->内部

将代码中的external改为tab即可

 "latex-workshop.view.pdf.viewer": "external"
 改为
 "latex-workshop.view.pdf.viewer": "tab"

5.简单使用

一个完整的Latex工程通常包括以下文件:

.tex:Latex的主文件
.cls:用于控制最终版面样式的文件(一般由模块提供)
.bib:用于汇总参考文献信息的文件
.bst:用于控制最终参考文献格式的文件(一般由模板提供)
.aux,.bbl,.log等临时文件:不要管
.pdf:编译之后生成的与.tex文件同名的对应文件
(.tex文件所在的目录为工程文件夹)

\指令
%注释
$$行内公式的环境
\begin{XXX} \end{XXX} 进入一个特定的环境
\usepackage{XXX} 引用宏包
\section{}\subsection{}\subsubsection{}一级、二级、三级标题
\chapter{}章节

打开文件夹后选择.tex文件然后进行如图操作即可得到对应的.pdf文件:

代码->pdf

选择代码右键点击,选择SyncTeX from cursor即可跳转到pdf对应的点

pdf->代码

内部pdf:在pdf文件中Ctrl+鼠标左键点击即可跳转到对应的代码处或双击

SumatraPDF:双击pdf文件即可跳转