Ask Your Question
0

Is it possible to use tikz in notebook

asked 2012-09-25 06:53:40 +0200

Kabi gravatar image

updated 2012-09-25 07:10:12 +0200

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:

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
2

answered 2012-09-25 11:55:33 +0200

updated 2012-09-25 15:02:41 +0200

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()
latex.add_to_jsmath_avoid_list('tikz')
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.add_to_mathjax_avoid_list('tikz')
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.

edit flag offensive delete link more
0

answered 2012-09-25 07:10:53 +0200

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

Comments

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 07:24:03 +0200 )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

Stats

Asked: 2012-09-25 06:53:40 +0200

Seen: 1,295 times

Last updated: Sep 25 '12