Add file `sage/misc/latex_standalone.py` and class `TikzPicture`
Closed this issue · 211 comments
The goal of this ticket is to import the TikzPicture module from the optional SageMath package slabbe into SageMath. This module has been created in 2016 and has evolved to a stable state and it is mature enough to go into SageMath now.
Here is a demo inside a Jupyter notebook (notice that the import line was since changed to from sage.misc.latex_standalone import TikzPicture):
<img src="tikz-jupyter-demo.png" width=width=600>
More examples:
sage: from sage.misc.latex_standalone import TikzPicture
sage: V = [[1,0,1],[1,0,0],[1,1,0],[0,0,-1],[0,1,0],[-1,0,0],[0,1,1],[0,0,1],[0,-1,0]]
sage: P = Polyhedron(vertices=V).polar()
sage: s = P.projection().tikz([674,108,-731],112)
sage: t = TikzPicture(s)
sage: t.pdf() # opens the pdf in a viewer
'/home/slabbe/.sage/temp/miami/5032/tikz_gvem9vu8.pdf'
sage: t.pdf('image.pdf') # when a filename is provided, it doesn't open in a viewer
'/home/slabbe/image.pdf'
It may create the image into png format (translation pdf -> png made with convert from imagemagick) or svg format (translation pdf -> svg made with pdf2svg):
sage: path_to_file = t.png()
sage: path_to_file = t.svg()
sage: path_to_file = t.tex()
In a Jupyter notebook, this shows the picture below the cell (using rich repr):
sage: t
In the terminal, this shows the header and tail of the tikzpicture code:
sage: t
\documentclass[tikz]{standalone}
\usepackage{amsmath}
\begin{document}
\begin{tikzpicture}%
[x={(0.249656cm, -0.577639cm)},
y={(0.777700cm, -0.358578cm)},
z={(-0.576936cm, -0.733318cm)},
scale=1.000000,
---
91 lines not printed (5169 characters in total).
Use print to see the full content.
---
\node[vertex] at (0.00000, -1.00000, 0.00000) {};
\node[vertex] at (-0.50000, -0.50000, -0.50000) {};
%%
%%
\end{tikzpicture}
\end{document}
Use print to see the full content:
sage: print(t)
A second example shows that it is faster than view and better (avoids to crop the vertices):
sage: g = graphs.PetersenGraph()
sage: %time _ = TikzPicture(latex(g), standalone_options=["border=4mm"], usepackage=['tkz-graph']).pdf()
CPU times: user 3.52 ms, sys: 12.7 ms, total: 16.2 ms
Wall time: 2.24 s
compared to using view (which crops the vertices and creates two pages pdf on mac):
sage: %time view(g, tightpage=True)
CPU times: user 126 ms, sys: 85 ms, total: 211 ms
Wall time: 6.06 s
Also, if dot2tex and graphviz available, one may directly construct a tikzpicture from a sagemath graph:
sage: t = TikzPicture.from_graph(g, prog='dot') # optional: dot2tex
The tikzpicture.py was developped within optional package slabbe since 2015. Its interface should be stable enough to go into sage now.
Meanwhile any user can use this tikzpicture module by installing the package:
sage -pip install slabbe
and
sage: from slabbe import TikzPicture
An example of usage of this module can be found at this url:
associated to the preprint
published in Journal of Modern Dynamics at
Depends on #32650
Depends on #33005
CC: @dkrenn @jplab @slel @fchapoton
Component: misc
Author: Sébastien Labbé
Branch: 5c9b41c
Reviewer: Jean-Philippe Labbé, Xavier Caruso, Vincent Delecroix, Matthias Koeppe
Issue created by migration from https://trac.sagemath.org/ticket/20343
Description changed:
---
+++
@@ -1,3 +1,23 @@
-From my optional Sage package [1] slabbe-0.2.spkg.
+Importing a file from my optional Sage package [slabbe-0.2.spkg](https://github.com/seblabbe/slabbe) into Sage.
-[1] https://github.com/seblabbe/slabbe
+```
+sage: from sage.misc.tikz_picture import TikzPicture
+sage: V = [[1,0,1],[1,0,0],[1,1,0],[0,0,-1],[0,1,0],[-1,0,0],[0,1,1],[0,0,1],[0,-1,0]]
+sage: P = Polyhedron(vertices=V).polar()
+sage: s = P.projection().tikz([674,108,-731],112)
+sage: t = TikzPicture(s)
+sage: t.pdf()
+```
+
+```
+sage: g = graphs.PetersenGraph()
+sage: s = latex(g)
+sage: t = TikzPicture(s, standalone_configs=["border=4mm"], packages=['tkz-graph'])
+sage: _ = t.pdf()
+```
+
+If dot2tex and graphviz available:
+
+```
+sage: t = TikzPicture.from_graph(g) # optional: dot2tex
+```Branch: u/slabbe/20343
Description changed:
---
+++
@@ -1,7 +1,9 @@
-Importing a file from my optional Sage package [slabbe-0.2.spkg](https://github.com/seblabbe/slabbe) into Sage.
+Importing `TikzPicture` class from my optional Sage package [slabbe-0.2.spkg](https://github.com/seblabbe/slabbe) into Sage.
+
+One example:
```
-sage: from sage.misc.tikz_picture import TikzPicture
+sage: from sage.misc.tikzpicture import TikzPicture
sage: V = [[1,0,1],[1,0,0],[1,1,0],[0,0,-1],[0,1,0],[-1,0,0],[0,1,1],[0,0,1],[0,-1,0]]
sage: P = Polyhedron(vertices=V).polar()
sage: s = P.projection().tikz([674,108,-731],112)
@@ -9,15 +11,27 @@
sage: t.pdf()
```
+Second example:
+
```
sage: g = graphs.PetersenGraph()
-sage: s = latex(g)
-sage: t = TikzPicture(s, standalone_configs=["border=4mm"], packages=['tkz-graph'])
-sage: _ = t.pdf()
+sage: %time _ = TikzPicture(latex(g), standalone_configs=["border=4mm"], packages=['tkz-graph']).pdf()
+CPU times: user 3.52 ms, sys: 12.7 ms, total: 16.2 ms
+Wall time: 2.24 s
```
-If dot2tex and graphviz available:
+compared to using view (which crops the vertices and creates two pages pdf on mac):
```
-sage: t = TikzPicture.from_graph(g) # optional: dot2tex
+sage: %time view(g, tightpage=True)
+CPU times: user 126 ms, sys: 85 ms, total: 211 ms
+Wall time: 6.06 s
```
+
+Also, if dot2tex and graphviz available:
+
+```
+sage: t = TikzPicture.from_graph(g, prog='dot') # optional: dot2tex
+```
+
+A comment:
t = TikzPicture(latex(transducers.GrayCode()))
t.pdf()
does not work as the preamble is not set correctly. However, there is
from sage.combinat.finite_state_machine import setup_latex_preamble
setup_latex_preamble()
(as well as the corresponding function in sage.graphs.graph_latex) to set this correctly, but it seems to be ignored by TikzPicture.
In contrast,
view(transducers.GrayCode())
works, but I have no idea if setup_latex_preamble is called (not needed to be done manually) or if tikz is included anyways.
Replying to @dkrenn:
A comment:
t = TikzPicture(latex(transducers.GrayCode())) t.pdf()does not work as the preamble is not set correctly.
As it is now, the user must set the libraries. This should work:
t = TikzPicture(latex(transducers.GrayCode()), tikzlibraries=['automata'])
t.pdf()
I am still not sure about the proper way to choose the arguments for the __init__ function and their especially their default values. Should the arguments be empty by default? equal to the current sage latex preamble? or full of a big load of libraries?
Replying to @seblabbe:
Replying to @dkrenn:
A comment:
t = TikzPicture(latex(transducers.GrayCode())) t.pdf()does not work as the preamble is not set correctly.
As it is now, the user must set the libraries. This should work:
t = TikzPicture(latex(transducers.GrayCode()), tikzlibraries=['automata']) t.pdf()
Works, thanks.
I am still not sure about the proper way to choose the arguments for the
__init__function and their especially their default values. Should the arguments be empty by default? equal to the current sage latex preamble? or full of a big load of libraries?
Without much thinking, I vote for: The second, equal to the current sage latex preamble.
Description changed:
---
+++
@@ -15,7 +15,7 @@
```
sage: g = graphs.PetersenGraph()
-sage: %time _ = TikzPicture(latex(g), standalone_configs=["border=4mm"], packages=['tkz-graph']).pdf()
+sage: %time _ = TikzPicture(latex(g), standalone_options=["border=4mm"], usepackage=['tkz-graph']).pdf()
CPU times: user 3.52 ms, sys: 12.7 ms, total: 16.2 ms
Wall time: 2.24 s
```Changed branch from u/slabbe/20343 to none
Description changed:
---
+++
@@ -34,4 +34,14 @@
sage: t = TikzPicture.from_graph(g, prog='dot') # optional: dot2tex
```
+To avoid losing my time, I will not post a branch here until somebody tells me he is willing to review this module. Meanwhile any user can use this tikzpicture module by installing my package:
+```
+sage -pip install slabbe
+```
+
+and
+
+```
+sage: from slabbe import TikzPicture
+```My module has evolved in my package slabbe, therefore the branch here is not up to date anymore. Also, I intend to make it more general during the next year allowing any content not only tikzpicture code. Therefore, I deleted the branch posted here.
Description changed:
---
+++
@@ -34,7 +34,9 @@
sage: t = TikzPicture.from_graph(g, prog='dot') # optional: dot2tex
```
-To avoid losing my time, I will not post a branch here until somebody tells me he is willing to review this module. Meanwhile any user can use this tikzpicture module by installing my package:
+I intend to make further improvements to this module during the next year. Also, I will wait until somebody tells me he is willing to review this module before publishing a branch here.
+
+Meanwhile any user can use this tikzpicture module by installing my package:
```
sage -pip install slabbeChanging status to needs work.
Reviewer: Jean-Philippe Labbé
Branch: u/caruso/tikzpicture
I'll be happy to revive this ticket.
New commits:
88c048c | import tikz_picture.py from slabbe gitlab repository |
Branch pushed to git repo; I updated commit sha1. New commits:
2917f4b | doctests pass |
Branch pushed to git repo; I updated commit sha1. New commits:
8cfc834 | typos |
I'm not convinced by all the methods from_dot_string, from_graph, etc. in the class TikzPicture. I would say that this code should instead be in the classes implementing the object we want to display. E.g. it could be a method _tikz_, just as _latex_, and TikzPicture just calls it.
What's your opinion?
There is no class for dot strings. Graphs already have a _latex_ method which returns a tikz picture string. It is what is used with for instance view(G). The from_graph class method really is using dot2tex + graphviz which is not the same layout sometimes. It is true that maybe adding a _tikz_ would avoid to replace the current behavior of _latex_. But then, having two different behaviors will be non pedagogical (how to know there is another one). Also, changing the default behavior will lead to problems...
I think it will be easier to add the new module as is and later on, in other tickets, add such _tikz_ methods in other parts of sage where it makes sense.
OK. Let's keep it for another ticket.
Changed reviewer from Jean-Philippe Labbé to Jean-Philippe Labbé, Xavier Caruso
please no "from future import ..."
Returns should be Return
Also if pdflatex is not installed on the machine, some doctests fail.
I don't know if this should be fixed but I think so because otherwise patchbots complain.
Changed branch from u/caruso/tikzpicture to u/slabbe/20343
Branch pushed to git repo; I updated commit sha1. New commits:
0022eb2 | 20343: Returns -> Return |
Branch pushed to git repo; I updated commit sha1. New commits:
03aa1bc | 20343: adding few optional tags |
I added a bunch of #optional latex and #optional imagemagick tags. That should fix most of the issues.
I also added one # optional pdf2svg for which I am not sure about. I don't know if the doctest framework know about this tag. I change to needs review to check that on the patchbot.
Why do you make it part of Sage and not an independent Python module? These could easily be declared as optional/standard packages afterwards.
Yes, I agree with that option too. Lately, I was thinking more about this option.
But, the module also depends on many things that are in Sage which would make this module quite unusable outside of sage as it is currently. For example:
To check if Graphviz is there:
from sage.features.graphviz import Graphviz
To check if pdflatex, convert and other programs are available:
from sage.misc.latex import have_pdflatex, have_convert, have_program
To load the latex macros from sage:
from sage.misc.latex import _Latex_prefs
from sage.misc.latex_macros import sage_latex_macros
To create a temporary file inside the sage folder of temporary files:
from sage.misc.temporary_file import tmp_filename
To open viewers:
from sage.misc.viewer import browser
from sage.misc.viewer import pdf_viewer
from sage.misc.viewer import png_viewer
To check whether we are in the JupyterNotebook or not:
from sage.repl.rich_output.backend_ipython import BackendIPythonNotebook
from sage.repl.rich_output.buffer import OutputBuffer
Branch pushed to git repo; I updated commit sha1. New commits:
7d59034 | 20343: adding few optional latex tags |
Replying to @seblabbe:
Yes, I agree with that option too. Lately, I was thinking more about this option.
The main advantage with that option is that the package can change versions independently of sage.
But, the module also depends on many things that are in Sage which would make this module quite unusable outside of sage as it is currently. For example:
This would be a Python module that depends on sage. There is nothing bad with that situation. And this is not the only one.
I am thinking about what to do now, either work on this ticket or create a new package. If I create a sagemath-tikzpicture package that imports many parts of sagemath, I will be responsible for fixing the package each time sagemath changes something that I import. On the other hand, if that modules goes into sagemath, then, it will become the responsability of the person which changes the dependencies to also fix the tikzpicture module. I agree with the idea of modularization, but for the above reason, I would prefer to make it go into sage. What do you think?
I prefer to have this directly in sage since I think that there are many other places in sage where it would be great to have a tikz output.
Because this module does not depend on any external libraries, there is nothing gained in the direction of modularization by making this a separate distribution package.
But perhaps you can find a better place for it than sage.misc? How about sage.typeset or sage.plot?
Branch pushed to git repo; I updated commit sha1. New commits:
d84b6a3 | Merge branch 'u/slabbe/20343' of trac.sagemath.org:sage into 20343 |
ee56d32 | adding optional tag latex to one doctest |
2692685 | 20343:avoid use of doctest continuation since it is not one |
2acfe61 | 20343: misc/tikz_picture.py -> plot/tikzpicture.py |
388a5f8 | from misc.tikz_picture -> from plot.tikzpicture |
b04ea0d | updated authors and copyright |
9f9f5ec | 20343: redoing recent changes by Vincent Delecroix in slabbe package (check_call -> run); improved header documentation |
Description changed:
---
+++
@@ -1,17 +1,67 @@
-Importing `TikzPicture` class from my optional Sage package [slabbe-0.2.spkg](https://github.com/seblabbe/slabbe) into Sage.
+Importing `TikzPicture` module from optional [SageMath](../wiki/SageMath) package [slabbe-0.6.2](https://pypi.org/project/slabbe/) into [SageMath](../wiki/SageMath).
One example:
```
-sage: from sage.misc.tikzpicture import TikzPicture
+sage: from sage.plot.tikzpicture import TikzPicture
sage: V = [[1,0,1],[1,0,0],[1,1,0],[0,0,-1],[0,1,0],[-1,0,0],[0,1,1],[0,0,1],[0,-1,0]]
sage: P = Polyhedron(vertices=V).polar()
sage: s = P.projection().tikz([674,108,-731],112)
sage: t = TikzPicture(s)
-sage: t.pdf()
+sage: t.pdf() # opens the pdf in a viewer
+'/home/slabbe/.sage/temp/miami/5032/tikz_gvem9vu8.pdf'
+sage: t.pdf('image.pdf')
+'/home/slabbe/image.pdf'
```
-Second example:
+It may create the image into png format (translation pdf -> png made with convert from imagemagick) or svg format (translation pdf -> svg made with pdf2svg):
+
+```
+sage: t.png()
+'/home/slabbe/.sage/temp/miami/5032/tikz_v0s5kl9l.png'
+sage: t.svg()
+'/home/slabbe/.sage/temp/miami/5032/tikz_sfww636u.svg'
+sage: t.tex()
+'/home/slabbe/.sage/temp/miami/5032/tikz_xxwqfjh5.tex'
+```
+
+In a Jupyter notebook, this shows the picture below the cell (using rich repr):
+
+```
+sage: t
+```
+
+In the terminal, this shows the header and tail of the tikzpicture code:
+
+```
+sage: t
+\documentclass[tikz]{standalone}
+\usepackage{amsmath}
+\begin{document}
+\begin{tikzpicture}%
+ [x={(0.249656cm, -0.577639cm)},
+ y={(0.777700cm, -0.358578cm)},
+ z={(-0.576936cm, -0.733318cm)},
+ scale=1.000000,
+---
+91 lines not printed (5169 characters in total).
+Use print to see the full content.
+---
+\node[vertex] at (0.00000, -1.00000, 0.00000) {};
+\node[vertex] at (-0.50000, -0.50000, -0.50000) {};
+%%
+%%
+\end{tikzpicture}
+\end{document}
+```
+
+Use `print` to see the full content:
+
+```
+sage: print(t)
+```
+
+A second example shows that it is faster than view and better (avoids to crop the vertices):
```
sage: g = graphs.PetersenGraph()
@@ -28,13 +78,13 @@
Wall time: 6.06 s
```
-Also, if dot2tex and graphviz available:
+Also, if dot2tex and graphviz available, one may directly construct a tikzpicture from a sagemath graph:
```
sage: t = TikzPicture.from_graph(g, prog='dot') # optional: dot2tex
```
-I intend to make further improvements to this module during the next year. Also, I will wait until somebody tells me he is willing to review this module before publishing a branch here.
+The `tikzpicture.py` was developped within optional package `slabbe` for the last 6 years. Its interface should be stable enough to go into sage now.
Meanwhile any user can use this tikzpicture module by installing my package:
Description changed:
---
+++
@@ -8,9 +8,9 @@
sage: P = Polyhedron(vertices=V).polar()
sage: s = P.projection().tikz([674,108,-731],112)
sage: t = TikzPicture(s)
-sage: t.pdf() # opens the pdf in a viewer
+sage: t.pdf() # opens the pdf in a viewer
'/home/slabbe/.sage/temp/miami/5032/tikz_gvem9vu8.pdf'
-sage: t.pdf('image.pdf')
+sage: t.pdf('image.pdf') # when a filename is provided, it does not open the pdf in a viewer
'/home/slabbe/image.pdf'
```
Description changed:
---
+++
@@ -10,7 +10,7 @@
sage: t = TikzPicture(s)
sage: t.pdf() # opens the pdf in a viewer
'/home/slabbe/.sage/temp/miami/5032/tikz_gvem9vu8.pdf'
-sage: t.pdf('image.pdf') # when a filename is provided, it does not open the pdf in a viewer
+sage: t.pdf('image.pdf') # when a filename is provided, it doesn't open in a viewer
'/home/slabbe/image.pdf'
```
Description changed:
---
+++
@@ -17,12 +17,9 @@
It may create the image into png format (translation pdf -> png made with convert from imagemagick) or svg format (translation pdf -> svg made with pdf2svg):
```
-sage: t.png()
-'/home/slabbe/.sage/temp/miami/5032/tikz_v0s5kl9l.png'
-sage: t.svg()
-'/home/slabbe/.sage/temp/miami/5032/tikz_sfww636u.svg'
-sage: t.tex()
-'/home/slabbe/.sage/temp/miami/5032/tikz_xxwqfjh5.tex'
+sage: path_to_file = t.png()
+sage: path_to_file = t.svg()
+sage: path_to_file = t.tex()
```
In a Jupyter notebook, this shows the picture below the cell (using rich repr):Description changed:
---
+++
@@ -81,9 +81,9 @@
sage: t = TikzPicture.from_graph(g, prog='dot') # optional: dot2tex
```
-The `tikzpicture.py` was developped within optional package `slabbe` for the last 6 years. Its interface should be stable enough to go into sage now.
+The `tikzpicture.py` was developped within optional package `slabbe` since 2015. Its interface should be stable enough to go into sage now.
-Meanwhile any user can use this tikzpicture module by installing my package:
+Meanwhile any user can use this tikzpicture module by installing the package:
```
sage -pip install slabbeI think we are close to it. The doctests work on my machine:
sage -t --optional=sage,optional,external --long --show-skipped tikzpicture.py
gives
Using --optional=4ti2,ccache,cryptominisat,debian,dot2tex,e_antic,external,fricas,glucose,latte_int,lidia,normaliz,notedown,pandoc_attributes,pip,pycosat,pynormaliz,rst2ipynb,sage,sage_numerical_backends_coin,sage_spkg
External software to be detected: cplex,ffmpeg,graphviz,gurobi,imagemagick,internet,latex,macaulay2,magma,maple,mathematica,matlab,octave,pandoc,rubiks,scilab
Doctesting 1 file.
sage -t --long --random-seed=0 tikzpicture.py
19 not tested tests not run
1 pdf2svg test not run
0 tests not run because we ran out of time
[161 tests, 15.43 s]
----------------------------------------------------------------------
All tests passed!
----------------------------------------------------------------------
Total time for all tests: 15.6 seconds
cpu time: 8.2 seconds
cumulative wall time: 15.4 seconds
External software detected for doctesting: imagemagick,latex
TODO:
- Let's see what the patchbot says.
- The doctest tagged with
pdf2svgis not recognized by the--optional=external. Maybe something needs to be done on the detection of external packages. - One issue is that the current
_rich_repr_always uses png instead of svg in the Jupyter Notebook even if I define'svg'before'png'in theprefer_vectortuple. I think that issue can be dealt in a later ticket.
That's it! Needs review!
This ad-hoc system package advice
+ if not have_program('pdf2svg'):
+ raise RuntimeError("pdf2svg does not seem to be installed. "
+ "Install it for example with ``brew install pdf2svg``"
+ " or ``apt-get install pdf2svg``.")
+
should be replaced by a Feature and an spkg pdf2svg with distros/ information from
https://repology.org/project/pdf2svg/versions
Ok, I will add a Feature for pdf2svg.
Also, patchbot says this:
src/sage/plot/tikzpicture.py:98:1 'subprocess.CalledProcessError' imported but unused
found 1 pyflakes errors in the modified files
Another TODO is to allow to get some verbosity useful when compilation of the picture breaks. I am writting it here not to forget.
I will work on that soon or later.
Branch pushed to git repo; I updated commit sha1. Last 10 new commits:
a96e94d | 32650: Creating feature pdf2svg |
870d461 | 32650: Creating features latex, xelatex, pdflatex, lualatex |
385569e | 32650: adding latex, xelatex, pdflatex, lualatex, pdf2svg, dvipng to doctest/external.py |
f3291d8 | 32650: using features instead of have_program in misc/latex.py |
d455993 | 32650: using features in plot/animate.py |
cf02094 | 32650: using features in plot/graphics.py and plot/multigraphics.py |
28f1f4a | 32650: deprecating have_* functions in misc/latex.py in favor of features |
5d9414d | 32650: .. note -> .. NOTE |
6d21ec7 | Merge branch #20343 onto #32650 |
80151c2 | 20343: using features instead of have_program, have_pdflatex, have_convert |
Branch pushed to git repo; I updated commit sha1. New commits:
997b8f1 | 20343: print the log when pdflatex fails compile the tikz image |
With the recent commit and subsequent work done in #32650, the remainings TODOs are:
- improve rich repr to use svg not always png
and as I suggested before, this one could be dealt in another ticket as it is still functional with png's in the Jupyter notebook.
Needs review!
Description changed:
---
+++
@@ -94,3 +94,8 @@
```
sage: from slabbe import TikzPicture
```
+
+An example of usage of this module can be found at this url:
+https://nbviewer.jupyter.org/url/www.slabbe.org/Publications/arXiv_1906_01104.ipynb
+associated to the preprint https://arxiv.org/abs/1906.01104 published in Journal of Modern Dynamics since one week at http://dx.doi.org/10.3934/jmd.2021017. It can be used to review the current ticket if one changes `from slabbe import TikzPicture` by `from sage.plot.tikzpicture import TikzPicture` at the beginning (oups! no, it will not work since the import is done within the package).
+For the review, one may checkDescription changed:
---
+++
@@ -98,4 +98,3 @@
An example of usage of this module can be found at this url:
https://nbviewer.jupyter.org/url/www.slabbe.org/Publications/arXiv_1906_01104.ipynb
associated to the preprint https://arxiv.org/abs/1906.01104 published in Journal of Modern Dynamics since one week at http://dx.doi.org/10.3934/jmd.2021017. It can be used to review the current ticket if one changes `from slabbe import TikzPicture` by `from sage.plot.tikzpicture import TikzPicture` at the beginning (oups! no, it will not work since the import is done within the package).
-For the review, one may checkDescription changed:
---
+++
@@ -97,4 +97,4 @@
An example of usage of this module can be found at this url:
https://nbviewer.jupyter.org/url/www.slabbe.org/Publications/arXiv_1906_01104.ipynb
-associated to the preprint https://arxiv.org/abs/1906.01104 published in Journal of Modern Dynamics since one week at http://dx.doi.org/10.3934/jmd.2021017. It can be used to review the current ticket if one changes `from slabbe import TikzPicture` by `from sage.plot.tikzpicture import TikzPicture` at the beginning (oups! no, it will not work since the import is done within the package).
+associated to the preprint https://arxiv.org/abs/1906.01104 published in Journal of Modern Dynamics since one week at http://dx.doi.org/10.3934/jmd.2021017. It can be used to review the current ticket if one uses `from sage.plot.tikzpicture import TikzPicture` and check that tikzpicture created within slabbe still works within the tikzpicture module introduced in this ticket.In a similar direction as comment:17, I think that the from_graph... and from_poset class methods should rather be changed to methods of Graph and Poset.
Also https://pypi.org/project/tikzplotlib/ looks interesting
I added two commits to do few improvements.
Replying to @mkoeppe:
In a similar direction as comment:17, I think that the
from_graph...andfrom_posetclass methods should rather be changed to methods ofGraphandPoset.
If you want to go in that direction, we need to have a design discussion before spending time changing the graph code. As you know,
sage: g = graphs.PetersenGraph()
sage: hasattr(g, '_latex_')
True
which returns an object of type:
sage: type(latex(g))
<class 'sage.misc.latex.LatexExpr'>
This latex expression starts with \begin{tikzpicture} and ends with \end{tikzpicture} which also depends on \usepackage{tkz-graph}:
sage: latex(g)
\begin{tikzpicture}
\definecolor{cv0}{rgb}{0.0,0.0,0.0}
\definecolor{cfv0}{rgb}{1.0,1.0,1.0}
...
\Edge[lw=0.1cm,style={color=cv6v9,},](v6)(v9)
\Edge[lw=0.1cm,style={color=cv7v9,},](v7)(v9)
%
\end{tikzpicture}
Notice that this output can be provided to TikzPicture with no problem with t = TikzPicture(latex(g), usepackage=['tkz-graph']) and t.pdf() works etc.
I don't think here we want to replace the output of g._latex_ by an object of type TikzPicture since a such change would break a lot of code.
An option is to create a new method def tikz(self) instead. I can do something like that. It could return the TikzPicture(latex(g), usepackage=['tkz-graph']) by default and if one asks, it could use the graphviz + dot2tex output instead. But, I was thinking of doing this in another ticket as a follow up, as this will open discussions that will just make longer before this ticket can go in.
Or maybe you would rather like to remove the from_graph and the from_poset methods to avoid to have to maintain them in the future? Well, currently, the from_graph also allows to merge the multiple edges which is something that would not go in the tikz method of a graph. Also, keeping the from_graph would make the passage from this module to sage easier for me because I have been using this method over and over and over in my research code.
I created ticket #33002 for the addition of a method to return an object of type TikzPicture in graph, posets and polyhedron code.
Branch pushed to git repo; I updated commit sha1. New commits:
d6d473a | 20343: fixing a bug, png output was not working |
Replying to @seblabbe:
With the recent commit and subsequent work done in #32650, the remainings TODOs are:
- improve rich repr to use svg not always png
and as I suggested before, this one could be dealt in another ticket as it is still functional with png's in the Jupyter notebook.
Also, I want to say that I believe that the sage.repl code is broken with respect to showing svg files.
# Jupyter cell 1
g = graphs.PetersenGraph()
from sage.plot.tikzpicture import TikzPicture
t = TikzPicture(latex(g), usepackage=['tkz-graph'])
t # works: it shows a png
# Jupyter cell 2
from sage.repl.rich_output import get_display_manager
dm = get_display_manager()
assert dm.preferences.graphics is None
dm.preferences.graphics = 'raster'
t # works: it shows a png
# Jupyter cell 3
dm.preferences.graphics = 'vector'
t # broken: it does not show anything
The following shows that the Jupyter notebook is able to handle a svg correctly:
# Jupyter cell 4
t.svg('a.svg')
from IPython.core.display import SVG
SVG(filename='a.svg')
I suggest to fix the issue with the showing of svg in another ticket.
Branch pushed to git repo; I updated commit sha1. New commits:
2da56a8 | 20343: using pdftocairo instead of pdf2svg |
Replying to @seblabbe:
Replying to @mkoeppe:
In a similar direction as comment:17, I think that the
from_graph...andfrom_posetclass methods should rather be changed to methods ofGraphandPoset.
I don't think here we want to replace the output of
g._latex_by an object of typeTikzPicturesince a such change would break a lot of code.
I agree
An option is to create a new method
def tikz(self)instead. I can do something like that.
Yes, this is what I meant
Branch pushed to git repo; I updated commit sha1. This was a forced push. Last 10 new commits:
d4ca003 | from misc.tikz_picture -> from plot.tikzpicture |
42011d6 | updated authors and copyright |
6894a5b | 20343: redoing recent changes by Vincent Delecroix in slabbe package (check_call -> run); improved header documentation |
2c07925 | 20343: using features instead of have_program, have_pdflatex, have_convert |
83b9173 | 20343: print the log when pdflatex fails compile the tikz image |
b26bc3f | 20343: few small improvements |
ae7b9bf | 20343: improving first example shown in documentation |
9f91d22 | 20343: fixing a bug, png output was not working |
f39e07a | 20343: using pdftocairo instead of pdf2svg |
9081777 | 20343:fixing 2 doctests + wrong trailing colons |
There was a syntax error in the file due to the previous commit. I fixed that.
I also rebased the branch on top of 9.5.beta9.
I confirm it works on my side:
$ sage -t tikzpicture.py
[...]
Doctesting 1 file.
sage -t --random-seed=33963620693510224516081799230454604818 tikzpicture.py
[158 tests, 4.59 s]
----------------------------------------------------------------------
All tests passed!
----------------------------------------------------------------------
Features detected for doctesting:
and
$ sage -t --show-skipped --long --optional=sage,optional,external tikzpicture.py
[...]
Doctesting 1 file.
sage -t --long --random-seed=204572532264437349038334999704478431314 tikzpicture.py
24 not tested tests not run
2 pdftocairo tests not run
0 tests not run because we ran out of time
[188 tests, 15.97 s]
----------------------------------------------------------------------
All tests passed!
----------------------------------------------------------------------
Features detected for doctesting: imagemagick,latex,pdf2svg
Attachment: tikz-jupyter-demo.png
Description changed:
---
+++
@@ -1,6 +1,10 @@
-Importing `TikzPicture` module from optional [SageMath](../wiki/SageMath) package [slabbe-0.6.2](https://pypi.org/project/slabbe/) into [SageMath](../wiki/SageMath).
+The goal of this ticket is to import the `TikzPicture` module from the optional [SageMath](../wiki/SageMath) package [slabbe](https://pypi.org/project/slabbe/) into [SageMath](../wiki/SageMath). This module has been created in 2016 and has evolved to a stable state now and it is mature enough to go into [SageMath](../wiki/SageMath).
-One example:
+Here is a demo inside a Jupyter notebook:
+
+<img src="tikz-jupyter-demo.png" width=width=600>
+
+More examples:
```
sage: from sage.plot.tikzpicture import TikzPictureUpdating the description: adding a screenshot of the behavior in Jupyter, hoping it motivates someone to review the ticket.
Description changed:
---
+++
@@ -1,4 +1,4 @@
-The goal of this ticket is to import the `TikzPicture` module from the optional [SageMath](../wiki/SageMath) package [slabbe](https://pypi.org/project/slabbe/) into [SageMath](../wiki/SageMath). This module has been created in 2016 and has evolved to a stable state now and it is mature enough to go into [SageMath](../wiki/SageMath).
+The goal of this ticket is to import the `TikzPicture` module from the optional SageMath package [slabbe](https://pypi.org/project/slabbe/) into SageMath. This module has been created in 2016 and has evolved to a stable state now and it is mature enough to go into SageMath.
Here is a demo inside a Jupyter notebook:
Description changed:
---
+++
@@ -1,4 +1,4 @@
-The goal of this ticket is to import the `TikzPicture` module from the optional SageMath package [slabbe](https://pypi.org/project/slabbe/) into SageMath. This module has been created in 2016 and has evolved to a stable state now and it is mature enough to go into SageMath.
+The goal of this ticket is to import the `TikzPicture` module from the optional SageMath package [slabbe](https://pypi.org/project/slabbe/) into SageMath. This module has been created in 2016 and has evolved to a stable state and it is mature enough to go into SageMath now.
Here is a demo inside a Jupyter notebook:
Description changed:
---
+++
@@ -101,4 +101,4 @@
An example of usage of this module can be found at this url:
https://nbviewer.jupyter.org/url/www.slabbe.org/Publications/arXiv_1906_01104.ipynb
-associated to the preprint https://arxiv.org/abs/1906.01104 published in Journal of Modern Dynamics since one week at http://dx.doi.org/10.3934/jmd.2021017. It can be used to review the current ticket if one uses `from sage.plot.tikzpicture import TikzPicture` and check that tikzpicture created within slabbe still works within the tikzpicture module introduced in this ticket.
+associated to the preprint https://arxiv.org/abs/1906.01104 published in Journal of Modern Dynamics at http://dx.doi.org/10.3934/jmd.2021017.Why this default usepackage=['amsmath']?
Moreover, you should never have a list as a default argument which is plugged as an attribute
sage: class A():
....: def __init__(self, a=[]):
....: self.a = a
sage: x = A()
sage: x.a.append(2)
sage: y = A()
sage: y.a
[2]
You are right. I also avoided the usage of shell=True for security reasons, see https://docs.python.org/3/library/subprocess.html#security-considerations
Doing so, the cmd needs to be a list and not a string. This is better also because it allows filenames to contain spaces: "Providing a sequence of arguments is generally preferred, as it allows the module to take care of any required escaping and quoting of arguments (e.g. to permit spaces in file names)", see https://docs.python.org/3/library/subprocess.html#frequently-used-arguments