Repository navigation
Closed
Description
Subject: When using U+2212 to denote a negative integer, sphinx can no longer build PDF.
Problem
Unicode character U+2212 in rst files breaks PDF generation.
Procedure to reproduce the problem
$ sphinx-quickstart
[…]
$ cd in_the_directory
$ printf "\n\nHello −4 world." >> index.rst
$ make latexpdf
Error logs / results
[…]
! Package inputenc Error: Unicode char − (U+2212)
(inputenc) not set up for use with LaTeX.
See the inputenc package documentation for explanation.
Type H <return> for immediate help.
...
l.86 Hello −
4 world.
?
(./minus.ind) [1] (./minus.aux) ){/usr/share/texlive/texmf-dist/fonts/enc/dvips
/base/8r.enc}</usr/share/texlive/texmf-dist/fonts/type1/urw/helvetic/uhvb8a.pfb
></usr/share/texlive/texmf-dist/fonts/type1/urw/helvetic/uhvbo8a.pfb></usr/shar
e/texlive/texmf-dist/fonts/type1/urw/times/utmb8a.pfb></usr/share/texlive/texmf
-dist/fonts/type1/urw/times/utmr8a.pfb>
Output written on minus.pdf (5 pages, 40041 bytes).
Transcript written on minus.log.
Latexmk: Index file 'minus.idx' was written
Latexmk: Log file says output to 'minus.pdf'
Latexmk: Errors, so I did not complete making targets
Collected error summary (may duplicate other messages):
pdflatex: Command for 'pdflatex' gave return code 256
Latexmk: Use the -f option to force complete processing,
unless error was exceeding maximum runs of latex/pdflatex.
Makefile:33: recipe for target 'minus.pdf' failed
make[1]: *** [minus.pdf] Error 12
make[1]: Leaving directory '/home/mdk/Downloads/test-minus/_build/latex'
Makefile:20: recipe for target 'latexpdf' failed
make: *** [latexpdf] Error 2
Expected results
I'd just expect my minus sign to be correctly rendered in a PDF file.
Environment info
- OS: Debian sid
- Python version: 3.6.2
- Sphinx version: 1.6.4
- texlive 2017.20171004-1
Looks like there a lots of problems with some unicode characters: https://tex.stackexchange.com/search?q=inputenc+2212
Maybe we should just add another DeclareUnicodeCharacter in utf8extra in latex.py?
Activity
JulienPalard commentedon Oct 9, 2017
Same with Є U+0404 CYRILLIC CAPITAL LETTER UKRAINIAN IE:
jfbu commentedon Oct 9, 2017
U+2212 belongs to the Symbol, Math category of the Mathematical Operators block. As such, naive stand of a LaTeX user would be that this is supposed to be used in math mode. Thus the nowadays standard LaTeX answer is to say to switch to Unicode engine xelatex or lualatex and use package unicode-math. Its default config will arrange for suitable math fonts hopefully having the glyph.
As per U+0404, then of course we can find thousands of similar example: utf8 option of inputenc can not cover the full Unicode range (but it did get expanded in recent years, so an up-to-date TeX install is always advisable.) Here again, the probably better answer is to switch to Unicode engine xelatex or lualatex.
You may also try option
utf8xfor inputenc, with traditional pdflatex. It may work, or not. Check 'inputenc' key. You need to solve also the font problem then, the LaTeX font must contain glyph of the wanted Unicode codepoint.Related: #3444
glyg commentedon Oct 10, 2017
Relevant tests from pandoc's default.latex template:
Hope this helps
To summerize:
And the tricky hyperref options...
jfbu commentedon Oct 10, 2017
Well, check Sphinx's latex_engine config setting. The default will use polyglossia.
One can recommend adding
\usepackage{unicode-math}to preamble (which provides\setmathfontin particular).About using
mathspec(xetex only) : it serves not much simply loading it without using its commands.As per
xunicodeI think it is now obsoleted by recentfontspecandxltxtranot sure.Probably also (no time to check now).It isxelatexspecific.Sphinx provides minimal set-up via latex_engine, using
polyglossiabut user should add preamble config for custom fonts. And possibly addunicode-math. This is not done by default because itverysignificantly slows down build time for PDF; but it is the de facto standard package for math mode with xelatex/lualatex. (mathspecdoes other things, xelatex only)jfbu commentedon Oct 10, 2017
@JulienPalard There is no problem with
So if your document uses suitable language it will ok, also with traditional pdflatex.
For an isolated letter, you may need more. Please provide a minimal example of Sphinx project displaying the problem. You could modify
'babel'key for extra language used for special letters. Then packagenewunicodecharcan facilitate using dedicated (TeX) font having that glyph.jfbu commentedon Oct 10, 2017
@JulienPalard
This:
shows (but see note below) how you can configure your Sphinx project latex preamble : you only need modify the 'babel' latex-elements key and add to preamble the two lines with
newunicodechar.It will fix your
Єproblem if it is a one-shot problem with this letter, with no change needed to produced Sphinx mark-up, only via Sphinx user customization of LaTeX document preamble.Important side note: there appears to be a LaTeX bug with
babel+ukrainianwhich causes extra spaces in output, sadly. As a result the thing to do is rather to get the preamble either to contain:or to use
russianand notukrainian:jfbu commentedon Oct 10, 2017
As proof of concept I have tried this in
conf.pythen I compile my project containing
Єcharacter usingmake latexpdf.There is no problem and the build succeeds. Warnings appear in the LaTeX log:
which says that the (TeX) Times font (from
\usepackage{times}) does not exist in theT2Aencoding loaded byrussianlanguage. But the Computer Modern font exists in this (TeX) encoding.Side note: there is a strange problem that if I use
and not
then the LaTeX compilation is done with Russian as main document language. This seems a bug of LaTeX-babel as it contradicts its documentation,
englishbeing the last option. There is something strange non-Sphinx related here.Perhaps interaction withBut it is not due to Sphinx.newunicodecharpackage, anyway, I have not debugged that so far.EDIT: this is indeed a documented LaTeX-babel bug. In situations like this:
the document language turns out be German !! If one suppresses the global class option
english, then the document language is English (last option tobabel). The babel documentation containsjfbu commentedon Oct 10, 2017
Still for cyrillic letter , there is an alternative to using
russianlanguage locally. One doesThis gives more efficient code (the
babelmethod also sets up hyphenation patterns which however are not needed for an isolated letter). Problem is that average LaTeX user will not know how to automatize, where does the\IeC {\CYRIE }come from. There is an intrinsic LaTeX problem which is that it makes such Unicode characters "active" via\usepackage[utf8]{inputenc}but the character will not by itself change the font: it knows whichfontencencoding to use, but if the current (TeX) font used is not with that encoding, it throws an error rather than changing by itself temporarily the font !Thus, we use
newunicodecharto modify the meaning, but we have to know the LICR representation. I don't have time now, but I suspect with some hacking we could automatize that, something like\sphinxnewunicodechar{Є}{T2A/cmr}the second argument indicating which TeX font to temporarily switch too to avoid the LaTeX error message of the style "no such character in font encoding T1".JulienPalard commentedon Oct 11, 2017
Wow, thanks for your extensive feedback everyone!!
I'm reviewing it all, I'll probably first try to use a mix between platex and xelatex (from python/cpython#3940 (comment)):
I'm strongly against all kinds of hardcoding like
sphinxnewunicodechar{Є}, for the record the documentation I try to build is the Python documentation, so I expect characters to be added regularly and don't want documentation writers and translators to have to update the sphinx configuration each time, this is a nonsense, so I hope we can do without this.JulienPalard commentedon Oct 11, 2017
Hum, I can't access language in my config.py, sphinx/config.py line 150 does not give it
execfile_(filename, config), butlanguage: 'fr'is stored in overrides, in my specific context... I'll still try to see if it works with xelatex for english and french, and platex for japanese.JulienPalard commentedon Oct 11, 2017
The following minimalist configuration:
builds english and french versions of the Python documentation. So I think we should add some information near the
latex_enginedocumentation telling about how to choose one? I don't expect sphinx users to know luatex, xetex, platex, I personally never heard of xetex and platex before today.JulienPalard commentedon Oct 13, 2017
I'm opening a specific issue about the documentation subject.
17 remaining items
jfbu commentedon Oct 21, 2017
Thanks for the explanation relative to the internationalization. I have git cloned the French translated repo, manually created
cpython/Doc/locales/fr/LC_MESSAGESsymlink in a checkout of cpython 3.6 tag with my patched conf.py, then I have manually run themake latexwith suitableSPHINXOPTSin a Python 3.6 environment.I got two innocuous warnings, then I cd to
build/latexand run theremake clean,make all-pdf. Ok, I see some problems.This one looks like a typo in French translation:
this is on line 101211 of library.tex. There is
ǸinNonewhich looks very suspicious.jfbu commentedon Oct 21, 2017
There is a U+200b in line
which is line 2096 of library.tex, right before
puisqueand this looks again very suspicious. Why should a zero-width space be there?jfbu commentedon Oct 21, 2017
I confirm I can build all French CPython Docs with pdflatex using the above. However there are bugs in the French translation strings (at least on typo and a possible stray zero-width space) which require extra steps. I did:
The package
uspacehandles Unicode space characters. It may not be installed at your TeX locale because it is relatively recent, see its repo.You an also simply do
to get rid of it (as it looked typo).
As per the
Ǹit is surely a typo so, I redeclared it to beN, I have not looked at how it should be defined really if it had been needed.Sorry for delays, but building all PDFs takes time each time.
Notice that ironically, bugs are revealed from not using Unicode savvy engine, as then input typos are detected.
I find it useful though to have spent time on this big project (library.pdf in French going to 1742 pages), as this can be used in future in case of Sphinx LaTeX changes as a test.
JulienPalard commentedon Oct 21, 2017
I fixed the
Ǹ, and I don't think the zero-width space is a thing, so I also removed them.JulienPalard commentedon Oct 21, 2017
Yes, I take note. Also having this in
conf.pyis way more suitable than havig to move the logic in https://github.com/python/docsbuild-scripts: It allows one to build more naturally using the cpython makefile instead of having to rely of another tool, I like it.Would you like to PR this on https://github.com/python/cpython (issue already opened: https://bugs.python.org/issue31589)?
jfbu commentedon Oct 21, 2017
I would be glad to PR, but unfortunately putting this in
conf.pywill break Japanese translated builds, because packagenewunicodecharworks only in context of\usepackage[utf8]{inputenc}(it is not compatible withutf8x), and is not compatible withplatexengine.But it is possible to enclose it in some LaTeX conditional branch and avoid executing it if engine is
platex. (or perhaps there is some Sphinx latex_elements key which we can use, offhand I don't remember will check now).Oh, simply I can useno sorry, my mistake. Will go the LaTeX conditional way.'utf8extra'key.Regarding Japanese docs, I understand you build with no LaTeX errors. But do the PDFs look fine regarding the problematic characters? (i.e. particularly the long s
ſand theЄ)jfbu commentedon Oct 21, 2017
@JulienPalard I have done the PR at python/cpython#4069
jfbu commentedon Oct 23, 2017
I have found a way for Japanese. I could not solve the problem with
pLaTeXwhich Sphinx uses by default for Japanese. But after some trial and error I perhaps have a way withupLaTeX. But: many files of the CPython documentation use Sphinx howto which usejreportwhich I do not know how to make work withupLaTeX. For the biggest file which islibrary.texit usesjsbookand one can make it work withupLaTeXusing LaTeX document class optionuplatex.In conf.py:
Unfortunately the extra class option
uplatexis not expected by LaTeX when not used withjsbookclass so it reports a warning in each latex compilation log when one tries the above with-D language=frfor example. This is mostly invisible to user but I should think better about gettinguplatexas class option only for Japanese (one can actually use own version of LaTeX template to add somestuff before the document class, inclusive of some conditional\PassOptionsToClass).Then we also need this in conf.py:
and finally we need this
latexmkjarcfile, which is fastest way I found to trick Sphinx to useupLaTeXand notpLaTeXengine for Japanese:edit:
upmendexapparently doesn't recognize-Uoption, which should be removed, it was option formendexto Set input/output kanji encoding to UTF-8. which perhaps makes no sense forupmendex(possibly similarly as-kanji=utf8perhaps also makes no sense as option foruplatex)I think the
-kanji = utf8option should be removed, but I can't read Japanese documentation and I worked from google translate... this file above is exactly the Sphinx providedlatexmkjarcexcept that I replacedplatexbyuplatexandmendexbyupmendex.With this set-up I can compile successfully for English, French, and Japanese: sadly however in Japanese only when
'manual'is chosen for the docclass not'howto'. So I compiled all CPython docs succesfully in Japanese, but after having forced'manual'everywhere.I can't use utf8 inputenc with pLaTeX, but I can with upLaTeX,
package newunicodechar refuses to work with upLaTeX, this is why I reverted to
\DeclareUnicodeCharacterthe method above is set-up to work both with pdflatex and with uplatex,
because this method uses the Latexmk way, it does not work on Windows, as currently Sphinx does not use Latexmk on Windows.
there is no need to set
latex_engine: Sphinx for Japanese uses'platex'but the used binary is actually the one listed in thelatexmkjarcfile ..., so we can workaround the fact that Sphinx does not (yet) provide a'uplatex'setting forlatex_engine.careful that when copying pasting from here the
Kit may becomeK.@cocoatomo do you have any comment? do you build Japanese CPython documentation with standard Sphinx set-up using
pLaTeX?I couldn't make pLaTeX work with the characters above, except the U+2212 − which at least gives reasonable output it seems with no extra set-up. I had some partial success but then unexpected results arose from using these characters in code-blocks or literal.
Or course there might be much better way, but as I can't read Japanese it is very hard for me to understand how LaTeX classes and engines work there.
JulienPalard commentedon Oct 23, 2017
nosy @methane ^
jfbu commentedon Oct 23, 2017
There is a
ujreportLateX document class for usage withupLaTeX.I can force Sphinx to use it by this kind of patching in
conf.py:With patch like the above I can then build the whole CPython documentation for Japanese (without having to replace
'howto'by'manual'as in my first try withupLaTeX).The
uplatexextra class option is ignored byujreportLaTeX class, and the log warning is inocuous.@tk0miya is there easier way to get Sphinx to use
ujreportas docclass rather thanjreportwhen language is'ja'and document type'howto'?Also it would be convenient to have a
'passoptionstoclass'latex_elements key, because this can have conditional LaTeX code that can't be put safely in'extraclassoptions'. Admittedly, this can now be done at user level using template override.brechtm commentedon Jun 9, 2020
I happened to see this issue when I was searching for tickets on another topic. I just wanted to mention that building a PDF using rinohtype might make things easier when dealing with languages other than English. As long as your fonts contain the needed glyphs, things should just work... provided the script doesn't require advanced typesetting features that are currently not supported by rinohtype. You should also know that rinohtype doesn't support typesettings of maths as of yet.
I would be interested to learn how rinohtype handles your document. I welcome any feedback in the rinohtype issue tracker.
tk0miya commentedon Jun 11, 2020
Now Sphinx uses
ujreportwhenlatex_engine = 'uplatex'. May I close this issue?jfbu commentedon Jun 11, 2020
@tk0miya: +1 for closing as far as I can see.