summaryrefslogtreecommitdiff
path: root/info/install-latex-guide-zh-cn/chapter/wsl.tex
blob: da388c34ab01ef6933c7eae3e67969659dfd2132 (plain)
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
% !TeX root = ../install-latex-guide-zh-cn.tex

\chapter{Windows Subsystem for Linux}

目前微软推出了 Windows Subsystem for Linux (WSL) 供开发人员使用.
在这里简要介绍如何在 WSL 中安装 \TeX{} Live.
我选择的是微软商店中的 Ubuntu.
此篇安装教程仅供参考.
这里称 WSL 中使用的命令行为 \textsf{bash}.
\textbf{不建议}对操作系统了解不多的用户阅读本章内容.

\section{安装 \TeX{} Live}

在主系统%
\footnote{在 Windows 10 中直接进行的操作即为在主系统中的操作}%
中下载
\href{https://mirrors.ctan.org/systems/texlive/Images/texlive2021.iso}{iso 镜像文件},
可选择国内源以加快下载速度.
下载完毕后, 在 \textsf{cmd} 中验证文件是否正常.
下载及文件验证方法同 \ref{sec:windows:install}~节.

在正式安装前,
用户需要在 \textsf{bash} 中执行
\begin{lstlisting}[language=bash]
  sudo apt install fontconfig
  sudo apt install ttf-mscorefonts-installer
\end{lstlisting}
这些命令是为了处理日后使用中可能出现的字体问题.
为避免安装速度过慢,
可仿照 \ref{subsec:fc-cache}~节更改文件 \texttt{sources.list}.
注意 WSL 中无法启动 \textsf{gedit},
因此需要将其替换为 \textsf{vim},
即
\begin{lstlisting}[language=bash]
  sudo vim /etc/apt/sources.list
\end{lstlisting}
不熟悉 \textsf{vim} 的用户,
可参考 \ref{sec:no-vim}~节.

接下来, 在主系统中将镜像挂载,
例如挂载到 \texttt{X:\textbackslash},
而后进入 \textsf{bash} 并执行如下命令
\begin{lstlisting}[language = bash]
  sudo mkdir /mnt/x
  sudo mount -t drvfs X: /mnt/x
\end{lstlisting}
如此便可在 \textsf{bash} 中找到挂载的光盘镜像.
之后执行
\begin{lstlisting}[language = bash]
  sudo /mnt/x/install-tl
\end{lstlisting}
进行安装.
在屏幕上应该能见到以下内容
\begin{lstlisting}
  ======================> TeX Live installation procedure <=====================
  
  ======>   Letters/digits in <angle brackets> indicate   <=======
  ======>   menu items for actions or customizations      <=======
  = help>   https://tug.org/texlive/doc/install-tl.html   <=======
  
   Detected platform: GNU/Linux on x86_64
   
   <B> set binary platforms: 1 out of 6
  
   <S> set installation scheme: scheme-full
  
   <C> set installation collections:
       40 collections out of 41, disk space required: 6963 MB
  
   <D> set directories:
     TEXDIR (the main TeX directory):
       /usr/local/texlive/2021
     TEXMFLOCAL (directory for site-wide local files):
       /usr/local/texlive/texmf-local
     TEXMFSYSVAR (directory for variable and automatically generated data):
       /usr/local/texlive/2021/texmf-var
     TEXMFSYSCONFIG (directory for local config):
       /usr/local/texlive/2021/texmf-config
     TEXMFVAR (personal directory for variable and automatically generated data):
       ~/.texlive2021/texmf-var
     TEXMFCONFIG (personal directory for local config):
       ~/.texlive2021/texmf-config
     TEXMFHOME (directory for user-specific files):
       ~/texmf
  
   <O> options:
     [ ] use letter size instead of A4 by default
     [X] allow execution of restricted list of programs via \write18
     [X] create all format files
     [X] install macro/font doc tree
     [X] install macro/font source tree
     [ ] create symlinks to standard directories
     [X] after install, set CTAN as source for package updates
  
   <V> set up for portable installation
  
  Actions:
   <I> start installation to hard disk
   <P> save installation profile to 'texlive.profile' and exit
   <Q> quit
  
  Enter command: 
\end{lstlisting}
用户直接点击 \keys{I} 使用默认配置安装.
如果用户对于 WSL 比较了解, 可以根据提示, 更改安装设置.
为简化起见,
以下内容均认为用户进行默认安装.
安装完毕后, 需继续在 \textsf{bash} 中执行
\begin{lstlisting}[language = bash]
  sudo umount /mnt/x
  sudo rmdir /mnt/x
\end{lstlisting}
弹出已加载的光盘镜像.

默认安装完成后, 用户需要设置环境变量.
继续在 \textsf{bash} 中执行
\begin{lstlisting}[language = bash]
  vim ~/.bashrc
\end{lstlisting}
在打开的文件末尾添加
\begin{lstlisting}
  # Add TeX Live to the PATH, MANPATH, INFOPATH
  export PATH=/usr/local/texlive/2021/bin/x86_64-linux:$PATH
  export MANPATH=/usr/local/texlive/2021/texmf-dist/doc/man:$MANPATH
  export INFOPATH=/usr/local/texlive/2021/texmf-dist/doc/info:$INFOPATH
\end{lstlisting}
并保存退出.
同样, 不熟悉 \textsf{vim} 的用户可参考 \ref{sec:no-vim}~节.
退出 WSL 再进入, 执行
\begin{lstlisting}[language=bash]
  tex -v
\end{lstlisting}
将显示
\begin{lstlisting}
  TeX 3.141592653 (TeX Live 2021)
  kpathsea version 6.3.3
  Copyright 2021 D.E. Knuth.
  There is NO warranty.  Redistribution of this software is
  covered by the terms of both the TeX copyright and
  the Lesser GNU General Public License.
  For more information about these matters, see the file
  named COPYING and the TeX source.
  Primary author of TeX: D.E. Knuth.
\end{lstlisting}
即为安装成功.

接下来仿照 Ubuntu 20.04 处理字体.
首先将配置文件复制到系统,
在 \textsf{Terminal} 执行
\begin{lstlisting}[language=bash]
  sudo cp /usr/local/texlive/2021/texmf-var/fonts/conf/texlive-fontconfig.conf /etc/fonts/conf.d/09-texlive.conf
\end{lstlisting}
然后在 \textsf{Terminal} 执行
\begin{lstlisting}[language=bash]
  sudo fc-cache -fsv
\end{lstlisting}
刷新字体缓存.

\subsection{在用户文件夹安装}

WSL 同样也可以在用户文件夹中安装,
它的用户文件夹往往在主系统的系统盘中.
具体过程也可以参考第~\ref{subsec:ubuntu-user-folder}~节.

\section{卸载 \TeX{} Live}

WSL 中的方法同 \ref{sec:ubuntu:uninstall}~节.

\section{跨版本升级 \TeX{} Live}

WSL 中的方法同 \ref{sec:ubuntu:version}~节.

\section{升级宏包}

WSL 中的方法同 \ref{sec:ubuntu:update}~节.

\section{安装宏包}

WSL 中的方法同 \ref{sec:ubuntu:installpackage}~节.

\section{调出宏包手册}

由于 WSL 的 \textsf{bash} 默认是无窗口化的纯命令行,
因此将 \TeX{} Live 安装至 WSL 后,
用户无法直接通过命令 \texttt{texdoc} 打开相应的手册.
下面几种方法均为收集而来的方案,
其中比较推荐更改 \texttt{texdoc.cnf}.

\subsubsection{更改 \texttt{texdoc.cnf}}

此方案源自本手册的 \href{https://github.com/OsbertWang/install-latex-guide-zh-cn/issues/13}{issue}.
首先在 \textsf{bash} 中执行
\begin{lstlisting}[language=bash]
  texdoc --files
\end{lstlisting}
系统返回类似如下内容
\begin{lstlisting}[language=bash]
  /mnt/d/texlive-wsl/2021/texmf-dist/scripts/texdoc/texdoclib.tlu 3.2.2
  Configuration file(s):
      active      /mnt/d/texlive-wsl/2021/texmf-dist/texdoc/texdoc.cnf
  Recommended file(s) for personal settings:
      /home/<username>/texmf/texdoc/texdoc.cnf
\end{lstlisting}
这里给出了 \texttt{texdoc} 的配置文件所在位置.
打开用户文件夹下的配置文件
\begin{lstlisting}[language=bash]
  vim ~/texmf/texdoc/texdoc.cnf
\end{lstlisting}
在文件内写入
\begin{lstlisting}
  viewer_pdf = (powershell.exe -Command Start-Process $(wslpath -w %s)) &
\end{lstlisting}
并且保存退出.
然后再执行 \texttt{texdoc} 即可打开相应文档.

\subsubsection{explorer 浏览}

先找到手册所在路径,
例如寻找 \texttt{lshort-zh-cn}
\begin{lstlisting}[language=bash]
  texdoc -l lshort-zh-cn
\end{lstlisting}
系统会显示
\begin{lstlisting}
   1 /usr/local/texlive/2021/texmf-dist/doc/latex/lshort-chinese/lshort-zh-cn.pdf
     = [zh] The document itself
  Enter number of file to view, RET to view 1, anything else to skip:
\end{lstlisting}
这时按其他键 (如 \keys{x}) 退出.
然后分别执行
\begin{lstlisting}[language=bash]
  cd /usr/local/texlive/2021/texmf-dist/doc/latex/lshort-chinese/
  explorer.exe .
\end{lstlisting}
主系统的资源管理器便会打开.
这时用户便可以在主系统中使用 pdf 阅读器打开手册进行阅读.

\subsubsection{everything 搜索}

使用 \href{https://www.voidtools.com/zh-cn/}{everything}.
通过 \texttt{texdoc -l} 找到相应文件名,
而后直接搜索.
具体方法这里不再赘述.

\subsubsection{借助其他工具}

目前有很多开源的 WSL 辅助程序,
相关讨论见本手册的 \href{https://github.com/OsbertWang/install-latex-guide-zh-cn/issues/13}{issue}.

\section{编译文件}

编译一个最小示例 \texttt{main.tex}.

在主系统的 \textsf{cmd} 中执行
\begin{lstlisting}[language=bash]
  mkdir D:\work-latex
  cd /d D:\work-latex
  notepad main.tex
\end{lstlisting}
建立文档,
内容为
\begin{lstlisting}[language = {[LaTeX]TeX}]
  \documentclass{article}
  \begin{document}
    Hello \LaTeX{} World!
  \end{document}
\end{lstlisting}
接下来在 \textsf{cmd} 中执行
\begin{lstlisting}[language=bash]
  bash -i -c "pdflatex main"
\end{lstlisting}
实现编译.

也可以直接在 WSL 中建立文档并编译,
在 \texttt{bash} 中依次执行
\begin{lstlisting}[language=bash]
  mkdir /mnt/d/work-latex
  cd /mnt/d/work-latex
  vim main.tex
\end{lstlisting}
建立文档,
内容同上,
然后在 \textsf{bash} 中执行
\begin{lstlisting}[language=bash]
  pdflatex main
\end{lstlisting}
实现编译.

待编译完成后, 可看到在工作路径中生成了 \texttt{main.pdf}
文件和其他同名的辅助文件 \texttt{main.aux} 与 \texttt{main.log}.
在主系统中可以打开 \texttt{main.pdf} 查看内容.

对于中文文档,
可以编写以下最小示例%
\footnote{注意使用 UTF-8 编码}%
\begin{lstlisting}[language={[LaTeX]TeX}]
  \documentclass[UTF8,fontset=fandol]{ctexart}
  \begin{document}
    你好 \LaTeX{} 世界!
  \end{document}
\end{lstlisting}
在 \textsf{cmd} 中执行
\begin{lstlisting}[language=bash]
  bash -i -c "xelatex main"
\end{lstlisting}
或在 \textsf{bash} 中执行
\begin{lstlisting}[language=bash]
  xelatex main
\end{lstlisting}
等待系统完成编译过程. 
\texttt{xelatex} 同样可以使用 WSL 内安装的字体%
\footnote{注意主系统中的字体默认不能直接被调用,
如果打算使用主系统中的字体,
见
\href{https://github.com/OsbertWang/install-latex-guide-zh-cn/issues/14}{github 上的讨论}},
在 WSL 中安装字体的方法见附录~\ref{chp:appendix:wsl}.

编译命令可添加参数, 这里与 \ref{sec:windows:compile}~节中的情形一致, 不再赘述.

\section{不懂 \textsf{vim} 的处理方法}\label{sec:no-vim}

\textsf{vim} 的使用方法与目前很多流行的编辑器不同,
因此这里给出不用 \textsf{vim} 的处理方法.
该方法借助 \href{https://code.visualstudio.com/}{VS Code} 的 \href{https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-wsl}{Remote-WSL} 插件实现.

先来更改 \texttt{.bashrc}.
将 \texttt{.bashrc} 备份为 \texttt{.bashrc.bak}
\begin{lstlisting}[language=bash]
  cp ~/.bashrc ~/.bashrc.bak
\end{lstlisting}
用 VS Code 打开文档
\begin{lstlisting}[language=bash]
  code ~/.bashrc
\end{lstlisting}
并对其中内容进行更改并保存.
若更改错误,
可以执行
\begin{lstlisting}[language=bash]
  cp ~/.bashrc.bak ~/.bashrc
\end{lstlisting}
恢复文档.

接下来更改 \texttt{sources.list}.
将 \texttt{sources.list} 备份为 \texttt{sources.list.bak}
\begin{lstlisting}[language=bash]
  sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
\end{lstlisting}
用 VS Code 打开文档
\begin{lstlisting}[language=bash]
  code /etc/apt/sources.list
\end{lstlisting}
并对其中内容进行更改,
并且另存为
\begin{lstlisting}
  ~/sources.list.sav
\end{lstlisting}
在 \textsf{bash} 中执行
\begin{lstlisting}[language=bash]
  sudo cp ~/sources.list.sav /etc/apt/sources.list
\end{lstlisting}
替换文档.
若发现文档有错误,
执行
\begin{lstlisting}[language=bash]
  sudo cp /etc/apt/sources.list.bak /etc/apt/sources.list
\end{lstlisting}
恢复文档.

使用 VS Code 同样可以编写 \texttt{tex} 文件,
这涉及到如何使用 \LaTeX{} 的编辑器,
这里不做展开.