Skip to content

Commit f9f1a74

Browse files
committed
LaTeX: new 'fontpkgmath' key of latex_elements, use unicode-math
Fixes sphinx-doc#12714. Related: sphinx-doc#2290, sphinx-doc#2348.
1 parent 4f270a5 commit f9f1a74

File tree

6 files changed

+118
-15
lines changed

6 files changed

+118
-15
lines changed

CHANGES.rst

+18
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,20 @@ Dependencies
77
Incompatible changes
88
--------------------
99

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

@@ -26,5 +40,9 @@ Bugs fixed
2640
so that it now runs after the docutils ``Footnotes`` resolution transform.
2741
Patch by Chris Sewell.
2842

43+
* #12714: LaTeX: Let ``\mathbf{\Lambda}`` work as expected if
44+
:confval:`latex_engine` is ``'xelatex'`` or ``'lualatex'``, via usage
45+
of ``unicode-math`` with XITS Math font.
46+
2947
Testing
3048
-------

bindep.txt

+2
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

+45-10
Original file line numberDiff line numberDiff line change
@@ -169,15 +169,16 @@ Keys that you may want to override include:
169169
For French with ``'lualatex'`` the default is to use ``babel``.
170170

171171
``'fontpkg'``
172-
Font package inclusion. The default is::
172+
Font package inclusion. The default with ``'pdflatex'`` is::
173173

174174
r"""\usepackage{tgtermes}
175175
\usepackage{tgheros}
176176
\renewcommand\ttdefault{txtt}
177177
"""
178178

179-
For ``'xelatex'`` and ``'lualatex'`` however the default is to use
180-
the GNU FreeFont.
179+
For ``'xelatex'`` and ``'lualatex'`` the default is to use the GNU
180+
FreeSerif, FreeSans, and FreeMono fonts
181+
(via ``\setmainfont`` etc... syntax from LaTeX package ``fontspec``).
181182

182183
.. versionchanged:: 1.2
183184
Defaults to ``''`` when the :confval:`language` uses the Cyrillic
@@ -186,16 +187,50 @@ Keys that you may want to override include:
186187
.. versionchanged:: 2.0
187188
Incorporates some font substitution commands to help support occasional
188189
Greek or Cyrillic in a document using ``'pdflatex'`` engine.
190+
At 4.0.0 these commands were moved to the ``'fontsubstitution'`` key.
189191

190192
.. versionchanged:: 4.0.0
191193

192-
- The font substitution commands added at ``2.0`` have been moved
193-
to the ``'fontsubstitution'`` key, as their presence here made
194-
it complicated for user to customize the value of ``'fontpkg'``.
195-
- The default font setting has changed: it still uses Times and
196-
Helvetica clones for serif and sans serif, but via better, more
197-
complete TeX fonts and associated LaTeX packages. The
198-
monospace font has been changed to better match the Times clone.
194+
The default font setting was changed. As shown above it still uses
195+
Times and Helvetica clones for serif and sans serif, but via better,
196+
more complete TeX fonts and associated LaTeX packages. The monospace
197+
font has been changed to better match the Times clone.
198+
199+
.. versionchanged:: 8.1.0 Configuration of math fonts should be done via
200+
the ``'fontpkgmath'`` key, which is new at 8.1.0. For ``'pdflatex'``
201+
this key injects ``\usepackage{amssymb}`` which is thus now executed
202+
after the contents of ``'fontpkg'``.
203+
204+
``'fontpkgmath'``
205+
LaTeX package for math fonts. It defaults to:
206+
207+
- ``r'\usepackage{amssymb}'`` if :confval:`latex_engine` is set to
208+
``'pdflatex'`` (or ``'uplatex'``).
209+
210+
- ``r'\usepackage{unicode-math}'`` for ``'xelatex'`` and ``'lualatex'``
211+
with extras to set the math font to XITS Math (which is an excellent
212+
match to FreeSerif text font). It also makes mark-up such as
213+
``\mathbf{\Gamma}`` work as it does for HTML and ``'pdflatex'``.
214+
215+
.. hint:: Use ``\boldsymbol`` for boldening *lowercase* Greek letter, for
216+
example
217+
``\mathbf{\Gamma\Delta\Lambda}\boldsymbol{\alpha\beta\gamma}``.
218+
219+
.. versionadded:: 8.1.0 The default TeX math fonts, and their AMS extensions
220+
loaded from ``\usepackage{amssymb}``, do not fit well with most text
221+
fonts, the quasi sole exceptions being the original Computer Modern fonts
222+
and their close derivatives the Latin Modern fonts.
223+
224+
Sphinx sets up per default a Times-alike font for text, which thus does
225+
not fit well with default math. It is planned at Sphinx 9, for
226+
``'pdflatex'`` :confval:`latex_engine`, to use the ``'fontpkgmath'`` key
227+
to inject usage of the ``stix2`` package (with suitable options) and not
228+
as currently ``amssymb``. For ``'xelatex'`` and ``'lualatex'`` the
229+
switch to XITS Math was done at 8.1.0 to fix immediately some issues
230+
with ``\mathbf`` and take this opportunity for math mode to better match
231+
GNU FreeSerif, which is the default text font with these engines. It
232+
also means that math glyphs can be copied from PDF output as Unicode
233+
characters.
199234

200235
``'fncychap'``
201236
Inclusion of the "fncychap" package (which makes fancy chapter titles),

doc/usage/builders/index.rst

+4-4
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

sphinx/builders/latex/constants.py

+48-1
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

+1
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)