3
$\begingroup$

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:

enter image description here

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}$

$\endgroup$
6
  • $\begingroup$ Could you give an example of how Collect gives the "wrong" parsing of an equation? $\endgroup$ Mar 7, 2020 at 2:48
  • 1
    $\begingroup$ Plus is Orderless $\endgroup$
    – Michael E2
    Mar 7, 2020 at 3:01
  • $\begingroup$ @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! $\endgroup$ Mar 7, 2020 at 3:16
  • 1
    $\begingroup$ 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 $\endgroup$
    – Michael E2
    Mar 7, 2020 at 3:32
  • $\begingroup$ I added code that can be copy-pasted. $\endgroup$ Mar 7, 2020 at 7:47

2 Answers 2

1
$\begingroup$

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

$\endgroup$
1
$\begingroup$

Perhaps this:

MonomialList[expression, 
     {p12, p13, p14, p32, p42}] /. 
   {args__} :> HoldForm[Plus[args]]

enter image description here

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.

$\endgroup$
1
  • $\begingroup$ Thanks. I also need the variables to be to the right of the coefficients. Is that also doable? $\endgroup$ Mar 9, 2020 at 19:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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