# How do I prevent Mathematica from reformatting the expressions that I copy as LaTeX?

When I choose something that looks like

$$W_1(1)=\rho ^{5} \omega _1 \rho ^{-5}\\ W_2(2)=\rho ^{23} \omega _2 \rho ^{-23}\\ W_3(3)=\rho ^{10} \omega _3 \rho ^{-10}\\ W_4(4)=\rho ^{9} \omega _4 \rho ^{-9}$$

in Mathematica and select Copy As > LaTeX I get something entirely different:

$$W_1(1)=\frac{\rho ^5 \omega _1}{\rho ^5}\\ W_2(2)=\frac{\rho ^{23} \omega _2}{\rho ^{23}}\\ W_3(3)=\frac{\rho ^{10} \omega _3}{\rho ^{10}}\\ W_4(4)=\frac{\rho ^9 \omega _4}{\rho ^9}$$

How do I prevent Mathematica from reformatting the expressions that I copy as LaTeX?

• It's not clear to me exactly what your scenario is, but if you're just interested in typesetting, then this recent answer may be applicable.
– Jens
Mar 9, 2015 at 23:54
• @Jens: I was hoping to find a way to prevent MMA from changing the formatting of my expressions when I copied them. (A pretty general "scenario".) Mar 10, 2015 at 0:18
• Yes, but how are you creating the "expressions" - the question should probably state that they are generated as output from Mathematica code (just guessing). The other scenario would be that you are just typing them in by hand. The optimal solutions would be quite different depending on the starting point...
– Jens
Mar 10, 2015 at 2:27
• @raxacoricofallapatorius: Is my answer of any help? Mar 10, 2015 at 7:22
• @Jens: A, sorry, I forgot to link. Mar 10, 2015 at 13:42

Unfortunately, the Mathematica's Copy As > LaTeX command is not (as you'd expect) a command that copies the selected expressions as LaTeX. Instead, it performs additional reformatting that can only be avoided by changing behavior of the relevant built-in function with:

SystemFEDumpCopyAsTeXMakeBoxes = #&


## Details

### Why copied boxes are reformatted

Copy As > LaTeX command, under the hood, calls FrontEndCopyAsTeX function.

If we spelunk it:

Import["https://raw.githubusercontent.com/szhorvat/Spelunking/master/Spelunking.m"]
Spelunk@FrontEndCopyAsTeX


we'll see it performs several steps. Relevant, for answer to this question, is call to SystemFEDumpTransformBoxesToTraditionalFormBoxes function. We can "discover" it using pop-up that appears after clicking on this function's name.

We'll see that, basically, it calls MakeExpression[..., "StandardForm"] on given boxes, than passes resulting expression to SystemFEDumpCopyAsTeXMakeBoxes function, together with boxes. CopyAsTeXMakeBoxes function calls MakeBoxes[..., "TraditionalForm"] on given expression.

This is the place where reformatting occurs.

If we take expression from question and pass it, converted to boxes, to TransformBoxesToTraditionalFormBoxes we'll get:

HoldForm[Equal[Subscript[W,1][1],Times[Power[ρ,5],Subscript[ω,1],Power[ρ,Times[-1,5]]]]] // MakeBoxes
SystemFEDumpTransformBoxesToTraditionalFormBoxes[%]
(* TagBox[RowBox[{RowBox[{SubscriptBox[W,1],(,1,)}],==,FractionBox[RowBox[{SuperscriptBox[ρ,5], ,SubscriptBox[ω,1]}],SuperscriptBox[ρ,5]]}],HoldForm] *)
% // DisplayForm


$W_1(1)=\frac{\rho ^5 \omega _1}{\rho ^5}$

We see that HoldForm TagBox is still there. TransformBoxesToTraditionalFormBoxes and CopyAsTeXMakeBoxes are written in safe way, there's no evaluation leak. Expression created from boxes is wrapped with HoldComplete and function applied to it has properly set HoldAllComplete attribute. Reformatting is done purely by MakeBoxes function.

TransformBoxesToTraditionalFormBoxes was added in Mathematica version 9. Verison 8 copies expressions without reformatting.

### How to change Copy As > LaTeX behavior

SystemFEDumpCopyAsTeXMakeBoxes takes two arguments first are (almost) original boxes, second is expression obtained from those boxes. Original boxes are returned when reconverting expression to boxes encounters any problems.

If we don't mind changing behavior of built-in function, we can force CopyAsTeXMakeBoxes to always return unchanged boxes by setting:

SystemFEDumpCopyAsTeXMakeBoxes = #&


Solution tested in Mathematica versions 9 and 10. Previous versions don't need it.

Despite the command name, "Copy As LaTeX", Mathematica's frontend does not just copy the material in the frontend, but evaluates it first, which indicates a round-trip to the kernel with any HoldForm etc. stripped: Your expressions will be evaluated when creating the $\TeX$ representation.

Your only recourse is to create a workaround that explicitly uses TeXForm to create $\TeX$ representation.

For example, you can use TeXForm and the answers to your other question:

Just apply TeXForm and get, e.g.:

W_5(1)=\rho^5\omega(1)\rho^{-5}


which happily formats in $\TeX$ as

$$W_5(1)=\rho ^5 \omega (1) \rho ^{-5}$$

Putting it all together: Use

TeXForm@HoldForm[Subscript[W,f[a,b]][c]=\[Rho]^f[a,b]\[Omega][c]\[Rho]^-f[a,b]]//.{f[a,b]->5,c->1}

• That process TeX in MMA. I need the expression as is in the Notebook, but when I copy I don't want it to change. Mar 10, 2015 at 13:43
• @raxacoricofallapatorius: Then you might be at a loss. But my "workaround" gives you the opportunity to just copy the output it produces and have what you need, no? Mar 10, 2015 at 16:09
• Yes. I'll hold for a bit before accepting and see if another solution turns up. But this may be as close as I get. (Typical Mathematica; every day I use it I find myself wondering why!) Mar 10, 2015 at 16:47
• @raxacoricofallapatorius: Looks like there is no other solution coming up. ;) Mar 14, 2015 at 19:30
• Can you confirm that it is impossible to prevent this (buggy) behavior of Mathematica? I'd like the answer to address that: e.g, "Mathematica's 'Copy As > LaTeX' command is not (as you'd expect) a WYSIWYG command but is really a 'simplify and then copy the result' command. In Mathematica there's no way to copy what you see; but you can manually generate LaTeX when you need wit with:" Mar 15, 2015 at 13:21