ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 02 Feb 2017 05:07:08 -0600How can I print equations just like latex?https://ask.sagemath.org/question/25820/how-can-i-print-equations-just-like-latex/Now I'm testing several equations and solutions with Sage. After testing the solutions, I'd present it to the group members. But I met a problem to print the equations.
For example, when I write the code.
sage: z(x)=6/(5*pi*50*(x^2+25))*exp((-x+sqrt(x^2+25))/50)
sage: view (z)
Then the sage doesn't show the equation that is exactly same with the code, but it just shows simplified form. Could anyone let me know how to show the original equation?
And it would be also appreciated, if you tell me how to modify the simplifying mechanism. I'd love to write equations that don't contain any large denominators with many exponential terms.
thank you.Fri, 13 Feb 2015 01:34:10 -0600https://ask.sagemath.org/question/25820/how-can-i-print-equations-just-like-latex/Answer by louisgag for <p>Now I'm testing several equations and solutions with Sage. After testing the solutions, I'd present it to the group members. But I met a problem to print the equations. </p>
<p>For example, when I write the code. </p>
<pre><code>sage: z(x)=6/(5*pi*50*(x^2+25))*exp((-x+sqrt(x^2+25))/50)
sage: view (z)
</code></pre>
<p>Then the sage doesn't show the equation that is exactly same with the code, but it just shows simplified form. Could anyone let me know how to show the original equation? </p>
<p>And it would be also appreciated, if you tell me how to modify the simplifying mechanism. I'd love to write equations that don't contain any large denominators with many exponential terms. </p>
<p>thank you.</p>
https://ask.sagemath.org/question/25820/how-can-i-print-equations-just-like-latex/?answer=36438#post-id-36438I've created an extended version of the script given by Thorsten and posted it on GitHub. This way I will keep it updated were I to make changes or receive suggestions: [sageToLatex.sage](https://github.com/louisgag/sagemath-scripts/blob/master/sageToLatex.sage)
LATEX PACKAGES REQUIRED:
*) add the following two lines to the header of you main Latex file if theses packages are not already declared:
`\usepackage{esdiff} % for non-italic derivatives`
`\usepackage{breqn} % automatic linebreaks for eqns`
INSTRUCTIONS:
1) paste the whole *sageToLatex.sage* file somewhere near the beginning of your worksheet and uncomment the following line to set the boolean printArticle as such:
printArticle = bool(1) # enable if want to print the text and equations for the article
2) in your document, include, where desired, the following string additions:
if (printArticle):
latexPrint += 'YOUR TEXT,' + get_expression('derivative(x/b + 5 + sqrt(3/b),x)', isEqn=bool(1))
3) at the end of your document create the Latex file that you can then include (ex: `\input{myEqns.tex}`) in your master Latex document:
if (printArticle):
fLATEX=open('myEqns.tex','w')
fLATEX.write(latexPrint)
fLATEX.close()Thu, 02 Feb 2017 05:07:08 -0600https://ask.sagemath.org/question/25820/how-can-i-print-equations-just-like-latex/?answer=36438#post-id-36438Answer by Thorsten for <p>Now I'm testing several equations and solutions with Sage. After testing the solutions, I'd present it to the group members. But I met a problem to print the equations. </p>
<p>For example, when I write the code. </p>
<pre><code>sage: z(x)=6/(5*pi*50*(x^2+25))*exp((-x+sqrt(x^2+25))/50)
sage: view (z)
</code></pre>
<p>Then the sage doesn't show the equation that is exactly same with the code, but it just shows simplified form. Could anyone let me know how to show the original equation? </p>
<p>And it would be also appreciated, if you tell me how to modify the simplifying mechanism. I'd love to write equations that don't contain any large denominators with many exponential terms. </p>
<p>thank you.</p>
https://ask.sagemath.org/question/25820/how-can-i-print-equations-just-like-latex/?answer=25855#post-id-25855 There is the hold option, which might can help:
z(x)=x.power(2).mul(x,hold=true)
view(z)
which evaluates to $x \mapsto x^2x$
For an easier typing one could use Infix opertors:
def hold_mult(a,b):
return a.mul(b,hold=true)
h = infix_operator('multiply')(hold_mult)
and then use `x^2 *h* x`.
However in your case if you need just the initial equation to display it might be the easiest just to print it as a string. If you need this functionality more often, an (extendet version of a) function like this could be helpful:
def paranthese_match(prefix,str):
matches = []
while (str.find(prefix) != -1):
p = -1
for li in [str.find(prefix)+len(prefix)..len(str)-1]:
if str[li] == '(':
p -=1
if str[li] == ')':
p += 1
if p==0:
matches.append(str[str.find(prefix)+len(prefix):li])
str = str[li:]
return matches
def print_expression(str):
str = str.replace('*',' \cdot ')
str = str.replace('pi',' \pi ')
for sr in paranthese_match('sqrt(',str):
str = str.replace('sqrt('+sr+")",'\sqrt{'+sr+'}')
for sr in paranthese_match('exp(',str):
str = str.replace('exp('+sr+")",'e^{'+sr+'}')
html("$"+str+"$")
Then `print_expresion("6/(5*pi*h*50*h*(x^2+25))*h*exp((-x+sqrt(x^2+25))/50)")` leads to
$6/(5 \cdot \pi \cdot h \cdot 50 \cdot h \cdot (x^2+25)) \cdot h \cdot e^{(-x+\sqrt{x^2+25})/50}$Tue, 17 Feb 2015 00:45:29 -0600https://ask.sagemath.org/question/25820/how-can-i-print-equations-just-like-latex/?answer=25855#post-id-25855Comment by louisgag for <p>There is the hold option, which might can help:</p>
<pre><code> z(x)=x.power(2).mul(x,hold=true)
view(z)
</code></pre>
<p>which evaluates to $x \mapsto x^2x$
For an easier typing one could use Infix opertors:</p>
<pre><code> def hold_mult(a,b):
return a.mul(b,hold=true)
h = infix_operator('multiply')(hold_mult)
</code></pre>
<p>and then use <code>x^2 *h* x</code>.</p>
<p>However in your case if you need just the initial equation to display it might be the easiest just to print it as a string. If you need this functionality more often, an (extendet version of a) function like this could be helpful: </p>
<pre><code>def paranthese_match(prefix,str):
matches = []
while (str.find(prefix) != -1):
p = -1
for li in [str.find(prefix)+len(prefix)..len(str)-1]:
if str[li] == '(':
p -=1
if str[li] == ')':
p += 1
if p==0:
matches.append(str[str.find(prefix)+len(prefix):li])
str = str[li:]
return matches
def print_expression(str):
str = str.replace('*',' \cdot ')
str = str.replace('pi',' \pi ')
for sr in paranthese_match('sqrt(',str):
str = str.replace('sqrt('+sr+")",'\sqrt{'+sr+'}')
for sr in paranthese_match('exp(',str):
str = str.replace('exp('+sr+")",'e^{'+sr+'}')
html("$"+str+"$")
</code></pre>
<p>Then <code>print_expresion("6/(5*pi*h*50*h*(x^2+25))*h*exp((-x+sqrt(x^2+25))/50)")</code> leads to
$6/(5 \cdot \pi \cdot h \cdot 50 \cdot h \cdot (x^2+25)) \cdot h \cdot e^{(-x+\sqrt{x^2+25})/50}$</p>
https://ask.sagemath.org/question/25820/how-can-i-print-equations-just-like-latex/?comment=36385#post-id-36385Thanks, that's exactly what I was looking for! I changed the line `html("$"+str+"$")` to `print "$"+str+"$"` to get raw Latex from a Sage expression. Note that the `print_expresion("6/(5*...` line should read `print_expression("6/(5*...` with 2 s's. Also, for those interested: you can print the Latex code for an already defined symbolic equation by converting it to a string as `print_expression(str(Lambda))` where Lambda is the name of your symbolic equation.Wed, 25 Jan 2017 11:27:44 -0600https://ask.sagemath.org/question/25820/how-can-i-print-equations-just-like-latex/?comment=36385#post-id-36385Answer by JesterEE for <p>Now I'm testing several equations and solutions with Sage. After testing the solutions, I'd present it to the group members. But I met a problem to print the equations. </p>
<p>For example, when I write the code. </p>
<pre><code>sage: z(x)=6/(5*pi*50*(x^2+25))*exp((-x+sqrt(x^2+25))/50)
sage: view (z)
</code></pre>
<p>Then the sage doesn't show the equation that is exactly same with the code, but it just shows simplified form. Could anyone let me know how to show the original equation? </p>
<p>And it would be also appreciated, if you tell me how to modify the simplifying mechanism. I'd love to write equations that don't contain any large denominators with many exponential terms. </p>
<p>thank you.</p>
https://ask.sagemath.org/question/25820/how-can-i-print-equations-just-like-latex/?answer=25856#post-id-25856I was looking for the same thing and found these comments on the issue.
This works with a _few_ functions, but not all.
- http://ask.sagemath.org/question/7577/prevent-sage-from-simplifying/
This is probably the best answer on why it does this simplification (and why there's no way to stop it)
- https://groups.google.com/forum/#!topic/sage-support/bPL5PDesza8
As far as the simplification mechanisms, you can look at this page. There are a number of them spread throughout that highlight different means:
- http://www.sagemath.org/doc/reference/calculus/sage/symbolic/expression.html
If you're looking to just reproduce the same text as your input, unfortunately I have found no better way than writing it out in LaTeX as a initial condition step so Sage doesn't mangle the form. This is a quick latex renderer that I modified from a [this post](http://ask.sagemath.org/question/7862/what-is-the-function-for-latex-on-a-notebook-cell/?answer=11961#post-id-11961) on ask.sagemath
def latexrender(s):
try:
pic = text("$" + s.replace('$','\$') + "$",(0,0),axes=False,figsize=1,color='black',fontsize=20)
pic.show()
except:
latexrender(latex(s))
returnTue, 17 Feb 2015 01:02:37 -0600https://ask.sagemath.org/question/25820/how-can-i-print-equations-just-like-latex/?answer=25856#post-id-25856