LaTeX options for graphs¶
This module provides a class to hold, manipulate and employ various options for rendering a graph in LaTeX, in addition to providing the code that actually generates a LaTeX representation of a (combinatorial) graph.
AUTHORS:
Rob Beezer (20090520):
GraphLatex
classFidel Barerra Cruz (20090520):
tkzgraph
commands to render a graphNicolas M. Thiéry (201002): dot2tex/graphviz interface
Rob Beezer (20100529): Extended range of
tkzgraph
options
LaTeX Versions of Graphs¶
Many mathematical objects in Sage have LaTeX representations, and graphs are no
exception. For a graph g
, the command view(g)
, issued at the Sage
command line or in the notebook, will create a graphic version of g
.
Similarly, latex(g)
will return a (long) string that is a representation of
the graph in LaTeX. Other ways of employing LaTeX in Sage, such as %latex
in a notebook cell, or the Typeset checkbox in the notebook, will handle g
appropriately.
Support through the tkzgraph
package is by Alain Matthes, the author of
tkzgraph
, whose work can be found at https://www.ctan.org/pkg/tkzgraph.
The range of possible options for customizing the appearance of a graph are
carefully documented at sage.graphs.graph_latex.GraphLatex.set_option()
.
As a broad overview, the following options are supported:
Prebuilt Styles: the prebuilt styles of the tkzgraph package provide nice drawings quickly
Dimensions: can be specified in natural units, then uniformly scaled after design work
Vertex Colors: the perimeter and fill color for vertices can be specified, including on a pervertex basis
Vertex Shapes: may be circles, shaded spheres, rectangles or diamonds, including on a pervertex basis
Vertex Sizes: may be specified as minimums, and will automatically sized to contain vertex labels, including on a pervertex basis
Vertex Labels: can use latex formatting, and may have their colors specified, including on a pervertex basis
Vertex Label Placement: can be interior to the vertex, or external at a configurable location
Edge Colors: a solid color with or without a second color down the middle, on a peredge basis
Edge Thickness: can be set, including on a peredge basis
Edge Labels: can use latex formatting, and may have their colors specified, including on a peredge basis
Edge Label Placement: can be to the left, right, above, below, inline, and then sloped or horizontal
Digraph Edges: are slightly curved, with arrowheads
Loops: may be specified by their size, and with a direction equaling one of the four compass points
To use LaTeX in Sage you of course need a working TeX installation and it will
work best if you have the dvipng
and convert
utilities. For graphs you
need the tkzgraph.sty
and tkzberge.sty
style files of the tkzgraph
package. TeX, dvipng, and convert should be widely available through package
managers or installers. You may need to install the tkzgraph style files in
the appropriate locations, a task beyond the scope of this introduction.
Primary locations for these programs are:
TeX: http://ctan.org/
convert: http://www.imagemagick.org (the ImageMagick suite)
tkzgraph: https://www.ctan.org/pkg/tkzgraph
Customizing the output is accomplished in several ways. Suppose g
is a
graph, then g.set_latex_options()
can be used to efficiently set or modify
various options. Setting individual options, or querying options, can be
accomplished by first using a command like opts = g.latex_options()
to
obtain a sage.graphs.graph_latex.GraphLatex
object which has several
methods to set and retrieve options.
Here is a minimal session demonstrating how to use these features. The following setup should work in the notebook or at the commandline.:
sage: H = graphs.HeawoodGraph()
sage: H.set_latex_options(
....: graphic_size=(5,5),
....: vertex_size=0.2,
....: edge_thickness=0.04,
....: edge_color='green',
....: vertex_color='green',
....: vertex_label_color='red'
....: )
At this point, view(H)
should call pdflatex
to process the string
created by latex(H)
and then display the resulting graphic.
To use this image in a LaTeX document, you could of course just copy and save
the resulting graphic. However, the latex()
command will produce the
underlying LaTeX code, which can be incorporated into a standalone LaTeX
document.:
sage: from sage.graphs.graph_latex import check_tkz_graph
sage: check_tkz_graph() # random  depends on TeX installation
sage: latex(H)
\begin{tikzpicture}
\definecolor{cv0}{rgb}{0.0,0.502,0.0}
\definecolor{cfv0}{rgb}{1.0,1.0,1.0}
\definecolor{clv0}{rgb}{1.0,0.0,0.0}
\definecolor{cv1}{rgb}{0.0,0.502,0.0}
\definecolor{cfv1}{rgb}{1.0,1.0,1.0}
\definecolor{clv1}{rgb}{1.0,0.0,0.0}
\definecolor{cv2}{rgb}{0.0,0.502,0.0}
\definecolor{cfv2}{rgb}{1.0,1.0,1.0}
\definecolor{clv2}{rgb}{1.0,0.0,0.0}
\definecolor{cv3}{rgb}{0.0,0.502,0.0}
\definecolor{cfv3}{rgb}{1.0,1.0,1.0}
\definecolor{clv3}{rgb}{1.0,0.0,0.0}
\definecolor{cv4}{rgb}{0.0,0.502,0.0}
\definecolor{cfv4}{rgb}{1.0,1.0,1.0}
\definecolor{clv4}{rgb}{1.0,0.0,0.0}
\definecolor{cv5}{rgb}{0.0,0.502,0.0}
\definecolor{cfv5}{rgb}{1.0,1.0,1.0}
\definecolor{clv5}{rgb}{1.0,0.0,0.0}
\definecolor{cv6}{rgb}{0.0,0.502,0.0}
\definecolor{cfv6}{rgb}{1.0,1.0,1.0}
\definecolor{clv6}{rgb}{1.0,0.0,0.0}
\definecolor{cv7}{rgb}{0.0,0.502,0.0}
\definecolor{cfv7}{rgb}{1.0,1.0,1.0}
\definecolor{clv7}{rgb}{1.0,0.0,0.0}
\definecolor{cv8}{rgb}{0.0,0.502,0.0}
\definecolor{cfv8}{rgb}{1.0,1.0,1.0}
\definecolor{clv8}{rgb}{1.0,0.0,0.0}
\definecolor{cv9}{rgb}{0.0,0.502,0.0}
\definecolor{cfv9}{rgb}{1.0,1.0,1.0}
\definecolor{clv9}{rgb}{1.0,0.0,0.0}
\definecolor{cv10}{rgb}{0.0,0.502,0.0}
\definecolor{cfv10}{rgb}{1.0,1.0,1.0}
\definecolor{clv10}{rgb}{1.0,0.0,0.0}
\definecolor{cv11}{rgb}{0.0,0.502,0.0}
\definecolor{cfv11}{rgb}{1.0,1.0,1.0}
\definecolor{clv11}{rgb}{1.0,0.0,0.0}
\definecolor{cv12}{rgb}{0.0,0.502,0.0}
\definecolor{cfv12}{rgb}{1.0,1.0,1.0}
\definecolor{clv12}{rgb}{1.0,0.0,0.0}
\definecolor{cv13}{rgb}{0.0,0.502,0.0}
\definecolor{cfv13}{rgb}{1.0,1.0,1.0}
\definecolor{clv13}{rgb}{1.0,0.0,0.0}
\definecolor{cv0v1}{rgb}{0.0,0.502,0.0}
\definecolor{cv0v5}{rgb}{0.0,0.502,0.0}
\definecolor{cv0v13}{rgb}{0.0,0.502,0.0}
\definecolor{cv1v2}{rgb}{0.0,0.502,0.0}
\definecolor{cv1v10}{rgb}{0.0,0.502,0.0}
\definecolor{cv2v3}{rgb}{0.0,0.502,0.0}
\definecolor{cv2v7}{rgb}{0.0,0.502,0.0}
\definecolor{cv3v4}{rgb}{0.0,0.502,0.0}
\definecolor{cv3v12}{rgb}{0.0,0.502,0.0}
\definecolor{cv4v5}{rgb}{0.0,0.502,0.0}
\definecolor{cv4v9}{rgb}{0.0,0.502,0.0}
\definecolor{cv5v6}{rgb}{0.0,0.502,0.0}
\definecolor{cv6v7}{rgb}{0.0,0.502,0.0}
\definecolor{cv6v11}{rgb}{0.0,0.502,0.0}
\definecolor{cv7v8}{rgb}{0.0,0.502,0.0}
\definecolor{cv8v9}{rgb}{0.0,0.502,0.0}
\definecolor{cv8v13}{rgb}{0.0,0.502,0.0}
\definecolor{cv9v10}{rgb}{0.0,0.502,0.0}
\definecolor{cv10v11}{rgb}{0.0,0.502,0.0}
\definecolor{cv11v12}{rgb}{0.0,0.502,0.0}
\definecolor{cv12v13}{rgb}{0.0,0.502,0.0}
%
\Vertex[style={minimum size=0.2cm,draw=cv0,fill=cfv0,text=clv0,shape=circle},LabelOut=false,L=\hbox{$0$},x=2.5cm,y=5.0cm]{v0}
\Vertex[style={minimum size=0.2cm,draw=cv1,fill=cfv1,text=clv1,shape=circle},LabelOut=false,L=\hbox{$1$},x=1.3874cm,y=4.7524cm]{v1}
\Vertex[style={minimum size=0.2cm,draw=cv2,fill=cfv2,text=clv2,shape=circle},LabelOut=false,L=\hbox{$2$},x=0.4952cm,y=4.0587cm]{v2}
\Vertex[style={minimum size=0.2cm,draw=cv3,fill=cfv3,text=clv3,shape=circle},LabelOut=false,L=\hbox{$3$},x=0.0cm,y=3.0563cm]{v3}
\Vertex[style={minimum size=0.2cm,draw=cv4,fill=cfv4,text=clv4,shape=circle},LabelOut=false,L=\hbox{$4$},x=0.0cm,y=1.9437cm]{v4}
\Vertex[style={minimum size=0.2cm,draw=cv5,fill=cfv5,text=clv5,shape=circle},LabelOut=false,L=\hbox{$5$},x=0.4952cm,y=0.9413cm]{v5}
\Vertex[style={minimum size=0.2cm,draw=cv6,fill=cfv6,text=clv6,shape=circle},LabelOut=false,L=\hbox{$6$},x=1.3874cm,y=0.2476cm]{v6}
\Vertex[style={minimum size=0.2cm,draw=cv7,fill=cfv7,text=clv7,shape=circle},LabelOut=false,L=\hbox{$7$},x=2.5cm,y=0.0cm]{v7}
\Vertex[style={minimum size=0.2cm,draw=cv8,fill=cfv8,text=clv8,shape=circle},LabelOut=false,L=\hbox{$8$},x=3.6126cm,y=0.2476cm]{v8}
\Vertex[style={minimum size=0.2cm,draw=cv9,fill=cfv9,text=clv9,shape=circle},LabelOut=false,L=\hbox{$9$},x=4.5048cm,y=0.9413cm]{v9}
\Vertex[style={minimum size=0.2cm,draw=cv10,fill=cfv10,text=clv10,shape=circle},LabelOut=false,L=\hbox{$10$},x=5.0cm,y=1.9437cm]{v10}
\Vertex[style={minimum size=0.2cm,draw=cv11,fill=cfv11,text=clv11,shape=circle},LabelOut=false,L=\hbox{$11$},x=5.0cm,y=3.0563cm]{v11}
\Vertex[style={minimum size=0.2cm,draw=cv12,fill=cfv12,text=clv12,shape=circle},LabelOut=false,L=\hbox{$12$},x=4.5048cm,y=4.0587cm]{v12}
\Vertex[style={minimum size=0.2cm,draw=cv13,fill=cfv13,text=clv13,shape=circle},LabelOut=false,L=\hbox{$13$},x=3.6126cm,y=4.7524cm]{v13}
%
\Edge[lw=0.04cm,style={color=cv0v1,},](v0)(v1)
\Edge[lw=0.04cm,style={color=cv0v5,},](v0)(v5)
\Edge[lw=0.04cm,style={color=cv0v13,},](v0)(v13)
\Edge[lw=0.04cm,style={color=cv1v2,},](v1)(v2)
\Edge[lw=0.04cm,style={color=cv1v10,},](v1)(v10)
\Edge[lw=0.04cm,style={color=cv2v3,},](v2)(v3)
\Edge[lw=0.04cm,style={color=cv2v7,},](v2)(v7)
\Edge[lw=0.04cm,style={color=cv3v4,},](v3)(v4)
\Edge[lw=0.04cm,style={color=cv3v12,},](v3)(v12)
\Edge[lw=0.04cm,style={color=cv4v5,},](v4)(v5)
\Edge[lw=0.04cm,style={color=cv4v9,},](v4)(v9)
\Edge[lw=0.04cm,style={color=cv5v6,},](v5)(v6)
\Edge[lw=0.04cm,style={color=cv6v7,},](v6)(v7)
\Edge[lw=0.04cm,style={color=cv6v11,},](v6)(v11)
\Edge[lw=0.04cm,style={color=cv7v8,},](v7)(v8)
\Edge[lw=0.04cm,style={color=cv8v9,},](v8)(v9)
\Edge[lw=0.04cm,style={color=cv8v13,},](v8)(v13)
\Edge[lw=0.04cm,style={color=cv9v10,},](v9)(v10)
\Edge[lw=0.04cm,style={color=cv10v11,},](v10)(v11)
\Edge[lw=0.04cm,style={color=cv11v12,},](v11)(v12)
\Edge[lw=0.04cm,style={color=cv12v13,},](v12)(v13)
%
\end{tikzpicture}
EXAMPLES:
This example illustrates switching between the builtin styles when using the tkz_graph format.:
sage: g = graphs.PetersenGraph()
sage: g.set_latex_options(tkz_style='Classic')
sage: from sage.graphs.graph_latex import check_tkz_graph
sage: check_tkz_graph() # random  depends on TeX installation
sage: latex(g)
\begin{tikzpicture}
\GraphInit[vstyle=Classic]
...
\end{tikzpicture}
sage: opts = g.latex_options()
sage: opts
LaTeX options for Petersen graph: {'tkz_style': 'Classic'}
sage: g.set_latex_options(tkz_style = 'Art')
sage: opts.get_option('tkz_style')
'Art'
sage: opts
LaTeX options for Petersen graph: {'tkz_style': 'Art'}
sage: latex(g)
\begin{tikzpicture}
\GraphInit[vstyle=Art]
...
\end{tikzpicture}
This example illustrates using the optional dot2tex module:
sage: g = graphs.PetersenGraph()
sage: g.set_latex_options(format='dot2tex', prog='neato')
sage: from sage.graphs.graph_latex import check_tkz_graph
sage: check_tkz_graph() # random  depends on TeX installation
sage: latex(g) # optional  dot2tex graphviz
\begin{tikzpicture}[>=latex,line join=bevel,]
...
\end{tikzpicture}
Among other things, this supports the flexible edge_options
option
(see sage.graphs.generic_graph.GenericGraph.graphviz_string()
);
here we color in red all edges touching the vertex 0
:
sage: g = graphs.PetersenGraph()
sage: g.set_latex_options(format="dot2tex", edge_options=lambda u_v_label: {"color": "red"} if u_v_label[0] == 0 else {})
sage: latex(g) # optional  dot2tex graphviz
\begin{tikzpicture}[>=latex,line join=bevel,]
...
\end{tikzpicture}
GraphLatex class and functions¶

class
sage.graphs.graph_latex.
GraphLatex
(graph, **options)¶ Bases:
sage.structure.sage_object.SageObject
A class to hold, manipulate and employ options for converting a graph to LaTeX.
This class serves two purposes. First it holds the values of various options designed to work with the
tkzgraph
LaTeX package for rendering graphs. As such, a graph that uses this class will hold a reference to it. Second, this class contains the code to convert a graph into the corresponding LaTeX constructs, returning a string.EXAMPLES:
sage: from sage.graphs.graph_latex import GraphLatex sage: opts = GraphLatex(graphs.PetersenGraph()) sage: opts LaTeX options for Petersen graph: {} sage: g = graphs.PetersenGraph() sage: opts = g.latex_options() sage: g == loads(dumps(g)) True

dot2tex_picture
()¶ Call
dot2tex
to construct a string of LaTeX commands representing a graph as atikzpicture
.EXAMPLES:
sage: g = digraphs.ButterflyGraph(1) sage: from sage.graphs.graph_latex import check_tkz_graph sage: check_tkz_graph() # random  depends on TeX installation sage: print(g.latex_options().dot2tex_picture()) # optional  dot2tex graphviz \begin{tikzpicture}[>=latex,line join=bevel,] %% \node (node_...) at (...bp,...bp) [draw,draw=none] {$\left(...\right)$}; \node (node_...) at (...bp,...bp) [draw,draw=none] {$\left(...\right)$}; \node (node_...) at (...bp,...bp) [draw,draw=none] {$\left(...\right)$}; \node (node_...) at (...bp,...bp) [draw,draw=none] {$\left(...\right)$}; \draw [black,>] (node_...) ..controls (...bp,...bp) and (...bp,...bp) .. (node_...); \draw [black,>] (node_...) ..controls (...bp,...bp) and (...bp,...bp) .. (node_...); \draw [black,>] (node_...) ..controls (...bp,...bp) and (...bp,...bp) .. (node_...); \draw [black,>] (node_...) ..controls (...bp,...bp) and (...bp,...bp) .. (node_...); % \end{tikzpicture}
We make sure trac ticket #13624 is fixed:
sage: G = DiGraph() sage: G.add_edge(3333, 88, 'my_label') sage: G.set_latex_options(edge_labels=True) sage: print(G.latex_options().dot2tex_picture()) # optional  dot2tex graphviz \begin{tikzpicture}[>=latex,line join=bevel,] %% \node (node_...) at (...bp,...bp) [draw,draw=none] {$...$}; \node (node_...) at (...bp,...bp) [draw,draw=none] {$...$}; \draw [black,>] (node_...) ..controls (...bp,...bp) and (...bp,...bp) .. (node_...); \definecolor{strokecol}{rgb}{0.0,0.0,0.0}; \pgfsetstrokecolor{strokecol} \draw (...bp,...bp) node {$\text{\texttt{my{\char`\_}label}}$}; % \end{tikzpicture}
Check that trac ticket #25120 is fixed:
sage: G = Graph([(0,1)]) sage: G.set_latex_options(edge_colors = {(0,1): 'red'}) sage: print(G.latex_options().dot2tex_picture()) # optional  dot2tex graphviz \begin{tikzpicture}[>=latex,line join=bevel,] ... \draw [red,] (node_0) ... (node_1); ... \end{tikzpicture}
Note
There is a lot of overlap between what
tkz_picture
anddot2tex
do. It would be best to merge them!dot2tex
probably can work withoutgraphviz
if layout information is provided.

get_option
(option_name)¶ Return the current value of the named option.
INPUT:
option_name
– the name of an option
OUTPUT:
If the name is not present in
__graphlatex_options
it is an error to ask for it. If an option has not been set then the default value is returned. Otherwise, the value of the option is returned.EXAMPLES:
sage: g = graphs.PetersenGraph() sage: opts = g.latex_options() sage: opts.set_option('tkz_style', 'Art') sage: opts.get_option('tkz_style') 'Art' sage: opts.set_option('tkz_style') sage: opts.get_option('tkz_style') == "Custom" True sage: opts.get_option('bad_name') Traceback (most recent call last): ... ValueError: bad_name is not a Latex option for a graph.

latex
()¶ Return a string in LaTeX representing a graph.
This is the command that is invoked by
sage.graphs.generic_graph.GenericGraph._latex_
for a graph, so it returns a string of LaTeX commands that can be incorporated into a LaTeX document unmodified. The exact contents of this string are influenced by the options set via the methodssage.graphs.generic_graph.GenericGraph.set_latex_options()
,set_option()
, andset_options()
.By setting the
format
option different packages can be used to create the latex version of a graph. Supported packages aretkzgraph
anddot2tex
.EXAMPLES:
sage: from sage.graphs.graph_latex import check_tkz_graph sage: check_tkz_graph() # random  depends on TeX installation sage: g = graphs.CompleteGraph(2) sage: opts = g.latex_options() sage: print(opts.latex()) \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{cv0v1}{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{$0$},x=2.5cm,y=5.0cm]{v0} \Vertex[style={minimum size=1.0cm,draw=cv1,fill=cfv1,text=clv1,shape=circle},LabelOut=false,L=\hbox{$1$},x=2.5cm,y=0.0cm]{v1} % \Edge[lw=0.1cm,style={color=cv0v1,},](v0)(v1) % \end{tikzpicture}
We check that trac ticket #22070 is fixed:
sage: edges = [(i,(i+1)%3,a) for i,a in enumerate('abc')] sage: G_with_labels = DiGraph(edges) sage: C = [[0,1], [2]] sage: kwds = dict(subgraph_clusters=C,color_by_label=True,prog='dot',format='dot2tex') sage: opts = G_with_labels.latex_options() sage: opts.set_options(edge_labels=True, **kwds) # optional  dot2tex graphviz sage: latex(G_with_labels) # optional  dot2tex graphviz \begin{tikzpicture}[>=latex,line join=bevel,] %% \begin{scope} \pgfsetstrokecolor{black} \definecolor{strokecol}{rgb}{...}; \pgfsetstrokecolor{strokecol} \definecolor{fillcol}{rgb}{...}; \pgfsetfillcolor{fillcol} \filldraw ... cycle; \end{scope} \begin{scope} \pgfsetstrokecolor{black} \definecolor{strokecol}{rgb}{...}; \pgfsetstrokecolor{strokecol} \definecolor{fillcol}{rgb}{...}; \pgfsetfillcolor{fillcol} \filldraw ... cycle; \end{scope} ... \end{tikzpicture}

set_option
(option_name, option_value=None)¶ Set, modify, clear a LaTeX option for controlling the rendering of a graph.
The possible options are documented here, because ultimately it is this routine that sets the values. However, the
sage.graphs.generic_graph.GenericGraph.set_latex_options()
method is the easiest way to set options, and allows several to be set at once.INPUT:
option_name
– a string for a latex option contained in the listsage.graphs.graph_latex.GraphLatex.__graphlatex_options
. AValueError
is raised if the option is not allowed.option_value
– a value for the option. If omitted, or set toNone
, the option will use the default value.
The output can be either handled internally by
Sage
, or delegated to the external softwaredot2tex
andgraphviz
. This is controlled by the optionformat
:format
– string (default:'tkz_graph'
); either'dot2tex'
or'tkz_graph'
.
If format is
'dot2tex'
, then all the LaTeX generation will be delegated todot2tex
(which must be installed).For
tkz_graph
, the possible option names, and associated values are given below. This first group allows you to set a style for a graph and specify some sizes related to the eventual image. (For more information consult the documentation for thetkzgraph
package.)tkz_style
– string (default:'Custom'
); the name of a predefinedtkzgraph
style such as'Shade'
,'Art'
,'Normal'
,'Dijkstra'
,'Welsh'
,'Classic'
, and'Simple'
, or the string'Custom'
. Using one of these styles alone will often give a reasonably good drawing with minimal effort. For a custom appearance set this to'Custom'
and use the options described below to override the default values.units
– string (default:'cm'
) – a natural unit of measurement used for all dimensions. Possible values are:'in'
,'mm'
,'cm'
,'pt'
,'em'
,'ex'
.scale
– float (default:1.0
); a dimensionless number that multiplies every linear dimension. So you can design at sizes you are accustomed to, then shrink or expand to meet other needs. Though fonts do not scale.graphic_size
– tuple (default:(5, 5)
); overall dimensions (width, length) of the bounding box around the entire graphic image.margins
– 4tuple (default:(0, 0, 0, 0)
); portion of graphic given over to a plain border as a tuple of four numbers: (left, right, top, bottom). These are subtracted from thegraphic_size
to create the area left for the vertices of the graph itself. Note that the processing done by Sage will trim the graphic down to the minimum possible size, removing any border. So this is only useful if you use the latex string in a latex document.
If not using a prebuilt style the following options are used, so the following defaults will apply. It is not possible to begin with a prebuilt style and modify it (other than editing the latex string by hand after the fact).
vertex_color
– (default:'black'
); a single color to use as the default for outline of vertices. For thesphere
shape this color is used for the entire vertex, which is drawn with a 3D shading. Colors must be specified as a string recognized by the matplotlib library: a standard color name like'red'
, or a hex string like'#2D87A7'
, or a single character from the choices'rgbcmykw'
. Additionally, a number between 0 and 1 will create a grayscale value. These color specifications are consistent throughout the options for atikzpicture
.vertex_colors
– a dictionary whose keys are vertices of the graph and whose values are colors. These will be used to color the outline of vertices. See the explanation above for thevertex_color
option to see possible values. These values need only be specified for a proper subset of the vertices. Specified values will supersede a default value.vertex_fill_color
– (default:'white'
); a single color to use as the default for the fill color of vertices. See the explanation above for thevertex_color
option to see possible values. This color is ignored for thesphere
vertex shape.vertex_fill_colors
– a dictionary whose keys are vertices of the graph and whose values are colors. These will be used to fill the interior of vertices. See the explanation above for thevertex_color
option to see possible values. These values need only be specified for a proper subset of the vertices. Specified values will supersede a default value.vertex_shape
– string (default:'circle'
); specifies the shape of the vertices. Allowable values are'circle'
,'sphere'
,'rectangle'
,'diamond'
. The sphere shape has a 3D look to its coloring and is uses only one color, that specified byvertex_color
andvertex_colors
, which are normally used for the outline of the vertex.vertex_shapes
– a dictionary whose keys are vertices of the graph and whose values are shapes. Seevertex_shape
for the allowable possibilities.vertex_size
– float (default: 1.0); the minimum size of a vertex as a number. Vertices will expand to contain their labels if the labels are placed inside the vertices. If you set this value to zero the vertex will be as small as possible (up to tkzgraph’s “inner sep” parameter), while still containing labels. However, if labels are not of a uniform size, then the vertices will not be either.vertex_sizes
– a dictionary of sizes for some of the vertices.vertex_labels
– boolean (default:True
); determine whether or not to display the vertex labels. IfFalse
subsequent options about vertex labels are ignored.vertex_labels_math
– boolean (default:True
); whenTrue
, if a label is a string that begins and ends with dollar signs, then the string will be rendered as a latex string. Otherwise, the label will be automatically subjected to thelatex()
method and rendered accordingly. IfFalse
the label is rendered as its textual representation according to the_repr
method. Support for arbitrarilycomplicated mathematics is not especially robust.vertex_label_color
– (default:'black'
); a single color to use as the default for labels of vertices. See the explanation above for thevertex_color
option to see possible values.vertex_label_colors
– a dictionary whose keys are vertices of the graph and whose values are colors. These will be used for the text of the labels of vertices. See the explanation above for thevertex_color
option to see possible values. These values need only be specified for a proper subset of the vertices. Specified values will supersede a default value.vertex_label_placement
– (default:'center'
); if'center'
the label is centered in the interior of the vertex and the vertex will expand to contain the label. Giving instead a pair of numbers will place the label exterior to the vertex at a certain distance from the edge, and at an angle to the positive xaxis, similar in spirit to polar coordinates.vertex_label_placements
– a dictionary of placements indexed by the vertices. See the explanation forvertex_label_placement
for the possible values.edge_color
– (default:'black'
); a single color to use as the default for an edge. See the explanation above for thevertex_color
option to see possible values.edge_colors
– a dictionary whose keys are edges of the graph and whose values are colors. These will be used to color the edges. See the explanation above for thevertex_color
option to see possible values. These values need only be specified for a proper subset of the vertices. Specified values will supersede a default value.edge_fills
– boolean (default:False
); whether an edge has a second color running down the middle. This can be a useful effect for highlighting edge crossings.edge_fill_color
– (default:'black'
); a single color to use as the default for the fill color of an edge. The boolean switchedge_fills
must be set to True for this to have an effect. See the explanation above for thevertex_color
option to see possible values.edge_fill_colors
– a dictionary whose keys are edges of the graph and whose values are colors. See the explanation above for thevertex_color
option to see possible values. These values need only be specified for a proper subset of the vertices. Specified values will supersede a default value.edge_thickness
– float (default: 0.1); specifies the width of the edges. Note thattkzgraph
does not interpret this number for loops.edge_thicknesses
– a dictionary of thicknesses for some of the edges of a graph. These values need only be specified for a proper subset of the vertices. Specified values will supersede a default value.edge_labels
– boolean (default:False
); determine if edge labels are shown. IfFalse
subsequent options about edge labels are ignored.edge_labels_math
– boolean (default:True
); control how edge labels are rendered. Read the explanation for thevertex_labels_math
option, which behaves identically. Support for arbitrarilycomplicated mathematics is not especially robust.edge_label_color
– (default:'black'
); a single color to use as the default for labels of edges. See the explanation above for thevertex_color
option to see possible values.edge_label_colors
– a dictionary whose keys are edges of the graph and whose values are colors. These will be used for the text of the labels of edges. See the explanation above for thevertex_color
option to see possible values. These values need only be specified for a proper subset of the vertices. Specified values will supersede a default value. Note that labels must be used for this to have any effect, and no care is taken to ensure that label and fill colors work well together.edge_label_sloped
– boolean (default:True
); specifies how edge labels are place.False
results in a horizontal label, whileTrue
means the label is rotated to follow the direction of the edge it labels.edge_label_slopes
– a dictionary of booleans, indexed by some subset of the edges. See theedge_label_sloped
option for a description of sloped edge labels.edge_label_placement
– (default: 0.50); either a number between 0.0 and 1.0, or one of:'above'
,'below'
,'left'
,'right'
. These adjust the location of an edge label along an edge. A number specifies how far along the edge the label is located.'left'
and'right'
are conveniences.'above'
and'below'
move the label off the edge itself while leaving it near the midpoint of the edge. The default value of0.50
places the label on the midpoint of the edge.edge_label_placements
– a dictionary of edge placements, indexed by the edges. See theedge_label_placement
option for a description of the allowable values.loop_placement
– (default:(3.0, 'NO')
); determine how loops are rendered. the first element of the pair is a distance, which determines how big the loop is and the second element is a string specifying a compass point (North, South, East, West) as one of'NO'
,'SO'
,'EA'
,'WE'
.loop_placements
– a dictionary of loop placements. See theloop_placements
option for the allowable values. While loops are technically edges, this dictionary is indexed by vertices.
For the
'dot2tex'
format, the possible option names and associated values are given below:prog
– string; the program used for the layout. It must be a string corresponding to one of the software of the graphviz suite:'dot'
,'neato'
,'twopi'
,'circo'
or'fdp'
.edge_labels
– boolean (default:False)
; whether to display the labels on edges.edge_colors
– a color; can be used to set a global color to the edge of the graph.color_by_label
– boolean (default:False
); colors the edges according to their labelssubgraph_clusters
– (default:[]
) a list of lists of vertices, if supported by the layout engine, nodes belonging to the same cluster subgraph are drawn together, with the entire drawing of the cluster contained within a bounding rectangle.
OUTPUT:
There are none. Success happens silently.
EXAMPLES:
Set, then modify, then clear the
tkz_style
option, and finally show an error for an unrecognized option name:sage: g = graphs.PetersenGraph() sage: opts = g.latex_options() sage: opts LaTeX options for Petersen graph: {} sage: opts.set_option('tkz_style', 'Art') sage: opts LaTeX options for Petersen graph: {'tkz_style': 'Art'} sage: opts.set_option('tkz_style', 'Simple') sage: opts LaTeX options for Petersen graph: {'tkz_style': 'Simple'} sage: opts.set_option('tkz_style') sage: opts LaTeX options for Petersen graph: {} sage: opts.set_option('bad_name', 'nonsense') Traceback (most recent call last): ... ValueError: bad_name is not a LaTeX option for a graph.
See
sage.graphs.generic_graph.GenericGraph.layout_graphviz()
for installation instructions forgraphviz
anddot2tex
. Furthermore, pgf >= 2.00 should be available inside LaTeX’s tree for LaTeX compilation (e.g. when usingview
). In case your LaTeX distribution does not provide it, here are short instructions:download pgf from http://sourceforge.net/projects/pgf/
unpack it in
/usr/share/texmf/tex/generic
(depends on your system)clean out remaining pgf files from older version
run texhash

set_options
(**kwds)¶ Set several LaTeX options for a graph all at once.
INPUT:
kwds
– any number of option/value pairs to set many graph latex options at once (a variable number, in any order). Existing values are overwritten, new values are added. Existing values can be cleared by setting the value toNone
. Errors are raised in theset_option()
method.
EXAMPLES:
sage: g = graphs.PetersenGraph() sage: opts = g.latex_options() sage: opts.set_options(tkz_style='Welsh') sage: opts.get_option('tkz_style') 'Welsh'

tkz_picture
()¶ Return a string of LaTeX commands representing a graph as a
tikzpicture
.This routine interprets the graph’s properties and the options in
_options
to render the graph with commands from thetkzgraph
LaTeX package.This requires that the LaTeX optional packages
tkzgraph
andtkzberge
be installed. You may also need a current version of the pgf package. If thetkzgraph
andtkzberge
packages are present in the system’s TeX installation, the appropriate\\usepackage{}
commands will be added to the LaTeX preamble as part of the initialization of the graph. If these two packages are not present, then this command will return a warning on its first use, but will return a string that could be used elsewhere, such as a LaTeX document.For more information about tkzgraph you can visit https://www.ctan.org/pkg/tkzgraph.
EXAMPLES:
With a prebuilt
tkzgraph
style specified, the latex representation will be relatively simple.sage: from sage.graphs.graph_latex import check_tkz_graph sage: check_tkz_graph() # random  depends on TeX installation sage: g = graphs.CompleteGraph(3) sage: opts = g.latex_options() sage: g.set_latex_options(tkz_style='Art') sage: print(opts.tkz_picture()) \begin{tikzpicture} \GraphInit[vstyle=Art] % \Vertex[L=\hbox{$0$},x=2.5cm,y=5.0cm]{v0} \Vertex[L=\hbox{$1$},x=0.0cm,y=0.0cm]{v1} \Vertex[L=\hbox{$2$},x=5.0cm,y=0.0cm]{v2} % \Edge[](v0)(v1) \Edge[](v0)(v2) \Edge[](v1)(v2) % \end{tikzpicture}
Setting the style to “Custom” results in various configurable aspects set to the defaults, so the string is more involved.
sage: from sage.graphs.graph_latex import check_tkz_graph sage: check_tkz_graph() # random  depends on TeX installation sage: g = graphs.CompleteGraph(3) sage: opts = g.latex_options() sage: g.set_latex_options(tkz_style='Custom') sage: print(opts.tkz_picture()) \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{cv0v1}{rgb}{0.0,0.0,0.0} \definecolor{cv0v2}{rgb}{0.0,0.0,0.0} \definecolor{cv1v2}{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{$0$},x=2.5cm,y=5.0cm]{v0} \Vertex[style={minimum size=1.0cm,draw=cv1,fill=cfv1,text=clv1,shape=circle},LabelOut=false,L=\hbox{$1$},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{$2$},x=5.0cm,y=0.0cm]{v2} % \Edge[lw=0.1cm,style={color=cv0v1,},](v0)(v1) \Edge[lw=0.1cm,style={color=cv0v2,},](v0)(v2) \Edge[lw=0.1cm,style={color=cv1v2,},](v1)(v2) % \end{tikzpicture}
See the introduction to the
graph_latex
module for more information on the use of this routine.


sage.graphs.graph_latex.
check_tkz_graph
()¶ Check if the proper LaTeX packages for the
tikzpicture
environment are installed in the user’s environment, and issue a warning otherwise.The warning is only issued on the first call to this function. So any doctest that illustrates the use of the tkzgraph packages should call this once as having random output to exhaust the warnings before testing output.
See also
sage.misc.latex.Latex.check_file()

sage.graphs.graph_latex.
have_tkz_graph
()¶ Return
True
if the proper LaTeX packages for thetikzpicture
environment are installed in the user’s environment, namelytikz
,tkzgraph
andtkzberge
.The result is cached.
See also
sage.misc.latex.Latex.has_file()

sage.graphs.graph_latex.
setup_latex_preamble
()¶ Add appropriate
\usepackage{...}
, and other instructions to the latex preamble for the packages that are needed for processing graphs(tikz
,tkzgraph
,tkzberge
), if available in theLaTeX
installation.See also
sage.misc.latex.Latex.add_package_to_preamble_if_available()
.EXAMPLES:
sage: sage.graphs.graph_latex.setup_latex_preamble()