Forcing a particular ordering of terms, i.e. overriding Mathematica's, is a recurrent issue, especially for users working within particular domains where there is a particular ordering tradition or where the problem itself is more "elegant" to a user when expressed in a certain way.
Both @Silvia and @Mr Wizard's answers to How do I reassign canonical ordering of symbols? are effective and educational (but see also 188576, 30216 and tag combination [formatting] [order]) but I have failed to apply the answers to this problem.
The issues involve the facts that the expression of interest is not a polynomial - MonomialList does not apply (though one can of course use List@@colEqn to get the terms of the Plus[]) - and that ordering information is implicit and somewhat buried.
The context is the Collatz problem (an entertaining diversion, no more than that!) in which, if one redefines "step" to be $n\to n'=\frac{3 k+1}{2^q}$ s.t. $n'$ is again odd, one can write the nth term in the sequence starting from an initial value k to be, for presentation purposes:
colEqnH =
HoldForm[(3^p *
k + (3^(p - 1)) + (3^(p - 2)) 2^Subscript[q,
1] + (3^(p - 3)) 2^(Subscript[q, 1] + Subscript[q,
2]) + \[Ellipsis] + (3^1) 2^(Subscript[q, 1] + Subscript[q,
2] + \[Ellipsis] + Subscript[q, n - 2]) +
2^(Subscript[q, 1] + Subscript[q, 2] + \[Ellipsis] + Subscript[
q, n - 1]))/2^Subscript[q, n]]
$\frac{3^p k+3^{p-1}+3^{p-2} 2^{q_1}+3^{p-3} 2^{q_1+q_2}+\ldots +3^1 2^{q_1+q_2+\ldots +q_{n-2}}+2^{q_1+q_2+\ldots +q_{n-1}}}{2^{q_n}}$
But on releasing the hold the output becomes:
$2^{-q_n} \left(k 3^p+3\ 2^{q_{n-2}+q_1+q_2+\ldots }+2^{q_{n-1}+q_1+q_2+\ldots }+3^{p-3} 2^{q_1+q_2}+3^{p-2} 2^{q_1}+3^{p-1}+\ldots \right)$
What I would like is to be able to manipulate an unheld equation and then to be able to present it in the preferred form.
Features of note/requirements for formatted output
- Powers of 3: the terms should be left to right in decreasing order of the powers of 3 - but the order is only implied by $p > p-1 > p-2 > ... > 1$
- In products, powers of 3 come first (before k, powers of 2)
- Powers of 2: the terms should be left to right in order or the subscripts (indices) of the $q$
- Ellipsis is used to indicate the omission of an indefinite number of terms, and its position in the input expression should be preserved
- superscripts/subscripts/exponents should be written $p-1$ and not $-1 +p$
- Fractions should be displayed as $\frac{a}{b}$
For reference, I generalised @Silvia's code and applied it as follows (clearly without success, but.. it shows effort :) )
ClearAll[prodSort]
prodSort[mono_, prodPriorityTable_] :=
If[Head[mono] =!= Times,
Row[{mono}],
Module[{termLst},
termLst = List @@ mono;
Row@SortBy[{termLst, prodPriorityTable@termLst}\[Transpose], Last][[All, 1]]
]
]
ClearAll[sumSort]
sumSort[polynom_, sumPriorityTable_] :=
Module[{termLst},
termLst = List @@ polynom;
SortBy[{termLst, sumPriorityTable@termLst}[Transpose], Last][[All, 1]] // Riffle[#, "+"] & // Row
]
containsQ[expr_, form_]:=Not@FreeQ[expr, form];
ClearAll[collatzProdPriorityTable];
collatzProdPriorityTable[termLst_]:=
Piecewise[
{
{ 0, containsQ[#, Power[3,_]]},
{ 20, containsQ[#, Power[2,_]]},
{100, containsQ[#, n]}
}, 50
] & /@ termLst;
ClearAll[collatzSumPriorityTable];
collatzSumPriorityTable[termLst_]:=
Piecewise[
{
{ 0, containsQ[#, n]},
{10, containsQ[#, 3]}
}, 50
] & /@ termLst;
present[expr_, sumPriorities_, prodPriorities_]:= sumSort[prodSort[#, prodPriorities] & /@ expr, sumPriorities] // TraditionalForm
And finally, the imperfect result from:
present[ReleaseHold@colEqnH, collatzSumPriorityTable, collatzProdPriorityTable]
No TeX available because "TeXForm of TemplateSlotSequence[1, ] is not supported"; 55363 applies? (I am using MMA 11.0.1) And yes, I now see I haven't handled the lastly-introduced fraction properly, but that doesn't affect the thrust of the question)
UPDATE
Using @kglr's tip setting BoxForm`$UseTemplateSlotSequenceForRow = False; I was able to obtain TeX at last
$2^{-q_n}+k 3^p+3\ 2^{q_{n-2}+q_1+q_2+\ldots }+2^{q_{n-1}+q_1+q_2+\ldots }+3^{p-3} 2^{q_1+q_2}+3^{p-2} 2^{q_1}+3^{p-1}+\ldots$
