当前位置: 首页 > 工具软件 > texlive.js > 使用案例 >

VScode+Texlive+SumatraPDF

宗冠宇
2023-12-01

安装文件

安装 SumatraPDF

https://www.sumatrapdfreader.org/free-pdf-reader

安装 texlive

texlive 的镜像文件(ISO):

https://mirrors.tuna.tsinghua.edu.cn/CTAN/systems/texlive/Images/
  • 加载 texlive 的 iso 文件

  • 右键 install-tl-windows,单击以管理员身份运行,进入安装界面,点击左下角的“Advanced”进入高级安装来取消你不需要安装的宏包。

确认安装版本,在cmd输入:

tex -v
latex -v
xelatex -v
pdflatex -v

安装 VSCode LATEX 插件

在扩展商店安装 LaTeX Workshop 插件。

安装完成后,随便打开一个 tex 源文件

后缀.tex

% This is a small sample LaTeX input file (Version of 10 April 1994)
%
% Use this file as a model for making your own LaTeX input file.
% Everything to the right of a  %  is a remark to you and is ignored by LaTeX.
 
% The Local Guide tells how to run LaTeX.
 
% WARNING!  Do not type any of the following 10 characters except as directed:
%                &   $   #   %   _   {   }   ^   ~   \   
 
\documentclass{article}        % Your input file must contain these two lines 
\begin{document}               % plus the \end{document} command at the end.
 
 
\section{Simple Text}          % This command makes a section title.
 
Words are separated by one or more spaces.  Paragraphs are separated by
one or more blank lines.  The output is not affected by adding extra
spaces or extra blank lines to the input file.
 
Double quotes are typed like this: ``quoted text''.
Single quotes are typed like this: `single-quoted text'.
Long dashes are typed as three dash characters---like this.
Emphasized text is typed like this: \emph{this is emphasized}.
Bold       text is typed like this: \textbf{this is bold}.
\subsection{A Warning or Two}  % This command makes a subsection title.
If you get too much space after a mid-sentence period---abbreviations
like etc.\ are the common culprits)---then type a backslash followed by
a space after the period, as in this sentence.
Remember, don't type the 10 special characters (such as dollar sign and
backslash) except as directed!  The following seven are printed by
typing a backslash in front of them:  \$  \&  \#  \%  \_  \{  and  \}.  
The manual tells how to make other symbols.
 
\end{document}                 % The input file ends with this command.

配置 VSCode LATEX 插件

在 VSCode 里面按住快捷键,CTRL+SHIFT+P,,然后键入“setjson”,点击“首选项: 打开设置(JSON)”
将下面的代码复制粘贴到你的 setting.json 文件最外面一对花括号里。

json配置文件

**注意:**只需把以下代码放入设置区的方括号里,不要删去方括号,不要忘记替换软件的路径。

// Latex配置
"latex-workshop.showContextMenu":true,                // 右键菜单
"latex-workshop.intellisense.package.enabled": true,  // 根据加载的包,自动完成命令或包  
"latex-workshop.latex.autoBuild.run": "never",        // 禁止保存文件时自动build
"latex-workshop.message.error.show": false,           // 不显示编译出错的弹窗
"latex-workshop.message.warning.show": false,
"latex-workshop.latex.tools": [
    {
        // 编译工具和命令
        "name": "xelatex", // 默认的编译工具是 latexmk,修改为中文环境常用的 xelatex
        "command": "xelatex",
        "args": [
            "-synctex=1",
            "-interaction=nonstopmode",
            "-file-line-error",
            "-pdf",
            "%DOCFILE%" // %DOC%替换成%DOCFILE%就可以支持编译中文路径下的文件
        ]
    },
    {
        "name": "pdflatex",
        "command": "pdflatex",
        "args": [
            "-synctex=1",
            "-interaction=nonstopmode",
            "-file-line-error",
            "%DOCFILE%"
        ]
    },
    {
        "name": "bibtex",
        "command": "bibtex",
        "args": [
            "%DOCFILE%"
        ]
    }
],

"latex-workshop.latex.recipes": [
    {
        "name": "xelatex",
        "tools": [
            "xelatex"
        ],
    },
    {
        "name": "pdflatex",
        "tools": [
            "pdflatex"
        ]
    },
    {
        "name": "xe->bib->xe->xe",
        "tools": [
            "xelatex",
            "bibtex",
            "xelatex",
            "xelatex"
        ]
    },
    {
        "name": "pdf->bib->pdf->pdf",
        "tools": [
            "pdflatex",
            "bibtex",
            "pdflatex",
            "pdflatex"
        ]
    }
],

// 设置Latex预览方式
"latex-workshop.view.pdf.viewer": "external",
"latex-workshop.view.pdf.ref.viewer":"external",
"latex-workshop.view.pdf.external.viewer.command":"C:/Program Files/SumatraPDF/SumatraPDF.exe",
"latex-workshop.view.pdf.external.viewer.args": [
    "%PDF%"
],

// 配置Syntex的搜索
// 将下一条注释代码复制到smatrapdf设置中
// "C:/Program Files/Microsoft VS Code/Code.exe" "C:/Program Files/Microsoft VS Code/resources/app/out/cli.js"  --ms-enable-electron-run-as-node -r -g "%f:%l"
"latex-workshop.view.pdf.external.synctex.command": "C:/Program Files/SumatraPDF/SumatraPDF.exe",
"latex-workshop.view.pdf.external.synctex.args": [
    "-forward-search",
    "%TEX%",
    "%LINE%",
    "-reuse-instance",
    "-inverse-search",
    "\"C:/Program Files/Microsoft VS Code/Code.exe\" \"C:/Program Files/Microsoft VS Code/resources/app/out/cli.js\"  --ms-enable-electron-run-as-node -r -g \"%f:%l\"",
    "%PDF%",
],
// 单击“SyncTeX from cursor”即可正向搜索/先固定源码中的光标,然后Ctrl + Alt + J
// 在PDF中双击即可反向搜索
// 不要清理生成的名字中带 synctex 的文件,否则就不能进行正向和反向搜索

// 编译后清除不必要文件
"latex-workshop.latex.autoClean.run": "onBuilt",
"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",
    // "*.gz"
],

bibtex

如需要使用 bibtex 可使用如下方法:

  • 在编译时单击 VSCode 界面左下角的小勾,单击“Build LaTeX project”,选择带 bib 的 Recipe,也可使用快捷键快速选择;
  • 将带 bib 的 Recipe 放到第一位,就可以作为默认 Recipe 编译了,也可以但因为编译次数比较多,速度会比较慢;
  • 在文档的开头添加 %!BIB program = bibtex

pdflatex

要使用 pdflatex,只需在 tex 文档首加入以下代码:

%!TEX program = pdflatex

快捷键设置

在 VSCode 界面下按下 F1,键入“keyjson”,选择“打开键盘快捷方式(JSON)”,然后把以下代码放入:

{
    "key": "alt+s",
    "command": "latex-workshop.synctex",
    "when": "editorTextFocus && !isMac"
},
{
    "key": "alt+b",
    "command": "latex-workshop.build",
    "when": "editorTextFocus && !isMac"
},
{
    "key": "alt+t",
    "command": "latex-workshop.kill",
    "when": "editorTextFocus && !isMac"
},
{
    "key": "alt+e",
    "command": "latex-workshop.recipes"
},

这段代码的意义是将 Alt+s 绑定到正向搜索,将 Alt+b 绑定到使用默认 recipe 编译,将 Alt+t 绑定到终止编译,将 Alt+e 绑定到选择其他 recipe 编译,可以自行更换为适合自己的快捷键,只需修改“key”那一项即可。

 类似资料: