Ask Your Question
1

embeding a variable in some html code in cloud.sagemath

asked 2014-01-03 08:24:24 +0200

jeanpat gravatar image

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 flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
2

answered 2014-01-03 09:09:40 +0200

niles gravatar image

updated 2014-01-06 13:19:42 +0200

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

edit flag offensive delete link more

Comments

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

jeanpat gravatar imagejeanpat ( 2014-01-03 09:48:09 +0200 )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

niles gravatar imageniles ( 2014-01-06 13:13:37 +0200 )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?

William Stein gravatar imageWilliam Stein ( 2014-01-13 12:11:34 +0200 )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 :)

niles gravatar imageniles ( 2014-01-13 15:33:24 +0200 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2014-01-03 08:24:24 +0200

Seen: 737 times

Last updated: Jan 06 '14