# "showing" both input and output

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

Sort by » oldest newest most voted

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)
tan(1/2*pi)
sage: latex(_)
\tan\left(\frac{1}{2} \, \pi\right)
sage: tan(pi/2)
Infinity
sage: latex(_)
\infty


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

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.

My computer says that with hold=True is an invalid syntax