# How to just print the calculation using sagetex

I want a command \simp which takes an algebraic expression E_1 and outputs E_1 = E_2 where E_2 is the simplified form of E_2. To do so I tried to use sagetex like this:

\documentclass{article}
\usepackage{sagetex}

\newcommand{\simp}[1]{$\sage{#1} = \sage{simplify(#1)}$}
\pagestyle{empty}

\begin{document}

\simp{3*x^(7-2) + 3*x^5}

\end{document}


Which results in

and not in

.

Any idea how to fix this? Solving this from the sage point of view doesn't seem to be so easy because sage seems to lack a global hold feature: http://trac.sagemath.org/ticket/10035

So is there any idea how to solve this in an elegant way from the LaTeX side?

• I want that * is printed as a \cdot when one has something like 3*(x + y) but that it is not printed at all if one has 3*x

• I am not sure about the correct spacing (see the difference in my second output) and how to get it.

I also really want to use sage for this simplification and not a handmade latex-solution because I plan to do similar but more complex things in the future such that it is a good idea to have the power of a computer algebra-system in the background.

-
Have you try to simply use \newcommand{\simp}[1]{$#1 = \sage{simplify(#1)}$} ? – projetmbc Oct 6 '13 at 8:29
This doesn't work because the TeX syntax is different from the sage syntax instead of $#1 = ... I need$\convert{#1} = ... where \convert converts the Sage to the TeX syntax... – Anna Oct 6 '13 at 14:38

I'm not sure I understand your question but I've used sagetex in a manner similar to what you're talking about. Here's how I approach it.

\documentclass{article}
\usepackage{amsmath}
\usepackage{sagetex}
\begin{document}
\begin{sagesilent}
b1 = 3
b2 = 3
e1 = 7
e2 = 2
e3 = 5
t1 = b1*x^(e1-e2)
t2 = b2*x^e3
\end{sagesilent}
\noindent $\sage{b1}x^{\sage{e1}-\sage{e2}}+\sage{b2}x^{\sage{e3}}$\\
$\sage{b1}x^{\sage{e1-e2}}+\sage{b2}x^{\sage{e3}}$\\
$\sage{t1+t2}$\\
Therefore, $\sage{b1}x^{\sage{e1}-\sage{e2}}+\sage{b2}x^{\sage{e3}}=\sage{t1+t2}$
\end{document}


By getting sage to simplify when you want (eg getting the exponents or getting sage to do the subtraction of exponents in \sage{b1}x^{\sage{e1-e2}}) you can show the steps you want in the solution. That means you're setting the latex formatting; if you want \cdot instead of * then type it.

-
The problem with your answer is that it is more complicated than just typesetting the equation manually. So it doesn't solve my problem. – Anna Oct 6 '13 at 7:56
If projetmbc's suggestion doesn't work then I doubt it can be done. I think there will have to be complications if you want the power of a CAS in the background: it needs to know the values b1, b2, e1, ...etc. Typesetting manually may be easier now but it will be prone to human error if you "plan to do similar but more complex things". In any event, you should consider asking your question here if nobody has a satisfactory answer. – DJP Oct 6 '13 at 18:41
What's a right way to do this while outputting the sage syntax, i.e., without using sagesilent? – Trevor Alexander Feb 18 '14 at 4:42
You could avoid sagesilent by using sage for a specific calculation as needed. For example, $3x^{7-2}+3x^{5}=3x^{\sage{7-2}}+3x^5$. The problem comes when you want to change the problem slightly to create a similar problem (eg change the 3s to 4s). Then you need to change it at every step. Having the sagesilent block lets you quickly change the coefficients (or even make them random). Anna's problem, involving general expressions, would, I think, require sagesilent. – DJP Feb 18 '14 at 5:43

Not sure what exactly you wanted,but here is what I did to produce your result in Sage:

\documentclass{article}
\usepackage{sagetex}
\newcommand{\simp}[1]{$3x^{(7-2)} + 3x^5$}
\pagestyle{empty}
\begin{document}
\simp{#1} $= \sage{simplify(3*x^(7-2) + 3*x^5)}$
\end{document}


Sage is still under development. We shall do some work ourselves before pushing it to Machines This is the resulting output:

-