Ask Your Question

Is it possible to use tikz in notebook

asked 2012-09-24 23:53:40 -0500

Kabi gravatar image

updated 2012-09-25 00:10:12 -0500


I am wondering if it is possible to render tikz from code inside the notebook.

If not: Is there some other way of drawing graphs with the same degree of control as you get with tikz?

I am using matplotlib for some plots in the notebook and the built-in Graph function for some graph theory. The problem is that sometimes I want to show a graph with specific proportions and with specific placement of nodes, lines and points, to make some argument clearly shown.

An example:

f= {'House 1': ['Water', 'Electricity', 'Phone Line'], 
'House 2': ['Water', 'Electricity', 'Phone Line'],
'House 3': ['Water', 'Electricity']}
g = Graph(f)


image description

Where I want something like:

image description

I hope it makes sense what I am asking....

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted

answered 2012-09-25 04:55:33 -0500

updated 2012-09-25 08:02:41 -0500

As long as you have tikz installed in your TeX installation, then sure, you can use tikz. See Sage's tutorial for information about how to tell Sage to use LaTeX to produce pictures in the notebook rather than jsMath or MathJax (these are the default, jsMath in older versions of Sage, MathJax in the most recent). Briefly:

from sage.graphs.graph_latex import setup_latex_preamble

should do it. Read the referenced section of the tutorial and also the following section.

Edit: using your vertex labels, I get a LaTeX error when I try this. So I modified your graph like this:

h1 = r"$\text{House 1}$"
h2 = r"$\text{House 2}$"
h3 = r"$\text{House 3}$"
w = r"$\text{Water}$"
e = r"$\text{Electricity}$"
ph = r"$\text{Phone Line}$"
f = {h1: [w, e, ph], h2: [w, e, ph], h3: [w, e]}
g = Graph(f)
from sage.graphs.graph_latex import setup_latex_preamble

Unfortunately, with these vertex labels, g.plot(...) doesn't work anymore. There may be ways of tinkering with the graph to get both view (using LaTeX) and plot to work.

edit flag offensive delete link more

answered 2012-09-25 00:10:53 -0500

No, you need a full installation of TeX for this. While mathjax (the engine to render TeX in sage nb) is quite capable, it can't do tikz.

edit flag offensive delete link more


I have a full installation of TeX. The question is if I can have tikz rendered in the notebook from code entered in the notebook. Or do I have to create the tikz picture outside the notebook and then import as image?

Kabi gravatar imageKabi ( 2012-09-25 00:24:03 -0500 )edit

Your Answer

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

Add Answer

Question Tools


Asked: 2012-09-24 23:53:40 -0500

Seen: 666 times

Last updated: Sep 25 '12