# Generating $\LaTeX$ source for binomial expansion

The binomial expansion is of the form

$(x+y)^n = \sum\limits_{i=0}^n {n \choose i} x^i y^{n-i}$.

I'd like to generate the appropriate Mathematica output (so I can then convert it to $\LaTeX$ source through TeXForm[]) showing each term. For instance, for $(x+y)^5$ I would like (after conversion and typesetting):

$1 x^0 y^5 + 5 x^1 y^4 + 10 x^2 y^3 + 10 x^3 y^2 + 5 x^4 y^1 + 1 x^5 y^0$.

The first step in my first attempt is:

myelements = Table[{ToString[Binomial[5, i]], StringForm["\!$$\*SuperscriptBox[\(x$$, $$$$]\)", i], StringForm["\!$$\*SuperscriptBox[\(y$$, $$$$]\)", 5 - i]}, {i, 0, 5}]

giving

{{"1", StringForm["\!$$\*SuperscriptBox[\(x$$, $$$$]\)", 0], StringForm["\!$$\*SuperscriptBox[\(y$$, $$$$]\)", 5]}, {"5", StringForm["\!$$\*SuperscriptBox[\(x$$, $$$$]\)", 1], StringForm[ "\!$$\*SuperscriptBox[\(y$$, $$$$]\)", 4]}, {"10", StringForm[ "\!$$\*SuperscriptBox[\(x$$, $$$$]\)", 2], StringForm[ "\!$$\*SuperscriptBox[\(y$$, $$$$]\)", 3]}, {"10", StringForm[ "\!$$\*SuperscriptBox[\(x$$, $$$$]\)", 3], StringForm[ "\!$$\*SuperscriptBox[\(y$$, $$$$]\)", 2]}, {"5", StringForm[ "\!$$\*SuperscriptBox[\(x$$, $$$$]\)", 4], StringForm[ "\!$$\*SuperscriptBox[\(y$$, $$$$]\)", 1]}, {"1", StringForm[ "\!$$\*SuperscriptBox[\(x$$, $$$$]\)", 5], StringForm[ "\!$$\*SuperscriptBox[\(y$$, $$$$]\)", 0]}}

to get the individual terms, but unfortunately these elements are not easily joined (since they are not all Strings or Text), nor is the separating "+" sign appearing. Then:

Flatten@Riffle[myelements, Table["+", {5}]]


There must be a way using Text or Boxes or whatever that yields a form that can be converted to $\LaTeX$, but I'm seeking the most elegant method.

Any ideas?

Sum[With[{b = Binomial[5, i], xexp = i, yexp = 5 - i},

$1 x^5 y^0+5 x^4 y^1+10 x^3 y^2+10 x^2 y^3+5 x^1 y^4+1 x^0 y^5$
• With the trivial fix of xexp = 5-i and yexp = i, this is perfect. Thanks! May 5, 2017 at 22:45