# a patch to have a correct baseline when usetex=True

Hello,

AFAIK, current backends (I only tested agg, pdf, and ps) do not
properly respect the text baseline when text is rendered using TeX.
The get_text_width_height_descent() method in Agg and PS backends
simply return 0 for the descent value. While PDF backend uses the
dviread module to figure out correct descent values, there are cases
this does not work well (e.g. $\frac{1}{2}\pi$).

As an example, the attached figure shows the result for the Agg
backend. In all cases, the texts are placed at (0,0) with
baseline-alignment. Leftmost one is when usetex=False, which has a
correct baseline. The middle one is when usetex=True. It is bottom
aligned, which is not intended. The rightmost one is also when
usetex=True but after the patch I describe below.

First of all, I borrowed this idea from the PyX which is in GPL.
Although there is little of copying, other than the basic idea, I'm
not 100% sure if this could be BSD-compatible.

Anyhow, the idea is that you can have LateX to print out the width,
height, and descent (=depth) of a given text by enclosing the text in
a box. For example,

\newbox\MatplotlibBox%
\setbox\MatplotlibBox=\hbox{$\frac{1}{2}\pi$}%
\copy\MatplotlibBox
\immediate\write16{MatplotlibBox:\the\wd\MatplotlibBox,\the\ht\MatplotlibBox,\the\dp\MatplotlibBox}%

I define a newbox (called MatplotlibBox) which encloses
$\frac{1}{2}\pi$. And then print out the width, height and depth of
the box.

Attached is a patch of a texmanager.py which utilize above method to
figure out the dimension of the text. The template string to generate
a ".tex" file is slightly modified. After latex is run, the
dimensional information of the text is extracted and saved in
".baseline" file and get_text_width_height_descent() method is added
under the TexManager class, which reads in the ".baseline" file and
return its content. (you need to empty out the tex.cache directory for
this work correctly).

A backend can simply call the get_text_width_height_descent() of
texmanager (a simple patch for the Agg backend is attached). I also
tested this with PS and PDF backends and they worked out fine.

So if the license issue is okay, I wonder if this patch can be
reviewed and applied (after any necessary modifications) to improve
the baseline handling in matploltib.

Regards,

-JJ

baseline_texmanager.diff (2.51 KB)

baseline_backend_agg.diff (1.02 KB)

I think it is fine to borrow the idea; what we need to do is a clean
room implementation with no copying. You can best answer that, so if
you tell us your patch is cleanly implemented, we can accept it.

JDH

···

On Thu, Aug 28, 2008 at 2:57 PM, Jae-Joon Lee <lee.j.joon@...149...> wrote:

First of all, I borrowed this idea from the PyX which is in GPL.
Although there is little of copying, other than the basic idea, I'm
not 100% sure if this could be BSD-compatible.

Thanks for the response.

Well, the only part I borrowed from PyX is TeX related commands they
use (there is not much of implementation as far as TeX-related code is
concerned). From their code, I learned the meaning and usage of the
following TeX commands

\newbox
\setbox
\immediate\write16

And I used the same TeX commands in my code.
But I personally think this is not a (code) copy.

Other than this, the code is clean.
Regards,

-JJ

···

On Thu, Aug 28, 2008 at 4:18 PM, John Hunter <jdh2358@...149...> wrote:

On Thu, Aug 28, 2008 at 2:57 PM, Jae-Joon Lee <lee.j.joon@...149...> wrote:

First of all, I borrowed this idea from the PyX which is in GPL.
Although there is little of copying, other than the basic idea, I'm
not 100% sure if this could be BSD-compatible.

I think it is fine to borrow the idea; what we need to do is a clean
room implementation with no copying. You can best answer that, so if
you tell us your patch is cleanly implemented, we can accept it.

JDH

Sphinx contains one way to do this in its new "pngmath" extension. It uses the LaTeX package "preview" which does all of this magic internally. And I believe it's a little more general. If I recall, the approach you're taking won't work with some LaTeX constructs such as:

\begin{align}
x & = 2
y & = 2
\end{align}

Plus, Sphinx is BSD-licensed, so it should be fine to copy-and-paste whatever code is necessary.

Of course, latex-preview is required to be installed, but I think it's a pretty common package.

See here:

Cheers,
Mike

Jae-Joon Lee wrote:

···

On Thu, Aug 28, 2008 at 4:18 PM, John Hunter <jdh2358@...149...> wrote:

On Thu, Aug 28, 2008 at 2:57 PM, Jae-Joon Lee <lee.j.joon@...149...> wrote:

First of all, I borrowed this idea from the PyX which is in GPL.
Although there is little of copying, other than the basic idea, I'm
not 100% sure if this could be BSD-compatible.

I think it is fine to borrow the idea; what we need to do is a clean
room implementation with no copying. You can best answer that, so if
you tell us your patch is cleanly implemented, we can accept it.

JDH

Thanks for the response.

Well, the only part I borrowed from PyX is TeX related commands they
use (there is not much of implementation as far as TeX-related code is
concerned). From their code, I learned the meaning and usage of the
following TeX commands

\newbox
\setbox
\immediate\write16

And I used the same TeX commands in my code.
But I personally think this is not a (code) copy.

Other than this, the code is clean.
Regards,

-JJ

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Matplotlib-devel mailing list
Matplotlib-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-devel

Thanks,

I quickly went through the code of the pngmath.py, and it seems that
the depth(descent) of the dvi file is reported by "dvipng" (but the
preview package must be used in the tex file for this to work
correctly). Therefore, with this method, we need to run dvipng even if
we use ps of pdf backend. Although this seems fine to me, I'll see if
I can extract the depth of the text without running the dvipng.

Regards,

-JJ

···

On Fri, Aug 29, 2008 at 7:59 AM, Michael Droettboom <mdroe@...31...> wrote:

Sphinx contains one way to do this in its new "pngmath" extension. It uses
the LaTeX package "preview" which does all of this magic internally. And I
believe it's a little more general. If I recall, the approach you're taking
won't work with some LaTeX constructs such as:

\begin{align}
x & = 2
y & = 2
\end{align}

Plus, Sphinx is BSD-licensed, so it should be fine to copy-and-paste
whatever code is necessary.

Of course, latex-preview is required to be installed, but I think it's a
pretty common package.

See here:

http://svn.python.org/projects/doctools/trunk/sphinx/ext/pngmath.py

Cheers,
Mike

Jae-Joon Lee wrote:

On Thu, Aug 28, 2008 at 4:18 PM, John Hunter <jdh2358@...149...> wrote:

On Thu, Aug 28, 2008 at 2:57 PM, Jae-Joon Lee <lee.j.joon@...149...> >>> wrote:

First of all, I borrowed this idea from the PyX which is in GPL.
Although there is little of copying, other than the basic idea, I'm
not 100% sure if this could be BSD-compatible.

I think it is fine to borrow the idea; what we need to do is a clean
room implementation with no copying. You can best answer that, so if
you tell us your patch is cleanly implemented, we can accept it.

JDH

Thanks for the response.

Well, the only part I borrowed from PyX is TeX related commands they
use (there is not much of implementation as far as TeX-related code is
concerned). From their code, I learned the meaning and usage of the
following TeX commands

\newbox
\setbox
\immediate\write16

And I used the same TeX commands in my code.
But I personally think this is not a (code) copy.

Other than this, the code is clean.
Regards,

-JJ

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's
challenge
Build the coolest Linux based applications with Moblin SDK & win great
prizes
Grand prize is a trip for two to an Open Source event anywhere in the
world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Matplotlib-devel mailing list
Matplotlib-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-devel

Here is a patch which uses a preview package. It uses a "showbox"
option in the preview package, with a slight tweak (this only patches
the texmanager.py. You still need to apply the agg backend patch in my
previous post). It would be good if this patch will be accepted, but
the extra requirement of the preview package may need some dicussion.
Although it seems that the preview package is commonly found with a
TeX installation, I guess it is not part of the major TeX distribution
(e.g. tetex, tex-live) yet. One way would be to make it as an optional
feature.

Regards,

-JJ

texmanager_with_preview.diff (3.13 KB)

···

On Fri, Aug 29, 2008 at 12:04 PM, Jae-Joon Lee <lee.j.joon@...149...> wrote:

Thanks,

I quickly went through the code of the pngmath.py, and it seems that
the depth(descent) of the dvi file is reported by "dvipng" (but the
preview package must be used in the tex file for this to work
correctly). Therefore, with this method, we need to run dvipng even if
we use ps of pdf backend. Although this seems fine to me, I'll see if
I can extract the depth of the text without running the dvipng.

Regards,

-JJ

On Fri, Aug 29, 2008 at 7:59 AM, Michael Droettboom <mdroe@...31...> wrote:

Sphinx contains one way to do this in its new "pngmath" extension. It uses
the LaTeX package "preview" which does all of this magic internally. And I
believe it's a little more general. If I recall, the approach you're taking
won't work with some LaTeX constructs such as:

\begin{align}
x & = 2
y & = 2
\end{align}

Plus, Sphinx is BSD-licensed, so it should be fine to copy-and-paste
whatever code is necessary.

Of course, latex-preview is required to be installed, but I think it's a
pretty common package.

See here:

http://svn.python.org/projects/doctools/trunk/sphinx/ext/pngmath.py

Cheers,
Mike

Jae-Joon Lee wrote:

On Thu, Aug 28, 2008 at 4:18 PM, John Hunter <jdh2358@...149...> wrote:

On Thu, Aug 28, 2008 at 2:57 PM, Jae-Joon Lee <lee.j.joon@...149...> >>>> wrote:

First of all, I borrowed this idea from the PyX which is in GPL.
Although there is little of copying, other than the basic idea, I'm
not 100% sure if this could be BSD-compatible.

I think it is fine to borrow the idea; what we need to do is a clean
room implementation with no copying. You can best answer that, so if
you tell us your patch is cleanly implemented, we can accept it.

JDH

Thanks for the response.

Well, the only part I borrowed from PyX is TeX related commands they
use (there is not much of implementation as far as TeX-related code is
concerned). From their code, I learned the meaning and usage of the
following TeX commands

\newbox
\setbox
\immediate\write16

And I used the same TeX commands in my code.
But I personally think this is not a (code) copy.

Other than this, the code is clean.
Regards,

-JJ

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's
challenge
Build the coolest Linux based applications with Moblin SDK & win great
prizes
Grand prize is a trip for two to an Open Source event anywhere in the
world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Matplotlib-devel mailing list
Matplotlib-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-devel

"Jae-Joon Lee" <lee.j.joon@...149...> writes:

Here is a patch which uses a preview package. It uses a "showbox"
option in the preview package, with a slight tweak (this only patches
the texmanager.py. You still need to apply the agg backend patch in my
previous post). It would be good if this patch will be accepted, but
the extra requirement of the preview package may need some dicussion.
Although it seems that the preview package is commonly found with a
TeX installation, I guess it is not part of the major TeX distribution
(e.g. tetex, tex-live) yet. One way would be to make it as an optional
feature.

FWIW, Debian provides preview.sty in the binary package
preview-latex-style (generated from the source package auctex).

Chris

···

On Fri, Aug 29, 2008 at 12:04 PM, Jae-Joon Lee <lee.j.joon@...149...> wrote:
> Thanks,
>
> I quickly went through the code of the pngmath.py, and it seems that
> the depth(descent) of the dvi file is reported by "dvipng" (but the
> preview package must be used in the tex file for this to work
> correctly). Therefore, with this method, we need to run dvipng even if
> we use ps of pdf backend. Although this seems fine to me, I'll see if
> I can extract the depth of the text without running the dvipng.
>
> Regards,
>
> -JJ
>
>
>
>
> On Fri, Aug 29, 2008 at 7:59 AM, Michael Droettboom <mdroe@...31...> wrote:
>> Sphinx contains one way to do this in its new "pngmath" extension. It uses
>> the LaTeX package "preview" which does all of this magic internally. And I
>> believe it's a little more general. If I recall, the approach you're taking
>> won't work with some LaTeX constructs such as:
>>
>> \begin{align}
>> x & = 2
>> y & = 2
>> \end{align}
>>
>> Plus, Sphinx is BSD-licensed, so it should be fine to copy-and-paste
>> whatever code is necessary.
>>
>> Of course, latex-preview is required to be installed, but I think it's a
>> pretty common package.
>>
>> See here:
>>
>> http://svn.python.org/projects/doctools/trunk/sphinx/ext/pngmath.py
>>
>> Cheers,
>> Mike
>>
>> Jae-Joon Lee wrote:
>>>
>>> On Thu, Aug 28, 2008 at 4:18 PM, John Hunter <jdh2358@...149...> wrote:
>>>
>>>>
>>>> On Thu, Aug 28, 2008 at 2:57 PM, Jae-Joon Lee <lee.j.joon@...149...> > >>>> wrote:
>>>>
>>>>
>>>>>
>>>>> First of all, I borrowed this idea from the PyX which is in GPL.
>>>>> Although there is little of copying, other than the basic idea, I'm
>>>>> not 100% sure if this could be BSD-compatible.
>>>>>
>>>>
>>>> I think it is fine to borrow the idea; what we need to do is a clean
>>>> room implementation with no copying. You can best answer that, so if
>>>> you tell us your patch is cleanly implemented, we can accept it.
>>>>
>>>> JDH
>>>>
>>>>
>>>
>>> Thanks for the response.
>>>
>>> Well, the only part I borrowed from PyX is TeX related commands they
>>> use (there is not much of implementation as far as TeX-related code is
>>> concerned). From their code, I learned the meaning and usage of the
>>> following TeX commands
>>>
>>> \newbox
>>> \setbox
>>> \immediate\write16
>>>
>>> And I used the same TeX commands in my code.
>>> But I personally think this is not a (code) copy.
>>>
>>> Other than this, the code is clean.
>>> Regards,
>>>
>>> -JJ
>>>
>>> -------------------------------------------------------------------------
>>> This SF.Net email is sponsored by the Moblin Your Move Developer's
>>> challenge
>>> Build the coolest Linux based applications with Moblin SDK & win great
>>> prizes
>>> Grand prize is a trip for two to an Open Source event anywhere in the
>>> world
>>> http://moblin-contest.org/redirect.php?banner_id=100&url=/
>>> _______________________________________________
>>> Matplotlib-devel mailing list
>>> Matplotlib-devel@lists.sourceforge.net
>>> https://lists.sourceforge.net/lists/listinfo/matplotlib-devel
>>>
>>
>>
>

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/_______________________________________________
Matplotlib-devel mailing list
Matplotlib-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-devel