Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
33002: adapting the doctests with the new behavior thus also avoiding…
Browse files Browse the repository at this point in the history
… deprecation warnings
  • Loading branch information
seblabbe committed Jul 6, 2022
1 parent 33afbd3 commit a1d5989
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 70 deletions.
37 changes: 24 additions & 13 deletions src/doc/en/thematic_tutorials/geometry/polytope_tikz.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ paper. TikZ is a very versatile tool to draw in scientific documents
and Sage can deal easily with 3-dimensional polytopes. Finally sagetex
makes everything work together nicely between Sage, TikZ and
LaTeX. Since version 6.3 of Sage, there is a function for (projection
of) polytopes to output a TikZ picture of the polytope. This short
tutorial shows how it all works.
of) polytopes to output a TikZ picture of the polytope. Since Version 9.7 of SageMath,
the tikz output can be a ``TikzPicture`` object from the sage module
``sage.misc.latex_standalone``. This short tutorial shows how it all works.

Instructions
""""""""""""
Expand All @@ -30,21 +31,23 @@ To put an image of a 3D-polytope in LaTeX using TikZ and Sage, simply follow the
- Visualize the polytope P using the command ``P.show(aspect_ratio=1)``
- This will open an interactive view in your default browser, where you can rotate the polytope.
- Once the desired view angle is found, click on the information icon in the lower right-hand corner and select *Get Viewpoint*. This will copy a string of the form '[x,y,z],angle' to your local clipboard.
- Go back to Sage and type ``Img = P.tikz([x,y,z],angle)``. You can paste the string here to save some typing.
- Go back to Sage and type ``Img = P.tikz([x,y,z],angle,output_type='LatexExpr')``. You can paste the string here to save some typing.
- *Img* now contains a Sage object of type ``LatexExpr`` containing the raw TikZ picture of your polytope

Then, you can either copy-paste it to your article by typing ``Img`` in Sage or save it to a file, by doing
Alternatively, you can save the tikz image to a file, by doing

.. CODE-BLOCK:: python
f = open('Img_poly.tex','w')
f.write(Img)
f.close()
Img = P.tikz([x,y,z], angle, output_type='TikzPicture')
Img.tex('Img_poly.tex')
Img.tex('Img_poly.tex', content_only=True)
Img.pdf('Img_poly.pdf')
.. end of output
Then in the pwd (present working directory of sage, the one of your article)
you will have a file named ``Img_poly.tex`` containing the tikzpicture of your polytope.
you will have a file named ``Img_poly.tex`` and ``Img_poly.pdf`` containing the
tikzpicture of the polytope ``P``.

Customization
"""""""""""""
Expand All @@ -57,6 +60,9 @@ You can customize the polytope using the following options in the command ``P.ti
- ``vertex_color`` : string (default: ``green``) representing colors which tikz recognize,
- ``opacity`` : real number (default: ``0.8``) between 0 and 1 giving the opacity of the front facets,
- ``axis`` : Boolean (default: ``False``) draw the axes at the origin or not.
- ``output_type`` : string (default: ``None``) ``None``, ``'LatexExpr'`` or
``'TikzPicture'``, the type of the output. Since SageMath 9.7, the value ``None`` is deprecated
as the default value will soon be changed from ``'LatexExpr'`` to ``'TikzPicture'``.

Examples
""""""""
Expand All @@ -80,15 +86,20 @@ When you found a good angle, follow the above procedure to obtain the values

::

Img = P.tikz([674,108,-731],112)
Img = P.tikz([674,108,-731], 112, output_type='TikzPicture')

.. end of output
Note: the ``output_type='TikzPicture'`` is necessary since SagMath 9.7 to avoid
a deprecation warning message since the default output type will soon change
from a ``LatexExpr`` (Python str) to a ``TikzPicture`` object (allowing more
versatility, like being able to view it directly in the Jupyter notebook).

Or you may want to customize using the command

::

Img = P.tikz([674,108,-731],112,scale=2, edge_color='orange',facet_color='red',vertex_color='blue',opacity=0.4)
Img = P.tikz([674,108,-731],112,scale=2, edge_color='orange',facet_color='red',vertex_color='blue',opacity=0.4, output_type='TikzPicture')

.. end of output
Expand Down Expand Up @@ -134,16 +145,16 @@ some possibilities.

.. CODE-BLOCK:: latex

\sagestr{(polytopes.permutahedron(4)).tikz([4,5,6],45,scale=0.75, facet_color='red',vertex_color='yellow',opacity=0.3)}
\sagestr{(polytopes.permutahedron(4)).tikz([4,5,6],45,scale=0.75, facet_color='red',vertex_color='yellow',opacity=0.3, output_type='LatexExpr')}

.. end of output
2) You may create the following tex commands

.. CODE-BLOCK:: latex

\newcommand{\polytopeimg}[4]{\sagestr{(#1).tikz(#2,#3,#4)}}
\newcommand{\polytopeimgopt}[9]{\sagestr{(#1).tikz(#2,#3,#4,#5,#6,#7,#8,#9)}}
\newcommand{\polytopeimg}[4]{\sagestr{(#1).tikz(#2,#3,#4,output_type='LatexExpr')}}
\newcommand{\polytopeimgopt}[9]{\sagestr{(#1).tikz(#2,#3,#4,#5,#6,#7,#8,#9,output_type='LatexExpr')}}

.. end of output
Expand Down
23 changes: 17 additions & 6 deletions src/doc/en/thematic_tutorials/geometry/visualization.rst
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,22 @@ This method returns a tikz picture of the polytope (must be 2 or
::

sage: c = polytopes.cube()
sage: c.tikz().splitlines()[:5]
['\\begin{tikzpicture}%',
'\t[x={(1.000000cm, 0.000000cm)},',
'\ty={(-0.000000cm, 1.000000cm)},',
'\tz={(0.000000cm, -0.000000cm)},',
'\tscale=1.000000,']
sage: c.tikz(output_type='TikzPicture')
\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}%
[x={(1.000000cm, 0.000000cm)},
y={(-0.000000cm, 1.000000cm)},
z={(0.000000cm, -0.000000cm)},
scale=1.000000,
...
Use print to see the full content.
...
\node[vertex] at (-1.00000, -1.00000, 1.00000) {};
\node[vertex] at (-1.00000, 1.00000, 1.00000) {};
%%
%%
\end{tikzpicture}
\end{document}

.. end of output
37 changes: 24 additions & 13 deletions src/sage/geometry/polyhedron/base6.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ class Polyhedron_base6(Polyhedron_base5):
sage: P = polytopes.cube()
sage: Polyhedron_base6.plot(P)
Graphics3d Object
sage: Polyhedron_base6.tikz(P)
sage: print(Polyhedron_base6.tikz(P, output_type='TikzPicture'))
\RequirePackage{luatex85}
\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}%
[x={(1.000000cm, 0.000000cm)},
y={(-0.000000cm, 1.000000cm)},
Expand Down Expand Up @@ -127,6 +130,7 @@ class Polyhedron_base6(Polyhedron_base5):
%%
%%
\end{tikzpicture}
\end{document}
sage: Q = polytopes.hypercube(4)
sage: Polyhedron_base6.show(Q)
Expand Down Expand Up @@ -544,18 +548,25 @@ def tikz(self, view=[0, 0, 1], angle=0, scale=1,
EXAMPLES::
sage: co = polytopes.cuboctahedron()
sage: Img = co.tikz([0,0,1], 0)
sage: print('\n'.join(Img.splitlines()[:9]))
sage: Img = co.tikz([0,0,1], 0, output_type='TikzPicture')
sage: Img
\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}%
[x={(1.000000cm, 0.000000cm)},
y={(0.000000cm, 1.000000cm)},
z={(0.000000cm, 0.000000cm)},
scale=1.000000,
back/.style={loosely dotted, thin},
edge/.style={color=blue!95!black, thick},
facet/.style={fill=blue!95!black,fill opacity=0.800000},
vertex/.style={inner sep=1pt,circle,draw=green!25!black,fill=green!75!black,thick}]
sage: print('\n'.join(Img.splitlines()[12:21]))
[x={(1.000000cm, 0.000000cm)},
y={(0.000000cm, 1.000000cm)},
z={(0.000000cm, 0.000000cm)},
scale=1.000000,
...
Use print to see the full content.
...
\node[vertex] at (1.00000, 0.00000, 1.00000) {};
\node[vertex] at (1.00000, 1.00000, 0.00000) {};
%%
%%
\end{tikzpicture}
\end{document}
sage: print('\n'.join(Img.content().splitlines()[12:21]))
%% with the command: ._tikz_3d_in_3d and parameters:
%% view = [0, 0, 1]
%% angle = 0
Expand All @@ -565,7 +576,7 @@ def tikz(self, view=[0, 0, 1], angle=0, scale=1,
%% opacity = 0.8
%% vertex_color = green
%% axis = False
sage: print('\n'.join(Img.splitlines()[22:26]))
sage: print('\n'.join(Img.content().splitlines()[22:26]))
%% Coordinate of the vertices:
%%
\coordinate (-1.00000, -1.00000, 0.00000) at (-1.00000, -1.00000, 0.00000);
Expand Down
112 changes: 74 additions & 38 deletions src/sage/geometry/polyhedron/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -1292,19 +1292,56 @@ def tikz(self, view=[0, 0, 1], angle=0, scale=1,
EXAMPLES::
sage: P1 = polytopes.small_rhombicuboctahedron()
sage: Image1 = P1.projection().tikz([1,3,5], 175, scale=4)
sage: Image1 = P1.projection().tikz([1,3,5], 175, scale=4, output_type='TikzPicture')
sage: type(Image1)
<class 'sage.misc.latex.LatexExpr'>
sage: print('\n'.join(Image1.splitlines()[:4]))
<class 'sage.misc.latex_standalone.TikzPicture'>
sage: Image1
\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}%
[x={(-0.939161cm, 0.244762cm)},
y={(0.097442cm, -0.482887cm)},
z={(0.329367cm, 0.840780cm)},
sage: with open('polytope-tikz1.tex', 'w') as f: # not tested
....: _ = f.write(Image1)
[x={(-0.939161cm, 0.244762cm)},
y={(0.097442cm, -0.482887cm)},
z={(0.329367cm, 0.840780cm)},
scale=4.000000,
...
Use print to see the full content.
...
\node[vertex] at (-2.41421, 1.00000, -1.00000) {};
\node[vertex] at (-2.41421, -1.00000, 1.00000) {};
%%
%%
\end{tikzpicture}
\end{document}
sage: _ = Image1.tex('polytope-tikz1.tex') # not tested
sage: _ = Image1.png('polytope-tikz1.png') # not tested
sage: _ = Image1.pdf('polytope-tikz1.pdf') # not tested
sage: _ = Image1.svg('polytope-tikz1.svg') # not tested
A second example::
sage: P2 = Polyhedron(vertices=[[1, 1],[1, 2],[2, 1]])
sage: Image2 = P2.projection().tikz(scale=3, edge_color='blue!95!black', facet_color='orange!95!black', opacity=0.4, vertex_color='yellow', axis=True)
sage: Image2 = P2.projection().tikz(scale=3, edge_color='blue!95!black', facet_color='orange!95!black', opacity=0.4, vertex_color='yellow', axis=True, output_type='TikzPicture')
sage: Image2
\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}%
[scale=3.000000,
back/.style={loosely dotted, thin},
edge/.style={color=blue!95!black, thick},
facet/.style={fill=orange!95!black,fill opacity=0.400000},
...
Use print to see the full content.
...
\node[vertex] at (1.00000, 2.00000) {};
\node[vertex] at (2.00000, 1.00000) {};
%%
%%
\end{tikzpicture}
\end{document}
The second example using a LatexExpr as output type::
sage: Image2 = P2.projection().tikz(scale=3, edge_color='blue!95!black', facet_color='orange!95!black', opacity=0.4, vertex_color='yellow', axis=True, output_type='LatexExpr')
sage: type(Image2)
<class 'sage.misc.latex.LatexExpr'>
sage: print('\n'.join(Image2.splitlines()[:4]))
Expand All @@ -1315,31 +1352,13 @@ def tikz(self, view=[0, 0, 1], angle=0, scale=1,
sage: with open('polytope-tikz2.tex', 'w') as f: # not tested
....: _ = f.write(Image2)
A third example::
sage: P3 = Polyhedron(vertices=[[-1, -1, 2],[-1, 2, -1],[2, -1, -1]])
sage: P3
A 2-dimensional polyhedron in ZZ^3 defined as the convex hull of 3 vertices
sage: Image3 = P3.projection().tikz([0.5,-1,-0.1], 55, scale=3, edge_color='blue!95!black',facet_color='orange!95!black', opacity=0.7, vertex_color='yellow', axis=True)
sage: print('\n'.join(Image3.splitlines()[:4]))
\begin{tikzpicture}%
[x={(0.658184cm, -0.242192cm)},
y={(-0.096240cm, 0.912008cm)},
z={(-0.746680cm, -0.331036cm)},
sage: with open('polytope-tikz3.tex', 'w') as f: # not tested
....: _ = f.write(Image3)
sage: P = Polyhedron(vertices=[[1,1,0,0],[1,2,0,0],[2,1,0,0],[0,0,1,0],[0,0,0,1]])
sage: P
A 4-dimensional polyhedron in ZZ^4 defined as the convex hull of 5 vertices
sage: P.projection().tikz()
Traceback (most recent call last):
...
NotImplementedError: The polytope has to live in 2 or 3 dimensions.
Using :class:`~sage.misc.latex_standalone.TikzPicture` as output type::
sage: P3 = Polyhedron(vertices=[[-1, -1, 2],[-1, 2, -1],[2, -1, -1]])
sage: t = P3.projection().tikz([0.5,-1,-0.1], 55, scale=3, edge_color='blue!95!black',facet_color='orange!95!black', opacity=0.7, vertex_color='yellow', axis=True, output_type='TikzPicture')
sage: t
sage: Image3 = P3.projection().tikz([0.5,-1,-0.1], 55, scale=3, edge_color='blue!95!black',facet_color='orange!95!black', opacity=0.7, vertex_color='yellow', axis=True, output_type='TikzPicture')
sage: Image3
\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}%
Expand All @@ -1356,7 +1375,20 @@ def tikz(self, view=[0, 0, 1], angle=0, scale=1,
%%
\end{tikzpicture}
\end{document}
sage: t.pdf(view=False) # not tested
sage: _ = Image3.tex('polytope-tikz3.tex') # not tested
sage: _ = Image3.png('polytope-tikz3.png') # not tested
sage: _ = Image3.pdf('polytope-tikz3.pdf') # not tested
sage: _ = Image3.svg('polytope-tikz3.svg') # not tested
A fourth example::
sage: P = Polyhedron(vertices=[[1,1,0,0],[1,2,0,0],[2,1,0,0],[0,0,1,0],[0,0,0,1]])
sage: P
A 4-dimensional polyhedron in ZZ^4 defined as the convex hull of 5 vertices
sage: P.projection().tikz(output_type='TikzPicture')
Traceback (most recent call last):
...
NotImplementedError: The polytope has to live in 2 or 3 dimensions.
.. TODO::
Expand All @@ -1365,7 +1397,7 @@ def tikz(self, view=[0, 0, 1], angle=0, scale=1,
sage: P=Polyhedron(vertices=[[1,1,0,0],[1,2,0,0],[2,1,0,0],[0,0,1,0],[0,0,0,1]])
sage: P
A 4-dimensional polyhedron in ZZ^4 defined as the convex hull of 5 vertices
sage: P.projection().tikz()
sage: P.projection().tikz(output_type='TikzPicture')
Traceback (most recent call last):
...
NotImplementedError: The polytope has to live in 2 or 3 dimensions.
Expand Down Expand Up @@ -1454,9 +1486,9 @@ def _tikz_2d(self, scale, edge_color, facet_color, opacity, vertex_color, axis):
Scientific notation is not used in the output (:trac:`16519`)::
sage: P=Polyhedron([[2*10^-10,0],[0,1],[1,0]],base_ring=QQ)
sage: tikzstr=P.projection().tikz()
sage: 'e-10' in tikzstr
sage: P = Polyhedron([[2*10^-10,0],[0,1],[1,0]],base_ring=QQ)
sage: tikz = P.projection().tikz(output_type='TikzPicture')
sage: 'e-10' in tikz.content()
False
.. NOTE::
Expand Down Expand Up @@ -1582,9 +1614,11 @@ def _tikz_2d_in_3d(self, view, angle, scale, edge_color, facet_color,
sage: with open('polytope-tikz3.tex', 'w') as f: # not tested
....: _ = f.write(Image)
::
sage: p = Polyhedron(vertices=[[1,0,0],[0,1,0],[0,0,1]])
sage: proj = p.projection()
sage: Img = proj.tikz([1,1,1],130,axis=True)
sage: Img = proj.tikz([1,1,1],130,axis=True, output_type='LatexExpr')
sage: print('\n'.join(Img.splitlines()[12:21]))
%% with the command: ._tikz_2d_in_3d and parameters:
%% view = [1, 1, 1]
Expand Down Expand Up @@ -1730,8 +1764,10 @@ def _tikz_3d_in_3d(self, view, angle, scale, edge_color,
sage: with open('polytope-tikz1.tex', 'w') as f: # not tested
....: _ = f.write(Image)
::
sage: Associahedron = Polyhedron(vertices=[[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]]).polar()
sage: ImageAsso = Associahedron.projection().tikz([-15,-755,-655], 116, scale=1)
sage: ImageAsso = Associahedron.projection().tikz([-15,-755,-655], 116, scale=1, output_type='LatexExpr')
sage: print('\n'.join(ImageAsso.splitlines()[12:30]))
%% with the command: ._tikz_3d_in_3d and parameters:
%% view = [-15, -755, -655]
Expand Down

0 comments on commit a1d5989

Please sign in to comment.