This semester I've been typesetting my math homeworks in Latex, and these often involve long expressions with many fractions and brackets. I'd love to be able to use macros to perform operations like expanding brackets, collecting like terms, factorizing polynomials, etc. Things like:

(a + b)( c + d) = ac + ab + bc + bd
a/b = c/d --> ad = bc 
(a + b + c)^2 = ...

It would be even better if there are macros that combine operations like these to execute more complicated tasks like rewriting an equation in terms of one of the variables.

  • 1
    Welcome to TeX.SX! It will be better if you show us your compilable MWE that you have tried so far. Also, you should add the math formulas as an image instead of some lines of code. – user156344 Nov 17 '18 at 3:16

The sagetex package gives you access to a computer algebra system, called SAGE, which can help. SAGE documentation for Symbolic equations is here and documentation for the package is here. Here is a sample of how you can combine SAGE with LaTeX through sagetex:

a,b,c,d = var("a,b,c,d")
eqn = a/b==c/d
Given variables $a, b, c$, and $d$; then:\\
$(a+b)(c+d)=\sage{expand((a+b)*(c+d))}$ and \\
$(a+b+c)^2=\sage{expand((a+b+c)^2)}$. \\
Moreover, $\frac{a}{b}=\frac{c}{d}$ implies $\sage{(b*d)*eqn}$
f = x^2 + y^2 == 1
solutions = f.solve(x)

If you have the equation $x^2+y^2=1$ then solving for $x$ gives
$\sage{f.solve(x)[0]}$ and $\sage{f.solve(x)[1]}$ 

SAGE recognizes x as a default variable, everything else should be declared using var(). Set up the calculations in SAGE through sagesilent and access the results using \sage{}. SAGE is not part of the LaTeX distribution so it should be installed on your computer or accessed through a free Cocalc account. The result of the code being compiled is shown below: enter image description here

It's easy to make a mistake typesetting math as you did in the first equation. SAGE helps eliminate mistakes.

  • +1 I didn't know either sagetex or SAGE, it's excellent, a thousand thanks! – AndréC Nov 17 '18 at 8:58
  • I just installed SAGE on my windows 10 with MikTeX and there are question marks (??) instead of results, do you know why? – AndréC Nov 17 '18 at 9:09
  • Most likely this is because you are assuming that you can just LaTeX your document and be done. That won't work because SAGE needs to run. The documentation explains it's a 3 step process. My answer here might help. If that doesn't work then it's probably a communication problem between TeX and SAGE which is addressed here. – DJP Nov 17 '18 at 13:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.