Skip to content

Commit

Permalink
Trac #20343: Add file sage/misc/latex_standalone.py and class `Tikz…
Browse files Browse the repository at this point in the history
…Picture`

The goal of this ticket is to import the `TikzPicture` module from the
optional !SageMath package [https://pypi.org/project/slabbe/ 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`):

[[Image(tikz-jupyter-demo.png, 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:

- https://nbviewer.jupyter.org/url/www.slabbe.org/Publications/arXiv_190
6_01104.ipynb

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

URL: https://trac.sagemath.org/20343
Reported by: slabbe
Ticket author(s): Sébastien Labbé
Reviewer(s): Jean-Philippe Labbé, Xavier Caruso, Vincent Delecroix,
Matthias Koeppe
  • Loading branch information
Release Manager committed Mar 30, 2022
2 parents 0991f82 + 5c9b41c commit 5166ac1
Show file tree
Hide file tree
Showing 2 changed files with 1,527 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/doc/en/reference/misc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ LaTeX
sagetex
sage/misc/latex
sage/misc/latex_macros
sage/misc/latex_standalone

Saving and Loading Sage Objects
-------------------------------
Expand Down
Loading

0 comments on commit 5166ac1

Please sign in to comment.