# Can one prevent $\TeX$ conversion from changing form of expressions?

This is similar to my previous question Can one prevent operators like Factor or Apart from changing full form of some algebraic expressions? but concerns $\TeX$ and I cannot figure out whether I might use the solution found there in this case. I found several questions here about $\LaTeX$ conversion, one of them (Override Equation structure change in mathematica - CopyToLaTeX) addressing this particular issue, but the only relevant answer (using HoldForm) does not work in my case (see below).

When I export expressions to $\TeX$, some undesirable (for me) form changes occur. A minimal example is e. g. (1+q+q^2)(1-4 q) which goes to $(1-4q)(q^2+q+1)$. This happens both while right-clicking and choosing "Copy as" -> "LaTeX" or issuing 1-q+q^2//TeXForm.

Doing TeXForm[StandardForm[(1+q+q^2)(1-4 q)]] helps only partially, it gives $(1-4q)(1+q+q^2)$, i. e. still interchanges the factors which I don't want to do.

In this particular case, TeXForm[HoldForm[(1+q+q^2)(1-4 q)]] helps, but I sometimes need to export really huge expressions where HoldForm does not work the way I want. For example, if I would have Sum[q^n,{n,0,2}](1-4q) instead (and I frequently have large expressions like that) then TeXForm[HoldForm[Sum[q^n,{n,0,2}](1-4q)]] gives $\left(\sum _{i=0}^2 q^i\right) (1-4 q)$ (while TeXForm[StandardForm[Sum[q^n,{n,0,2}](1-4q)]] still interchanges factors giving $(1-4 q) \left(1+q+q^2\right)$).

Clearly it is very annoying to have to change everything back into form I need by hand, especially with large expressions. Is there a way to tell the system to convert just the expression the way it is seen, without changing anything?

• Will something silly like TeXForm@DisplayForm@HoldForm[(1 + q + q^2) (1 - 4 q)] work? Mar 14, 2016 at 16:34
• In what way does HoldForm not work exactly? I can't figure it out from your post. Do you mean that you want the summation expanded but the factors not reordered? Mar 14, 2016 at 16:36
• @march You are right it is not clear since I could not figure out how to give an explicit example. What happens in practice is this: I have rendered some huge expression and now see it in output form. Now I want to convert it to $\TeX$ using HoldForm. But issuing TeXForm[HoldForm[%]] just gives \%. Assigning some new symbol also does not work - if I say first e=% and then TeXForm[HoldForm[e]] this just gives e. Mar 14, 2016 at 18:19
• If you want expression to evaluate and use HoldForm just to prevent additional reordering done by TeXForm, then you can use Evaluate: % // Evaluate // HoldForm // TeXForm. If you want partial evaluation like in your example with Sum, then maybe making some operations Inactive combined with activeForm would work for you: Inactive[Times][Sum[q^n, {n, 0, 2}], (1 - 4 q)] // activeForm // TeXForm, it can be combined with Evaluate // HoldForm if needed. Mar 16, 2016 at 22:17
• @jkuczm Thank you, I will check asap. In the meanwhile - maybe it was not well formulated, what I want is to have $\TeX$ rendered exactly the way I see it in the output. Mar 17, 2016 at 6:26

As far as I know there are two sources of reordering in examples from OP.

First is Orderless attribute of functions like Plus and Times which, in process of standard evaluation, sorts their arguments to canonical order.

q + 1 + q^2
(* Out[]= 1 + q + q^2 *)


Second is that TraditionalForm, which boxes are used internally by TeXForm, does additional reordering of sums and products.

1 + q + q^2
(* Out[]= 1 + q + q^2 *)
q^2 + q + 1 *)


Both reorderings can be prevented by wrapping expression with HoldForm.

q + 1 + q^2 // HoldForm // TeXForm
(* Out[]//TeXForm=
q+1+q^2 *)


If you have an already evaluated expression and you're satisfied with ordering in it, you can prevent additional reorderings, done by conversion to TraditionalForm boxes, by combining HoldForm with Evaluate.

1 + q + q^2
% // Evaluate // HoldForm // TeXForm
(* Out[]= 1 + q + q^2 *)
(* Out[]//TeXForm=
1+q+q^2 *)


More complicated situation is when you want some sub-expressions to evaluate, but want to prevent some of reorderings that evaluation causes. If you control creation of those expressions there are couple of ways to prevent such reorderings.

You could completely prevent evaluation (and thus reordering) of some instances of Plus or Times by wrapping them with Inactive, let whole expression evaluate, then wrap result in HoldForm, then Activate and convert it.

Inactive[Times][Sum[q^n, {n, 0, 2}], (1 - 4 q)]
% // Evaluate // HoldForm // Activate
% // TeXForm
(* Out[]= (1 + q + q^2)*(1 - 4 q) *)
(* Out[]= (1 + q + q^2) (1 - 4 q) *)
(* Out[]//TeXForm=
\left(1+q+q^2\right) (1-4 q) *)


Different approach is to locally "switch off" Orderless attribute of some functions.

InternalInheritedBlock[{Times},
ClearAttributes[Times, Orderless];
Sum[q^n, {n, 0, 2}] (1 - 4 q) // Evaluate // HoldForm // TeXForm
]
(* Out[]//TeXForm=
\left(1+q+q^2\right) (1-4 q) *)
`

If you already have an expression with some ordering you don't like, you can also ask Mathematica to reorder it afterward to desired order, but this requires teaching Mathematica what this desired order is. See e.g. How to sort arguments of Times in nonstandard order?