% !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}

在 Windows 11 中,
WSL 已经能够自动完成可视化工作,
这为用户提供了一些便利.
但更多的安装操作涉及 "apt" 操作.
为避免安装速度过慢,
可仿照第~\ref{sec:addition:source}~节更改文件 \texttt{sources.list}.
不习惯在 WSL 中做文本操作的 Windows 用户,
可参考第~\ref{sec:addition:wsl-editor}~节.
其余安装前的准备工作见第~\ref{sec:ubuntu:prepare}~节.

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

接下来挂载镜像.
这里需要先通过一个例子解释 WSL 的路径和主系统路径的对应关系.
例如, \texttt{iso} 镜像文件在主系统中的路径是 \path{C:\Users\username\Downloads\texlive2026.iso},
那么在 WSL 中其路径是 \path{/mnt/c/Users/username/Downloads/texlive2026.iso}.
理解上述路径对应关系后,
进入 \textsf{bash} 并执行如下命令
\begin{lstlisting}
  sudo mkdir -p /mnt/iso
  sudo mount -o loop /mnt/c/Users/username/Downloads/texlive2026.iso /mnt/iso
\end{lstlisting}
如此便可在 \textsf{bash} 中找到装载的光盘镜像.

后续安装过程与第~\ref{chap:ubuntu:sec:install}~节一样,
安装完毕后, 需继续在 \textsf{bash} 中执行
\begin{lstlisting}
  sudo umount /mnt/iso
  sudo rmdir /mnt/iso
\end{lstlisting}
弹出已装载的光盘镜像.

安装完成后,
用户需要设置环境变量,
方法同第~\ref{sec:ubuntu:env}~节.
退出 WSL 再进入,
执行
\begin{lstlisting}
  tex -v
\end{lstlisting}
将显示
\begin{lstlisting}[language = {}]
  TeX 3.141592653 (TeX Live 2026)
  kpathsea version 6.4.2
  Copyright 2026 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}
即为安装成功.

后续配置字体缓存的方法同第~\ref{sec:ubuntu:font}~节.

\section{升级宏包}

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

\section{安装宏包}

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

\section{调出宏包手册}

由于 WSL 的 \textsf{bash} 默认是无窗口化的纯命令行,
因此将 \TeX~Live 安装至 WSL 后,
用户无法直接通过命令 "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}
  texdoc --files
\end{lstlisting}
系统返回类似如下内容
\begin{lstlisting}[language = {}]
  /usr/local/texlive/2026/texmf-dist/scripts/texdoc/texdoclib.tlu 4.1.1
  Configuration file(s):
      active      /usr/local/texlive/2026/texmf-dist/texdoc/texdoc.cnf
  Recommended file(s) for personal settings:
      /home/osbertwang/texmf/texdoc/texdoc.cnf
\end{lstlisting}
这里给出了 "texdoc" 的配置文件所在位置.
打开用户文件夹下的配置文件
\begin{lstlisting}[deletekeywords = texdoc]
  mkdir -p ~/texmf/texdoc && touch ~/texmf/texdoc/texdoc.cnf
  gnome-text-editor ~/texmf/texdoc/texdoc.cnf
\end{lstlisting}
在文件内写入
\begin{lstlisting}
  viewer_pdf = (powershell.exe -Command Start-Process $(wslpath -w %s)) &
\end{lstlisting}
并且保存退出.
然后再执行 "texdoc" 即可打开相应文档.

\subsubsection{explorer 浏览}

先找到手册所在路径,
例如寻找 \textsf{lshort-zh-cn}
\begin{lstlisting}
  texdoc -l lshort-zh-cn
\end{lstlisting}
系统会显示
\begin{lstlisting}[language = {}]
   1 /usr/local/texlive/2026/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 = {}]
  cd /usr/local/texlive/2026/texmf-dist/doc/latex/lshort-chinese/
  explorer.exe .
\end{lstlisting}
主系统的资源管理器便会打开.
这时用户便可以在主系统中使用 PDF 阅读器打开手册进行阅读.

\subsubsection{everything 搜索}

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

\subsubsection{借助其他工具}

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

\section{编译文档}

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

在主系统的 \textsf{cmd} 中执行
\begin{lstlisting}[deletekeywords = tex]
  mkdir D:\work-latex
  cd /d D:\work-latex
  notepad main.tex
\end{lstlisting}
%\begin{lstlisting}
%  Push-Location (New-Item -ItemType Directory -Path 'D:\work-latex' -Force)
%  notepad.exe main.tex
%\end{lstlisting}
建立文档,
内容为
\begin{lstlisting}[language = mwe]
  \documentclass{article}
  \begin{document}
    Hello \LaTeX\ World!
  \end{document}
\end{lstlisting}
接下来在 \textsf{cmd} 中执行
\begin{lstlisting}
  bash -l -c "pdflatex main"
\end{lstlisting}
实现编译.

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

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

对于中文文档,
可以编写以下最小示例\footnote{注意使用 UTF-8 编码}%
\begin{lstlisting}[language = mwe]
  \documentclass{ctexart}
  \begin{document}
    你好 \LaTeX\ 世界！
  \end{document}
\end{lstlisting}
在 \textsf{cmd} 中执行
\begin{lstlisting}
  bash -l -c "xelatex main"
\end{lstlisting}
或在 \textsf{bash} 中执行
\begin{lstlisting}
  xelatex main
\end{lstlisting}
等待系统完成编译过程.
"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}~节中的情形一致, 不再赘述.

\section{卸载 \TeX~Live}

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

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

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