Is it possible to use tikz in notebook

Hi

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)
g.plot(pos=g.layout_planar())


Gives:

Where I want something like:

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

edit retag close merge delete

Sort by » oldest newest most voted

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
setup_latex_preamble()
view(g)


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
setup_latex_preamble()
latex.extra_preamble()
latex.engine('pdflatex')
view(g)


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.

more

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.

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?

( 2012-09-25 00:24:03 -0600 )edit