ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 25 Sep 2012 04:55:33 -0500Is it possible to use tikz in notebookhttp://ask.sagemath.org/question/9348/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:
![image description](/upfiles/13485671088307033.png)
Where I want something like:
![image description](/upfiles/13485677815431312.png)
I hope it makes sense what I am asking....Mon, 24 Sep 2012 23:53:40 -0500http://ask.sagemath.org/question/9348/is-it-possible-to-use-tikz-in-notebook/Answer by Dima for <p>Hi</p>
<p>I am wondering if it is possible to render tikz from code inside the notebook.</p>
<p>If not: Is there some other way of drawing graphs with the same degree of control as you get with tikz?</p>
<p>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.</p>
<p>An example:</p>
<pre><code>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())
</code></pre>
<p>Gives:</p>
<p><img alt="image description" src="/upfiles/13485671088307033.png"/></p>
<p>Where I want something like:</p>
<p><img alt="image description" src="/upfiles/13485677815431312.png"/></p>
<p>I hope it makes sense what I am asking....</p>
http://ask.sagemath.org/question/9348/is-it-possible-to-use-tikz-in-notebook/?answer=13993#post-id-13993No, 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.Tue, 25 Sep 2012 00:10:53 -0500http://ask.sagemath.org/question/9348/is-it-possible-to-use-tikz-in-notebook/?answer=13993#post-id-13993Comment by Kabi for <p>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.</p>
http://ask.sagemath.org/question/9348/is-it-possible-to-use-tikz-in-notebook/?comment=19008#post-id-19008I 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?Tue, 25 Sep 2012 00:24:03 -0500http://ask.sagemath.org/question/9348/is-it-possible-to-use-tikz-in-notebook/?comment=19008#post-id-19008Answer by John Palmieri for <p>Hi</p>
<p>I am wondering if it is possible to render tikz from code inside the notebook.</p>
<p>If not: Is there some other way of drawing graphs with the same degree of control as you get with tikz?</p>
<p>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.</p>
<p>An example:</p>
<pre><code>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())
</code></pre>
<p>Gives:</p>
<p><img alt="image description" src="/upfiles/13485671088307033.png"/></p>
<p>Where I want something like:</p>
<p><img alt="image description" src="/upfiles/13485677815431312.png"/></p>
<p>I hope it makes sense what I am asking....</p>
http://ask.sagemath.org/question/9348/is-it-possible-to-use-tikz-in-notebook/?answer=14067#post-id-14067As long as you have tikz installed in your TeX installation, then sure, you can use tikz. See [Sage's tutorial](http://sagemath.org/doc/tutorial/latex.html#customizing-latex-processing) 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](http://sagemath.org/doc/tutorial/latex.html#an-example-combinatorial-graphs-with-tkz-graph).
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.Tue, 25 Sep 2012 04:55:33 -0500http://ask.sagemath.org/question/9348/is-it-possible-to-use-tikz-in-notebook/?answer=14067#post-id-14067