Skip to content

Latest commit

 

History

History
378 lines (299 loc) · 13.3 KB

README.md

File metadata and controls

378 lines (299 loc) · 13.3 KB
title
LaTeX

LaTeX

参考资料

链接

书籍

  • 《The TeXbook》Knuth 1984
  • 《LaTeX: a Document Preparation System》Lamport 1994
  • 《The LaTeX Companion》Mittelbach 2023
  • Text and Math Into LaTeX》Grätzer 2024
  • TeX》and 《LaTeX》on Wikibooks

⚠️ 信息污染源

  • 百度搜索
  • 中文博客
  • 祖传文档

发行版与前端

TeX Live

TeX Live 是一款开源、跨平台的 TeX 发行版 (distribution)

  1. 下载并完整安装 TeX Live,大约需要 5 GB 硬盘空间。
  2. 安装完成后,应当可以用 texdoc ctex 命令打开《CTeX 宏集手册》。

TeXstudio

TeXstudio 是一款开源、跨平台的 TeX 集成开发环境 (IDE)

LaTeX Workshop

LaTeX Workshop 是一款基于 VS Code 的轻量级 TeX 前端

首次安装后,在 ~/Library/Application Support/Code/User/settings.json 中加入以下内容,即可按绿色三角按钮进行编译

{
  "latex-workshop.latex.autoBuild.run": "never",
  "latex-workshop.latex.recipe.default": "lastUsed",
  "latex-workshop.latex.tools": [
    {
      "name": "xelatex",
      "command": "xelatex",
      "args": [
        "-synctex=1",
        "-shell-escape",
        "-interaction=nonstopmode",
        "%DOC%"
      ],
      "env": {}
    },
    {
      "name": "bibtex",
      "command": "bibtex",
      "args": [
        "%DOCFILE%"
      ],
      "env": {}
    },
    {
      "name": "nomencl",
      "command": "makeindex",
      "args": [
        "%DOCFILE%.nlo",
        "-s",
        "nomencl.ist",
        "-o",
        "%DOCFILE%.nls"
      ],
      "env": {}
    },
  ],
  "latex-workshop.latex.recipes": [
    {
      "name": "pvcthesis",
      "tools": [
        "xelatex",
        "bibtex",
        "nomencl",
        "xelatex",
        "xelatex"
      ]
    },
  ],
}

中文支持

  1. 卸载 CTeX 套装,原因参见《2018年,为什么不推荐使用 CTeX 套装了》。
  2. 完整安装 TeX Live
  3. 从以下两种方式中任选一种,创建中文文档 hello.tex
  • CTeX 文档类,例如 ctexartctexbook 等。
  • 其他文档类 + CTeX 宏包
  1. xelatex 命令编译:
    mkdir build
    cd build
    xelatex ../hello.tex

字体设置

此宏包用于设置文档正文字体,默认情况下会影响到 \mathrm数学字体(宏包选项 no-math 可以消除对数学字体的影响):

\usepackage[no-math]{fontspec}
\setmainfont{Courier}

此宏包用于设置(数学环境中)阿拉伯数字拉丁字母希腊字母的字体,默认情况下以 no-math 选项加载 fontspec 宏包

\usepackage{mathspec}
\setmainfont{Palatino}
\setmathsfont{Courier}
\setmathrm{Optima}

CTeX 文档类会自动加载 fontspec 宏包。 如果要使用 mathspec 宏包。 如果要使用 mathspec 宏包,则应

  • \documentclass 之前将 no-math 选项传入 fontspec 宏包
    \PassOptionsToPackage{no-math}{fontspec}
    \documentclass{ctexart}
    \usepackage{mathspec}
  • 或者选择其他(不自动加载 fontspec 宏包的)文档类,而在 mathspec 宏包的)文档类,而在 mathspec 宏包之后加载 ctex:
    \documentclass{article}
    \usepackage{mathspec}
    \usepackagep[heading]{ctex}

此宏包用于设置数学符号字体:

\usepackage{unicode-math}
\unimathsetup{math-style=TeX}
\setmathfont{texgyrepagella-math.otf}
\setmathfont{Neo-Euler}[range=\mathalpha]

完整安装 TeX Live 后,应当可以用 texdoc unimath 命令打开《Every symbol (most symbols) defined by unicode-math》。

数学物理符号

符号类型与间距

TeX 将数学符号分为以下几类:

类型 命令 示例 代码
Ordinary \mathord $a$ a
Operator \mathop $\sum$ \sum
Binary \mathbin $\times$ \times
Relation \mathrel $\le$ \le
Opening \mathopen $\biggl($ \biggl(
Closing \mathclose $\biggr)$ \biggr)
Punctuation \mathpunct $,$ ,
Inner \mathinner $\left(\dfrac12\right)$ \left(\dfrac12\right)

并将它们之间的距离定义为(其中 * = 不可能,0 = 无间距,1 = \thinmuskip,2 = \mediumskip,3 = \thickmuskip,( ) = 在上下标模式中忽略)

Ord Op Bin Rel Open Close Punct Inner
Ord 0 1 (2) (3) 0 0 0 (1)
Op 1 1 * (3) 0 0 0 (1)
Bin (2) (2) * * (2) * * (2)
Rel (3) (3) * 0 (3) 0 0 (3)
Open 0 0 * 0 0 0 0 0
Close 0 1 (2) (3) 0 0 0 (1)
Punct (1) (1) * (1) (1) (1) (1) (1)
Inner (1) 1 (2) (3) (1) 0 (1) (1)

比较以下写法(注意括号前后的间距):

示例 代码 缺点
$f ( \dfrac{x^2}{2} ) dx$ f ( \dfrac{x^2}{2} ) dx 括号尺寸偏小,微分号前缺间距
$f \left( \dfrac{x^2}{2} \right) dx$ f \left( \dfrac{x^2}{2} \right) dx 函数名与开括号之间有多余间距
$f \biggl( \dfrac{x^2}{2} \biggr) , dx$ f \biggl( \dfrac{x^2}{2} \biggr) \, dx 括号尺寸需手动调整
$f \mathopen{} \left( \dfrac{x^2}{2} \right) dx$ f \mathopen{} \left( \dfrac{x^2}{2} \right) dx 需手动插入 \mathopen{}
$ \SI[<options>]{<number>}[<preunit>]{<unit>} $
$ R = \SI{8.3144598(48)}{J.mol^{-1}.K^{-1}} $
$ R = \SI{8.3144598(48)}{\joule\per\kelvin\per\mole} $

代码高亮

使用此宏包需安装 pygments

pip  install pygments  # for python2
pip3 install pygments  # for python3

绘图

LyX

LyX 是一款开源、支持所见即所思的 LaTeX 前端,兼具 LaTeX 排版效果优美和 Word 所及即所得的优势。

先完整安装 TeX Live,再安装 LyX。二者都安装好后,在 LyX【首选项】中设置环境变量 PATH 以确保 xelatex 等命令能够被 LyX 搜索到。

中文支持

新建一个 LyX 文档,其【文档类】可任选,然后进入【文档】→【首选项】进行如下设置:

  1. 【LaTeX 导言区】中增加一行 \usepackage{ctex}
  2. 【语言】→【语言】→【汉语(简体中文)】
  3. 【语言】→【文件编码】→【其他】→【Unicode (XeTeX) (utf8)】
  4. 【字体】→【使用非 TeX 字体(通过 XeTeX/LuaTeX)】

字体设置

CTeX 文档类及《LyX 中文支持》一节第 4 步的【使用非 TeX 字体】都会自动加载 fontspec 宏包。 如果进一步勾选【数学:非 TeX 字体默认值】,则还会自动加载 unicode-math 宏包

如果要进行更精细的字体设置,则不应勾选【使用非 TeX 字体】,而是在【LaTeX 导言区】中手动加载字体设置宏包。

代码高亮

进入【文档】→【首选项】进行如下设置:

  1. 【程序列表】→【语法高亮支持包】→【Minted】。

  2. 【程序列表】→【空白处】可以设置 minted 宏包参数,例如:

    style=xcode
    frame=leftline
    baselinestretch={1.0}
    breaklines=true
    fontsize={\footnotesize}
  3. 【输出格式】→【允许运行外部程序】。

  4. 首次编译该文档时,会弹出警告对话框,选择【对该文档总是允许】。

在 LyX 文件中插入外部代码文件的步骤如下:

  1. 【插入】→【文件】→【子文档】,弹出对话框。
  2. 【文件】后的空白处输入源文件地址,或点击【浏览】选择源文件。
  3. 【包含类别】选择【程序列表】。
  4. 【更多参数】右侧的空白处添加语言,例如 language=python

MathJax

常用链接

【方法一】直接在网页(HTML 文件)内配置、加载:

<script>
MathJax = {
  tex: {
    inlineMath: [['$', '$'], ['\\(', '\\)']]
  },
};
</script>
<script type="text/javascript" id="MathJax-script" async
  src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-svg.js">
</script>

【方法二】将配置写入 mathjax-config.js 文件,再由网页依次加载此文件及 MathJax 组件:

window.MathJax = {
  tex: {
    inlineMath: [['$', '$'], ['\\(', '\\)']]
  },
  svg: {
    fontCache: 'global'
  }
};
<script src="mathjax-config.js" defer></script>
<script type="text/javascript" id="MathJax-script" defer
  src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml.js">
</script>

【方法三】将配置、加载均写入 mathjax-load.js 文件,再由网页加载此文件:

window.MathJax = {
  tex: {
    inlineMath: [['$', '$'], ['\\(', '\\)']]
  },
  svg: {
    fontCache: 'global'
  }
};

(function () {
  var script = document.createElement('script');
  script.src = 'https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml.js';
  script.async = true;
  document.head.appendChild(script);
})();
<script src="mathjax-load.js" async></script>

【方法一】直接在数学环境中定义,作用域为当前网页内的所有数学环境:

$$
   \def\RR{\boldsymbol{R}}
   \def\bold#1{\boldsymbol{#1}}
$$

【方法二】在配置 MathJax 时定义,作用域为加载该配置的所有数学环境:

window.MathJax = {
  tex: {
    macros: {
      RR: "{\boldsymbol{R}}",
      bold: ["{\boldsymbol{#1}}", 1]
    }
  }
};

特殊字符转义

  • Markdown 中,同一行里成对的 _* 均表示强调 (emphasize)
  • 在 TeX 中,单个字符 _ 的语义为下标 (subscript)

某些 Markdown 解析器不能正确地将行内公式中的上述字符按 TeX 语义解析,此时应以转义字符 \_\* 代替之。