Ask Your Question

embed sage vars in html notebook cell

asked 2012-12-01 00:53:18 -0500

PatrickSurry gravatar image

updated 2012-12-01 07:29:25 -0500

Again I feel like I'm missing something. I'm trying to add some mostly text content to a notebook cell, but I want to include an equation contained in a notebook variable. For example I have defined

var('u, v, w')
x = u * v * w^2

And then I want to write a few paragraphs that includes the formula contained in x.

If I use the HTML editor and put something in dollar signs, it formats as math but doesn't eval the content, i.e. if I say $x$ then I just get a literal x in italics.

If I call the html() function, I can do something like html('testing $'+latex(x)+'$') to manually substitute myself, but this gets formatted in fixed-width font for reasons I don't understand.

If I start a cell with %html, then I can't figure out how to get the latex/html representation of the formula in x, but otherwise it formats it nicely. (I guess like an HTML editor cell)

Also, is there some documentation of how the different cell tags like %latex, %html, etc work?

edit retag flag offensive close merge delete


Another interesting directive that I just stumbled on and didn't see documented is %hide, which seems to hide the remainder of your input cell text

PatrickSurry gravatar imagePatrickSurry ( 2012-12-06 04:43:37 -0500 )edit

2 answers

Sort by ยป oldest newest most voted

answered 2012-12-01 07:28:09 -0500

PatrickSurry gravatar image

Aha! I did some code exploration of sage/misc/ and discovered the <sage> tag, so you can just write something like this in a a cell:

We can see that <sage>x</sage> is the key result

and it will do sage_eval() and format the result in math mode, so you get nice inline math display of the formula contained in x. That is exactly what I need - is that <sage> tag documented anywhere?

edit flag offensive delete link more


That is awesome! I have never (or maybe very long ago?) seen this, so if it's not, it should be!

kcrisman gravatar imagekcrisman ( 2012-12-03 06:25:20 -0500 )edit

I've followed up at with a question about this, thank you very much for finding it.

kcrisman gravatar imagekcrisman ( 2012-12-03 06:31:41 -0500 )edit

Doing this in cloud.sagemath fails: x=2;%html; We can see that <sage>x</sage> is the key result

jeanpat gravatar imagejeanpat ( 2014-01-03 00:40:28 -0500 )edit

answered 2012-12-01 04:38:14 -0500

ppurka gravatar image

I don't think there is any straightforward way to display dynamically evaluated expressions. You can use a combination of the html command and the pretty_print command. The output does seem to have monospaced fonts.

x = u^2 + v^2 + w + integral(e^(u^2), u)
html('The output:')
html('Is it correct?')

Not sure about the documentation of those %latex, %html functions.

edit flag offensive delete link more

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-12-01 00:53:18 -0500

Seen: 164 times

Last updated: Dec 01 '12