# Order of Terms in Algebraic Expression

When I write an algebraic expression of the following form,

B*OverBar[A]*C^3 + A*OverBar[C]*B

B C^3 Overscript[A, _]+A B Overscript[C, _]


the order of the terms in the output and inside the terms change. How to keep the order as it is in the input. I used "..." for this purpose, which works only on the factors of the individual terms. The issue is that I can't copy the "..." expression in the input form. And I need it to copy as input to use later. Thanks!

Edited:

I have found a simple solution. See the following. When I use the quotation "...". The order does not change which is good. But the problem is that I cannot properly copy the "Output" as an "Input". For all copying options, I get the following mess given in the output.

Input:

"B \!$$\*SuperscriptBox[\(X$$, $$3$$]\) \!$$\*OverscriptBox[\(C$$, \
$$_$$]\)" + "A B \!$$\*OverscriptBox[\(C$$, $$_$$]\)"


Output:

"A B \!$$\*OverscriptBox[\(C$$, $$_$$]\)" + "B \
\!$$\*SuperscriptBox[\(X$$, $$3$$]\) \!$$\*OverscriptBox[\(C$$, \
$$_$$]\)"

• Do you really type B !(*OverscriptBox[(A), ()]) C^3 + A !(*OverscriptBox[(C), ()]) B? For starters that is not a a complete expression. Second it's a notebook Box, not InputForm. Please format your Wolfram Language code example properly , easy to copy&paste, in Raw InputForm Feb 3 at 12:56
• Possible duplicate mathematica.stackexchange.com/questions/127333/… Feb 3 at 13:44
• The order of terms in an expression is done automatically. It is a bad Idea to work against the system and will only bring troubles. Feb 3 at 14:26
• There is a solution. It, however, has a property that it is only to look at, but not to operate with. One can use it for presentations, rather than for further calculations. If this is OK with you, I can answer. Feb 3 at 15:25
• Prior to copy and paste into Mathematica StackExchange, convert the Mathematica cells into InputForm or Raw InputForm' (and ideally turn off the In / Out cell labels). Feb 3 at 16:30

Let us introduce the following function:

rearrange[expr_, finalPositions_List] := Module[{lst, newlst},
lst = List @@ expr;
newlst =
Table[lst[[Position[finalPositions, i][[1, 1]]]], {i, 1,
Length[lst]}];
];


The function rearrange[expr,listOfFinalPositions] rewrites an expression (a sum or a product) in the order prescribed by the list entitled "listOfFinalPositions."

Arguments: expr is the expression with the head Plus or Times.

listOfFinalPositions is a list. Its length must be equal to the length of the expression. It indicates the positions the terms of the sum or product must take in the end.

For example, for the expression a+b+c, the listOfFinalPositions {2,3,1} means that a must go to the second position, b - to the third, and c - to the first one resulting in c+a+b

expr = B*OverBar[A]*C^3 One can see that here B has the first position. It must stay there. C^3 has the second one; it must go to position 3. A_bar must go to position 2. Finally, listOfFinalPositions={1,3,2}

rearrange[B*OverBar[A]*C^3, {1, 3, 2}] Have fun!

• Thanks for the nice answer, but in actual I have really long long expressions, I am not sure how I can apply this method there. Feb 3 at 20:51
• @SciJewel That's precisely why I asked you what you are after. If one has very long expressions (with many tens or hundreds of terms), it is typically useless to look at each. Then it is better to think about reasonable ways to group terms somehow and simplify these groups, if possible. With long expressions ordering of terms (as in my example) is counterproductive. In contrast, as soon as you get the final answer which you want to publish or show in a presentation, such a method may become helpful. Feb 3 at 21:29
• Thanks! So manual re-arranging is the only option. I have used the quotation mark as the easy solution, however, in this copying as an input does not work. Please read the edited part of the quesiton. Feb 4 at 8:07
• @SciJevel As soon as you write something in quotes, you type a string rather than a symbol. Feb 4 at 11:13
• @SciJevel (continuation). Therefore, your input is a string as soon as you copy-paste it as the input. You cannot operate with it as with symbols. It is for this reason that I wrote that there are ways to order as you like, but these ways are only to use them to look at, not to operate with. Feb 4 at 11:33

It really depends on how you want to use the result. For example, you say

I need it to copy as input to use later

This makes any of the Hold* wrappers a bit problematic (you'll probably have to use ReleaseHold to be able to use it how you want).

As it happens there is the Defer wrapper, and to me that sounds like what you want (@DanielLichtblau already suggested this in the comments). So, this

Defer[B*OverBar[A]*C^3 + A*OverBar[C]*B]


will display without rearranging anything, but if you copy-pasted the result to somewhere else, the Defer wrapper will automatically get stripped.

On the other hand, if you're primarily concerned with just the display (and not with subsequent evaluation), my goto construct is a template. This gives you fine grained control of what you want to the parameters to be. So, let's say that your A, B, and C were really your objects of interest that you want to sit nicely in a structure expression. You could do this:

TemplateApply[
TemplateExpression[
TemplateSlot*OverBar[TemplateSlot]*TemplateSlot^3 + TemplateSlot*OverBar[TemplateSlot]*TemplateSlot],
{A, B, C},
InsertionFunction -> HoldForm]


Of course, you can pull out that TemplateExpression into a variable for re-use.

Or maybe you just care that the sum appear in the right order. Then you could use

mySum = TemplateExpression[TemplateSlot + TemplateSlot]
TemplateApply[mySum, {y x, 5}, InsertionFunction -> HoldForm]


x y + 5`

(I used a simpler example for clarity.) Notice that since the product was part of the argument to TemplateApply, it was evaluated and ordered per Mathematica's preference, but the sum was held in its original order.

Or maybe your specific case falls somewhere between these two examples. Just be aware that since the InsertionFunction wrapped the result in HoldForm, you'll need to ReleaseHold this if you want to do further computeations.