7
$\begingroup$

If I generate a hundred plots in Mathematica, is there an easy way to insert them into a Word file and add a caption?

Currently I am outputting individual images as .emf's with a text file of captions and then using MATLAB to read them in and use their routines for inserting graphics into Word, but since MATLAB has this capability, it seems like Mathematica should have something similar.

My coworker who gave me the MATLAB code snippet to insert the graphics into Word is giving me a bunch of grief... like why don't I just generate the plots in MATLAB.

$\endgroup$
  • 5
    $\begingroup$ TIP: don't use Word. Use Latex instead. You'll thank me later. $\endgroup$ – AccidentalFourierTransform Jul 11 '18 at 22:23
  • $\begingroup$ If you don't find a better solution, you could use Matlab commands through mathematica with matlink package: matlink.org Probably faster than what you are doing $\endgroup$ – Fraccalo Jul 11 '18 at 22:26
  • 2
    $\begingroup$ HAHAHA! Yeah, I wish I could use LaTeX for work (I'm using it for personal publishing!), but... you know... stuck with Word in that environment! I'll look into matlink. Thanks! $\endgroup$ – mh2000 Jul 11 '18 at 22:37
  • 1
    $\begingroup$ Can you post the matlab code? On Mac you can probably automate this using Applescript. In the past I have created a CellEvaluationFunction that writes to word documents from Mathematica using Applescript. If you are on windows I'd like to see what Matlab are doing for any clues as t how this might work in Mathematica $\endgroup$ – Mike Honeychurch Jul 12 '18 at 0:00
  • 1
    $\begingroup$ Also: if you export to EMF, the quality of the figures will be reduced. If you export to PDF and insert to Word (possible on OS X), again you risk reducing the quality. Word is just a crappy format. But I do know very well how and why one ends up being forced to use it for writing papers regardless. $\endgroup$ – Szabolcs Jul 12 '18 at 11:12
4
$\begingroup$

Since I never got a real answer to this question, I'll post the answer that I found and is workig flawlessly for me! Revisiting what matlab was doing passing commands through ActiveX, I realized it was very similar to running VBA scripts in Word, so I wrote a couple functions that save my plots to temp files and writes a single VBA script for reading them in and adding captions, so I can dump a pile of images into a Word file, add the appropriate captions etc. and... presto! An automatic way to insert a bunch of images into Word! I'm finding 360 dpi .png files look perfect! Best!

(* Read this into a new notebook using Get["X:\\mathematica\\WordInsert.m"] *)
BeginWordInsert[path_] := Module[{},
   mmamacro = OpenWrite[path <> "\\mma_insert.bas"];
   WriteString[mmamacro,
    "Attribute VB_Name = \"NewMacros\"\n\n", "Sub mma_insert()\n",
    "Attribute Macro1.VB_ProcData.VB_Invoke_Func = \
\"Normal.NewMacros.mmaInsert\"\n",
    "'\n", "' ", "mmaInsert1 Macro\n", "'\n", "'\n"];
   mmaInsertN = 1;
   pathMI = path;];
(* *)
EndWordInsert[] :=
  Module[{}, WriteString[mmamacro,"End Sub"]; Close[mmamacro]];
(* *)
WordInsertP[graphic_, title_:"  Caption", IMres_:300,
   fileType_: "png"] := Module[{},
   fileN =
    pathMI <> "\\mmaEP" <> ToString[mmaInsertN] <> "." <> fileType;
   Export[fileN, graphic, ImageResolution -> IMres];
   WriteString[mmamacro, "Selection.TypeParagraph\n",
    If[mmaInsertN > 1,
     "Selection.TypeParagraph\nSelection.TypeParagraph\n", ""],
    "Selection.InlineShapes.AddPicture FileName:= _\n",
    "\"", fileN, "\", ", "LinkToFile:=False, _\n",
    "SaveWithDocument:=True\n",
    "Selection.ParagraphFormat.Alignment=wdAlignParagraphCenter\n",
"Selection.Start = Selection.Start-1\n",
    "Selection.InsertCaption Label:=\"Figure\" , _\n",
    "Title:=\"  ", title,
    "\", Position:=wdCaptionPositionBelow,ExcludeLabel:=0\n",
    "Selection.ParagraphFormat.Alignment=wdAlignParagraphCenter\n",
    "'\n"];
   mmaInsertN = mmaInsertN + 1;
   ];
(* *)
WordInsertT[graphic_, title_: "  Caption", IMres_: 300,
   fileType_: "png"] := Module[{},
   fileN =
    pathMI <> "\\mmaEP" <> ToString[mmaInsertN] <> "." <> fileType;
   Export[fileN, graphic, ImageResolution -> IMres];
   WriteString[mmamacro, "Selection.TypeParagraph\n",
    If[mmaInsertN > 1,
     "Selection.TypeParagraph\nSelection.TypeParagraph\n", ""],
    "Selection.InlineShapes.AddPicture FileName:= _\n",
    "\"", fileN, "\", ", "LinkToFile:=False, _\n",
    "SaveWithDocument:=True\n",
    "Selection.ParagraphFormat.Alignment=wdAlignParagraphCenter\n",
"Selection.Start = Selection.Start-1\n",
    "Selection.InsertCaption Label:=\"Table\" , _\n",
    "Title:=\"  ", title,
    "\", Position:=wdCaptionPositionAbove,ExcludeLabel:=0\n",
    "Selection.ParagraphFormat.Alignment=wdAlignParagraphCenter\n",
    "'\n"];
   mmaInsertN = mmaInsertN + 1;
   ];
Print["\n**You've loaded the WordInsert Package**\n\nTo start your \
session, run \"BeginWordInsert[path]\", where the path string points \
to your working directory.\n\nNote escape characters need to be used \
in the path definition \n(IE. \"X:\\\\mathematica\").\n\nWrapers \
WordInsertP and WordInsertT are for inserting (P)lots and (T)ables \
respectively.\n The insert functions are of the form \
WordInsert[graphic,title,ext], \nwhere graphic is any Mathematica \
graphic object and the title is the string used in Word for the \
Figure or Table caption (needs to be in quotes).\n ext is the file \
extension defining the type of graphic output to use (IE. \"png\").\n\
\nThis package writes a \".bas\" file that can be inported into MS \
Word.\nAll Plots and Tables will be inserted with captions when \
\"mmainsert\" macro is run in Word.\n\nTo end your WordInsert session and finalize the .bas file creation,\n you \
must execute the \"EndWordInsert[]\" function.\n\n
A typical run sequence:\n\nBeginWordInsert[\"X:\\mathematica\"]\nWordInsertP[g1,title1]\nWordInsertP[g2,title2]
WordInsertT[g3,title3]\nEndWordInsert[]\n\n"]
| improve this answer | |
$\endgroup$
1
$\begingroup$

If you start off with a template document containing EMFs you can rename the .docx to .zip, unzip it, replace the EMFs in the media folder. Zip it back up, rename back to .docx and now your document contains your images. This is simple to automate.

Note, the template images need to be all different. If you copy and paste a page 100 times, when the document is saved there will just be one image in the media folder (referenced 100 times) because Word recognises identical images during the Save process.

You can use NETLink interop functions to build a template, or make one in Word - a bit tedious for 100 images but the template can be used over and over again.

| improve this answer | |
$\endgroup$
  • $\begingroup$ Thanks! That doesn't seem like a great workaround for me since there will be different numbers of graphics each time (sometimes 62 and sometimes 364...). I look into the LaTeX/.pdf to word options because I can write a little awk script to automatically build a LaTeX file and maybe go from there. $\endgroup$ – mh2000 Jul 13 '18 at 17:44
  • $\begingroup$ I added my "package" that does this below. Put into a text file and then "Get" in mma and the instructions and example will print to screen. Then just follow directions and use! Works for Figures and Table. 360dpi png look the cleanest to me. $\endgroup$ – mh2000 May 29 '19 at 5:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.