Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFC] LaTeX: provide suitable default font configuration for XeLaTeX/LuaLaTeX #5251

Closed
jfbu opened this issue Aug 1, 2018 · 5 comments
Closed
Labels
builder:latex help wanted type:enhancement enhance or introduce a new feature
Milestone

Comments

@jfbu
Copy link
Contributor

jfbu commented Aug 1, 2018

Although LaTeX from recent years with xelatex or lualatex uses by default Latin Modern, issue #5247 shows Sphinx should make a better default configuration of font package. Currently it makes no font choice at all.

I don't know what could be good choice. Here are two proposals:

Attention: on many systems XeLaTeX requires \setmainfont with filenames when the font are in the texlive repertories. I used font names below, and it worked at my locale because I have there symlinks to places XeLaTeX look at for fonts.

Computer Modern Unicode

diff --git a/sphinx/writers/latex.py b/sphinx/writers/latex.py
index bf2215a51..52685f2af 100644
--- a/sphinx/writers/latex.py
+++ b/sphinx/writers/latex.py
@@ -159,7 +159,9 @@ ADDITIONAL_SETTINGS = {
         'polyglossia':  '\\usepackage{polyglossia}',
         'babel':        '',
         'fontenc':      '\\usepackage{fontspec}',
-        'fontpkg':      '',
+        'fontpkg':     ('\\setmainfont{CMU Serif}\n'
+                        '\\setsansfont{CMU Sans Serif}\n'
+                        '\\setmonofont{CMU Typewriter Text}'),
         'utf8extra':   ('\\catcode`^^^^00a0\\active\\protected\\def^^^^00a0'
                         '{\\leavevmode\\nobreak\\ }'),
         'fvset':        '\\fvset{fontsize=auto}',
@@ -169,7 +171,9 @@ ADDITIONAL_SETTINGS = {
         'polyglossia':  '\\usepackage{polyglossia}',
         'babel':        '',
         'fontenc':      '\\usepackage{fontspec}',
-        'fontpkg':      '',
+        'fontpkg':     ('\\setmainfont{CMU Serif}\n'
+                        '\\setsansfont{CMU Sans Serif}\n'
+                        '\\setmonofont{CMU Typewriter Text}'),
         'utf8extra':   ('\\catcode`^^^^00a0\\active\\protected\\def^^^^00a0'
                         '{\\leavevmode\\nobreak\\ }'),
         'fvset':        '\\fvset{fontsize=auto}',

Pros: comes with any reasonable TeX installation. Provides coverage of Latin, Greek and Cyrillic scripts. It is mentioned as canonical choice in babel-russian documentation.

In future, the support of Greek could be interesting to stop Sphinx escaping Greek letters to LateX macros, which do not work in PDF bookmarks. (#5248)

Latin Modern

edit: in retrospect I should not have included this (it is already default choice of recent enough)LaTeX with XeLaTeX/LuaLaTeX) as it does not provide Cyrillic support. I was temporarily thinking loading it as below would provide Cyrillic, but this was based on confused state of mind.

diff --git a/sphinx/writers/latex.py b/sphinx/writers/latex.py
index bf2215a51..530e4d90a 100644
--- a/sphinx/writers/latex.py
+++ b/sphinx/writers/latex.py
@@ -159,7 +159,9 @@ ADDITIONAL_SETTINGS = {
         'polyglossia':  '\\usepackage{polyglossia}',
         'babel':        '',
         'fontenc':      '\\usepackage{fontspec}',
-        'fontpkg':      '',
+        'fontpkg':     ('\\setmainfont{Latin Modern Roman}\n'
+                        '\\setsansfont{Latin Modern Sans}\n'
+                        '\\setmonofont{Latin Modern Mono}'),
         'utf8extra':   ('\\catcode`^^^^00a0\\active\\protected\\def^^^^00a0'
                         '{\\leavevmode\\nobreak\\ }'),
         'fvset':        '\\fvset{fontsize=auto}',
@@ -169,7 +171,9 @@ ADDITIONAL_SETTINGS = {
         'polyglossia':  '\\usepackage{polyglossia}',
         'babel':        '',
         'fontenc':      '\\usepackage{fontspec}',
-        'fontpkg':      '',
+        'fontpkg':     ('\\setmainfont{Latin Modern Roman}\n'
+                        '\\setsansfont{Latin Modern Sans}\n'
+                        '\\setmonofont{Latin Modern Mono}'),
         'utf8extra':   ('\\catcode`^^^^00a0\\active\\protected\\def^^^^00a0'
                         '{\\leavevmode\\nobreak\\ }'),
         'fvset':        '\\fvset{fontsize=auto}',

Pros: comes with any reasonable TeX installation. Covers Latin and Cyrillic (no!). Perhaps covers more European languages than Computer Modern Unicode (not sure), but these languages may not be supported by Sphinx anyhow.

Cons: does not support Greek. This is not really a problem currently with Sphinx as Unicode Greek letters (without diacritics) are escaped by Sphinx to LaTeX math macro. But as mentioned above, this causes problem in PDF bookmarks (#5248)

edit

Cons:

  • it does NOT support Cyrillic. (Sorry for initial confusion).

  • also, the exact names for LM fonts have changed at least twice the in the last ten years, I do dearly hope this is stabilized now.

remarks

  • Whether CMU or LM, this will hardly change looks of current PDF built with Sphinx using xelatex/lualatex, the fonts look very much alike.

  • I am asking for people to make suggestions for alternatives or better comparison between the two choices.

  • Mac OS X users with xetex have the problem that XeTeX does not find the OpenType fonts in the TeXLive repertories, but only the systemwide or user fonts. Thus, one either needs to load the fonts by filenames which is cumbersome as it requires also specifying for bold and italic, besides there is the problem of taking into account the pointsize, or one can use the trick to add symlinks, but this needs special documentation: in my ~/Library/Fonts I have symlinks

     TeXOpenType -> /Library/TeX/Root/texmf-dist/fonts/opentype
    TeXTrueType -> /Library/TeX/Root/texmf-dist/fonts/truetype
    

    This relies on /Library/TeX/Root which itself is a symlink to current TeXLive install. Most Mac users use MacTeX installation, and I think it creates this /Library/TeX/Root symlink (I used MacTeX some years ago but now install directly TeXLive; MacTeX is 99,9% same as TeXLive). But the two extra symlinks above in ~/Library/Fonts are my own trick to let XeTeX find the fonts in TeXLive tree by name.

  • notice that the above fonts closely related to Knuth original TeX fonts are different from the times+helvetica+courier default choice made by Sphinx for pdflatex. But for some time now, Sphinx with xelatex/lualatex has let LaTeX itself decides of font and this means it uses Latin Modern. Switching to Computer Modern Unicode will keep very similar looks to document and would fix LaTeX: PDF does not build with default font config and xelatex/lualatex for Russian language #5247.

Environment info

  • OS: must work for all...
  • Sphinx version: 1.8+
  • sufficiently recent TeXLive

Relates #5247, #5248

@jfbu jfbu added type:enhancement enhance or introduce a new feature builder:latex labels Aug 1, 2018
@jfbu jfbu added this to the 1.8.0 milestone Aug 1, 2018
@jfbu
Copy link
Contributor Author

jfbu commented Aug 1, 2018

Erratum: I have been confused about Latin Modern because of this

$ otfinfo -s `kpsewhich lmroman10-regular.otf`
DFLT		Default
cyrl		Cyrillic
latn		Latin
latn.AZE	Latin/Azeri
latn.CRT	Latin/Crimean Tatar
latn.MOL	Latin/Moldavian
latn.NLD	Latin/Dutch
latn.PLK	Latin/Polish
latn.ROM	Latin/Romanian
latn.TRK	Latin/Turkish

which led me believe it supported Cyrillic. This appears not be the case, and the Mac Book Font applications says:

Latin Modern Roman 10 Regular

	Nom PostScript	LMRoman10-Regular
	Nom complet	Latin Modern Roman 10 Regular
	Famille	Latin Modern Roman
	Style	10 Regular
	Type	OpenType PostScript
	Langue	afrikaans, albanais, alémanique, allemand, anglais, assou, basque, bemba, béna, bosniaque, capverdien, catalan, chambala, cornique, créole mauricien, croate, danois, diola-fogny, embou, espagnol, espéranto, estonien, féroïen, filipino, finnois, français, galicien, gallois, ganda, gusii, hongrois, igbo, indonésien, irlandais, islandais, italien, kalenjin, kamba, kiga, kikuyu, letton, lituanien, luo, machame, makhuwa-meetto, makonde, malais, malgache, maltais, manx, merou, ndébélé du Nord, néerlandais, norvégien bokmål, norvégien nynorsk, nyankolé, oluluyia, oromo, polonais, portugais, romanche, rombo, roumain, roundi, rwa, rwanda, samburu, sangho, sangu, sena, shona, slovaque, slovène, soga, somali, suédois, swahili, swahili du Congo, taita, tchèque, teso, turc, turkmène, vietnamien, vunjo, yoruba, zoulou
	Script	latin
	Version	Version 2.004;PS 2.004;hotconv 1.0.49;makeotf.lib2.0.14853
	Emplacement	/usr/local/texlive/2018/texmf-dist/fonts/opentype/public/lm/lmroman10-regular.otf
	Nom unique	2.004;UKWN;LMRoman10-Regular
	Copyright	Copyright 2003, 2009 B. Jackowski and J. M. Nowacki (on behalf of TeX users groups). This work is released under the GUST Font License --  see http://tug.org/fonts/licenses/GUST-FONT-LICENSE.txt for details.
	Marque déposée	Please refer to the Copyright section for the font trademark attribution notices.
	Activée	Oui
	Doublon	Non
	Protégé contre la recopie	Non
	Nombre glyphes	821

Although "Latin" Modern is quite clear name I thought it supported Cyrillic due to some state of confusion caused by the otfinfo output above. (also, with pdflatex and lmodern package, the Cyrillic letters are not supported but one does not realize it because there is silent font substitution by the cmr font if one has issued \usepackage[T1,T2A]{fontenc} hence the PDF is ok).

Confirmation from an 2013 answer at tex.sx. See also the Latin Modern font page

Thus currently this reduces my 2 proposals to only Computer Modern Unicode...

@jfbu
Copy link
Contributor Author

jfbu commented Aug 2, 2018

Libertinus

https://ctan.org/pkg/libertinus

diff --git a/sphinx/writers/latex.py b/sphinx/writers/latex.py
index 40a69f1c1..a8c8a8218 100644
--- a/sphinx/writers/latex.py
+++ b/sphinx/writers/latex.py
@@ -160,7 +160,9 @@ ADDITIONAL_SETTINGS = {
         'polyglossia':  '\\usepackage{polyglossia}',
         'babel':        '',
         'fontenc':      '\\usepackage{fontspec}',
-        'fontpkg':      '',
+        'fontpkg':     ('\\setmainfont{Libertinus Serif}\n'
+                        '\\setsansfont[Scale=MatchLowercase]{Libertinus Sans}\n'
+                        '\\setmonofont[Scale=MatchLowercase]{Libertinus Mono}'),
         'utf8extra':   ('\\catcode`^^^^00a0\\active\\protected\\def^^^^00a0'
                         '{\\leavevmode\\nobreak\\ }'),
         'fvset':        '\\fvset{fontsize=auto}',
@@ -170,7 +172,9 @@ ADDITIONAL_SETTINGS = {
         'polyglossia':  '\\usepackage{polyglossia}',
         'babel':        '',
         'fontenc':      '\\usepackage{fontspec}',
-        'fontpkg':      '',
+        'fontpkg':     ('\\setmainfont{Libertinus Serif}\n'
+                        '\\setsansfont[Scale=MatchLowercase]{Libertinus Sans}\n'
+                        '\\setmonofont[Scale=MatchLowercase]{Libertinus Mono}'),
         'utf8extra':   ('\\catcode`^^^^00a0\\active\\protected\\def^^^^00a0'
                         '{\\leavevmode\\nobreak\\ }'),
         'fvset':        '\\fvset{fontsize=auto}',

It has much wider glyph coverage than Latin Modern family. In particular it supports Cyrillic, Greek and Hebrew.

Libertinus Serif Regular
Libertinus Serif Regular

	Nom PostScript	LibertinusSerif
	Nom complet	Libertinus Serif
	Famille	Libertinus Serif
	Style	Regular
	Type	OpenType PostScript
	Langue	afrikaans, aghem, akan, albanais, alémanique, allemand, anglais, assou, azéri, bafia, bambara, basque, bemba, béna, biélorusse, bosniaque, breton, bulgare, capverdien, catalan, chambala, cornique, créole mauricien, croate, danois, diola-fogny, douala, embou, espagnol, espéranto, estonien, éwé, éwondo, féroïen, filipino, finnois, français, galicien, gallois, ganda, grec, groenlandais, gusii, haoussa, hawaïen, hébreu, hongrois, igbo, indonésien, irlandais, islandais, italien, kabyle, kalenjin, kamba, kazakh, kiga, kikuyu, kirghize, koyra chiini, kwasio, langi, letton, lingala, lituanien, luba-katanga, luo, macédonien, machame, makhuwa-meetto, makonde, malais, malgache, maltais, manx, masai, merou, meta', mongol, mundang, nama, ndébélé du Nord, néerlandais, norvégien bokmål, norvégien nynorsk, nuer, nyankolé, oluluyia, oromo, ouzbek, peul, polonais, portugais, romanche, rombo, roumain, roundi, russe, rwa, rwanda, samburu, sangho, sangu, sena, serbe, shona, slovaque, slovène, soga, somali, songhaï koyraboro senni, suédois, swahili, swahili du Congo, tadjik, taita, tamazight, tasawaq, tchèque, teso, tongien, turc, turkmène, ukrainien, vietnamien, vunjo, yangben, yoruba, zarma, zoulou
	Script	cyrillique, grec, hébreu, latin
	Version	Version 6.5 
	Emplacement	/usr/local/texlive/2018/texmf-dist/fonts/opentype/public/libertinus/libertinusserif-regular.otf
	Nom unique	6.5;ALIF;LibertinusSerif
	Fabricant	Khaled Hosny
	Concepteur	Philipp H. Poll
	Copyright	Copyright © 2012-2018 The Libertinus Project Authors.
	Licence	This Font Software is licensed under the SIL Open Font License, Version 1.1.
	Activée	Oui
	Doublon	Non
	Protégé contre la recopie	Non
	Nombre glyphes	2 645

Besides, it has an accompanying OpenMath font which can be set-up with \usepackage{unicode-math}\setmathfont{Libertinus Math}.

It has narrow glyphs a bit like Times font used by Sphinx with PDFLaTeX. This helps for code-blocks (long lines) and indices.

It comes with TeX distributions such as TeXLive. Requires perhaps recent ones as there it was actively maintained in recent years.

(edit) additional remarks:

  • The asterisk * is high above baseline in this font, here is example (this is with Mono font, inside code-block) from a build of Sphinx own doc with xelatex and this font:

    capture d ecran 2018-08-02 a 11 15 01

    With PDFLaTeX, Sphinx uses Courier font (from \usepackage{times}) and the * is lowered to baseline. The problem of elevated * (problematic for monospaced fonts used for code-blocks) may arise with other fonts than Libertinus and needs to be checked. This problem does not arise with Computer Modern Unicode (nor with Latin Modern).

    I have opened a ticket.

    For Sphinx, a macro based solution is possible (either via an active character or perhaps using some XeTeX specifics or LuaLaTeX ones) but adds complications for maintenance.

  • Recently (April 2018) a LaTeX package libertinus-otf has been made available. It simplifies using the font to simply \usepackage{libertinus-otf} and adds some features. Regarding the Mono font it loads it with additional option FakeStretch = 0.8 which makes it narrower looking. The asterisk position is still high above baseline.

@tk0miya
Copy link
Member

tk0miya commented Aug 4, 2018

I don't have strong opinion for this. But I think font is a part of design. So it would be nice if it will be provided as LaTeX-theming in future.
Anyway, I suppose users who specifies latex_engine to lualatex or xelatex has knowledge of TeX. So everybody would say okay to either setting.

@tk0miya tk0miya modified the milestones: 1.8.0, 2.0 Aug 19, 2018
jfbu added a commit to jfbu/sphinx that referenced this issue Nov 16, 2018
0. do not escape Unicode Greek letters via LaTeX math mark-up: pass them
   through un-modified to LaTeX document,

1. if "fontenc" receives extra option LGR, then pdflatex will support
   Unicode Greek letters (not in math), and with extra option T2A it
   will support (most) Unicode Cyrillic letters.

2. for pdflatex with LGR, this will use "textalpha" LaTeX package and
   "substitutefont" package to set up some automatic font substitution
   to work around the unavailability of Greek with "times"
   package (which is default font package chosen by Sphinx for
   pdflatex), same with T2A and "substitutefont" for Cyrillic.

3. for xelatex/lualatex, set up Computer Modern Unicode as default font,
   as it supports Cyrillic and Greek scripts,

4. for platex, don't do anything special as the engine already has
   its default font supporting Cyrillic and Greek (even in math mode!)

Closes: sphinx-doc#5251
Fixes:  sphinx-doc#5248
Fixes:  sphinx-doc#5247
@jfbu
Copy link
Contributor Author

jfbu commented Nov 17, 2018

We must take into account that our reference TeX installation on Ubuntu/Xenial via Debian texlive packages. Requirements:

  • the font support the Latin, Cyrillic and Greek scripts,

  • the font comes with Roman, Sans-Serif and Monospace families,

  • (ideally) the font is available as a separate Ubuntu package, not requiring big texlive-fonts-extra.

Latin Modern supports only Latin, Computer Modern Unicode is not available as separate package from texlive-fonts-extra, Libertinus also is not available separately.

OpenType fonts available separately, providing Cyrillic and Greek support, and coming in Serif, Sans Serif and Monospace include:

I am probably missing some but this is what I extract from texlive-fonts-extra dependencies. Perhaps it is too much a constraint to require Serif, Sans, and Mono all available.

A reasonable approach could be to go for Linux Libertine, and later to switch to Libertinus which is interesting in XeLaTeX/LuaLaTeX due to acccompanying math font (if at later version unicode-math is made part of default setting).

@jfbu
Copy link
Contributor Author

jfbu commented Nov 17, 2018

But Linus Libertine Mono does not support Cyrillic. And Libertinus Mono does not either (at my locale TL2018). This seems to leave FreeFont and DejaVu.

DejaVu is very readable on screen, very clear, FreeMono takes less place (perhaps a bit like the intent was in legacy Sphinx into using "times"), is more "aristocratic".

DejaVu Serif Book

capture d ecran 2018-11-17 a 19 28 41

FreeSerif Normal

capture d ecran 2018-11-17 a 19 29 04

Incidentally FreeSerif has the ℇ (EULER CONSTANT U+2107) which is lacking from DejaVu Serif, but this is anecdotical I guess.

I have not tested but my expectation is that the standard TeX math fonts fit better with FreeFont than with DejaVu, because they have very thin hairlines.

Here is an extract of Sphinx own documentation with xelatex and FreeFont:

capture d ecran 2018-11-17 a 19 47 47

and here with DejaVu (due to a pagebreak I could not take same snapshot).

capture d ecran 2018-11-17 a 19 51 43

There is more equilibrium between default sizes of Serif and Mono. But fontspec could be use to scale the FreeSerif.

With FreeFont 390 pages, with DejaVu 410 pages. But DejaVu might benefit from a bit of extra linespread (\linespread{1.05}) and then there will be yet more pages.

Very close file sizes.

Extract of the index (with 2.0.0+, uses monospace font for entries, and normal italic for extras)

FreeFont:

capture d ecran 2018-11-17 a 19 58 42

DejaVu:

capture d ecran 2018-11-17 a 19 55 38

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 9, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
builder:latex help wanted type:enhancement enhance or introduce a new feature
Projects
None yet
Development

No branches or pull requests

2 participants