Skip to content

Commit d5ba3ba

Browse files
committed
LaTeX: use unicode-math with xelatex and lualatex
Fixes #12714. Related: #2290, #2348.
1 parent 3d49941 commit d5ba3ba

File tree

7 files changed

+115
-19
lines changed

7 files changed

+115
-19
lines changed

CHANGES.rst

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,19 @@ Dependencies
1010
Incompatible changes
1111
--------------------
1212

13+
* LaTeX: The inclusion of ``amssymb`` LaTeX package is done after the contents
14+
of :confval:`latex_elements`\ ``['fontpkg']``, and originates in the new
15+
``'fontpkgmath'`` key of :confval:`latex_elements`.
16+
17+
- Thus, move custom math font set-up from ``'fontpkg'`` to ``'fontpkgmath'``
18+
if needed.
19+
- Regarding Unicode latex engines, they do not use ``amssymb`` anymore but
20+
XITS Math font via ``unicode-math``, as default of ``'fontpkgmath'``.
21+
This fixes some issues and provides math glyphs much better matching GNU
22+
FreeSerif which is the default text font.
23+
- For ``'pdflatex'`` as :confval:`latex_engine`, it is planned to replace at
24+
Sphinx 9 usage of ``amssymb`` by ``stix2`` in order for the math glyphs to
25+
much better match the default Times-like text font.
1326
* #12763: Remove unused internal class ``sphinx.util.Tee``.
1427
Patch by Adam Turner.
1528

@@ -69,6 +82,9 @@ Bugs fixed
6982
Patch by Hugo van Kemenade.
7083
* #12645: Correctly support custom gettext output templates.
7184
Patch by Jeremy Bowman.
85+
* #12714: LaTeX: Let ``\mathbf{\Lambda}`` work as expected if
86+
:confval:`latex_engine` is ``'xelatex'`` or ``'lualatex'``, via usage
87+
of ``unicode-math`` with XITS Math font.
7288
* #12717: LaTeX: let :option:`-q <sphinx-build -q>` (quiet) option for
7389
:program:`sphinx-build -M latexpdf` or :program:`make latexpdf` (``O=-q``)
7490
get passed to :program:`latexmk`. Let :option:`-Q <sphinx-build -Q>`

bindep.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ texlive-tex-gyre [platform:rpm]
1616
texlive-pict2e [platform:rpm]
1717
texlive-fontawesome5 [platform:rpm]
1818
texlive-xcolor [platform:rpm]
19+
texlive-unicode-math [platform:rpm]
20+
texlive-xits [platform:rpm]
1921
latexmk [platform:rpm]
2022

2123
texlive-latex-recommended [platform:dpkg]

doc/latex.rst

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,43 @@ Keys that you may want to override include:
202202
more complete TeX fonts and associated LaTeX packages. The monospace
203203
font has been changed to better match the Times clone.
204204

205+
.. versionchanged:: 8.1.0 Custom configuration of math fonts, if any,
206+
should be done via the ``'fontpkgmath'`` key, which is new at 8.1.0.
207+
208+
.. _fontpkgmath:
209+
210+
``'fontpkgmath'``
211+
LaTeX configuration for math fonts, which is executed after contents of
212+
``'fontpkg'``. It defaults to:
213+
214+
- :code-tex:`r'\\usepackage{amssymb}'` if :confval:`latex_engine` is set to
215+
``'pdflatex'`` (or ``'uplatex'``).
216+
217+
- :code-tex:`r'\\usepackage{unicode-math}'` for ``'xelatex'`` and
218+
``'lualatex'`` with extras to set the math font to XITS Math. It also
219+
allows mark-up such as :code-tex:`\\mathbf{\\Gamma}` (which is already
220+
legit both for HTML and for PDF via ``'pdflatex'``).
221+
222+
.. hint::
223+
224+
Use :code-tex:`\\boldsymbol` for boldening *lowercase* Greek letter,
225+
:code-tex:`\\boldsymbol{\\alpha\\beta\\gamma}`.
226+
227+
.. versionadded:: 8.1.0
228+
The default TeX math fonts, and their AMS extensions loaded via
229+
:code-tex:`\\usepackage{amssymb}` (a package whose usage could formerly
230+
not be canceled, only overriden via additional math packages in
231+
``'fontpkg'``), do not fit well with most text fonts (they are much
232+
thinner), the quasi sole exceptions being the original Computer Modern
233+
text fonts and their close derivatives the Latin Modern text fonts.
234+
235+
Sphinx sets up per default a Times-alike font for text, which does not
236+
fit well with LaTeX default math fonts (in particuler for Greek
237+
letters). This is solved for ``'xelatex'`` and ``'lualatex'`` via usage
238+
of XITS Math font. A possibility in future for ``'pdflatex'`` could be
239+
for Sphinx to use the ``'fontpkgmath'`` key to inject usage of the
240+
``stix2`` package in replacement of ``amssymb``.
241+
205242
``'fncychap'``
206243
Inclusion of the "fncychap" package (which makes fancy chapter titles),
207244
default :code-tex:`r'\\usepackage[Bjarne]{fncychap}'` for English documentation

doc/usage/builders/index.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,7 @@ The most common builders are:
237237

238238
* ``texlive-latex-recommended``
239239
* ``texlive-fonts-recommended``
240-
* ``texlive-fonts-extra`` (needed for ``fontawesome5``, see the 7.4.0
241-
change notice below)
240+
* ``texlive-fonts-extra``
242241
* ``tex-gyre`` (if :confval:`latex_engine` left to default)
243242
* ``texlive-latex-extra``
244243
* ``latexmk``
@@ -249,8 +248,9 @@ The most common builders are:
249248
.. versionchanged:: 7.4.0
250249
LaTeX package ``xcolor`` is now required (it is part of Ubuntu
251250
``texlive-latex-recommended`` anyhow). The LaTeX package
252-
``fontawesome5`` is recommended. See the :ref:`'sphinxsetup'
253-
<latexsphinxsetup>` ``iconpackage`` key for more.
251+
``fontawesome5`` is recommended (part of Ubuntu ``texlive-fonts-extra``).
252+
See the :ref:`'sphinxsetup' <latexsphinxsetup>` ``iconpackage`` key for
253+
more.
254254

255255
Additional packages are needed in some circumstances:
256256

doc/usage/configuration.rst

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2806,20 +2806,6 @@ These options influence LaTeX output.
28062806
this, the :ref:`fontenc` key of :ref:`latex_elements
28072807
<latex_elements_confval>` must be used appropriately.
28082808

2809-
.. note::
2810-
2811-
Contrarily to :ref:`MathJaX math rendering in HTML output <math-support>`,
2812-
LaTeX requires some extra configuration to support Unicode literals in
2813-
:rst:dir:`math`:
2814-
the only comprehensive solution (as far as we know) is to
2815-
use ``'xelatex'`` or ``'lualatex'`` *and* to add
2816-
``r'\usepackage{unicode-math}'``
2817-
(e.g. via the :ref:`preamble` key of :ref:`latex_elements
2818-
<latex_elements_confval>`).
2819-
You may prefer ``r'\usepackage[math-style=literal]{unicode-math}'``
2820-
to keep a Unicode literal such as ``α`` (U+03B1) as-is in output,
2821-
rather than being rendered as :math:`\alpha`.
2822-
28232809
.. versionchanged:: 2.1.0
28242810
Use ``'xelatex'`` (and LaTeX package ``xeCJK``)
28252811
by default for Chinese documents.
@@ -2833,6 +2819,13 @@ These options influence LaTeX output.
28332819
.. versionchanged:: 4.0
28342820
Use ``'uplatex'`` by default for Japanese documents.
28352821

2822+
.. versionchanged:: 8.1.0
2823+
``'xelatex'`` and ``'lualatex'`` trigger inclusion of the LaTeX
2824+
``unicode-math`` package and as a result, Unicode literals are
2825+
allowed in math mode (but their shapes may be normalized by
2826+
``unicode-math``). You can customize this via the
2827+
:ref:`fontpkgmath` key of :confval:`latex_elements`.
2828+
28362829
.. confval:: latex_documents
28372830
:type: :code-py:`Sequence[tuple[str, str, str, str, str, bool]]`
28382831
:default: The default LaTeX documents

sphinx/builders/latex/constants.py

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
\renewcommand{\ttdefault}{txtt}
1111
'''
1212

13+
PDFLATEX_DEFAULT_FONTPKGMATH = r'''
14+
\usepackage{amssymb}% will become stix2 with suitable options at Sphinx 9
15+
'''
16+
1317
PDFLATEX_DEFAULT_FONTSUBSTITUTION = r'''
1418
\expandafter\ifx\csname T@LGR\endcsname\relax
1519
\else
@@ -58,12 +62,50 @@
5862
]
5963
'''
6064

65+
XELATEX_DEFAULT_FONTPKGMATH = r'''
66+
\usepackage{unicode-math}
67+
\setmathfont{XITSMath-Regular.otf}[
68+
StylisticSet=1,% choice of shape for "\mathcal"
69+
BoldFont=XITSMath-Bold.otf,
70+
NFSSFamily=XITS,
71+
]
72+
\makeatletter
73+
\AtBeginDocument{%
74+
% work around unicode-math problems with \mathbf{\Gamma} et al.
75+
\SetMathAlphabet{\mathrm}{normal}{TU}{XITS}{m}{n}
76+
\SetMathAlphabet{\mathbf}{normal}{TU}{XITS}{b}{n}
77+
\SetMathAlphabet{\mathrm}{bold}{TU}{XITS}{b}{n}
78+
\SetMathAlphabet{\mathbf}{bold}{TU}{XITS}{b}{n}
79+
\def\Gamma{Γ}
80+
\def\Delta{Δ}
81+
\def\Theta{Θ}
82+
\def\Lambda{Λ}
83+
\def\Xi{Ξ}
84+
\def\Pi{Π}
85+
\def\Sigma{Σ}
86+
\def\Upsilon{Υ}
87+
\def\Phi{Φ}
88+
\def\Psi{Ψ}
89+
\def\Omega{Ω}
90+
% Make subscripts a bit larger for legibility
91+
\def\defaultscriptratio{.8}
92+
\def\defaultscriptscriptratio{.6}
93+
\DeclareMathSizes{9}{9}{7}{5}
94+
\DeclareMathSizes{\@xpt}{\@xpt}{8}{6}
95+
\DeclareMathSizes{\@xipt}{\@xipt}{8.76}{6.57}
96+
\DeclareMathSizes{\@xiipt}{\@xiipt}{9.6}{7.2}
97+
\DeclareMathSizes{\@xivpt}{\@xivpt}{11.52}{8.64}
98+
}
99+
\makeatother
100+
'''
101+
61102
XELATEX_GREEK_DEFAULT_FONTPKG = (XELATEX_DEFAULT_FONTPKG +
62103
'\n\\newfontfamily\\greekfont{FreeSerif}' +
63104
'\n\\newfontfamily\\greekfontsf{FreeSans}' +
64105
'\n\\newfontfamily\\greekfonttt{FreeMono}')
65106

66107
LUALATEX_DEFAULT_FONTPKG = XELATEX_DEFAULT_FONTPKG
108+
LUALATEX_DEFAULT_FONTPKGMATH = XELATEX_DEFAULT_FONTPKGMATH
67109

68110
DEFAULT_SETTINGS: dict[str, Any] = {
69111
'latex_engine': 'pdflatex',
@@ -82,11 +124,12 @@
82124
'utf8extra': '',
83125
'cmappkg': '\\usepackage{cmap}',
84126
'fontenc': '\\usepackage[T1]{fontenc}',
85-
'amsmath': '\\usepackage{amsmath,amssymb,amstext}',
127+
'amsmath': '\\usepackage{amsmath,amstext}',
86128
'multilingual': '',
87129
'babel': '\\usepackage{babel}',
88130
'polyglossia': '',
89131
'fontpkg': PDFLATEX_DEFAULT_FONTPKG,
132+
'fontpkgmath': PDFLATEX_DEFAULT_FONTPKGMATH,
90133
'fontsubstitution': PDFLATEX_DEFAULT_FONTSUBSTITUTION,
91134
'substitutefont': '',
92135
'textcyrillic': '',
@@ -142,6 +185,7 @@
142185
'fontenc': ('\\usepackage{fontspec}\n'
143186
'\\defaultfontfeatures[\\rmfamily,\\sffamily,\\ttfamily]{}'),
144187
'fontpkg': XELATEX_DEFAULT_FONTPKG,
188+
'fontpkgmath': XELATEX_DEFAULT_FONTPKGMATH,
145189
'fvset': '\\fvset{fontsize=\\small}',
146190
'fontsubstitution': '',
147191
'textgreek': '',
@@ -155,6 +199,7 @@
155199
'fontenc': ('\\usepackage{fontspec}\n'
156200
'\\defaultfontfeatures[\\rmfamily,\\sffamily,\\ttfamily]{}'),
157201
'fontpkg': LUALATEX_DEFAULT_FONTPKG,
202+
'fontpkgmath': LUALATEX_DEFAULT_FONTPKGMATH,
158203
'fvset': '\\fvset{fontsize=\\small}',
159204
'fontsubstitution': '',
160205
'textgreek': '',
@@ -166,6 +211,7 @@
166211
'babel': '',
167212
'classoptions': ',dvipdfmx',
168213
'fontpkg': PDFLATEX_DEFAULT_FONTPKG,
214+
'fontpkgmath': PDFLATEX_DEFAULT_FONTPKGMATH,
169215
'fontsubstitution': '',
170216
'textgreek': '',
171217
'fncychap': '',
@@ -176,6 +222,7 @@
176222
'babel': '',
177223
'classoptions': ',dvipdfmx',
178224
'fontpkg': PDFLATEX_DEFAULT_FONTPKG,
225+
'fontpkgmath': PDFLATEX_DEFAULT_FONTPKGMATH,
179226
'fontsubstitution': '',
180227
'textgreek': '',
181228
'fncychap': '',

sphinx/templates/latex/latex.tex.jinja

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
<%= substitutefont %>
4747
<%= textcyrillic %>
4848
<%= fontpkg %>
49+
<%= fontpkgmath %>
4950
<%= fontsubstitution %>
5051
<%= textgreek %>
5152
<%= fncychap %>

0 commit comments

Comments
 (0)