Opened 10 years ago

# LaTeX export for plots

Reported by: Owned by: nthiery jason, was major sage-7.3 graphics latex, plot ddrake, jhpalmieri, kcrisman, bertheville.chloe@…, thomas.ayoub@…, arthur.mauvezin@…, tscrim Thomas Ayoub, Bertheville Chloé, Arthur Mauvezin N/A

### Description

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

### comment:1 Changed 10 years ago by nthiery

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

### comment:3 Changed 10 years ago by kcrisman

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

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: ↓ 8 Changed 10 years ago by ppurka

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

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

### 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:

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

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