jupyter / nbconvert

Jupyter Notebook Conversion
https://nbconvert.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
1.71k stars 563 forks source link

Latex base template: remove \usepackage[utf8x]{inputenc} #530

Open alcrene opened 7 years ago

alcrene commented 7 years ago

Since the LaTeX exporter uses XeTeX now, it's recommended not to use the inputenc package, but fontspec instead: http://tex.stackexchange.com/a/3000.

Similarly, the unicode support provided by \usepackage[mathletters]{ucs} should already be provided by fontspec, so that line could also be deleted. The ucs package in particular is known to cause a number of compatibility problems.

I can do the changes myself, but I'm not sure what to test against to ensure backwards compatibility.

mpacer commented 7 years ago

I want to remove both of these, and ideally including mathspec rather than fontspec. See #537 for a more comprehensive PR trying to improve our unicode support.

Developing tests for LaTeX templates is an ongoing challenge.

jochym commented 7 years ago

As of now the unicode in pdf converter is broken. A lot of characters are simply missing in the output. This is caused by xelatex back-end. If you convert to latex and run pdflatex all is fine. I can provide simple example if needed.

alcrene commented 7 years ago

Normally xelatex provides much better character support than pdflatex, but you may be seeing issues because the current preamble is still using a few pdflatex packages (the ones I mentioned in this issue).

Perhaps as a warning to future readers: the workaround of using pdflatex is likely to break at some point in the future, when the latex template is updated with xetex-only replacements packages. At that point though, it should be possible to compile directly to a pdf, or convert to latex and run xelatex.

jochym commented 7 years ago

The problem is the template is not coordinated with the nbconvert setup - and this is sub-optimal. Xetex is better as a back-and maybe we can make quick update to latex templates to fix this problem? It is slightly annoying to have half of your name removed ... (my co-author)

mpacer commented 7 years ago

Yes, I've been intending to get back to this, but I was having trouble reproducing any (marginally) successful solutions across systems (especially on travis for testing.

This seems to be (in part) related to font support: latin modern is better than computer modern for Unicode purposes, but it's still pretty impoverished (e.g., doesn't seem to have greek characters).

If you want to see the earlier efforts: https://github.com/jupyter/nbconvert/pull/537

But I'm extremely sorry that that is happening to your co-author. My hope is to have a solution soon.

alcrene commented 7 years ago

As an immediate workaround, you can derive the latex template locally and change the packages to something that works on your system. I use something like the following:

((*- extends 'article.tplx' -*))
[unrelated template changes]
((* block packages *))
    % Hide [utf8x]{inputenc} as it should not be used with xetex.
    % Also hide ucs which conflicts with a bunch of stuff.
    % http://tex.stackexchange.com/a/39418
    \makeatletter
    \newcommand{\dontusepackage}[2][]{%
      \@namedef{ver@#2.sty}{9999/12/31}%
      \@namedef{opt@#2.sty}{#1}}
    \makeatother
    \dontusepackage[utf8x]{inputenc}
    \dontusepackage[mathletters]{ucs}
((( super() )))
    \usepackage{unicode-math}
    \usepackage{fontspec}
    \usepackage[Latin,Greek]{ucharclasses}
    \newfontfamily\substitutefont{CMU Serif}
    \setTransitionsForGreek{\begingroup\substitutefont}{\endgroup}
((* endblock packages *))

Save that as mylatex.tplx, put it in the same directory as your notebook (or your global nbconvert templates directory), and export the notebook with

jupyter nbconvert --to pdf [notebook.ipynb] --template=mylatex.tplx

A few caveats:

  1. As mspacer has said, finding a solution that works across systems is difficult, so you may have to tweak this a bit. It works for me on Ubuntu and openSUSE.
  2. I wrote some of that from memory, so there may be typos.
jochym commented 7 years ago

Thanks. That i what I plan to do. The code above will help. But still including thin in studenta materials is sub-optimal from my POV. Anyway - thanks for help. I hope a permanent solution is not far off.