# Organizing Algebraic Expressions (for $\mathrm\LaTeX$ conversion)

I would ideally like to not have to spend a lot of time writing LaTeX for complicated algebraic expressions that I've worked out in Mathematica. I know that I can "copy-as-LaTeX", but my current issue is that my algebraic expressions are often formatted slightly wrong.

(Additionally, any general tips for working between $$\mathrm\LaTeX$$ and Mathematica would be appreciated!)

EDIT: Explicitly showing the code:

expression = -p12 β - 1/2 I (2 p12 (Δc - Δp) - p42 Ωa + p13 Ωc - p32 Ωp + p14 Ωs)


For example for the following algebraic expression:

I would like it to be sorted by variables in the following order (p12, p13, p14, p32, p42):

To look like:

$$(-\beta - i (\text{\Delta c}-\text{\Delta p}))\text{p12}+\text{\Omega c} \text{p13} +\text{\Omega s} \text{p14} - \text{\Omega p} \text{p32}-\text{\Omega a} \text{p42}$$

If I use this code:

Collect[expression, {p12, p13, p14, p32, p42}]


it doesn't seem to organize the variables in this order (of {p12, p13, p14, p32, p42}). instead, it returns an ordering: Additionally...I've observed that if I copy-as latex, what is above appears. But if I look at what I am copying, it has this form:

$$\text{p12} (-\beta +i (\text{\Delta p}-\text{\Delta c}))-\frac{i \text{p13} \text{\Omega c}}{2}-\frac{i \text{p14} \text{\Omega s}}{2}+\frac{i \text{p32} \text{\Omega p}}{2}+\frac{i \text{p42} \text{\Omega a}}{2}$$

• Could you give an example of how Collect gives the "wrong" parsing of an equation? – David G. Stork Mar 7 at 2:48
• – Michael E2 Mar 7 at 3:01
• @DavidG.Stork, I added the example for me, but it's a bit strange. What I see it returning as an output appears to be sorted incorrectly, but when I copy it as latex, it actually gives me the correct order! – Steven Sagona Mar 7 at 3:16
• People here generally like users to post code as Mathematica code instead of just images or TeX, so they can copy-paste it. It makes it convenient for them and more likely you will get someone to help you. You may find this meta Q&A helpful – Michael E2 Mar 7 at 3:32
• I added code that can be copy-pasted. – Steven Sagona Mar 7 at 7:47

As is mentioned in the comment, Plus is orderless. To solve this problem, one may replace Plus by another function, say plus, and define the format for plus. For example,

Format[plus[x__]]:=HoldForm[Plus[x]]; var={p12, p13, p14, p32, p42}; SortBy[plus@@Collect[expression,var],Cases[#,Alternatives@@var,{0,Infinity}]&]//TeXForm

Perhaps this:

MonomialList[expression,
{p12, p13, p14, p32, p42}] /.
{args__} :> HoldForm[Plus[args]] TraditionalForm[expression] shows the monomials in the same order, but I think it's merely a coincidence.

You have to hold Plus to keep the arguments from being reordered, just as @Wen Chern has done, too. This sort of manipulation is usually only done for the purposes of presenting output in a more human-readable form. It is not usually a convenient way to compute with expressions.

• Thanks. I also need the variables to be to the right of the coefficients. Is that also doable? – Steven Sagona Mar 9 at 19:11