Draw a graph of matrices in LaTeX using Sage
18 octobre 2012 | Catégories: latex, sage | View CommentsIn version 5.3 of Sage, if the vertices of a graph are matrices, then the default latex output does not compile. The default format is a tikzpicture and uses the tkz-berge library. One solution is to change the format to dot2tex. See below.
The default tikz output is:
sage: m = matrix(3, range(9)) sage: m.set_immutable() sage: G = Graph() sage: G.add_vertex(m) sage: latex(G) \begin{tikzpicture} % \useasboundingbox (0,0) rectangle (5.0cm,5.0cm); % \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} % \Vertex[style={minimum size=1.0cm,draw=cv0,fill=cfv0,text=clv0,shape=circle}, LabelOut=false,L=\hbox{$\left(\begin{array}{rrr} 0 & 1 & 2 \\ 3 & 4 & 5 \\ 6 & 7 & 8 \end{array}\right)$},x=2.5cm,y=2.5cm]{v0} % % \end{tikzpicture}
This output does not compile. Here is the error I get:
sage: view(G) An error occurred. This is pdfTeX, Version 3.1415926-1.40.10 (TeX Live 2009) (format=pdflatex 2011.4.19) 18 OCT 2012 21:34 entering extended mode [...] ! Use of \tikz@@scope@env doesn't match its definition. \pgfutil@ifnextchar #1#2#3->\let \pgfutil@reserved@d =#1\def \pgfutil@reserved@a { #2}\def \pgf util@reserved@b {#3}\futurelet \pgfutil@let@token \pgfutil@ifnch l.54 \end{array}\right)$},x=2.5cm,y=2.5cm]{v0} If you say, e.g., `\def\a1{...}', then you must always put `1' after `\a', since control sequence names are made up of letters only. The macro here has not been followed by the required stuff, so I'm ignoring it. ! Use of \tikz@@scope@env doesn't match its definition. [...] ! ==> Fatal error occurred, no output PDF file produced! Latex error
The command to install dot2tex:
sage -i dot2tex-2.8.7-2
As the documentation of G.layout_graphviz() says, install graphviz >= 2.14 so that the programs dot, neato, ... are in your path. This allows the following to work:
sage: m = matrix(3, range(9)) sage: m.set_immutable() sage: G = Graph() sage: G.add_vertex(m) sage: G.set_latex_options(format='dot2tex') sage: view(G)
To obtain the tikz code:
sage: tikz_string = G.latex_options().dot2tex_picture() sage: print tikz_string \begin{tikzpicture}[>=latex,line join=bevel,] %% \node (012345678) at (34bp,22bp) [draw,draw=none] {$\left(\begin{array}{rrr}0 & 1 & 2 \\3 & 4 & 5 \\6 & 7 & 8\end{array}\right)$}; % \end{tikzpicture}