Opened 10 years ago

Last modified 6 years ago

#13232 new enhancement

LaTeX export for plots

Reported by: nthiery Owned by: jason, was
Priority: major Milestone: sage-7.3
Component: graphics Keywords: latex, plot
Cc: ddrake, jhpalmieri, kcrisman, bertheville.chloe@…, thomas.ayoub@…, arthur.mauvezin@…, tscrim Merged in:
Authors: Thomas Ayoub, Bertheville Chloé, Arthur Mauvezin Reviewers:
Report Upstream: N/A Work issues:
Branch: Commit:
Dependencies: Stopgaps:

Status badges

Description

Implement latex methods for plots to produce latex/tikz pictures.

Attachments (4)

trac_13232-plot_latex-nt.patch (17.0 KB) - added by nthiery 10 years ago.
alcove.png (68.6 KB) - added by nthiery 10 years ago.
A sage plot
alcove.tex (17.2 KB) - added by nthiery 10 years ago.
The TeX produced from it (with a little manual tweak)
alcove.pdf (10.1 KB) - added by nthiery 10 years ago.
The pdf obtained by compiling the TeX

Download all attachments as: .zip

Change History (20)

Changed 10 years ago by nthiery

comment:1 Changed 10 years ago by nthiery

  • Cc ddrake added

First draft written by three students of mine. More details to come after dinner :-)

comment:2 Changed 10 years ago by jhpalmieri

  • Cc jhpalmieri added

comment:3 Changed 10 years ago by kcrisman

  • Cc kcrisman added

Oh wow, this would be cool. Now I don't know which would look better - SageTeX with sageplot environment or this?

comment:4 Changed 10 years ago by nthiery

  • Authors set to Thomas Ayoub, Bertheville Chloé, Arthur Mauvezin

comment:5 Changed 10 years ago by nthiery

  • Cc bertheville.chloe@… thomas.ayoub@… arthur.mauvezin@… added

Changed 10 years ago by nthiery

A sage plot

Changed 10 years ago by nthiery

The TeX produced from it (with a little manual tweak)

Changed 10 years ago by nthiery

The pdf obtained by compiling the TeX

comment:6 Changed 10 years ago by nthiery

As an example, I attached the result of using this patch on the following plot::

sage: L = RootSystem(['A',2,1]).weight_space()
sage: P = L.plot(size=[[-1..1],[-1..1]],alcovewalks=[[0,2,0,1,2,1,2,0,2,1]]); P

I had to tweak a tiny bit the latex, for tikz crashes with an arithmetic error when given the option color={rgb: red,0;green,0;blue,0}. Otherwise, I guess that using

sage: view(P)

would have worked.

Anyway, it would be best to use named colors instead, and not to repeat opacity=1 all the time.

comment:7 follow-up: Changed 10 years ago by ppurka

Some minor comments:

  1. Is it necessary or useful to add _latex method to GraphicsPrimitive and override that from the arc, line, etc? I think this method is used only in Graphics so that only one \begin{tikzpicture} is used, right? Since the classes Arc, Line, etc are not exposed hence it doesn't seem to matter whether the method is named latex or _latex. I am not sure which is the correct approach here, so I am just curious.
  2. Should it return an empty string from Graphics if there are no objects in it? Again, I am just wondering because I am not sure which behavior is more appropriate. Currently, this patch results in the following behavior:
    sage: G = Graphics()
    sage: latex(G)
    \begin{tikzpicture}
    
    \end{tikzpicture}
    sage: len(G)
    0
    
  3. The check for None should be done by using is or is not, according to this PEP.

comment:8 in reply to: ↑ 7 Changed 10 years ago by nthiery

Replying to ppurka:

Some minor comments:

  1. Is it necessary or useful to add _latex method to GraphicsPrimitive and override that from the arc, line, etc? I think this method is used only in Graphics so that only one \begin{tikzpicture} is used, right? Since the classes Arc, Line, etc are not exposed hence it doesn't seem to matter whether the method is named latex or _latex. I am not sure which is the correct approach here, so I am just curious.

In any cases, all the methods should be named _latex_, for consistency with SageObject?. At this point, I don't see a use for having _latex_ in GraphicsPrimitive?, since there is already a default (admittedly pretty useless in this case) implementation in SageObject?.

  1. Should it return an empty string from Graphics if there are no objects in it? Again, I am just wondering because I am not sure which behavior is more appropriate. Currently, this patch results in the following behavior:
    sage: G = Graphics()
    sage: latex(G)
    \begin{tikzpicture}
    
    \end{tikzpicture}
    sage: len(G)
    0
    

That's sounds reasonable; let's not introduce a special case.

  1. The check for None should be done by using is or is not, according to this PEP.

+1

Thanks for your comments!

comment:9 Changed 10 years ago by nthiery

Chloe, Thomas, Arthur,

Are you planning to work further on this patch?

If not, any volunteer to take on this project?

comment:10 Changed 10 years ago by ddrake

A plot of a function produces unexciting results:

sage: p = plot(sin(x), (0, 2*pi))
sage: latex(p)
\begin{tikzpicture}
\draw [color={rgb: red,0;green,0;blue,1}, line width=1pt, opacity=1] (0.0,0.0) -- (0.0199042066897,0.0199028924494);
\end{tikzpicture}

But perhaps we should focus on getting plots to work later.

comment:11 Changed 10 years ago by ddrake

Also, here are a couple links to projects for turning matplotlib things into TikZ:

https://github.com/nschloe/matplotlib2tikz

https://github.com/pwuertz/matplotlib-backend-pgf/

We might still want to do our own TikZ export, but those are potentially of use.

comment:12 Changed 9 years ago by jdemeyer

  • Milestone changed from sage-5.11 to sage-5.12

comment:13 Changed 8 years ago by vbraun_spam

  • Milestone changed from sage-6.1 to sage-6.2

comment:14 Changed 8 years ago by vbraun_spam

  • Milestone changed from sage-6.2 to sage-6.3

comment:15 Changed 8 years ago by vbraun_spam

  • Milestone changed from sage-6.3 to sage-6.4

comment:16 Changed 6 years ago by tscrim

  • Cc tscrim added
  • Milestone changed from sage-6.4 to sage-7.3

In some ways, this is already handled by matplotlib's PGF export, but I think it would still be good to have a native (and more compact) version of the graphics objects (in particular, so it doesn't go through show, need to create a temp file, and come with all of the "standard" plot things).

Note: See TracTickets for help on using tickets.