ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 29 Jul 2020 20:50:27 +0200How to LaTeX the graph of a permutation?https://ask.sagemath.org/question/52649/how-to-latex-the-graph-of-a-permutation/Trying to LaTeX the graph generated by something like
Permutation([3,1,2]).show()
The command
latex(Permutation([3,1,2]).show())
returns the graph followed by \mathrm{None}
Fri, 24 Jul 2020 15:04:17 +0200https://ask.sagemath.org/question/52649/how-to-latex-the-graph-of-a-permutation/Answer by slelievre for <p>Trying to LaTeX the graph generated by something like <br>
Permutation([3,1,2]).show()
The command
latex(Permutation([3,1,2]).show()) <br>
returns the graph followed by \mathrm{None}</p>
https://ask.sagemath.org/question/52649/how-to-latex-the-graph-of-a-permutation/?answer=52655#post-id-52655Slightly annoying indeed: the `show` method for a permutation
- computes a graph corresponding to that permutation
- displays it
- but does not return it
Instead it returns nothing, which in Python amounts to returning `None`.
That method should be decomposed into a method that provides
the graph and a method that shows it.
Here is a workaround until such a method is provided.
One can check what the `show` method does by inspecting the source code:
sage: p = Permutation([3, 1, 2])
sage: p.show??
This helps us figure out how to produce the graph.
sage: p = Permutation([3, 1, 2])
sage: g = DiGraph([(i+1, p[i]) for i in range(len(p))], loops=True)
sage: g
Looped digraph on 3 vertices
Now we can get the LaTeX TikZ code for it:
sage: latex(g)
\begin{tikzpicture}
\definecolor{cv0}{rgb}{0.0,0.0,0.0}
\definecolor{cfv0}{rgb}{1.0,1.0,1.0}
\definecolor{clv0}{rgb}{0.0,0.0,0.0}
\definecolor{cv1}{rgb}{0.0,0.0,0.0}
\definecolor{cfv1}{rgb}{1.0,1.0,1.0}
\definecolor{clv1}{rgb}{0.0,0.0,0.0}
\definecolor{cv2}{rgb}{0.0,0.0,0.0}
\definecolor{cfv2}{rgb}{1.0,1.0,1.0}
\definecolor{clv2}{rgb}{0.0,0.0,0.0}
\definecolor{cv0v2}{rgb}{0.0,0.0,0.0}
\definecolor{cv1v0}{rgb}{0.0,0.0,0.0}
\definecolor{cv2v1}{rgb}{0.0,0.0,0.0}
%
\Vertex[style={minimum size=1.0cm,draw=cv0,fill=cfv0,text=clv0,shape=circle},LabelOut=false,L=\hbox{$1$},x=1.6204cm,y=5.0cm]{v0}
\Vertex[style={minimum size=1.0cm,draw=cv1,fill=cfv1,text=clv1,shape=circle},LabelOut=false,L=\hbox{$2$},x=0.0cm,y=0.0cm]{v1}
\Vertex[style={minimum size=1.0cm,draw=cv2,fill=cfv2,text=clv2,shape=circle},LabelOut=false,L=\hbox{$3$},x=5.0cm,y=1.1015cm]{v2}
%
\Edge[lw=0.1cm,style={post, bend right,color=cv0v2,},](v0)(v2)
\Edge[lw=0.1cm,style={post, bend right,color=cv1v0,},](v1)(v0)
\Edge[lw=0.1cm,style={post, bend right,color=cv2v1,},](v2)(v1)
%
\end{tikzpicture}
This might print a couple of warnings about required LaTeX packages.
The urls for them are outdated, see instead
- https://www.ctan.org/pkg/tkz-berge
- https://www.ctan.org/pkg/tkz-graph
Updating those links in the warnings and in the LaTeX tutorial is tracked at
- https://trac.sagemath.org/ticket/30212
Fri, 24 Jul 2020 16:42:07 +0200https://ask.sagemath.org/question/52649/how-to-latex-the-graph-of-a-permutation/?answer=52655#post-id-52655Comment by magviana for <p>Slightly annoying indeed: the <code>show</code> method for a permutation</p>
<ul>
<li>computes a graph corresponding to that permutation</li>
<li>displays it</li>
<li>but does not return it</li>
</ul>
<p>Instead it returns nothing, which in Python amounts to returning <code>None</code>.</p>
<p>That method should be decomposed into a method that provides
the graph and a method that shows it.</p>
<p>Here is a workaround until such a method is provided.</p>
<p>One can check what the <code>show</code> method does by inspecting the source code:</p>
<pre><code>sage: p = Permutation([3, 1, 2])
sage: p.show??
</code></pre>
<p>This helps us figure out how to produce the graph.</p>
<pre><code>sage: p = Permutation([3, 1, 2])
sage: g = DiGraph([(i+1, p[i]) for i in range(len(p))], loops=True)
sage: g
Looped digraph on 3 vertices
</code></pre>
<p>Now we can get the LaTeX TikZ code for it:</p>
<pre><code>sage: latex(g)
\begin{tikzpicture}
\definecolor{cv0}{rgb}{0.0,0.0,0.0}
\definecolor{cfv0}{rgb}{1.0,1.0,1.0}
\definecolor{clv0}{rgb}{0.0,0.0,0.0}
\definecolor{cv1}{rgb}{0.0,0.0,0.0}
\definecolor{cfv1}{rgb}{1.0,1.0,1.0}
\definecolor{clv1}{rgb}{0.0,0.0,0.0}
\definecolor{cv2}{rgb}{0.0,0.0,0.0}
\definecolor{cfv2}{rgb}{1.0,1.0,1.0}
\definecolor{clv2}{rgb}{0.0,0.0,0.0}
\definecolor{cv0v2}{rgb}{0.0,0.0,0.0}
\definecolor{cv1v0}{rgb}{0.0,0.0,0.0}
\definecolor{cv2v1}{rgb}{0.0,0.0,0.0}
%
\Vertex[style={minimum size=1.0cm,draw=cv0,fill=cfv0,text=clv0,shape=circle},LabelOut=false,L=\hbox{$1$},x=1.6204cm,y=5.0cm]{v0}
\Vertex[style={minimum size=1.0cm,draw=cv1,fill=cfv1,text=clv1,shape=circle},LabelOut=false,L=\hbox{$2$},x=0.0cm,y=0.0cm]{v1}
\Vertex[style={minimum size=1.0cm,draw=cv2,fill=cfv2,text=clv2,shape=circle},LabelOut=false,L=\hbox{$3$},x=5.0cm,y=1.1015cm]{v2}
%
\Edge[lw=0.1cm,style={post, bend right,color=cv0v2,},](v0)(v2)
\Edge[lw=0.1cm,style={post, bend right,color=cv1v0,},](v1)(v0)
\Edge[lw=0.1cm,style={post, bend right,color=cv2v1,},](v2)(v1)
%
\end{tikzpicture}
</code></pre>
<p>This might print a couple of warnings about required LaTeX packages.</p>
<p>The urls for them are outdated, see instead</p>
<ul>
<li><a href="https://www.ctan.org/pkg/tkz-berge">https://www.ctan.org/pkg/tkz-berge</a></li>
<li><a href="https://www.ctan.org/pkg/tkz-graph">https://www.ctan.org/pkg/tkz-graph</a></li>
</ul>
<p>Updating those links in the warnings and in the LaTeX tutorial is tracked at</p>
<ul>
<li><a href="https://trac.sagemath.org/ticket/30212">https://trac.sagemath.org/ticket/30212</a></li>
</ul>
https://ask.sagemath.org/question/52649/how-to-latex-the-graph-of-a-permutation/?comment=52749#post-id-52749It would be nice to be able to adapt it to specific representations such as
Permutation([3,1,2]).show(representation = "braid")Wed, 29 Jul 2020 20:50:27 +0200https://ask.sagemath.org/question/52649/how-to-latex-the-graph-of-a-permutation/?comment=52749#post-id-52749Comment by slelievre for <p>Slightly annoying indeed: the <code>show</code> method for a permutation</p>
<ul>
<li>computes a graph corresponding to that permutation</li>
<li>displays it</li>
<li>but does not return it</li>
</ul>
<p>Instead it returns nothing, which in Python amounts to returning <code>None</code>.</p>
<p>That method should be decomposed into a method that provides
the graph and a method that shows it.</p>
<p>Here is a workaround until such a method is provided.</p>
<p>One can check what the <code>show</code> method does by inspecting the source code:</p>
<pre><code>sage: p = Permutation([3, 1, 2])
sage: p.show??
</code></pre>
<p>This helps us figure out how to produce the graph.</p>
<pre><code>sage: p = Permutation([3, 1, 2])
sage: g = DiGraph([(i+1, p[i]) for i in range(len(p))], loops=True)
sage: g
Looped digraph on 3 vertices
</code></pre>
<p>Now we can get the LaTeX TikZ code for it:</p>
<pre><code>sage: latex(g)
\begin{tikzpicture}
\definecolor{cv0}{rgb}{0.0,0.0,0.0}
\definecolor{cfv0}{rgb}{1.0,1.0,1.0}
\definecolor{clv0}{rgb}{0.0,0.0,0.0}
\definecolor{cv1}{rgb}{0.0,0.0,0.0}
\definecolor{cfv1}{rgb}{1.0,1.0,1.0}
\definecolor{clv1}{rgb}{0.0,0.0,0.0}
\definecolor{cv2}{rgb}{0.0,0.0,0.0}
\definecolor{cfv2}{rgb}{1.0,1.0,1.0}
\definecolor{clv2}{rgb}{0.0,0.0,0.0}
\definecolor{cv0v2}{rgb}{0.0,0.0,0.0}
\definecolor{cv1v0}{rgb}{0.0,0.0,0.0}
\definecolor{cv2v1}{rgb}{0.0,0.0,0.0}
%
\Vertex[style={minimum size=1.0cm,draw=cv0,fill=cfv0,text=clv0,shape=circle},LabelOut=false,L=\hbox{$1$},x=1.6204cm,y=5.0cm]{v0}
\Vertex[style={minimum size=1.0cm,draw=cv1,fill=cfv1,text=clv1,shape=circle},LabelOut=false,L=\hbox{$2$},x=0.0cm,y=0.0cm]{v1}
\Vertex[style={minimum size=1.0cm,draw=cv2,fill=cfv2,text=clv2,shape=circle},LabelOut=false,L=\hbox{$3$},x=5.0cm,y=1.1015cm]{v2}
%
\Edge[lw=0.1cm,style={post, bend right,color=cv0v2,},](v0)(v2)
\Edge[lw=0.1cm,style={post, bend right,color=cv1v0,},](v1)(v0)
\Edge[lw=0.1cm,style={post, bend right,color=cv2v1,},](v2)(v1)
%
\end{tikzpicture}
</code></pre>
<p>This might print a couple of warnings about required LaTeX packages.</p>
<p>The urls for them are outdated, see instead</p>
<ul>
<li><a href="https://www.ctan.org/pkg/tkz-berge">https://www.ctan.org/pkg/tkz-berge</a></li>
<li><a href="https://www.ctan.org/pkg/tkz-graph">https://www.ctan.org/pkg/tkz-graph</a></li>
</ul>
<p>Updating those links in the warnings and in the LaTeX tutorial is tracked at</p>
<ul>
<li><a href="https://trac.sagemath.org/ticket/30212">https://trac.sagemath.org/ticket/30212</a></li>
</ul>
https://ask.sagemath.org/question/52649/how-to-latex-the-graph-of-a-permutation/?comment=52658#post-id-52658Adding a method to return the graph for a permutation is now tracked at:
- [Sage Trac ticket 30214: Add graph method to permutations](https://trac.sagemath.org/ticket/30214)Fri, 24 Jul 2020 23:40:24 +0200https://ask.sagemath.org/question/52649/how-to-latex-the-graph-of-a-permutation/?comment=52658#post-id-52658Comment by magviana for <p>Slightly annoying indeed: the <code>show</code> method for a permutation</p>
<ul>
<li>computes a graph corresponding to that permutation</li>
<li>displays it</li>
<li>but does not return it</li>
</ul>
<p>Instead it returns nothing, which in Python amounts to returning <code>None</code>.</p>
<p>That method should be decomposed into a method that provides
the graph and a method that shows it.</p>
<p>Here is a workaround until such a method is provided.</p>
<p>One can check what the <code>show</code> method does by inspecting the source code:</p>
<pre><code>sage: p = Permutation([3, 1, 2])
sage: p.show??
</code></pre>
<p>This helps us figure out how to produce the graph.</p>
<pre><code>sage: p = Permutation([3, 1, 2])
sage: g = DiGraph([(i+1, p[i]) for i in range(len(p))], loops=True)
sage: g
Looped digraph on 3 vertices
</code></pre>
<p>Now we can get the LaTeX TikZ code for it:</p>
<pre><code>sage: latex(g)
\begin{tikzpicture}
\definecolor{cv0}{rgb}{0.0,0.0,0.0}
\definecolor{cfv0}{rgb}{1.0,1.0,1.0}
\definecolor{clv0}{rgb}{0.0,0.0,0.0}
\definecolor{cv1}{rgb}{0.0,0.0,0.0}
\definecolor{cfv1}{rgb}{1.0,1.0,1.0}
\definecolor{clv1}{rgb}{0.0,0.0,0.0}
\definecolor{cv2}{rgb}{0.0,0.0,0.0}
\definecolor{cfv2}{rgb}{1.0,1.0,1.0}
\definecolor{clv2}{rgb}{0.0,0.0,0.0}
\definecolor{cv0v2}{rgb}{0.0,0.0,0.0}
\definecolor{cv1v0}{rgb}{0.0,0.0,0.0}
\definecolor{cv2v1}{rgb}{0.0,0.0,0.0}
%
\Vertex[style={minimum size=1.0cm,draw=cv0,fill=cfv0,text=clv0,shape=circle},LabelOut=false,L=\hbox{$1$},x=1.6204cm,y=5.0cm]{v0}
\Vertex[style={minimum size=1.0cm,draw=cv1,fill=cfv1,text=clv1,shape=circle},LabelOut=false,L=\hbox{$2$},x=0.0cm,y=0.0cm]{v1}
\Vertex[style={minimum size=1.0cm,draw=cv2,fill=cfv2,text=clv2,shape=circle},LabelOut=false,L=\hbox{$3$},x=5.0cm,y=1.1015cm]{v2}
%
\Edge[lw=0.1cm,style={post, bend right,color=cv0v2,},](v0)(v2)
\Edge[lw=0.1cm,style={post, bend right,color=cv1v0,},](v1)(v0)
\Edge[lw=0.1cm,style={post, bend right,color=cv2v1,},](v2)(v1)
%
\end{tikzpicture}
</code></pre>
<p>This might print a couple of warnings about required LaTeX packages.</p>
<p>The urls for them are outdated, see instead</p>
<ul>
<li><a href="https://www.ctan.org/pkg/tkz-berge">https://www.ctan.org/pkg/tkz-berge</a></li>
<li><a href="https://www.ctan.org/pkg/tkz-graph">https://www.ctan.org/pkg/tkz-graph</a></li>
</ul>
<p>Updating those links in the warnings and in the LaTeX tutorial is tracked at</p>
<ul>
<li><a href="https://trac.sagemath.org/ticket/30212">https://trac.sagemath.org/ticket/30212</a></li>
</ul>
https://ask.sagemath.org/question/52649/how-to-latex-the-graph-of-a-permutation/?comment=52676#post-id-52676I implemented the graph of a permutation function amenable to TeX as:
def pgraph(perm):
return DiGraph([(i+1, perm[i]) for i in range(len(perm))], loops=True)Sun, 26 Jul 2020 00:03:27 +0200https://ask.sagemath.org/question/52649/how-to-latex-the-graph-of-a-permutation/?comment=52676#post-id-52676Comment by magviana for <p>Slightly annoying indeed: the <code>show</code> method for a permutation</p>
<ul>
<li>computes a graph corresponding to that permutation</li>
<li>displays it</li>
<li>but does not return it</li>
</ul>
<p>Instead it returns nothing, which in Python amounts to returning <code>None</code>.</p>
<p>That method should be decomposed into a method that provides
the graph and a method that shows it.</p>
<p>Here is a workaround until such a method is provided.</p>
<p>One can check what the <code>show</code> method does by inspecting the source code:</p>
<pre><code>sage: p = Permutation([3, 1, 2])
sage: p.show??
</code></pre>
<p>This helps us figure out how to produce the graph.</p>
<pre><code>sage: p = Permutation([3, 1, 2])
sage: g = DiGraph([(i+1, p[i]) for i in range(len(p))], loops=True)
sage: g
Looped digraph on 3 vertices
</code></pre>
<p>Now we can get the LaTeX TikZ code for it:</p>
<pre><code>sage: latex(g)
\begin{tikzpicture}
\definecolor{cv0}{rgb}{0.0,0.0,0.0}
\definecolor{cfv0}{rgb}{1.0,1.0,1.0}
\definecolor{clv0}{rgb}{0.0,0.0,0.0}
\definecolor{cv1}{rgb}{0.0,0.0,0.0}
\definecolor{cfv1}{rgb}{1.0,1.0,1.0}
\definecolor{clv1}{rgb}{0.0,0.0,0.0}
\definecolor{cv2}{rgb}{0.0,0.0,0.0}
\definecolor{cfv2}{rgb}{1.0,1.0,1.0}
\definecolor{clv2}{rgb}{0.0,0.0,0.0}
\definecolor{cv0v2}{rgb}{0.0,0.0,0.0}
\definecolor{cv1v0}{rgb}{0.0,0.0,0.0}
\definecolor{cv2v1}{rgb}{0.0,0.0,0.0}
%
\Vertex[style={minimum size=1.0cm,draw=cv0,fill=cfv0,text=clv0,shape=circle},LabelOut=false,L=\hbox{$1$},x=1.6204cm,y=5.0cm]{v0}
\Vertex[style={minimum size=1.0cm,draw=cv1,fill=cfv1,text=clv1,shape=circle},LabelOut=false,L=\hbox{$2$},x=0.0cm,y=0.0cm]{v1}
\Vertex[style={minimum size=1.0cm,draw=cv2,fill=cfv2,text=clv2,shape=circle},LabelOut=false,L=\hbox{$3$},x=5.0cm,y=1.1015cm]{v2}
%
\Edge[lw=0.1cm,style={post, bend right,color=cv0v2,},](v0)(v2)
\Edge[lw=0.1cm,style={post, bend right,color=cv1v0,},](v1)(v0)
\Edge[lw=0.1cm,style={post, bend right,color=cv2v1,},](v2)(v1)
%
\end{tikzpicture}
</code></pre>
<p>This might print a couple of warnings about required LaTeX packages.</p>
<p>The urls for them are outdated, see instead</p>
<ul>
<li><a href="https://www.ctan.org/pkg/tkz-berge">https://www.ctan.org/pkg/tkz-berge</a></li>
<li><a href="https://www.ctan.org/pkg/tkz-graph">https://www.ctan.org/pkg/tkz-graph</a></li>
</ul>
<p>Updating those links in the warnings and in the LaTeX tutorial is tracked at</p>
<ul>
<li><a href="https://trac.sagemath.org/ticket/30212">https://trac.sagemath.org/ticket/30212</a></li>
</ul>
https://ask.sagemath.org/question/52649/how-to-latex-the-graph-of-a-permutation/?comment=52661#post-id-52661Thanks --- I implemented the graph viewer as
def pgraph(perm): ## permutation graph amenable to latex
return DiGraph([(i+1, perm[i]) for i in range(len(perm))], loops=True)Sat, 25 Jul 2020 00:26:08 +0200https://ask.sagemath.org/question/52649/how-to-latex-the-graph-of-a-permutation/?comment=52661#post-id-52661Answer by Sébastien for <p>Trying to LaTeX the graph generated by something like <br>
Permutation([3,1,2]).show()
The command
latex(Permutation([3,1,2]).show()) <br>
returns the graph followed by \mathrm{None}</p>
https://ask.sagemath.org/question/52649/how-to-latex-the-graph-of-a-permutation/?answer=52651#post-id-52651Unfortunately, `p.show` computes the graph internally and just outputs the figure output. Put, we can do it from scratch:
sage: p = Permutation([3,1,2])
sage: edges = list(enumerate(p,start=1))
sage: d = DiGraph(edges, format='list_of_edges', loops=True)
sage: d
Looped digraph on 3 vertices
sage: latex(d) # prints the latexoutput in the default tikz
sage: view(d) # opens the pdf in a viewer
There is another way to use graphviz + dot2tex, but I always forget how to do it. Personnaly, I like to use the TikzPicture class from my package which sets up properly the options to use graphviz + dot2tex to constructs the tikzpicture:
sage: from slabbe import TikzPicture
sage: tikz = TikzPicture.from_graph(d)
sage: print(tikz)
\documentclass[tikz]{standalone}
\standaloneconfig{border=4mm}
\usepackage{amsmath}
\begin{document}
\begin{tikzpicture}[>=latex,line join=bevel,]
%%
\node (node_0) at (23.0bp,104.5bp) [draw,draw=none] {$1$};
\node (node_2) at (6.0bp,55.5bp) [draw,draw=none] {$3$};
\node (node_1) at (23.0bp,6.5bp) [draw,draw=none] {$2$};
\draw [black,->] (node_0) ..controls (18.571bp,91.254bp) and (14.669bp,80.466bp) .. (node_2);
\draw [black,->] (node_1) ..controls (23.0bp,27.341bp) and (23.0bp,65.653bp) .. (node_0);
\draw [black,->] (node_2) ..controls (10.429bp,42.254bp) and (14.331bp,31.466bp) .. (node_1);
%
\end{tikzpicture}
\end{document}
sage: tikz.pdf() # opens up a viewer
sage: tikz.pdf('filename.pdf') # saves the pdf file
To install those optional packages, you do:
sage -i dot2tex
sage -pip install slabbe
At some point, I might move the TikzPicture code from my package to sage. Is this something you would like for instance?Fri, 24 Jul 2020 15:35:01 +0200https://ask.sagemath.org/question/52649/how-to-latex-the-graph-of-a-permutation/?answer=52651#post-id-52651Comment by slelievre for <p>Unfortunately, <code>p.show</code> computes the graph internally and just outputs the figure output. Put, we can do it from scratch:</p>
<pre><code>sage: p = Permutation([3,1,2])
sage: edges = list(enumerate(p,start=1))
sage: d = DiGraph(edges, format='list_of_edges', loops=True)
sage: d
Looped digraph on 3 vertices
sage: latex(d) # prints the latexoutput in the default tikz
sage: view(d) # opens the pdf in a viewer
</code></pre>
<p>There is another way to use graphviz + dot2tex, but I always forget how to do it. Personnaly, I like to use the TikzPicture class from my package which sets up properly the options to use graphviz + dot2tex to constructs the tikzpicture:</p>
<pre><code>sage: from slabbe import TikzPicture
sage: tikz = TikzPicture.from_graph(d)
sage: print(tikz)
\documentclass[tikz]{standalone}
\standaloneconfig{border=4mm}
\usepackage{amsmath}
\begin{document}
\begin{tikzpicture}[>=latex,line join=bevel,]
%%
\node (node_0) at (23.0bp,104.5bp) [draw,draw=none] {$1$};
\node (node_2) at (6.0bp,55.5bp) [draw,draw=none] {$3$};
\node (node_1) at (23.0bp,6.5bp) [draw,draw=none] {$2$};
\draw [black,->] (node_0) ..controls (18.571bp,91.254bp) and (14.669bp,80.466bp) .. (node_2);
\draw [black,->] (node_1) ..controls (23.0bp,27.341bp) and (23.0bp,65.653bp) .. (node_0);
\draw [black,->] (node_2) ..controls (10.429bp,42.254bp) and (14.331bp,31.466bp) .. (node_1);
%
\end{tikzpicture}
\end{document}
sage: tikz.pdf() # opens up a viewer
sage: tikz.pdf('filename.pdf') # saves the pdf file
</code></pre>
<p>To install those optional packages, you do:</p>
<pre><code>sage -i dot2tex
sage -pip install slabbe
</code></pre>
<p>At some point, I might move the TikzPicture code from my package to sage. Is this something you would like for instance?</p>
https://ask.sagemath.org/question/52649/how-to-latex-the-graph-of-a-permutation/?comment=52745#post-id-52745Related question on sage-support a few days later:
- [sage-support, 2020-07-29, sometimes show just display broken latex](https://groups.google.com/d/topic/sage-support/oM3jVh8NzAA/discussion)Wed, 29 Jul 2020 18:40:31 +0200https://ask.sagemath.org/question/52649/how-to-latex-the-graph-of-a-permutation/?comment=52745#post-id-52745Comment by magviana for <p>Unfortunately, <code>p.show</code> computes the graph internally and just outputs the figure output. Put, we can do it from scratch:</p>
<pre><code>sage: p = Permutation([3,1,2])
sage: edges = list(enumerate(p,start=1))
sage: d = DiGraph(edges, format='list_of_edges', loops=True)
sage: d
Looped digraph on 3 vertices
sage: latex(d) # prints the latexoutput in the default tikz
sage: view(d) # opens the pdf in a viewer
</code></pre>
<p>There is another way to use graphviz + dot2tex, but I always forget how to do it. Personnaly, I like to use the TikzPicture class from my package which sets up properly the options to use graphviz + dot2tex to constructs the tikzpicture:</p>
<pre><code>sage: from slabbe import TikzPicture
sage: tikz = TikzPicture.from_graph(d)
sage: print(tikz)
\documentclass[tikz]{standalone}
\standaloneconfig{border=4mm}
\usepackage{amsmath}
\begin{document}
\begin{tikzpicture}[>=latex,line join=bevel,]
%%
\node (node_0) at (23.0bp,104.5bp) [draw,draw=none] {$1$};
\node (node_2) at (6.0bp,55.5bp) [draw,draw=none] {$3$};
\node (node_1) at (23.0bp,6.5bp) [draw,draw=none] {$2$};
\draw [black,->] (node_0) ..controls (18.571bp,91.254bp) and (14.669bp,80.466bp) .. (node_2);
\draw [black,->] (node_1) ..controls (23.0bp,27.341bp) and (23.0bp,65.653bp) .. (node_0);
\draw [black,->] (node_2) ..controls (10.429bp,42.254bp) and (14.331bp,31.466bp) .. (node_1);
%
\end{tikzpicture}
\end{document}
sage: tikz.pdf() # opens up a viewer
sage: tikz.pdf('filename.pdf') # saves the pdf file
</code></pre>
<p>To install those optional packages, you do:</p>
<pre><code>sage -i dot2tex
sage -pip install slabbe
</code></pre>
<p>At some point, I might move the TikzPicture code from my package to sage. Is this something you would like for instance?</p>
https://ask.sagemath.org/question/52649/how-to-latex-the-graph-of-a-permutation/?comment=52662#post-id-52662Not specifically as the graphs are just for illustration in a larger text document, so I just cut/paste the LaTeX output. I implemented it as
def pgraph(perm): ## permutation graph amenable to latex
return DiGraph([(i+1, perm[i]) for i in range(len(perm))], loops=True)Sat, 25 Jul 2020 00:31:36 +0200https://ask.sagemath.org/question/52649/how-to-latex-the-graph-of-a-permutation/?comment=52662#post-id-52662