14

How can I transfer code from a Mathematica notebook to a Microsoft Word document in a way that all formatting I see in the notebook is preserved?

I want to preserve the font, the indentation and the syntax colouring.

I am using Mathematica on Windows.

Szabolcs
  • 234,956
  • 30
  • 623
  • 1,263
henry
  • 2,510
  • 14
  • 28
  • 2
    You might consider http://library.wolfram.com/infocenter/Demos/5698/ written by Mike Honeychurch. I don't know if the described process still works (as it was written in 2005). – JimB Jan 29 '17 at 18:47
  • 2
    Depending on how much editing you want to do, there's also "Save as...RTF". – JimB Jan 29 '17 at 19:07
  • @Jim Baldwin I tried that, but ist not working for me... – henry Jan 30 '17 at 07:21
  • You can save the notebook as web-page and further open it by Word. However, modern versions of MS Word has a lot of security restrictions preventing the evaluation of an active content of web-pages and other files. So, you may be need to switch off some of the security settings to open this web-page for edition in Word. But it is fully possible. The rtf-version of the notebook appears too ugly.. – Rom38 Jan 30 '17 at 07:41
  • 5
    You could always use $\LaTeX$ and forget about word altogether :) – Nasser Jan 30 '17 at 07:44
  • http://mathematica.stackexchange.com/a/102747/363 – Chris Degnen Jan 30 '17 at 09:53
  • I think this is in need of clarifications. Do you want to convert a whole notebook, with text and images, or do you just want to include some code in a Word document? Or do you perhaps want equations? What's wrong with copying and pasting code, then formatting in a fixed width font? Is it syntax highlighting that you are after? What does MATLAB do exactly, for those of us who don't use this functionality? – Szabolcs Jan 30 '17 at 14:36
  • When I first saw this question, I considered if it should be closed until it's better explained. But then I thought that it'll probably get cleared up after a few comments. That didn't happen, in fact now it's even less clear what is being asked precisely, and the one answer doesn't seem to fit. I am now voting to close as unclear. I will retract the vote once the question has been made much more clear. – Szabolcs Jan 30 '17 at 14:39
  • @Szabolcs I made an edit. – henry Jan 30 '17 at 14:40
  • Try this: select the cell's bracket, then choose Copy As... -> EMF (or WMF, I don't remember which one is available). Then paste into Word. This may work on Windows. On OS X the same is possible with PDF. This is better than a screenshot in that it produces vector graphics. – Szabolcs Jan 30 '17 at 14:53
  • @Szabolcs does not work for me. I can only choose between: Plain Text, Cell Expression or Notebook expression – henry Jan 30 '17 at 15:01
  • DoHe: @Szabolcs is giving you good advice. When responding please give something more explicit than "does not work for me." In other words, please state "what" is not working. Are there errors? Or how is it not what you want? – JimB Jan 30 '17 at 15:18
  • @Szabolcs I cannot see your Options ...EMF (or WMF). As stated above, I only have the Options: Plain Text, Cell Expression or Notebook Expression --> therefore I cannot Export it – henry Jan 30 '17 at 15:44
  • Look in the Edit menu if it is not present in the context menu. – Szabolcs Jan 30 '17 at 16:12
  • I rephrased your question. Please check the edit and make sure that this is what you wanted to ask. – Szabolcs Jan 30 '17 at 17:31
  • @Szabolcs Yes, this is eactly my question! Thanks a lot. Okay, I will try the edit menu, – henry Jan 30 '17 at 19:00
  • OK, let us know if it worked. I cannot try this on Windows, only on a Mac. There it works well with PDF. There's no EMF-copy on Mac and there's no PDF-copy on Windows, so the solution is OS-specific. – Szabolcs Jan 30 '17 at 19:27
  • @Szabolcs It works ! See my answere. :) Thanks a lot ! – henry Jan 30 '17 at 20:49

3 Answers3

12

The user Szabolcs gave the answer.

Here are the steps:

  1. Open your Mathematica Notebook
  2. Ctrl+A, if you want to selct all your cells or simply select your desired cells
  3. Go to: Edit -> Copy as -> Metafile
  4. Open Word: Ctrl+V (paste)

:)

henry
  • 2,510
  • 14
  • 28
6

You can copy-as-MathML from Mathematica directly into Word. Have fun! Edit

To address your comment. This is a fragment of my Mathematica notebook:

enter image description here

These are the corresponding formulas inserted into the Formel Editor of Word 2013

enter image description here

It works for me.

Alexei Boulbitch
  • 39,397
  • 2
  • 47
  • 96
1

enter image description hereHere are the steps:

  • Install MathType Editor
  • Open your Mathematica Notebook
  • ctrl+c your formula and paste MathtypeEditor
  • ctrl+c your formula in MathtypeEditor
  • Open Word: ctrl+v (paste)
  • You can also arrange your formula in Word.
  • 1
    Does it address syntax highlighting, indentation and fonts? – Kuba Mar 26 '18 at 20:01
  • Yes, MathType Editor correspond to MS Office. You can easily try it. – Sinan Emre Çankaya Mar 26 '18 at 21:32
  • 1
    I know what it is about. But I don't see how syntax highlighting information is supposed to be preserved here. Can you show an example, with let's say Module[{x}, x] and x highlighted? – Kuba Mar 26 '18 at 21:58
  • 1
    As you can see relative font sizes are not preserved, neither indentation. There is not syntax highlighting so can't judge by the image. – Kuba Mar 27 '18 at 07:15