Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/graphs/dot2tex_utils.py
4045 views
1
r"""
2
This file contains some utility functions for the interface with dot2tex
3
"""
4
#*****************************************************************************
5
# Copyright (C) 2010 Nicolas M. Thiery <nicolas.thiery at u-psud.fr>
6
#
7
# Distributed under the terms of the GNU General Public License (GPL)
8
# http://www.gnu.org/licenses/
9
#*****************************************************************************
10
11
import re
12
from sage.misc.latex import latex
13
14
def have_dot2tex():
15
"""
16
Returns whether ``dot2tex`` >= 2.8.7 and graphviz are installed
17
and functional
18
19
EXAMPLES::
20
21
sage: sage.graphs.dot2tex_utils.have_dot2tex() # optional - requires dot2tex and graphviz
22
True
23
sage: sage.graphs.dot2tex_utils.have_dot2tex() in [True, False]
24
True
25
"""
26
try:
27
import dot2tex
28
# Test for this required feature from dot2tex 2.8.7
29
return dot2tex.dot2tex("graph {}", format = "positions") == {}
30
except:
31
return False
32
return True
33
34
def assert_have_dot2tex():
35
"""
36
Tests whether ``dot2tex`` >= 2.8.7 and graphviz are installed and
37
functional, and raises an error otherwise
38
39
EXAMPLES::
40
41
sage: sage.graphs.dot2tex_utils.assert_have_dot2tex() # optional - requires dot2tex and graphviz
42
"""
43
check_error_string = """
44
An error occurs while testing the dot2tex installation.
45
46
Please see :meth:`sage.graphs.generic_graph.GenericGraph.layout_graphviz`
47
and check the installation of graphviz and the dot2tex spkg.
48
49
For support, please contact <sage-combinat-devel at googlegroups.com>.
50
"""
51
missing_error_string = """
52
dot2tex not available.
53
54
Please see :meth:`sage.graphs.generic_graph.GenericGraph.layout_graphviz`
55
for installation instructions.
56
"""
57
try:
58
import dot2tex
59
if dot2tex.dot2tex("graph {}", format = "positions") != {}:
60
raise RuntimeError(check_error_string)
61
except ImportError:
62
raise RuntimeError(missing_error_string)
63
64
def quoted_latex(x):
65
"""
66
Strips the latex representation of ``x`` to make it suitable for a
67
``dot2tex`` string.
68
69
EXAMPLES::
70
71
sage: sage.graphs.dot2tex_utils.quoted_latex(matrix([[1,1],[0,1],[0,0]]))
72
'\\left(\\begin{array}{rr}1 & 1 \\\\0 & 1 \\\\0 & 0\\end{array}\\right)'
73
"""
74
return re.sub("\"|\r|(%[^\n]*)?\n","", latex(x))
75
76
def quoted_str(x):
77
"""
78
Strips the string representation of ``x`` to make it suitable for
79
a ``dot2tex`` string, and especially a node label (``dot2tex``
80
gets confused by newlines, and braces)
81
82
EXAMPLES::
83
84
sage: sage.graphs.dot2tex_utils.quoted_str(matrix([[1,1],[0,1],[0,0]]))
85
'[1 1]\\n\\\n[0 1]\\n\\\n[0 0]'
86
sage: print sage.graphs.dot2tex_utils.quoted_str(matrix([[1,1],[0,1],[0,0]]))
87
[1 1]\n\
88
[0 1]\n\
89
[0 0]
90
"""
91
return re.sub("\n",r"\\n\\"+"\n", re.sub("\"|\r|}|{","", str(x)))
92
93
def key(x):
94
r"""
95
Strips the string representation of ``x`` of quotes and newlines
96
to get a key suitable for naming vertices in a dot2tex string.
97
98
EXAMPLES::
99
100
sage: sage.graphs.dot2tex_utils.key(matrix([[1,1],[0,1],[0,0]]))
101
'110100'
102
sage: sage.graphs.dot2tex_utils.key("blah{bleh}\nblih{")
103
'blahblehblih'
104
"""
105
return re.sub("[\\\'\"\[\]() \t\r\n{}]","", str(x))
106
107
def key_with_hash(x):
108
"""
109
Same as :function:`key`, except that the hash of the object is
110
prepended to better ensure uniqueness. This requires the object to
111
be hashable, but this is anyway necessary to use it as a vertex of
112
a graph.
113
114
EXAMPLES::
115
116
sage: sage.graphs.dot2tex_utils.key_with_hash(3)
117
'3_3'
118
sage: sage.graphs.dot2tex_utils.key_with_hash((1,2,3))
119
'1,2,3_...'
120
121
"""
122
return key(x)+"_"+str(hash(x))
123
124