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

comment on http://matplotlib.org/users/pgf.html#pgf-tutorial - minor issue with xits font #1500

Closed
fmitha opened this issue Nov 15, 2012 · 15 comments
Assignees

Comments

@fmitha
Copy link

fmitha commented Nov 15, 2012

I have a comment on http://matplotlib.org/users/pgf.html#pgf-tutorial,
which has a minor issue with fonts. Specifically, the example below does
not work with my machine - Debian squeeze, Tex Live 2012 backported from
unstable, matplotlib 1.2 backported from unstable.

  1. The documentation says

Rc parameters that control the behavior of the pgf backend:
[...]
pgf.texsystem Either “xelatex”, “lualatex” or “pdflatex”

It is worth mentioning the default is (it seems) "xelatex".

  1. It seems the default is "xelatex", but the specification
\setmathfont{XITS Math}

does not work with xelatex, at least for me, and possibly not for anyone
using Tex Live. See for example

http://tex.stackexchange.com/questions/50381/xelatex-and-unicode-math-problems
and
http://tex.stackexchange.com/questions/82113/subscript-is-set-too-low-in-math-mode-with-xits-math

It seems xelatex expects something like

\setmathfont{xits-math.otf}   

I'm not sure why. In any case, I had to make a change to make this example
work. First, I have the original example, and then my changed version.

HERE IS THE ORIGINAL EXAMPLE. SUGGESTED CHANGES FOLLOW.

# -*- coding: utf-8 -*-

import matplotlib as mpl
mpl.use("pgf")
pgf_with_custom_preamble = {
       "font.family": "serif", # use serif/main font for text elements
       "text.usetex": True,    # use inline math for ticks
       "pgf.rcfonts": False,   # don't setup fonts from rc parameters
       "pgf.preamble": [
              r"\usepackage{units}",        # load additional packages
              r"\usepackage{metalogo}",        # load additional packages
              r"\usepackage{unicode-math}", # unicode math setup
              r"\setmathfont{XITS Math}",
              r"\setmainfont{DejaVu Serif}", # font setup via preamble
              ]
}
mpl.rcParams.update(pgf_with_custom_preamble)

import matplotlib.pyplot as plt
plt.figure(figsize=(4.5,2.5))
plt.plot(range(5))
plt.xlabel(u"unicode text: я, ψ, €, ü, \\unitfrac[10]{°}{µm}")
plt.ylabel(u"\\XeLaTeX")
plt.legend([u"unicode math: $λ=∑_i^∞ μ_i^2$"])
plt.tight_layout(.5)

Option 1:

Add the line

"pgf.texsystem": "lualatex"

to the pgf_with_custom_preamble dict. I.e. switch to lualatex. Or

Option 2:

Replace

r"\setmainfont{XITS}",
r"\setmathfont{XITS Math}",

with

r"\setmainfont{xits-math.otf}",

           Regards, Faheem
@pwuertz
Copy link
Contributor

pwuertz commented Nov 15, 2012

I think the problem with XITS Math is that the debian package doesn't place the font in a folder where it can be found by the OS. One of the ideas behind Xe/Lua-Latex is to use the opentype fonts known to the OS as any other application does. I assume when opening a font-selection dialog of your choice (say inkscape), you won't find XITS or XITS Math there. You can get a list of non-bitmap fonts available to XeTeX via fontconfig
fc-list :outline -f "%{family}\n",
as suggested in http://tex.stackexchange.com/questions/12881/how-to-get-a-list-of-all-available-ttf-fonts-with-xetex . What I did to solve this is to download XITS Math or LM Math (http://oldwww.gust.org.pl/projects/e-foundry/lm-math for computer modern fontface) and install them in ~/.fonts. The fonts now pop up system-wide and \setmathfont{XITS Math} works as expected.

It appears to be a matter of using the fonts from the latex-distribution versus using the fonts from the system. I find this very confusing since I had the feeling that the majority of the unicode-math posts and introductions I found used XITS Math instead of xits-math.otf, but TexLive seems to be in favor of placing the fonts where only latex could find them. This might be different for other Tex distributions which is probably why you'll find different definitions throughout the net. I don't know which method to prefer..

@ghost ghost assigned pwuertz Nov 15, 2012
@pwuertz
Copy link
Contributor

pwuertz commented Nov 15, 2012

Ah, since you brought up lualatex.. Do you see any difference in the behaviour of xelatex and lualatex concerning the use of "XITS Math"? Does lualatex recognize the font by its name in contrast to xelatex?

@fmitha
Copy link
Author

fmitha commented Nov 15, 2012

Hi Peter,

On Thu, 15 Nov 2012, Peter Würtz wrote:

Ah, since you brought up lualatex.. Do you see any difference in the
behaviour of xelatex and lualatex concerning the use of "XITS Math"?
Does lualatex recognize the font by its name in contrast to xelatex?

Yes, lualatex works with your example with no changes. As I mentioned, you
just need to switch to

"pgf.texsystem": "lualatex",

to get the backend to use lualatex. The problem is with xelatex. I hope
I'm being clear.

                                                     Regards, Faheem

@fmitha
Copy link
Author

fmitha commented Nov 15, 2012

Hi Peter,

On Thu, 15 Nov 2012, Peter Würtz wrote:

I think the problem with XITS Math is that the debian package doesn't
place the font in a folder where it can be found by the OS. One of the
ideas behind Xe/Lua-Latex is to use the opentype fonts known to the OS
as any other application does. I assume when opening a font-selection
dialog of your choice (say inkscape), you won't find XITS or XITS Math
there. You can get a list of non-bitmap fonts available to XeTeX via
fontconfig fc-list :outline -f "%{family}\n", as suggested in
http://tex.stackexchange.com/questions/12881/how-to-get-a-list-of-all-available-ttf-fonts-with-xetex
. What I did to solve this is to download XITS Math or LM Math
(http://oldwww.gust.org.pl/projects/e-foundry/lm-math for computer
modern fontface) and install them in ~/.fonts. The fonts now pop up
system-wide and \setmathfont{XITS Math} works as expected.

It appears to be a matter of using the fonts from the latex-distribution
versus using the fonts from the system. I find this very confusing since
I had the feeling that the majority of the unicode-math posts and
introductions I found used XITS Math instead of xits-math.otf, but
TexLive seems to be in favor of placing the fonts where only latex could
find them. This might be different for other Tex distributions which is
probably why you'll find different definitions throughout the net. I
don't know which method to prefer..

Thanks for replying. I know almost nothing about fonts myself (I'm just a
LaTeX user), but this issue is not Debian specific, since as you can see
other people not using the Debian packages have experienced it. It may,
however, be TeX Live specific. I don't know why lualatex and xelatex are
doing things differently here, but I don't think it is worth spending much
time on.

If I understand your posting correctly, then you are saying that TeX Live
puts XITS in a location where the system doesn't know about it. It is true
that

fc-list

does not give me XITS here either. BTW, the Debian fontconfig package here
does not have an executable called fontconfig, just

/usr/bin/fc-cat
/usr/bin/fc-scan
/usr/bin/fc-match
/usr/bin/fc-list
/usr/bin/fc-query
/usr/bin/fc-cache

This answer by Khaled Hosny XITS font not found on OS
X
seems relevant. It seems
this is by design, but does seem kinda wacky to me. In particular, the

XeTeX can't locate fonts in TeX tree by font name (LuaTeX based packages
does, though)

suggests that LuaTeX has capabilities in this area that XeTeX lacks.

I suggest you leave your example as it is, and just mention, like in a
note, that if the user is using xelatex and experiences difficulties, and
particularly if he/she is using TeX Live, then he/she could try replacing

 r"\setmainfont{XITS}",
 r"\setmathfont{XITS Math}",

with

 r"\setmainfont{xits-math.otf}",

or alternatively, use lualatex instead. Perhaps also point to this issue.
Does that seem reasonable?

If you want further clarification on this issue, you could do worse than
post a question on tex.stackexchange. The folks there are very friendly,
and include some real TeX experts.

Finally, thanks very much for your work on the PGF backend. I am a long
time Python user, but most of the documents I write are LaTeX, and I
haven't used matplotlib for a long time because the figures produced don't
integrate well with the text. I have used PGF/TikZ directly, or R with R's
TikZ driver, TikZDevice (https://github.com/Sharpie/RTikZDevice). This is
R's approximate equivalent of your work, I guess.

Anyway, when I noticed matplotlib had got a TikZ backend in 1.2 I decided
to try it, so backported 1.2 from unstable, and just today started with
running your examples, which is when I ran into this issue.

                                                       Regards, Faheem

@jenshnielsen
Copy link
Member

For me adding the fonts to /etc/fonts/local.conf solved the problems.

I added the following to that file:

<?xml version="1.0"?>
<!DOCTYPE fontconfig SYSTEM "fonts.dtd">
<fontconfig>
 <dir>/usr/local/texlive/2012/texmf-dist/fonts</dir>
</fontconfig>

and ran

sudo fc-cache -f -v

This solved the issue for me on Ubuntu 12.10 using a local texlive2012.

If you adapt this to the dir of your texlive fonts I guess that it should resolve the issue for you too.

@fmitha
Copy link
Author

fmitha commented Nov 16, 2012

On Thu, 15 Nov 2012, Jens H Nielsen wrote:

For me adding the fonts to /etc/fonts/local.conf solved the problems.

I added the following to that file:

/usr/local/texlive/2012/texmf-dist/fonts

and ran

sudo fc-cache -f -v

This solved the issue for me on Ubuntu 12.10 using a local texlive2012.

If you adapt this to the dir of your texlive fonts I guess that it
should resolve the issue for you too.

Hi Jens.

Thanks for your reply, but I don't want a local workaround for this issue.
In general I find local config changes to allow code to work are not very
useful, since then the code doesn't work for other people or on other
machines. I personally prefer to use e.g.

\setmathfont{xits-math.otf}

in this case if it is necessary.
Regards, Faheem

@jenshnielsen
Copy link
Member

I see your point but perhaps both types of workarounds should be added to the documentation. A third workaround
is of course to install the fonts locally one more time but I think that should be avoided. IMHO if you are using the Debian packed Texlive it is a bug in Debian that they don't index the Texlive otf fonts, but apparently the issue also exists on OS X (http://tex.stackexchange.com/questions/43642/xits-font-not-found-on-os-x). In my case I use a local Texlive installation since the Debian/Ubuntu one is more often than not outdated (I believe they went from Texlive 2009 to 2012 directly i.e. Ubuntu 12.04 still ships 2009). In this case it is expected that as a part of the installation I have to make sure the fonts are indexed my self.

Note that indexing the fonts have the additional advantage that they are available to other programs too. I.e. I can use the Texlive fonts directly in Inkscape.

@fmitha
Copy link
Author

fmitha commented Nov 16, 2012

Hi Jen,

On Thu, 15 Nov 2012, Jens H Nielsen wrote:

I see your point but perhaps both types of workarounds should be added
to the documentation.

Sure. I think it is a good idea to document different kinds of
workarounds. As long as they have been properly tested and are clearly
documented, it's all good. The goal is to reduce the frustration level for
users.

A third workaround is of course to install the fonts locally one more
time but I think that should be avoided. IMHO if you are using the
Debian packed Texlive it is a bug in Debian that they don't index the
Texlive otf fonts, but apparently the issue also exists on OS X
(http://tex.stackexchange.com/questions/43642/xits-font-not-found-on-os-x).

I think TeX Live does not make the fonts available to the system. It seems
(I have not asked the TeX Live project directly) that this is a feature,
not a bug. :-)

In my case I use a local Texlive installation since the Debian/Ubuntu
one is more often than not outdated (I believe they went from Texlive
2009 to 2012 directly i.e. Ubuntu 12.04 still ships 2009). In this case
it is expected that as a part of the installation I have to make sure
the fonts are indexed my self.

That is true. The reason is that Debian packaging is entirely volunteer,
and Ubuntu most of the time does not help, it just takes the packaging and
incorporates it as part of it's distribution. These days the packaging of
TeX Live is mostly the solo work of Norbert Preining, who doesn't get a
lot of time to work on it.

Note that indexing the fonts have the additional advantage that they are
available to other programs too. I.e. I can use the Texlive fonts
directly in Inkscape.

Good point.
Regards, Faheem

@pwuertz
Copy link
Contributor

pwuertz commented Nov 16, 2012

Although I personally prefer the idea of addressing fonts by their name instead of filenames and the option of using them in inkscape as well, the official unicode-math documentation also uses "xits-math.otf" instead of "XITS Math". This is probably due to the fact that xelatex is missing the feature of finding fonts in the tex tree by name. With lualatex supporting this feature, this convention might change in the (far?) future, but for now I think it's best to follow Faheem's proposal.

Thanks for pointing this out and saving users a lot of headaches ;).

@fmitha
Copy link
Author

fmitha commented Nov 16, 2012

Hi Peter,

On Fri, 16 Nov 2012, Peter Würtz wrote:

Although I personally prefer the idea of addressing fonts by their name
instead of filenames and the option of using them in inkscape as well,
the official unicode-math documentation also uses "xits-math.otf"
instead of "XITS Math". This is probably due to the fact that xelatex is
missing the feature of finding fonts in the tex tree by name. With
lualatex supporting this feature, this convention might change in the
(far?) future, but for now I think it's best to follow Faheem's
proposal.

Thanks for pointing this out and saving users a lot of headaches ;).

You're welcome. Thanks for the changes. They look good.

                                    Regards, Faheem

@dmcdougall
Copy link
Member

@pwuertz @fmitha Has this issue been resolved?

@pwuertz
Copy link
Contributor

pwuertz commented Nov 26, 2012

Ah, thanks for reminding! Closing the issue.

@pwuertz pwuertz closed this as completed Nov 26, 2012
@TheChymera
Copy link

hi guys, what do I have to add to the code you pasted here (and which is also available on the official matplotlib docs to actually save or show my plot?

@pwuertz
Copy link
Contributor

pwuertz commented Oct 21, 2013

Just plt.savefig("figure.pdf") for precompiled or plt.savefig("figure.pgf") for raw tex code.

@TheChymera
Copy link

thank you :) though I did find that out in the mean time. If you would forgive the OT - I am still having trouble with tight_layout and pgf. You suggested a fix here - but that didn't work (as I detailed in my comment). Could you help me out? (running matplotlib-1.3.0).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants