Is there a way to have LaTeX perform distribution?
The polynom
package has a low level function \polyprint
which prints the expanded form of a polynomial:
\documentclass{article}
\usepackage{polynom}
\begin{document}
\polyprint{(x^2-1)(x+2)}
\end{document}
The sagetex
package has this feature for polynomials and more. You need Sage installed locally or, better yet, Sagemath Cloud to run (no installation needed).
\documentclass{article}
\usepackage{sagetex}
\usepackage{amsmath}
\begin{document}
\begin{sagesilent}
x = var("x")
a = x+1
b = x^2-5*x+2
c = Integer(randint(2,7))*x^2-Integer(randint(2,7))*x+ Integer(randint(2,9))
# c is creating a random quadratic
d = sin(x)+2
e = 2^x+1
\end{sagesilent}
\noindent If we want to expand $(\sage{a})(\sage{b})$ the answer is
$\sage{expand(a*b)}$. The expansion of $(\sage{b})(\sage{c})$
gives $\sage{b*c}$. Finally $(\sage{a})(\sage{b})(\sage{c})=\sage{expand(a*b*c)}$.
We can multiply out things which aren't polynomials, too:
\[(\sage{d})(\sage{e})=\sage{expand(d*e)}=\sage{simplify(expand(d*e))}\]
\end{document}
Which gives this output: The Sage documentation on expand() and simplify() is here.