Ask Your Question

"showing" both input and output

asked 2014-09-06 15:41:45 -0500

mathochist gravatar image

updated 2014-09-06 16:19:14 -0500

tmonteil gravatar image

Is there a way to get Sage to show the "pretty" versions of both the input and the output of a calculation, instead of just the output? (sort of like the TI-89 and such)

For example, when I enter as input "integrate(x^2, x, 0, 3)" it correctly returns 9. But can I get it to return something like (the typeset version of) \int_0^3 x^2 dx = 9 ? It's obvious enough in this example what I'm asking for but some queries get complicated and seeing them as they would be printed helps make sure I'm asking Sage for what I mean to ask for.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2014-09-06 16:16:56 -0500

tmonteil gravatar image

updated 2014-09-06 18:35:15 -0500

Usually, for symbolic expressions, there is a hold parameter that prevents Sage to evaluate your input so that you can get its latex representation, for example:

sage: (x^2).add(x^2, hold=True)
x^2 + x^2
sage: latex(_)
x^{2} + x^{2}

sage: tan(pi/2, hold=True)
sage: latex(_)
\tan\left(\frac{1}{2} \, \pi\right)
sage: tan(pi/2)
sage: latex(_)

The problem here is that there is no hold parameter for integration:

sage: integral(x^2, x, 0, 3, hold=True)
TypeError: integrate() got an unexpected keyword argument 'hold'

The workaround could be to use definite_integral that supports this paramter:

sage: from sage.symbolic.integration.integral import definite_integral
sage: definite_integral(x^2, x, 0, 3, hold=True)
integrate(x^2, x, 0, 3)
sage: latex(_)
\int_{0}^{3} x^{2}\,{d x}

Once trac ticket 10035 and trac ticket 16941 (which i opened for the occasion, and needs rewiew) will be solved, you will be able to write something along the lines:

sage: def myshow(s):
....:    with hold=True:
....:        left = str(eval(s))
....:    right = str(eval(s))
....:    return left + '=' + right

sage: myshow('integral(x^2, x, 0, 3)')
\int_{0}^{3} x^{2}\,{d x} = 9
edit flag offensive delete link more


I see. So it looks like untill the 'hold' command works on all (or almost all) input, I won't quite get what I want. Thank you for the effort.

mathochist gravatar imagemathochist ( 2014-09-09 09:22:12 -0500 )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


Asked: 2014-09-06 15:41:45 -0500

Seen: 151 times

Last updated: Sep 06 '14