Ask Your Question

# Revision history [back]

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.

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).