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

15

4

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?

orome

Posted 2015-03-09T16:30:08.150

Reputation: 12 197

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 – 2015-03-09T23:54:20.793

@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".) – orome – 2015-03-10T00:18:58.660

1Yes, 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 – 2015-03-10T02:27:17.603

@raxacoricofallapatorius: Is my answer of any help? – Jinxed – 2015-03-10T07:22:16.540

@Jens: A, sorry, I forgot to link.

– orome – 2015-03-10T13:42:25.983

Answers

13

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:

System`FEDump`CopyAsTeXMakeBoxes = #&

Details

Why copied boxes are reformatted

Copy As > LaTeX command, under the hood, calls FrontEnd`CopyAsTeX function.

If we spelunk it:

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

we'll see it performs several steps. Relevant, for answer to this question, is call to System`FEDump`TransformBoxesToTraditionalFormBoxes 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 System`FEDump`CopyAsTeXMakeBoxes 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
System`FEDump`TransformBoxesToTraditionalFormBoxes[%]
(* 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

System`FEDump`CopyAsTeXMakeBoxes 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:

System`FEDump`CopyAsTeXMakeBoxes = #&

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

jkuczm

Posted 2015-03-09T16:30:08.150

Reputation: 14 388

5

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}

Jinxed

Posted 2015-03-09T16:30:08.150

Reputation: 3 590

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. – orome – 2015-03-10T13:43:41.317

@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? – Jinxed – 2015-03-10T16:09:24.823

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!) – orome – 2015-03-10T16:47:49.590

@raxacoricofallapatorius: Looks like there is no other solution coming up. ;) – Jinxed – 2015-03-14T19:30:06.560

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:" – orome – 2015-03-15T13:21:11.257

@raxacoricofallapatorius: I updated the answer to reflect my findings on this issue more clearly. – Jinxed – 2015-03-15T14:51:26.407

@raxacoricofallapatorius: Did my answer work out for you? If so, I would be glad if you accepted it. – Jinxed – 2015-03-21T16:22:25.013

Would everybody please refrain from times and again changing my answer? – Jinxed – 2015-03-27T16:39:43.893

We have (in effect) two votes for revision 4. I think we should stick with that (that's what I'll accept). – orome – 2015-03-27T16:45:50.053

@Jinxed "... with any HoldForm etc. stripped: Your expressions will be evaluated ..." are you sure about this? I can select 1+2 in notebook choose "Copy As LaTeX" and get 1+2 not 3, it's the same e.g. for output of HoldForm[1+2]. I don't believe reformatting from question is caused by evaluation of copied expression. I think it's done at level of MakeBoxes or equivalent function, as I tried to describe it in answer to raxacoricofallapatorius`s previous question.

– jkuczm – 2015-03-27T17:38:34.030

What precisely happens depends on what precisely you select in notebook. And whether you select as much as Mathematica needs to know that you're copying TraditionalForm boxes, otherwise they will be interpreted as StandardForm boxes and Mathematica will reconvert them to TraditionalForm boxes. – jkuczm – 2015-03-27T17:42:11.600

@jkuczm: For the OP's question, the behavior is just as described. There may be other situations, but the case at hand is as described in the answer. – Jinxed – 2015-03-27T17:52:12.883