% !TeX root = ../install-latex-guide-zh-cn.tex \chapter{Windows Subsystem for Linux} 目前微软推出了 Windows Subsystem for Linux (WSL) 供开发人员使用. 在这里简要介绍如何在 WSL 中安装 \TeX{} Live. 我选择的是 Windows 11 微软商店中的 Ubuntu. 此篇安装教程仅供参考. 这里称 WSL 中使用的命令行为 \textsf{bash}. \textbf{不建议}对操作系统了解不多的用户阅读本章内容. \section{安装 \TeX{} Live} 在主系统% \footnote{在 Windows 11 中直接进行的操作即为在主系统中的操作}% 中下载 \href{https://mirrors.ctan.org/systems/texlive/Images/texlive2024.iso}{iso 镜像文件}, 可选择国内源以加快下载速度. 下载完毕后, 在 \textsf{cmd} 中验证文件是否正常. 下载及文件验证方法同 \ref{sec:windows:install}~节. 在正式安装前, 用户需要在 \textsf{bash} 中执行 \begin{lstlisting}[language=bash] sudo apt install fontconfig gedit \end{lstlisting} 以减少后续字体问题和文本编辑问题. 在 Windows 11 中, WSL 已经能够自动完成可视化工作, 这为一些用户提供了便利. 为避免安装速度过慢, 可仿照 \ref{sec:addition:source}~节更改文件 \texttt{sources.list}. 不习惯在 \textsf{gedit} 中做文本操作的 Windows 用户, 可参考 \ref{sec:addition:wsl-editor}~节. 接下来, 在主系统中将镜像装载, 例如挂载到 \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 indicate <======= ======> menu items for actions or customizations <======= = help> https://tug.org/texlive/doc/install-tl.html <======= Detected platform: GNU/Linux on x86_64 set binary platforms: 1 out of 15 set installation scheme: scheme-full set installation collections: 40 collections out of 41, disk space required: 8315 MB (free: 974100 MB) set directories: TEXDIR (the main TeX directory): /usr/local/texlive/2024 TEXMFLOCAL (directory for site-wide local files): /usr/local/texlive/texmf-local TEXMFSYSVAR (directory for variable and automatically generated data): /usr/local/texlive/2024/texmf-var TEXMFSYSCONFIG (directory for local config): /usr/local/texlive/2024/texmf-config TEXMFVAR (personal directory for variable and automatically generated data): ~/.texlive2024/texmf-var TEXMFCONFIG (personal directory for local config): ~/.texlive2024/texmf-config TEXMFHOME (directory for user-specific files): ~/texmf 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 set up for portable installation Actions: start installation to hard disk

save installation profile to 'texlive.profile' and exit 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] gedit ~/.profile \end{lstlisting} 在打开的文件末尾添加 \begin{lstlisting} # Add TeX Live to the PATH, MANPATH, INFOPATH export PATH=/usr/local/texlive/2024/bin/x86_64-linux:$PATH export MANPATH=/usr/local/texlive/2024/texmf-dist/doc/man:$MANPATH export INFOPATH=/usr/local/texlive/2024/texmf-dist/doc/info:$INFOPATH \end{lstlisting} 并保存退出. 同样, 不习惯于 \textsf{gedit} 处理文本的 Windows 用户可参考 \ref{sec:addition:wsl-editor}~节. 退出 WSL 再进入, 执行 \begin{lstlisting}[language=bash] tex -v \end{lstlisting} 将显示 \begin{lstlisting} TeX 3.141592653 (TeX Live 2024) kpathsea version 6.4.0 Copyright 2024 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 22.04 处理字体. 首先将配置文件复制到系统, 在 \textsf{bash} 执行 \begin{lstlisting}[language=bash] sudo cp /usr/local/texlive/2024/texmf-var/fonts/conf/texlive-fontconfig.conf /etc/fonts/conf.d/09-texlive.conf \end{lstlisting} 然后在 \textsf{bash} 执行 \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] /usr/local/texlive/2024/texmf-dist/scripts/texdoc/texdoclib.tlu 4.0.1 Configuration file(s): active /usr/local/texlive/2024/texmf-dist/texdoc/texdoc.cnf Recommended file(s) for personal settings: /home/osbertwang/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/2024/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/2024/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 gedit 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 上的讨论}, 鉴于 WSL2 和主系统文件系统互访速度慢, 该方法很可能拖慢 xelatex 速度}, 在 WSL 中安装字体的方法见 \ref{sec:addition:font}~节. 编译命令可添加参数, 这里与 \ref{sec:windows:compile}~节中的情形一致, 不再赘述.