Ask Your Question

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

edit retag close merge delete

## 2 Answers

Sort by » oldest newest most voted

Slightly 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

Updating those links in the warnings and in the LaTeX tutorial is tracked at

more

## Comments

Adding a method to return the graph for a permutation is now tracked at:

( 2020-07-24 23:40:24 +0200 )edit

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

( 2020-07-25 00:26:08 +0200 )edit

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

( 2020-07-26 00:03:27 +0200 )edit

It would be nice to be able to adapt it to specific representations such as

Permutation([3,1,2]).show(representation = "braid")

( 2020-07-29 20:50:27 +0200 )edit

Unfortunately, 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?

more

## Comments

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

( 2020-07-25 00:31:36 +0200 )edit

Related question on sage-support a few days later:

( 2020-07-29 18:40:31 +0200 )edit

## Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

## Stats

Asked: 2020-07-24 15:04:17 +0200

Seen: 123 times

Last updated: Jul 24 '20