环境:使用Windows10 + TeX Live 2023 + VS code
问题:
在运行fontspec
包或xeCJK
包时均出现了下述运行错误:
d:/texlive/2023/texmf-dist/tex/latex/fontspec/fontspec.sty:45: Fatal Package fontspec Error: The fontspec package requires either XeTeX or
(fontspec) LuaTeX.
(fontspec)
(fontspec) You must change your typesetting engine to,
(fontspec) e.g., "xelatex" or "lualatex" instead of
(fontspec) "latex" or "pdflatex".
Type <return> to continue.
...
l.45 \msg_fatal:nn {fontspec} {cannot-use-pdftex}
d:/texlive/2023/texmf-dist/tex/xelatex/xecjk/xeCJK.sty:43: Critical Package xeCJK Error: The xeCJK package requires XeTeX to function.
(xeCJK)
(xeCJK) You must change your typesetting engine to
(xeCJK) "xelatex"
(xeCJK) instead of plain "latex" or "pdflatex" or
(xeCJK) "lualatex".
(xeCJK) Loading xeCJK will abort!
Type <return> to continue.
...
l.43 ..._critical:nn { xeCJK } { Require-XeTeX } }
这是因为我们当前的默认编译器是latex
或者pdflatex
,提示我们需要把编译器设置为xelatex
。
解决办法:
在VS code的设置中直接搜索“json”找到“编辑settings.json”并打开:
选择以下两种方式的一种来更改LaTex-Worshop拓展的编译器设置:
方法一:加入设置默认编译器的命令:
"latex-workshop.latex.recipe.default": "xelatex"
方法二:找到settings.json
中关于"latex-workshop.latex.recipes": [ ]
的部分, 将xelatex
放在第一个recipe的位置即可。
附:settings.json设置:
{
"tabnine.experimentalAutoImports": true,
"json.schemas": [
],
//========= Latex ===========
// Build Recipes
"latex-workshop.latex.recipes": [
{
"name": "xelatex",
"tools": [
"xelatex"
]
},
{
"name": "pdfLaTeX",
"tools": [
"pdflatex"
]
},
{
"name": "latexmk",
"tools": [
"latexmk"
]
},
{
"name": "pdflatex -> bibtex -> pdflatex*2",
"tools": [
"pdflatex",
"bibtex",
"pdflatex",
"pdflatex"
]
},
{
"name": "xelatex -> bibtex -> xelatex*2",
"tools": [
"xelatex",
"bibtex",
"xelatex",
"xelatex"
]
},
{
"name":"pdf -> biber -> pdf*2",
"tools":[
"pdflatex",
"biber",
"pdflatex",
"pdflatex"
]
}
],
// Latex Tools
"latex-workshop.latex.tools": [
{
"name": "latexmk",
"command": "latexmk",
"args": [
"-synctex=1",
"-interaction=nonstopmode",
"-file-line-error",
"-pdf",
"-outdir=%OUTDIR%",
"%DOC%"
],
"env": {}
},
{
"name": "xelatex",
"command": "xelatex",
"args": [
"-synctex=1",
"-interaction=nonstopmode",
"-file-line-error",
"%DOC%"
],
"env": {}
},
{
"name": "pdflatex",
"command": "pdflatex",
"args": [
"-synctex=1",
"-interaction=nonstopmode",
"-file-line-error",
"%DOC%"
],
"env": {}
},
{
"name": "bibtex",
"command": "bibtex",
"args": [
"%DOCFILE%"
],
"env": {}
},
{
"name":"biber",
"command":"biber",
"args":[
"%DOCFILE%"
],
"env": {}
}
]
}
注意:
xeCJK是针对中日韩文字排版的宏包,通常在安装TeX Live时不自带。
如果这是你第一次使用和运行\usepackage{xeCJK}
,则或许需要检查是否安装了xeCJK
这个包,如果没有:
-
点击这里下载xeCJK。
-
解压后放入TeX Live文件夹的以下路径:
D:\texlive\2023\texmf-dist\tex\latex
- 在当前路径调出cmd,运行
texhash
,使TeX程序查找并记住所添加文件的位置。
现在就可以愉快地在.tex中使用中文字体啦~