Sharing some programming knowledge.

0%

Ubuntu下安装LaTeX中文环境配置指南

Step 0: 安装合适的LaTex

  1. 安装LaTeX的发行版,最好选择texlive-full: sudo apt install texlive-full
  2. 安装xelatex编译引擎,即一个把Tex语言转换为排版的一个程序,支持unicode编码: sudo apt install texlive-xetex
  3. 安装中文包,xeCJK: sudo apt install texlive-lang-chinese
  4. 额外的字体支持: sudo apt install texlive-fonts-extra

Step 1: 配置Vscode使其支持LaTeX

  1. 安装 LaTeX Workshop 插件
  2. 打开命令面板:ctrl+shift+P, 写入 settings.json 来打开设置
  3. 将以下代码加入 settings.json:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
"latex-workshop.latex.recipes": [    // the first recipe is the default compile tool
{
"name": "xelatex", // you can change this to your compile tool if different
"tools": ["xelatex"]
},
{ // the seconde recipe is the recipe chain compile
"name": "xelatex->bibtex->xelatex*2",
"tools": [
"xelatex",
"bibtex",
"xelatex",
"xelatex"
]
}
],
"latex-workshop.latex.tools":[
{ // compile tools and commands
"name":"xelatex",
"command": "xelatex",
"args": [
"-synctex=1",
"-interaction=nonstopmode",
"-file-line-error",
"%DOCFILE%"
]
}, {
"name":"bibtex",
"command": "bibtex",
"args": [
"%DOCFILE%"
]
}
],
"latex-workshop.latex.clean.fileTypes": [ // (Optional)choose which kind of files to clean
"*.aux",
"*.log",
"*.synctex.gz"
],
"latex-workshop.latex.autoClean.run": "onBuilt", // (Optional)clean auxiliary files if you want a clean working directory
"latex-workshop.latex.autoBuild.run": "never", // (Optional)never auto build .tex file when the file is auto-saved
"latex-workshop.view.pdf.hand": true, // (Optional)set default to hand mode when open pdf viewer in vscode

Step 2: 创建tex文件测试

1
2
3
4
5
\documentclass{article}
\usepackage{xeCJK}
\begin{document}
中文测试
\end{document}

Note:需导入xeCJK包

这时候应该就可以build并view了

Step 3: (可选)导入windows中的字体

ubuntu自带的中文字体较少,可以选择导入Windows里的字体。

在windows系统中找到字体文件,一般位于C:\windows\fonts, 再搬到ubuntu下的/usr/share/fonts/(当然,可以不必全部搬过来),可以重命名其为winfonts,再打开终端执行以下命令

1
2
3
4
cd /usr/share/fonts/winfonts
sudo mkfontscale
sudo mkfontdir
sudo fc-cache -fv

查看字体是否存在了: fc-list :lang=zh | grep '宋体'

Step 4: 排除问题

这样操作下来后,build的时候我遇到了如下Warnings:

1
2
3
4
Fandol is being set as the default font for CJK text.
(xeCJK) Please make sure it has been properly installed.
Font "FandolSong-Regular" does not contain requested
(fontspec) Script "CJK".

虽然有warnings, 但中文还是可以正常现实的,如果要解决它们,可以换字体, 只需加入,比如: \setCJKmainfont{Microsoft YaHei}, \setCJKmainfont{SimSun}

(事实上如果用v3.2.10 or later version的xeCJK, 使用中文字体Fandol得到时候总是会出现烦人的warning”,所以还是换字体吧…)

如果还是遇到 Font "font-name" does not contain requested (fontspec) Script "CJK". 的waring, 但中文又是可以正常现实的,那么只要加入 \PassOptionsToPackage{quiet}{fontspec} 即可, (就是简单地disable警告)

总之,我测试的时候,这样就可以没有警告地编译并预览了:

1
2
3
4
5
6
7
\documentclass{article}
% \PassOptionsToPackage{quiet}{fontspec}
\usepackage{xeCJK}
\setCJKmainfont{Microsoft YaHei}
\begin{document}
中文测试
\end{document}