← Back to DAGDA | Support | Source: https://github.com/sphinx-doc/sphinx/issues/4159
Skip to content

Maybe use xelatex instead of pdflatex by default #4159

Closed as not planned
Closed as not planned

Description

@JulienPalard

Subject: I built the cpython documentation in french and japanese, and found it non-trivial to find the right set of options.

Problem

Given that:

  • Non-ascii characters are more and more used, even in english (see the Є in https://docs.python.org/3.7/whatsnew/3.7.html#optimizations)
  • Documentations are sometimes written in other languages like Japanese, French, and so on.
  • Documentations are sometimes translated from one language to another.

We could expect to find non-ascii characters everywhere, which are badly supported by pdflatex, even by using utf8x which come with another set of issues.

Proposed solution

I finally found that xelatex handle very well unicode characters, but does not work well with japanese. And platex works well with japanese.

platex is already the default with the latex_engine is not explicitly configured, which is already nice, but there is no way to configure xelatex for all languages and platex for japanese (#4150).

It forces everyone to learn a lot about latex and PDF generation, and finally force them to use -D with external logic to switch between working engines like https://github.com/python/docsbuild-scripts/pull/34/files.

Also, the documentation is not very explicit about the usages of those engines (see #4149).

What I propose is to switch the default from 'pdflatex' if language != 'ja' else 'platex' to 'xelatex' if language != 'ja' else 'platex' which is a combination that works without any other modification to build cpython documentation in english, french, and japanese.

Activity

  1. tk0miya commented on Oct 17, 2017

    @tk0miya
    Member

    @jfbu could you give us comments for this please?

    I can't determine it's better or not. I don't know which is good successor of PDFLatex, LuaTeX and XeTeX. And Also don't know they are enough stable or not for usage of Sphinx.

    Of course, I will agree to change default latex engine if either one is enough stable.

  2. jfbu commented on Oct 17, 2017

    @jfbu
    Contributor

    I will make general remarks.

    LuaLaTeX is actively maintained and will probably offer more and more features via dedicated packages which achieve things currently impossible in TeX. But these advantages are probably not needed by vast majority of Sphinx projects. Besides, it appears that ̀LuaLaTeX opens up new security concerns in TeX world, due to scripting language Lua not having the restrictions for file opening and writing which exist with pdfTeX binaries as distributed with major installations (TeXLive and MikTeX). It may be said that such concerns already exist from running Python scripts,... nevertheless this makes one think twice before adopting it on grand scale by default. Thus, I would not recommend using lualatex by default, before experience has accumulated elsewhere.

    XeLaTeX does not have this issue.

    • but switching to it does not solve all Unicode related problems: in fact, in hand-written documents authors manually switch languages according to needed glyphs, and they set-up appropriate fonts for languages (at least indirectly via polyglossia package)

    • so far Sphinx LaTeX writer does not support multi-lingual documents. Even if it did, author of Sphinx project would need to manually add mark-up to source in case of exotic Unicode characters to signal the change of language, hence possible OpenType font to use: fonts do not support all scripts, although indeed some fonts do support a wide range of scripts.

    • it appears that polyglossia support for French lags behind babel+french features, so if we at Sphinx set usage of ̀xelatex+polyglossia default, we may raise specific French issues -- admittedly they may be relevant only to expert LaTeX users which will know how to switch back to babel+french usage.

    • they are issues with xelatex regarding math mode: it has some currently non-fixed bugs there, but this is arguably not a very strong deterrent for Sphinx projects.

    Making xelatex default will modify looks of all Sphinx produced PDFs, because xelatex should be used with OpenType fonts. It can be used with traditional TeX fonts, but then hyphenation mechanism of TeX is broken in some languages. Recently the LaTeX team has modified behaviour of LaTeX so that by default if used with xelatex engine it will use OpenType version of lmodern font.

    So making xelatex default also requires reviewing font configuration and all Sphinx supported languages and as I said it will change the default looks of all Sphinx build PDF documentations.

    This looks like quite some work at Sphinx side... I think first step is to move Sphinx towards supporting multi-lingual documents. Because making xelatex default engine is not by itself a 100% solution to all problems related to Unicode input. It requires extra steps.

  3. jfbu commented on Oct 17, 2017

    @jfbu
    Contributor

    One last pros and cons:

    • typically xelatex produced PDFs are smaller than pdflatex produced ones, when using traditional TeX fonts, because xelatex better compresses the font; but as explained already, xelatex should not be used with traditional TeX fonts for optimal results,

    • compilation times with xelatex or lualatex are often significantly increased compared to pdflatex builds.

  4. JulienPalard commented on Oct 20, 2017

    @JulienPalard
    ContributorAuthor

    That's a lot to consider and I'm no latex expert. I just noticed that the current default (pdflatex/platex) put me in a hard situation when building english, french, and japanese:

    • With default configuration, only japanese succeed (platex)
    • Adding utf8x fix english build, (don't remember if it break platex-japanese)
    • Building with xelatex (and removing utf8x) to try to fix french breaks japanese (no more platex by default for japanese)

    So I just can't have a successful build with conf.py, I have to use sphinx-build -D flags to pass the right latex_engine for the right language, with an external logic.

    It took me some time to find the "right combination", which looks in fact really simple, just replace pdflatex with xelatex as a default engine but keep the "default to platex for japanese if default engine is used".

    In one hand I may be short sighted as I tested a single project, in the other hand the Python documentation is huge (230k lines of rst).

  5. fyears commented on Oct 21, 2017

    @fyears
    Contributor

    +1 to xelatex

    I can confidently say that most Chinese LaTeX users prefer xelatex to pdflatex nowadays, because xelatex has MUCH better support for opentype fonts, thus Chinese uses find it WAY MORE easier to display Chinese characters in the generated pdf. The same technology applies to Japanese and Korean characters too (we often refer their fonts together as CJKfonts).

    @jfbu In my understanding, sphinx-doc maintains its default template of pdf, thus something like “front issue” should not be a problem (to users)?

    @JulienPalard switching from pdflatex to xelatex for JP doc is not THAT trivial. At least you should set \setCJKmainfont , otherwise JP characters are not expected to be displayed correctly. Still, it’s kind of easy for simple cases, see https://tex.stackexchange.com/questions/139081/cjk-blank-output-for-japanese-characters

  6. fyears commented on Oct 21, 2017

    @fyears
    Contributor

    Some more helpful info here:

    1. xelatex is stable enough to use. luatex is not as popular as xelatex for Chinese users.
    2. Refer https://www.sharelatex.com/learn/Japanese (also, check pages for Chinese and Korean). the xetex packages is universal for CJK environments, if we only need to display some characters and not consider complicated locales (e.g. how dates are rendered). One true issue to be considered, is how to determine the \setCJKmainfont for different systems (win/linux/osx maintain different fonts!) and different languages (sorry but people in CJK develop different fonts).
  7. jfbu commented on Oct 21, 2017

    @jfbu
    Contributor

    There is no notion of seamless experience in LaTeX regarding Unicode, although xelatex and lualatex have considerably improved the situation.

    Already, Sphinx does the minimal right thing regarding xelatex which is not to use inputenc nor fontenc. With a recent LaTeX this means it will automatically use the Latin Modern OpenType font which has good coverage of European (in the large sense) languages.

    $ otfinfo -s 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
    

    It has no coverage for Chinese or Hebrew for example. This means Sphinx user for a project in these languages must customize LaTeX preamble to appropriately use \setmainfont (or \setCJKmainfont as documented by @fyears) to pick suitable font (Sphinx loads fontspec which provides this macro; but xelatex has its own font loading primitives which advanced xelatex users use directly; normal users will use fontspec and they will have had to read partly its documentation; does this include the average Sphinx-doc user?).

    The way this is done is system dependent regarding fonts which are provided with TeX itself (and on Mac OS X one must use different methods depending on whether the OpenType font is a system/user font or in the TeX tree).

    Even the minimal Sphinx set-up for xelatex contains elements which are not satisfactory: the coverage of French language by polyglossia is far more restricted than what the babel-frenchb module provides: with polyglossia there is no conformity regarding footnotes and lists with the French typographical rules.

    Besides, latex-babel is now (after some years of stagnation) actively maintained and being developed in direction of xelatex/lualatex support. As a result it is not clear if polyglossia will remain preferable to babel in future.

    Regarding French as I said it is not. Sphinx French user of xelatex is now well advised to modify latex_elements 'babel''s key to set it to '\usepackage{babel}'. Sphinx internally has 'polyglossia' but will obey 'babel' key if the user has set it:

            # set up multilingual module...
            # 'babel' key is public and user setting must be obeyed
            if self.elements['babel']:
                # this branch is not taken for xelatex/lualatex if default settings
    

    Making xelatex default makes no sense if reasonable font defaults for all Sphinx covered languages are not provided.

    For example, similarly as we have specific coverage of japanese [1]_, we can provide specific coverage of Chinese if consensus emerges on how to best set-it up with XeLaTeX and this must be done Windows, Mac OS X, Unixen... Contributions are most welcome !

    .. [1] which as mentioned already in this thread goes currently via platex engine which does not support Unicode.

    And, stressing again, this does not solve problems one may encounter with stray Unicode characters !

  8. jfbu commented on Oct 21, 2017

    @jfbu
    Contributor

    Here is basic test of Hebrew with xelatex:

    \documentclass[hebrew]{article}
    \usepackage{polyglossia}
    \setmainlanguage{hebrew}
    \begin{document}
    מבוא
    \end{document}

    Produces errors:

    ./testhebrew.tex:4: Package polyglossia Error: The current roman font does not 
    contain the Hebrew script!
    (polyglossia)                Please define \hebrewfont with \newfontfamily.
    
    See the polyglossia package documentation for explanation.
    Type  H <return>  for immediate help.
     ...                                              
                                                      
    l.4 \begin{document}
                        
    (That was another \errmessage.)
    
    Missing character: There is no מ in font [lmroman10-regular]:mapping=tex-text;!
    Missing character: There is no ב in font [lmroman10-regular]:mapping=tex-text;!
    Missing character: There is no ו in font [lmroman10-regular]:mapping=tex-text;!
    Missing character: There is no א in font [lmroman10-regular]:mapping=tex-text;!
    
    ./testhebrew.tex:6: Package polyglossia Error: The current roman font does not 
    contain the Hebrew script!
    (polyglossia)                Please define \hebrewfont with \newfontfamily.
    
    See the polyglossia package documentation for explanation.
    

    Attempting to try Sphinx on minimal Hebrew document with xelatex leads to plenty of problems:

    .. FOO documentation master file, created by
       sphinx-quickstart on Sat Oct 21 14:57:01 2017.
       You can adapt this file completely to your liking, but it should at least
       contain the root `toctree` directive.
    
    תוכן הענייני
    ============
    
    רשימת הטבלאות
    

    in conf.py:

    language = 'he'
    latex_engine = 'xelatex'
    
    Package bidi Error: Oops! you have loaded package xcolor after bidi package. Please load package xcolor before bidi package, and then try to run xelatex on your document again.
    
    Package bidi Error: Oops! you have loaded package float after bidi package. Please load package float before bidi package, and then try to run xelatex on your document again.
    
    Package bidi Error: Oops! you have loaded package framed after bidi package. Please load package framed before bidi package, and then try to run xelatex on your document again.
    
    Package bidi Error: Oops! you have loaded package wrapfig after bidi package. Please load package wrapfig before bidi package, and then try to run xelatex on your document again.
    
    etc... etc...
    

    and the one of interest to this thread:

    Package polyglossia Error: The current roman font does not contain the Hebrew script!
    ...
    

    (as above)

    This confirms Sphinx-doc user will have to know a minimum of LaTeX macros (\newfontfamily) and documentation (fontspec, polyglossia) before reaching usable status for Hebrew language documents even with xelatex as latex_engine.

    (we at Sphinx should probably take care of loading polyglossia hence bidi at the right place)

  9. tk0miya commented on Oct 21, 2017

    @tk0miya
    Member

    jfbu, Thank you for comment.

    As you said, moving to xelatex is not silver bullet. AFAIK, there are no common settings that works well for all languages.

    @fyears For Chinese docs, #3272 is proposed. It tries to move to xelatex and ctex only if language is zh_*.

  10. tk0miya commented on Oct 21, 2017

    @tk0miya
    Member

    Note:

    This looks like quite some work at Sphinx side... I think first step is to move Sphinx towards supporting multi-lingual documents.

    I don't know this is really needed. I've never seen such request. So it's okay to support only one language per project at once.

    (edit) oh, I understand #4159 requires it...

  11. jfbu commented on Oct 21, 2017

    @jfbu
    Contributor

    @tk0miya in the case of CPython docs (which is big...), for example French translation is only at 27.2% currently.

    It could make sense (not only for PDF perhaps, but for PDF it is important due to hyphenation which depends on language) to have multi-lingual. Currently only portions of CPython's library.pdf (about 1800 pages) are in French but the whole is treated as French document. This means that hyphenation is wrong for all English text, which is vast majority of document.

    (I am using make latex SPHINXOPTS="-D locale_dirs=locales -D language='fr' -D gettext_compact=0" to build the CPython French documentation, with Doc/locales/fr/LC_MESSAGES a symlink to the python-docs-fr cloned repo at 3.6 branch)

  12. tk0miya commented on Oct 22, 2017

    @tk0miya
    Member

    Ah, I understand. Surely, it is mixture of English and French.
    I feel it is very difficult to support it in Sphinx. We must mark languages per sentences or words...

  13. 2 remaining items

  14. mitya57 commented on Oct 29, 2017

    @mitya57
    Contributor

    @jfbu: In Sphinx, the .. class:: directive has a different meaning. Use .. rst-class:: language-XY if you want to insert the original Docutils directive. It should work then.

  15. jfbu commented on Oct 29, 2017

    @jfbu
    Contributor

    @mitya57: thanks for the tip, which does work indeed for html target, producing same lang attributes as rst2html.py. But it fails for latex target (as expected from actual writers/latex.py code...); the fulllineitems environments are gone however, the output simply losing all traces of the language tags in reST sources.

  16. added this to the some future version milestone on Sep 5, 2018
  17. jfbu commented on Dec 20, 2018

    @jfbu
    Contributor

    Sphinx 2.0 will use GNU FreeFont with xelatex, providing good coverage of Latin, Cyrillic and Greek scripts (as well as Arabic and Hebrew). This adds new requirement fonts-freefont-otf on Ubuntu xenial or e.g. in Fedora 29 texlive-gnu-freefont. Perhaps Sphinx 3.0 can then have 'xelatex' as default latex_engine, for non-Japanese projects.

    (edit: and make suitable choice of fonts for Chinese with 'xelatex')

  18. goyalyashpal commented on Mar 6, 2024

    @goyalyashpal

    french and japanese
    - @ JulienPalard at #4159 (comment)

    i use hindi, and same problem will be faced with using any indian language script (Hindi, Nepali, Tamil, Telugu, Pubjabi, Marathi, Gujarati, ...).

    compilation times with xelatex or lualatex are often significantly increased compared to pdflatex builds.
    - @ jfbu at #4159 (comment)

    that's 'cz xelatex outputs in pdf, and modifying pdf is what takes time. to save on that, latexmk uses xelatex to fastly generate output of intermediate passes in .xdv files; then converts that via xdvipdfmx to .pdf only once at last.

    Ref (abridged by me, original at: latexmk-pdf):

      -pdfxe Generate pdf version of document using xelatex [and xdvipdfmx via
             .xdv intermediate files].  Note that production of a .xdv file by
             xelatex is fast, [but of] a .pdf file can be quite time consuming
             when  document includes  large graphics files. So [this approach]
             can result in substantial gains in procesing time, since the .pdf
             file is produced once rather than on every run of xelatex.
    
    Unabridged verbatim:
      -pdfxe Generate  pdf  version  of document using xelatex.  Note that to
             optimize processing time, latexmk uses xelatex  to  generate  an
             .xdv  file rather than a pdf file directly.  Only after possibly
             multiple runs to generate a fully up-to-date .xdv file does  la-
             texmk then call xdvipdfmx to generate the final .pdf file.
    
             (Note:  The  reason  why latexmk arranges for xelatex to make an
             .xdv file instead of the xelatex's default of a .pdf file is  as
             follows:  When the document includes large graphics files, espe-
             cially .png files, the production of a .pdf file  can  be  quite
             time consuming, even when the creation of the .xdv file by xela-
             tex  is  fast.  So the use of the intermediate .xdv file can re-
             sult in substantial gains in procesing time, since the .pdf file
             is produced once rather than on every run of xelatex.)
    
  19. jfbu commented on Jul 21, 2024

    @jfbu
    Contributor

    @goyalyashpal

    There is in our docs this tip:

    Also, if latexmk is at version 4.52b or higher (January 2017) LATEXMKOPTS="-xelatex" speeds up PDF builds via XeLateX in case of numerous graphics inclusions.

    This -xelatex option is (with current Latexmk) equivalent to -pdfxe -dvi- -ps-.

    It is probably time in 2017 we do this unconditionally.

  20. jfbu commented on Dec 22, 2025

    @jfbu
    Contributor

    I am sorry for this issue having been kept open so long.

    • xelatex is as of today unmaintained,
  21. jfbu commented on Dec 22, 2025

    @jfbu
    Contributor

    I am sorry for this issue having been kept open so long.

    * xelatex is as of today unmaintained,
    

    Sorry I posted by mistake from keyboard combination. Anyway continuing:

    • LaTeX team is making lualatex the engine of choice,
    • however compiling with lualatex is much slower,
    • besides there was an important security issue until a few years back with luatex, and as many Linux distros systematically lag years behing current TeX world status, we can not endorse luatex as default engine.

    So I will close this as not-planned.

  22. goyalyashpal commented on Dec 23, 2025

    @goyalyashpal

    oh glad i left the *tex world behind. it's ways a mess.

    long live typst.

  23. AA-Turner commented on Dec 23, 2025

    @AA-Turner
    Member

    @jfbu is there a link for further details on XeLaTeX's maintenence status? If it is unmaintained, we should consider updating our docs to alert users to this.

    A

  24. jfbu commented on Dec 23, 2025

    @jfbu
    Contributor

    @AA-Turner My comment is based in part upon what one reads at this blog post, dating back to November 2024, from one of the core LaTeX maintainers. I hope it is ok if I copy-paste the most relevant parts here (omitting some sentences so that I do not too much infringe any copyright)

    XeTeX

    ... XeTeX has none of the flexibility available in LuaTeX. ... XeTeX cannot create accessible PDFs at all: this is unlikely to change. The engine itself is unmaintained, and with no access to internals, fixing the current issues alone would not really help. XeTeX does not produce PDFs directly, and that essentially rules out full tagging support...
    ... it is time to move away from XeTeX: certainly for new documents, and even for existing ones. Users should look at alternative approaches here, even if it means some source changes.

    The comments relative to tagging were also reiterated at some other locations accompanying the recent LaTeX kernel updates, but I would need to dig up these sources. As per the status of maintainance of XeTeX, one can try to query at their mailing list. There is an active community of users of XeTeX, but regarding XeLaTeX (and we at Sphinx do use LaTeX not some other TeX formats such as ConTeXt or OpTeX (PRs welcome!) 1), as reported above, the LaTeX team has announced its advice to users to transition to LuaLaTeX.

    Footnotes

    1. edit. This is a bit obscure because ConTeXt is itself another TeX engine, and OpTeX is a macro format (like LaTeX) which is based upon LuaTeX. Sorry for confusing language. ↩

  25. jfbu commented on Dec 23, 2025

    @jfbu
    Contributor

    If it is unmaintaine, we should consider updating our docs to alert users to this.

    We could perhaps recommend to choose LuaLaTeX, but the current benefits are not really there: elevated build times and PDF sizes 1 When tagging is ripe and stable enough at LaTeX core for us to actively try to support it, there will be no other choice indeed than to prominently encourage users to choose lualatex as latex_engine.

    Footnotes

    1. in comparison to XeLaTeX. ↩

  26. jfbu commented on Dec 23, 2025

    @jfbu
    Contributor

    Some more on XeTeX maintenance status. This is already more than 4 years old but I don't think significant changes occurred in the intervening years since.

  27. locked as resolved and limited conversation to collaborators on Jan 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

builder:latextype:proposal

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions