4

I have obtained a formula in Mathematica and I want to transfer it to Microsoft Word so I did this:

ff = (-2 a17 denP[re] + b17 Sqrt[denE[re]] denP[re]^(3/2))/(
  2 (a17 - b17 Sqrt[denE[re]] Sqrt[denP[re]] + 
     c171 denE[re] denP[re])^2);
nn = ff /. {denE[re] -> 
     "\!\(\*SubscriptBox[\(\[Rho]\), \
\(e\)]\)(\!\(\*SubscriptBox[\(r\), \(e\)]\))", 
    denP[re] -> 
     "\!\(\*SubscriptBox[\(\[Rho]\), \
\(p\)]\)(\!\(\*SubscriptBox[\(r\), \(e\)]\))", 
    a17 -> Subscript[a, 17], b17 -> Subscript[b, 17], 
    c171 -> Subscript[c, 171]} // TraditionalForm

but when I copy and paste it in Word, "" are present enter image description here on the other hand if I remove "" in nn Mathematica doesn't print them. Any idea?

Wisdom
  • 1,258
  • 7
  • 13
  • 2
    I deleted my answer. You can't add explicit ( ) using strings. Ofcourse they will show as strings in word. Better to not do this and leave it as is. That is the right way. But if insist on having () around each term, may be there is a way that someone could find for you. – Nasser Apr 25 '22 at 06:42
  • Thanks but I have a lot long formulas which I apply such a procedure to them, so finding a efficient method will help me so much. – Wisdom Apr 25 '22 at 07:02
  • If you right click and choose Copy As > LaTeX, it seems to give an accurate LaTeX expression, without ". Can you then put LaTeX in a Word equation? Not sure how Word works, so not sure how feasible this is. – thorimur Apr 25 '22 at 07:04
  • @thorimur I tried this, but Word has problem with showing LaTex. At the moment I copy and paste the code which includes "" and then remove them using "Find and Replace' in Word. – Wisdom Apr 25 '22 at 07:06
  • Hmm, what version of word are you using? It seems that Word does support LaTeX input in the equation editor, you might just have to fiddle with it a bit. If you get LaTeX working in Word, try using TeXForm instead of the whole {TraditionalForm then Copy As > ...} routine; it'll be quicker, especially if combined with CopyToClipboard which will copy the output directly upon evaluating! :) – thorimur Apr 25 '22 at 07:15
  • The issue is not how to copy it from Mathematica to Word. There are many ways. Either using Latex or otherwise. But the question wants an explicit "( )" added into the expression around each term. Converting to Latex first does not do that. So I am not sure why Latex comes into play here. – Nasser Apr 25 '22 at 07:35
  • Yes, @Nasser is right, the problem has nothing to do with copy procedure but print () as a string – Wisdom Apr 25 '22 at 07:42
  • @Nasser OP's solution already successfully adds () around each term; the problem is that with the way OP is copying it, "'s are also added around each ( and ). For whatever reason, converting to LaTeX successfully excludes these pesky "'s, and so if OP could use LaTeX in word, the problem would be solved. – thorimur Apr 25 '22 at 08:00
  • (For the record, here is the (Copy As > LaTeX) version of the output OP's code produces on my machine: \frac{b_{17} \sqrt{\rho _e\left(r_e\right)} \rho _p\left(r_e\right){}^{3/2}-2 a_{17} \rho _p\left(r_e\right)}{2 \left(a_{17}-b_{17} \sqrt{\rho _e\left(r_e\right)} \sqrt{\rho _p\left(r_e\right)}+c_{171} \rho _e\left(r_e\right) \rho _p\left(r_e\right)\right){}^2}. As you can see, no "! I'm on v13.0.0.0.) – thorimur Apr 25 '22 at 08:02
  • @thorimur You are right. I did not try TeXForm on the edited version with the strings added. So Yes, this could work. My version of word does not support inserting raw Latex into word, so I can't try it. But your method would work better if there is a way to insert raw latex into the equation editor of Word. I am sure there is a way. But my word does not do it. (old version) – Nasser Apr 25 '22 at 08:04
  • Ah I see. That is a bummer! Hmm. One way, similar to the answer below but providing better quality, is to use a website such as this one to generate an SVG of the LaTeX output (be sure to surround with $ first of course!). An SVG, being a vector image, will be infinitely scalable and thus "infinitely" high quality, and won't run into resolution issues. (Not sure if your version of word supports SVGs, but they're pretty standard—a high-quality PNG is also available from that website if needed, though—click on the Download menu, then PNG (HQ).) Good luck! :) – thorimur Apr 25 '22 at 08:16

2 Answers2

5
ClearAll["Global`*"]

Format[re] := Subscript[r, e]
Format[a17] := Subscript[a, 17]
Format[b17] := Subscript[b, 17]
Format[c171] := Subscript[c, 171]
Format[denE] := Subscript[ρ, e]
Format[denP] := Subscript[ρ, p]

(ff = (-2 a17 denP[re] + 
      b17 Sqrt[
        denE[re]] denP[re]^(3/2))/(2 (a17 - 
         b17 Sqrt[denE[re]] Sqrt[denP[re]] + 
         c171 denE[re] denP[re])^2)) // TraditionalForm

enter image description here

Edit | Copy As | MathML then paste into Word

This would be simplified a little if you used indexed variables such as a[17], b[17], c[171], r[e] instead of a17, b17, c171, re. Then you could use

ClearAll["Global`*"]

(Format[#[n_]] := Subscript[#, n]) & /@ {a, b, c, r}; Format[denE] := Subscript[ρ, e] Format[denP] := Subscript[ρ, p]

(ff = (-2 a[17] denP[r[e]] + b[17] Sqrt[ denE[r[e]]] denP[r[e]]^(3/2))/(2 (a[17] - b[17] Sqrt[denE[r[e]]] Sqrt[denP[r[e]]] + c[171] denE[r[e]] denP[r[e]])^2)) // TraditionalForm

enter image description here

Bob Hanlon
  • 157,611
  • 7
  • 77
  • 198
4

Update:

From https://superuser.com/questions/340650/type-math-formulas-in-microsoft-word-the-latex-way it says

If you are running Office 365 version 1707 or later, the Equation Editor allows you to convert LaTex into the native format. See the first part of this answer.

I have old word (2010) so I can't test the above. My word equation editor will not accept inserting raw latex in it.

enter image description here

But if you have newer version of word, you can convert the modified expression, after you adding those explicit "()" around the terms, to latex using TeXForm (which will keep the parentheses (surprise)), as mentioned in comments above by thorimur.

Then copy the raw latex into Word equation editor. It should work.

Alternative is to use copy as bitmap:

enter image description here

To do that, select the output from Mathematica using the mouse, then copy/paste as bitmap into word.

enter image description here

I would not recommend doing all of this myself. Output does not look best. Using bitmaps for math is not good. Using Word itself for math is not good.

Best solution is to throw away Word. Generate pure Latex from Mathematica, and use a Latex compiler to generate the PDF. That will produce best output. A professional looking document. But if insist on using Word, the above should do it.

OLD answer

Just remove the "" from the code you used, then it works. Where did the "" stuff come from? If you add "" then it will show up in word ofcourse.

ff = (-2 a17 denP[re] + 
     b17 Sqrt[
       denE[re]] denP[re]^(3/2))/(2 (a17 - 
        b17 Sqrt[denE[re]] Sqrt[denP[re]] + c171 denE[re] denP[re])^2);
nn = ff /. {denE[
      re] -> \!\(\*SubscriptBox[\(\[Rho]\), \(e\)]\) \
(\!\(\*SubscriptBox[\(r\), \(e\)]\)), 
    denP[re] -> \!\(\*SubscriptBox[\(\[Rho]\), \(p\)]\) \
(\!\(\*SubscriptBox[\(r\), \(e\)]\)), a17 -> Subscript[a, 17], 
    b17 -> Subscript[b, 17], c171 -> Subscript[c, 171]} // 
  TraditionalForm

Gives

enter image description here

The copied this to Word, and it works:

enter image description here

on the other hand if I remove "" in nn Mathematica doesn't print them.

I am not seeing this. Does not print what? The question is, where did the "" come? Did you add then yourself?

Nasser
  • 143,286
  • 11
  • 154
  • 359
  • Thanks but this is not true! I want to get \rho_e (r_e) not what you have gotten in Word, Look at the result carefully to get my mean – Wisdom Apr 25 '22 at 06:36
  • @Wisdom I see., You want to have explicit ( ) added? Well., adding them using strings is not the right way to do this. But now I understand. Will delete this then. – Nasser Apr 25 '22 at 06:39