2
$\begingroup$

I used Mathematica to greatly simplify an algebraic expression. I now want to copy it into some notes, but it by defaults things in a very non-intuitive manner. I have a sorting function that will sort the expression properly, but the output still reverts to Mathematica's default sorting algorithm.

As an example, let's say I want it to output: f[b] f[a]. The sorting function

In[1]:= sorted = Sort[Level[f[b] f[a], 1], OrderedQ[{(#2)[[1]], (#1)[[1]]}] &]

Out[1]= {f[b], f[a]}

properly returns a sorted list. But when I then apply the Times function to bring it back to an algebraic expression, it goes back to the default way of sorting:

In[2]:= Apply[Times, sorted]

Out[2]= f[a] f[b]

Is there a trick for formatting the output of an expression to match the ordering I would like? Preferably something I can can both view directly in Mathematica as well as copy to LaTeX.

$\endgroup$
0

1 Answer 1

1
$\begingroup$

Times has the Orderless attribute so this sorting cannot be prevented if you also want to do calculations with the expression. However, if you only need to display it once, then you can do something like this:

Suppose you already have

times[f[b], f[a]]

sorted as you like. The just wrap it in HoldForm and replace times with Times.

HoldForm[
  times[f[b], f[a]]
] /. times -> Times

Mathematica graphics

Instead of times you can of course just use List, but once you need to deal with more than a single type of operation (e.g. both multiplication and addition), it is convenient to use distinguishable heads.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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