Latex equations and SVG export

Hi,

When I do

m.text(0,0,'$\sum_{n=1}^{100}$');m.axis('off');m.savefig('test.svg')

matplotlib renders it fine, but won't save it to svg correctly - the summation symbol doesn't show up.

Is this a configuration issue on my part, or is svg support for latex currently incomplete?

I'm on win vista with python 2.5

I'm so happy that pylab exports to svg, BTW. I'm using inkscape to annotate by graphs and then export to pdf, and this work flow is so nice!

thanks!
-Kaushik

kaushik.ghose wrote:

m.text(0,0,'\\sum\_\{n=1\}^\{100\}');m.axis('off');m.savefig('test.svg')

matplotlib renders it fine, but won't save it to svg correctly - the summation symbol doesn't show up.

Is this a configuration issue on my part, or is svg support for latex currently incomplete?

AFAIK, the latex support for the SVG backend isn't fully working yet. For rcParams['text.usetex']=True you'll get an error. With rcParams['text.usetex']=False I get the same issue that you see (Sum symbol missing when I open the .svg in Inkscape).

See also [Matplotlib-users] usetex with svg or pdf (was: SVG vs PNG)

···

--
cheers,
steve

Random number generation is the art of producing pure gibberish as quickly as possible.

Hi kaushik,

Hi,

When I do

m.text(0,0,'\\sum\_\{n=1\}^\{100\}');m.axis('off');m.savefig('test.svg')

Shouldn't that be:
r'\\sum\_\{n=1\}^\{100\}' # i.e. a "raw" string.

or:

'\\\\sum\_\{n=1\}^\{100\}' # Escaped backslash

Does this help?

Cheers,
Edin

···

On 7/6/07, kaushik.ghose <kaushik.ghose@...287...> wrote:

Hi Edin,

Edin Salkovic wrote:

m.text(0,0,'\\sum\_\{n=1\}^\{100\}');m.axis('off');m.savefig('test.svg')

Shouldn't that be:
r'\\sum\_\{n=1\}^\{100\}' # i.e. a "raw" string.

or:

'\\\\sum\_\{n=1\}^\{100\}' # Escaped backslash

Actually the original string renders as desired. Its the svg save that causes the problem.

I gather this is due to incomplete implementation of svg export.

thanks!
-Kaushik

Edin Salkovic wrote:

Hi kaushik,

Hi,

When I do

m.text(0,0,'\\sum\_\{n=1\}^\{100\}');m.axis('off');m.savefig('test.svg')

Shouldn't that be:
r'\\sum\_\{n=1\}^\{100\}' # i.e. a "raw" string.

or:

'\\\\sum\_\{n=1\}^\{100\}' # Escaped backslash

From my experience, with usetex True and without r'', the TeX string is parsed

correctly even for stuff like text(0,0,'\\sum\_\{n=1\}^\{100\} with \\Omega'), i.e.
mathmode and textmode combined.

Here, indeed, with usetex False, using r'' produces the \sum sign in the .svg but
"n=1" and "100" are not alinged correctly (they both appear above \sum). With usetex
True there is the NotImplementedError (mpl 0.90.0dev3131).

···

On 7/6/07, kaushik.ghose <kaushik.ghose@...287...> wrote:

--
cheers,
steve

I love deadlines. I like the whooshing sound they make as they fly by. -- Douglas Adams

Sorry, I see the problem now. Unfortunately I wont have the resources
to investigate into this this weekend. It's past midnight here, and
tomorrow early I have to travel.

Best,
Edin

···

On 7/6/07, Kaushik Ghose <kghose@...1674...> wrote:

Hi Edin,

Edin Salkovic wrote:
>>
>> m.text(0,0,'\\sum\_\{n=1\}^\{100\}');m.axis('off');m.savefig('test.svg')
>
> Shouldn't that be:
> r'\\sum\_\{n=1\}^\{100\}' # i.e. a "raw" string.
>
> or:
>
> '\\\\sum\_\{n=1\}^\{100\}' # Escaped backslash
>

Actually the original string renders as desired. Its the svg save that
causes the problem.

I gather this is due to incomplete implementation of svg export.

thanks!
-Kaushik

> Hi Edin,

(...)

> Actually the original string renders as desired. Its the svg save that
> causes the problem.
>
> I gather this is due to incomplete implementation of svg export.
>
> thanks!
> -Kaushik

Sorry, I see the problem now. Unfortunately I wont have the resources
to investigate into this this weekend. It's past midnight here, and
tomorrow early I have to travel.

Best,
Edin

I looked into this. You just have to install (copy) the BaKoMa fonts
(TrueType version of the Computer Modern fonts) into your system's
font dir, so the svg viewer can see them. These fonts are located in
the "matplotlib/mpl-data/fonts/ttf" dir (the cm*.ttf files).

HTH,
Edin

···

On 7/7/07, Edin Salkovic <edin.salkovic@...287...> wrote:

On 7/6/07, Kaushik Ghose <kghose@...1674...> wrote:

> I looked into this. You just have to install (copy) the BaKoMa fonts
> (TrueType version of the Computer Modern fonts) into your system's
> font dir, so the svg viewer can see them. These fonts are located in
> the "matplotlib/mpl-data/fonts/ttf" dir (the cm*.ttf files).
>

Thanks Edin! Yes, that worked! There is still an issue with sub/super scripts though. They are inverted i.e. superscripts become subscripts. I'm attaching an example, created using

m.text(0 ,0 ,'\\sum\_\{n=1\}^\{100\} = 99^5 \* q\_i \+ \\delta');m.axis('off');m.savefig('eraseme.svg')

thanks!
-Kaushik

export.png

Hey Edin, if you get a minute, could you contribute a patch against
the faq in htdocs/faq.html.template explaining how to install the
bakoma fonts for svg viewers? Is this sufficiently difficult that it
merits a FAQ?

JDH

···

On 7/10/07, Kaushik Ghose <kghose@...1674...> wrote:

>
> I looked into this. You just have to install (copy) the BaKoMa fonts
> (TrueType version of the Computer Modern fonts) into your system's
> font dir, so the svg viewer can see them. These fonts are located in
> the "matplotlib/mpl-data/fonts/ttf" dir (the cm*.ttf files).
>

Thanks Edin! Yes, that worked! There is still an issue with sub/super
scripts though. They are inverted i.e. superscripts become subscripts.
I'm attaching an example, created using

Thanks Edin! Yes, that worked!

You're welcome!

There is still an issue with sub/super
scripts though. They are inverted i.e. superscripts become subscripts.
I'm attaching an example, created using

m.text(0 ,0 ,'\\sum\_\{n=1\}^\{100\} = 99^5 \* q\_i \+ \\delta');m.axis('off');m.savefig('eraseme.svg')

Hmm. I'm not sure whether this is a problem with matplotlib or
inkscape. I had some weird problems with inkscape on Ubuntu Feisty
recently, like: when I open the file from Nautilus (right click->open
with Inkscape) it is completely turned around; when I open Inkscape
(blank file), and open the file from within inkscape everything is
displayed correctly.

Have you opened the file in other viewers?

Is anyone else having the same problem?

Cheers,
Edin

···

On 7/10/07, Kaushik Ghose <kghose@...1674...> wrote:

Hi Edin,

Hmm. I'm not sure whether this is a problem with matplotlib or
inkscape. I had some weird problems with inkscape on Ubuntu Feisty
recently, like: when I open the file from Nautilus (right click->open
with Inkscape) it is completely turned around; when I open Inkscape
(blank file), and open the file from within inkscape everything is
displayed correctly.

Have you opened the file in other viewers?

Good point. I tried just now on Firefox and got the same error.
For an image of what I mean see this http://python-pieces.blogspot.com/2007/07/latex-and-svg-export.html

I'm attaching the svg output here in case someone wants to try it out:

<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
   "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd&quot;&gt;
<!-- Created with matplotlib (http://matplotlib.sourceforge.net/) -->
<svg width="586" height="442" viewBox="0 0 586 442"
    xmlns="http://www.w3.org/2000/svg&quot;
    xmlns:xlink="http://www.w3.org/1999/xlink&quot;
    version="1.1"
    id="svg1">
<g id="figure1">
<polygon style="fill: #ffffff; stroke: #ffffff; stroke-width: 1.000000; stroke-linejoin: miter; stroke-linecap: square; opacity: 1.000000" points = "0.000000,442.800000 0.000000,0.000000 586.800000,0.000000 586.800000,442.800000"/>
<g id="axes1">
<g id="mathtext1">
<text style="font-size: 12.000000; font-family: cmr10; fill: #000000;" x="88.182031" y="358.020000" >1</text>
<text style="font-size: 12.000000; font-family: cmr10; fill: #000000;" x="78.859766" y="358.020000" >=</text>
<text style="font-size: 12.000000; font-family: cmmi10; fill: #000000;" x="71.658594" y="358.020000" >n</text>
<text style="font-size: 8.400000; font-family: cmmi10; fill: #000000;" x="127.162500" y="370.020000" >i</text>
<text style="font-size: 12.000000; font-family: cmmi10; fill: #000000;" x="139.373148" y="373.020000" >±</text>
<text style="font-size: 12.000000; font-family: cmr10; fill: #000000;" x="130.050882" y="373.020000" >+</text>
<text style="font-size: 12.000000; font-family: cmmi10; fill: #000000;" x="122.115625" y="373.020000" >q</text>
<text style="font-size: 12.000000; font-family: cmsy10; fill: #000000;" x="116.115625" y="373.020000" >¤</text>
<text style="font-size: 8.400000; font-family: cmr10; fill: #000000;" x="111.920313" y="379.020000" >5</text>
<text style="font-size: 12.000000; font-family: cmr10; fill: #000000;" x="105.998438" y="373.020000" >9</text>
<text style="font-size: 12.000000; font-family: cmr10; fill: #000000;" x="99.998438" y="373.020000" >9</text>
<text style="font-size: 12.000000; font-family: cmr10; fill: #000000;" x="90.676172" y="373.020000" >=</text>
<text style="font-size: 12.000000; font-family: cmr10; fill: #000000;" x="85.935938" y="388.020000" >0</text>
<text style="font-size: 12.000000; font-family: cmr10; fill: #000000;" x="79.935938" y="388.020000" >0</text>
<text style="font-size: 12.000000; font-family: cmr10; fill: #000000;" x="73.935938" y="388.020000" >1</text>
<text style="font-size: 12.000000; font-family: cmex10; fill: #000000;" x="73.350000" y="386.020000" >X</text>
</g>
</svg>

(...)

This should be fixed in SVN (r3346 or r3353) --- Norbert Nemec
recently fixed the mathtext support in the SVG backend.

Best,
Edin

···

On 7/10/07, Kaushik Ghose <kghose@...1674...> wrote:

Hi Edin,
Good point. I tried just now on Firefox and got the same error.
For an image of what I mean see this
http://python-pieces.blogspot.com/2007/07/latex-and-svg-export.html

I'm attaching the svg output here in case someone wants to try it out:

It sure is easy to forget :slight_smile:

Attached is the patch to the FAQ. I don't have the htdocs dir checked
out. John, can you apply the patch?

Any comments?

Best,
Edin

faq.html.template.patch (760 Bytes)

faq.html.template.new (43.7 KB)

···

On 7/10/07, John Hunter <jdh2358@...287...> wrote:

Hey Edin, if you get a minute, could you contribute a patch against
the faq in htdocs/faq.html.template explaining how to install the
bakoma fonts for svg viewers? Is this sufficiently difficult that it
merits a FAQ?

Thanks to Michael Droettboom fonts can now be embedded in the SVG file.

See:
http://www.nabble.com/Embedded-fonts-in-SVG-t4057222.html
http://comments.gmane.org/gmane.comp.python.matplotlib.devel/2944

I also updated the FAQ.

Index: faq.html.template

···

On 7/10/07, Edin Salkovic <edin.salkovic@...287...> wrote:

On 7/10/07, John Hunter <jdh2358@...287...> wrote:
> Hey Edin, if you get a minute, could you contribute a patch against
> the faq in htdocs/faq.html.template explaining how to install the
> bakoma fonts for svg viewers? Is this sufficiently difficult that it
> merits a FAQ?

It sure is easy to forget :slight_smile:

Attached is the patch to the FAQ. I don't have the htdocs dir checked
out. John, can you apply the patch?

===================================================================
--- faq.html.template (revision 3500)
+++ faq.html.template (working copy)
@@ -977,6 +977,23 @@
docs</a> for details.
""" ),

+ ('SVG-BAKOMA-FONTS-MATHTEXT',
+ "My SVG viewer doesn't properly display special (TeX) characters
+ ('\sum', '\infty',etc.) generated by mathtext. Is this a bug?",
+ """
+You need to install the BaKoMa fonts (TrueType version of the Computer
+Modern fonts) on your system. The BaKoMa fonts come packaged with
+matplotlib. They are located in the
+"site-packages/matplotlib/mpl-data/fonts/ttf" dir (the cm*.ttf files).
+Installation of the fonts varies from OS to OS.
+
+Alternatively, as of r3498 (matplotlib > 0.90.1) you can set
+svg.embed_char_paths to True in your matplotlibrc file. This
+setting makes matplotlib embed all fonts as paths in the SVG file,
+which results in a "portable" SVG file, at the cost of bigger file
+size and uneditable text.
+""" ),
+
       ('LEAKS',
        'matplotlib appears to be leaking memory, what should I do?',
"""\