ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 09 Sep 2014 09:22:12 -0500"showing" both input and outputhttp://ask.sagemath.org/question/24039/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.Sat, 06 Sep 2014 15:41:45 -0500http://ask.sagemath.org/question/24039/showing-both-input-and-output/Answer by tmonteil for <p>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)</p>
<p>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.</p>
http://ask.sagemath.org/question/24039/showing-both-input-and-output/?answer=24040#post-id-24040Usually, 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](http://trac.sagemath.org/ticket/10035) and [trac ticket 16941](http://trac.sagemath.org/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
Sat, 06 Sep 2014 16:16:56 -0500http://ask.sagemath.org/question/24039/showing-both-input-and-output/?answer=24040#post-id-24040Comment by mathochist for <p>Usually, for symbolic expressions, there is a <code>hold</code> parameter that prevents Sage to evaluate your input so that you can get its latex representation, for example:</p>
<pre><code>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
</code></pre>
<p>The problem here is that there is no <code>hold</code> parameter for integration:</p>
<pre><code>sage: integral(x^2, x, 0, 3, hold=True)
TypeError: integrate() got an unexpected keyword argument 'hold'
</code></pre>
<p>The workaround could be to use <code>definite_integral</code> that supports this paramter:</p>
<pre><code>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}
</code></pre>
<p>Once <a href="http://trac.sagemath.org/ticket/10035">trac ticket 10035</a> and <a href="http://trac.sagemath.org/ticket/16941">trac ticket 16941</a> (which i opened for the occasion, and needs rewiew) will be solved, you will be able to write something along the lines:</p>
<pre><code>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
</code></pre>
http://ask.sagemath.org/question/24039/showing-both-input-and-output/?comment=24067#post-id-24067I 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.Tue, 09 Sep 2014 09:22:12 -0500http://ask.sagemath.org/question/24039/showing-both-input-and-output/?comment=24067#post-id-24067