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, 17 Jan 2012 02:31:49 -0600What is the function for "%latex" on a notebook cell ?http://ask.sagemath.org/question/7862/what-is-the-function-for-latex-on-a-notebook-cell/In a notebook cell one can write:
%latex
Hello
\ [
x=f(x)
\ ]
and an image appear with perfect math notation. (not jsmath)
What is the sage command that does the same receiving a string ? Something like latexrender(r"Hello \ [x=f(x)\ ]")Tue, 11 Jan 2011 01:17:27 -0600http://ask.sagemath.org/question/7862/what-is-the-function-for-latex-on-a-notebook-cell/Answer by John Palmieri for <p>In a notebook cell one can write:</p>
<p>%latex</p>
<p>Hello</p>
<p>\ [</p>
<p>x=f(x)</p>
<p>\ ]</p>
<p>and an image appear with perfect math notation. (not jsmath)</p>
<p>What is the sage command that does the same receiving a string ? Something like latexrender(r"Hello \ [x=f(x)\ ]")</p>
http://ask.sagemath.org/question/7862/what-is-the-function-for-latex-on-a-notebook-cell/?answer=11963#post-id-11963In the notebook, I believe that executing a `%latex` cell is equivalent to the command
Latex().eval("string", globals=globals)
For example,
Latex().eval(r"""Hello
\[
x=f(x)
\]
Goodbye""", globals=globals)
Tue, 11 Jan 2011 03:50:50 -0600http://ask.sagemath.org/question/7862/what-is-the-function-for-latex-on-a-notebook-cell/?answer=11963#post-id-11963Comment by kcrisman for <p>In the notebook, I believe that executing a <code>%latex</code> cell is equivalent to the command</p>
<pre><code>Latex().eval("string", globals=globals)
</code></pre>
<p>For example,</p>
<pre><code>Latex().eval(r"""Hello
\[
x=f(x)
\]
Goodbye""", globals=globals)
</code></pre>
http://ask.sagemath.org/question/7862/what-is-the-function-for-latex-on-a-notebook-cell/?comment=22296#post-id-22296Okay, that is good to know. The issue about the raw string is just that in the command line one would have to be careful to add white space for LaTeX, and/or carriage returns, which I don't know quite how to do without actual carriage returns - does \n or \r work? Yes, producing the png makes sense - what does latex.show(...) do? Pop it up? That behavior should be available, at any rate.Tue, 11 Jan 2011 14:49:11 -0600http://ask.sagemath.org/question/7862/what-is-the-function-for-latex-on-a-notebook-cell/?comment=22296#post-id-22296Comment by kcrisman for <p>In the notebook, I believe that executing a <code>%latex</code> cell is equivalent to the command</p>
<pre><code>Latex().eval("string", globals=globals)
</code></pre>
<p>For example,</p>
<pre><code>Latex().eval(r"""Hello
\[
x=f(x)
\]
Goodbye""", globals=globals)
</code></pre>
http://ask.sagemath.org/question/7862/what-is-the-function-for-latex-on-a-notebook-cell/?comment=22302#post-id-22302Well, not quite - because he needed a *raw* string, and because Latex() is not in the default namespace. But that's enough for me to open a ticket - see http://trac.sagemath.org/sage_trac/ticket/10592Tue, 11 Jan 2011 04:16:00 -0600http://ask.sagemath.org/question/7862/what-is-the-function-for-latex-on-a-notebook-cell/?comment=22302#post-id-22302Comment by DSM for <p>In the notebook, I believe that executing a <code>%latex</code> cell is equivalent to the command</p>
<pre><code>Latex().eval("string", globals=globals)
</code></pre>
<p>For example,</p>
<pre><code>Latex().eval(r"""Hello
\[
x=f(x)
\]
Goodbye""", globals=globals)
</code></pre>
http://ask.sagemath.org/question/7862/what-is-the-function-for-latex-on-a-notebook-cell/?comment=22303#post-id-22303You win. :^) Tue, 11 Jan 2011 03:56:33 -0600http://ask.sagemath.org/question/7862/what-is-the-function-for-latex-on-a-notebook-cell/?comment=22303#post-id-22303Comment by John Palmieri for <p>In the notebook, I believe that executing a <code>%latex</code> cell is equivalent to the command</p>
<pre><code>Latex().eval("string", globals=globals)
</code></pre>
<p>For example,</p>
<pre><code>Latex().eval(r"""Hello
\[
x=f(x)
\]
Goodbye""", globals=globals)
</code></pre>
http://ask.sagemath.org/question/7862/what-is-the-function-for-latex-on-a-notebook-cell/?comment=22301#post-id-22301Latex() is in the default name space for the notebook. You can also replace "Latex()" with "latex" (note the lack of parentheses in the second one). I also don't understand your comment about the raw string. Running "latex.eval(...)" from the command line seems to produce png files containing the image, rather than popping up a window with a picture.Tue, 11 Jan 2011 04:40:03 -0600http://ask.sagemath.org/question/7862/what-is-the-function-for-latex-on-a-notebook-cell/?comment=22301#post-id-22301Answer by DSM for <p>In a notebook cell one can write:</p>
<p>%latex</p>
<p>Hello</p>
<p>\ [</p>
<p>x=f(x)</p>
<p>\ ]</p>
<p>and an image appear with perfect math notation. (not jsmath)</p>
<p>What is the sage command that does the same receiving a string ? Something like latexrender(r"Hello \ [x=f(x)\ ]")</p>
http://ask.sagemath.org/question/7862/what-is-the-function-for-latex-on-a-notebook-cell/?answer=11961#post-id-11961I don't know that there is one. But from the console, you can use the text command, and wrap the latex in $:
<pre><code>text("Behold the power of TeX: $\\frac{x^2}{y^3} = 7$", (0, 0), axes=False,fontsize=20)
</code></pre>
So you can trivially write a latexrender command yourself:
<pre><code>def latexrender(s):
pic = text("$" + s.replace('$','\$') + "$",(0,0),axes=False, color='black',fontsize=20)
pic.show()
latexrender(r"17 + \sum_{i=2}^{q} \, k^2 = 29383")
</code></pre>
Adding extra features like allowing you to specify the properties via **kwargs is left as an exercise for the reader. I should note though that the above uses matplotlib's TeX implementation, not any system verson, AFAIK.
Let's see if I can figure out how to do this from the notebook.. okay, the above still works there. I'll see if I can figure out how to get LaTeX itself called (although I should say that the matplotlib mathtext has worked pretty well for me).
After a little thought, something like the following should get the job done:
<pre><code>import matplotlib
matplotlib.rcParams['text.usetex']=True
def latexrender(s):
pic = text("$" + s.replace('$','\$') + "$",(0,0),axes=False, color='black',fontsize=20)
pic.show()
latexrender(r"17 + \displaystyle\sum\limits_{i=2}^q \, k^2 = 29383")
</code></pre>Tue, 11 Jan 2011 02:40:23 -0600http://ask.sagemath.org/question/7862/what-is-the-function-for-latex-on-a-notebook-cell/?answer=11961#post-id-11961Answer by pang for <p>In a notebook cell one can write:</p>
<p>%latex</p>
<p>Hello</p>
<p>\ [</p>
<p>x=f(x)</p>
<p>\ ]</p>
<p>and an image appear with perfect math notation. (not jsmath)</p>
<p>What is the sage command that does the same receiving a string ? Something like latexrender(r"Hello \ [x=f(x)\ ]")</p>
http://ask.sagemath.org/question/7862/what-is-the-function-for-latex-on-a-notebook-cell/?answer=13131#post-id-13131If you just want to typeset some latex using jsmath, use for example:
html('$\pi$')Tue, 17 Jan 2012 02:31:49 -0600http://ask.sagemath.org/question/7862/what-is-the-function-for-latex-on-a-notebook-cell/?answer=13131#post-id-13131Answer by kcrisman for <p>In a notebook cell one can write:</p>
<p>%latex</p>
<p>Hello</p>
<p>\ [</p>
<p>x=f(x)</p>
<p>\ ]</p>
<p>and an image appear with perfect math notation. (not jsmath)</p>
<p>What is the sage command that does the same receiving a string ? Something like latexrender(r"Hello \ [x=f(x)\ ]")</p>
http://ask.sagemath.org/question/7862/what-is-the-function-for-latex-on-a-notebook-cell/?answer=11962#post-id-11962Percent directives in the notebook literally call other systems directly. (So you need $\LaTeX$ in your PATH to do this, of course, which I don't.)
So there should be a way to use the stuff in `sage.misc.latex` to do this. In theory,
sage: show(latex('mystring'))
should work for anything. Certainly it will work if you put Sage objects and numbers in it, but with strings I think it's tricky - `latex()` wraps them with `\texttt{}` in general, while `%latex` wraps LATEX_HEADER around things - see
sage: sage.misc.latex.Latex??
or the whole source for `sage.misc.latex`.
So that's not a full answer, but maybe it will lead to a full answer. I just don't understand well enough how the notebook calls other systems, otherwise I'd look there and give a complete answer.Tue, 11 Jan 2011 03:18:58 -0600http://ask.sagemath.org/question/7862/what-is-the-function-for-latex-on-a-notebook-cell/?answer=11962#post-id-11962