# embeding a variable in some html code in cloud.sagemath

Hello, I try to produce an equation and to display it as follow:

import random
nombres = [-3, 1, -4, -1, 5, 2, 7]
nombre = random.choice(nombres)

%html
<h2> R&eacute;solvons une &eacute;quation </h2>
<font size="+3">
<div align="center">
$x+ b=k$
</div>
</font>

<h3> Exemple :  résoudre : $x+$ <sage>nombre</sage>$=12$ </h3>


But the last line failed to display the value of nombre in the equation

What is the good way to do that? Thank you

edit retag close merge delete

Sort by » oldest newest most voted

You can do this by using the html function directly:

## Background

%html just works on text -- it doesn't perform any sage (or python) parsing. It does this by applying sage's html function to whatever you type. So your code above is equivalent to

html("""
<h2> R&eacute;solvons une &eacute;quation </h2>
<font size="+3">
<div align="center">
$x+ b=k$
</div>
</font>

<h3> Exemple :  r&eacute;soudre : $x+$ <sage>nombre</sage>$=12$ </h3>
""")


[Note that I had to change é to &eacute; in the last line for it to parse correctly -- hopefully this will not be necessary in a future update of sage.]

## Solution

So the key to doing what you want is using sage to create the string with the variable value inserted, and then pass the result to the html function. The format operator is a good way to do this:

beginning = """
<h2> R&eacute;solvons une &eacute;quation </h2>
<font size="+3">
<div align="center">
$x+ b=k$
</div>
</font>

"""
exemple = "<h3> Exemple :  r&eacute;soudre : $x+ {0} =12$ </h3>".format(nombre)

html(beginning+exemple)    # addition for strings is concatenation


## Followup

You can see what's happening by just printing the strings:

print(exemple)


should show you something like

<h3> Exemple : r&eacute;soudre : $x + 7 =12$ </h3>

because the format operator sticks the value of nombre into the the string.

## Edit

To fix things like x + -1, define the right hand side as a sage expression and let sage format it:

var('x')
nombre = nombres[3]
expr = x + nombre


The string for expr should give you x - 1. If your expressions get more complicated, you can also use latex(expr) to get a latex formatted string (for things like exponents, parentheses, etc).

more

Thanks a lot. By the way would you know a way to avoid having things like x+-1=12 (I could use brackets too ...)

( 2014-01-03 02:48:09 -0600 )edit

if you make the left hand side as a symbolic expression in sage, it will take care of things like that for you; I'll add this to my answer

( 2014-01-06 06:13:37 -0600 )edit

"%html just works on text -- it doesn't perform any sage (or python) parsing." -- true. I wonder, maybe it should do something kind of like SageTex, via a special tag or something... thoughts?

( 2014-01-13 05:11:34 -0600 )edit

Hmmm; at least the OP thought it was a natural expectation. But using html() instead of %html is *sooo* easy that just pointing this out in the help for %html (wherever that is) would probably suffice. And it would avoid the slippery slope of similar expectations for %otherformats. But I tend to be more conservative about things like this and others may point out that if it's easy to make %html behave more usefully and more naturally, then it should be done :)

( 2014-01-13 08:33:24 -0600 )edit