sagemath / sage

Main repository of SageMath
https://www.sagemath.org
Other
1.43k stars 479 forks source link

LaTeX export for plots #13232

Open nthiery opened 12 years ago

nthiery commented 12 years ago

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

CC: @dandrake @jhpalmieri @kcrisman bertheville.chloe@club-internet.fr thomas.ayoub@u-psud.fr arthur.mauvezin@u-psud.fr @tscrim

Component: graphics

Keywords: latex, plot

Author: Thomas Ayoub, Bertheville Chloé, Arthur Mauvezin

Issue created by migration from https://trac.sagemath.org/ticket/13232

nthiery commented 12 years ago

Attachment: trac_13232-plot_latex-nt.patch.gz

nthiery commented 12 years ago
comment:1

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

kcrisman commented 12 years ago
comment:3

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

nthiery commented 12 years ago

Author: Thomas Ayoub, Bertheville Chloé, Arthur Mauvezin

nthiery commented 12 years ago

Attachment: alcove.png

A sage plot

nthiery commented 12 years ago

Attachment: alcove.tex.gz

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

nthiery commented 12 years ago

The pdf obtained by compiling the TeX

nthiery commented 12 years ago
comment:6

Attachment: alcove.pdf.gz

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.

ppurka commented 12 years ago
comment:7

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
  1. The check for None should be done by using is or is not, according to this PEP.
nthiery commented 12 years ago
comment:8

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!

nthiery commented 12 years ago
comment:9

Chloe, Thomas, Arthur,

Are you planning to work further on this patch?

If not, any volunteer to take on this project?

tscrim commented 8 years ago
comment:16

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).