1 | initial version |
You can do this by using the html
function directly:
%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ésolvons une équation </h2>
<font size="+3">
<div align="center">
$ x+ b=k $
</div>
</font>
<h3> Exemple : résoudre : $ x+$ <sage>nombre</sage>$=12 $ </h3>
""")
[Note that I had to change é to é
in the last line for it to parse correctly -- hopefully this will not be necessary in a future update of sage.]
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ésolvons une équation </h2>
<font size="+3">
<div align="center">
$ x+ b=k $
</div>
</font>
"""
exemple = "<h3> Exemple : résoudre : $ x+ {0} =12 $ </h3>".format(nombre)
html(beginning+exemple) # addition for strings is concatenation
You can see what's happening by just printing the strings:
print(exemple)
should show you something like
<h3> Exemple : résoudre : $ x + 7 =12 $ </h3>
because the format
operator sticks the value of nombre
into the the string.
2 | making RHS a sage symbolic expression |
You can do this by using the html
function directly:
%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ésolvons une équation </h2>
<font size="+3">
<div align="center">
$ x+ b=k $
</div>
</font>
<h3> Exemple : résoudre : $ x+$ <sage>nombre</sage>$=12 $ </h3>
""")
[Note that I had to change é to é
in the last line for it to parse correctly -- hopefully this will not be necessary in a future update of sage.]
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ésolvons une équation </h2>
<font size="+3">
<div align="center">
$ x+ b=k $
</div>
</font>
"""
exemple = "<h3> Exemple : résoudre : $ x+ {0} =12 $ </h3>".format(nombre)
html(beginning+exemple) # addition for strings is concatenation
You can see what's happening by just printing the strings:
print(exemple)
should show you something like
<h3> Exemple : résoudre : $ x + 7 =12 $ </h3>
because the format
operator sticks the value of nombre
into the the string.
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).