# How to run LaTeX from Word?

I am writing a journal article using LaTeX. Currently, the review process is very difficult as my professor has to comment on the pdf.

Previously, I have written a journal paper using Word. Though the whole process using Word is horrendous, Word does have very convenient tools for reviewing/editing the journal article. This is especially true when there are more than three co-authors.

Hence I was trying to come up with a method by which I can write the whole article in Word but in LaTeX syntax. My professor can understand most of the LaTeX syntax, but he doesn't want to use and maintain LaTeX on his personal computer. It would be great if anyone could suggest some ideas.

-
Do you want to use Word as the LaTeX editor? –  kiss my armpit Jul 2 '12 at 16:52
Won't work. To use Word's review features, you need to use it's native .docx or .doc formats. That's no good for LaTeX, which needs .tex format: plain text. –  Joseph Wright Jul 2 '12 at 17:09
Take a look at github.com/mikemaccana/python-docx which is a python module capable of extracting text from .docx archives. You can use python to extrac the contents to a temporary .tex file and even drive the latex compilation (and later cleanup) from python. –  JLDiaz Jul 2 '12 at 17:19
Mendeley (mendeley.com) allows groups of collaborators to insert comments in pdfs. So consider writing in TeX, uploading the pdf and asking your professor to comment there. –  Ethan Bolker Jul 2 '12 at 18:47
@ShashankSawant Ok, I see, so the workflow suggested by JLDiaz is more what you are looking for. A completely different workflow could involve online compilation of the .tex document and normal tex comments or maybe the fixme package (supports multiple authors). Or, third workflow, use a professional version control system like git, Mercurial or Subversion. –  matth Jul 4 '12 at 9:45

## 2 Answers

Elaborating my answer in a comment to the question, this is what I got so far.

You need to install Python (I installed python2.7), and lxml and PIL. The easiest way I've found to install the later in Windows is going to http://www.lfd.uci.edu/~gohlke/pythonlibs/, and download lxml-2.3.4.win32-py2.7.‌exe and PIL-1.1.7.win32-py2.7.‌exe (note that you have to choose the appropiate files for your python version). Running those exe, the appropiate libraries and bindings are installed.

Then you can download https://github.com/mikemaccana/python-docx. I didn't try to properly install this one. I only uncompressed it in a folder, open a cmd shell, navigate to that folder and run the provided examples (example-extracttext.py and example-makedocument.py) which worked. My setup was fine.

Then I adapted the code of example-extracttext to our needs, and wrote the following script, which I named run.py:

#!/usr/bin/env python2.7
'''
This file opens a docx (Office 2007) file and dumps the text. Then it uses pdflatex to compile it.
'''
from docx import *
import os
import sys
if __name__ == '__main__':
try:
wordfile = sys.argv[1]
latexfile = sys.argv[1].replace('docx', 'tex')
logfile = sys.argv[1].replace('docx', 'log')
document = opendocx(wordfile)
newfile = open(latexfile,'w')
except:
print('Please supply an input file. For example:')
print('''  run.py 'MyDocument.docx' ''')
exit()
# Fetch all the text out of the document we just created
paratextlist = getdocumenttext(document)

# Make explicit unicode version
newparatextlist = []
for paratext in paratextlist:
newparatextlist.append(paratext.encode("utf-8"))

## Print our documnts test with two newlines under each paragraph
newfile.write('\n\n'.join(newparatextlist))
newfile.close()

## Now use pdflatex to compile the result
os.system("pdflatex %s" % latexfile)
while "Rerun" in open(logfile).read():
os.system("pdflatex %s" % latexfile)

To test it, I wrote the following Word document (note that I used Word styles to mark the section titles, and used a table to insert the code of a tikz picture, and even inserted an image showing the result for that figure, obviously not in the first pass, but later). Note also which I used a Word bulleted list to help marking the itemized list. All this Word styles will be dropped when converting to plain text, but allows us to make the display more clear.

I saved this document with the name Prueba.docx in the same folder than the script run.py, and ran the script on the word file:

C:\Users\jldiaz\Downloads\mikemaccana-python-docx-647ee97>python run.py Prueba.docx

After two compilations (the script takes care of compiling again if references are not solved), the resulting pdf is the following:

(at this point I used IrfanView to screen-capture the tikz picture and paste it into the word document)

Note: If you use SumatraPDF as pdf reader, you don't need to close the pdf document before compiling again. SumatraPDF updates the view when the pdf changes.

UPDATE:

Tested also with math, comments and revision marks. All works as expected (comments are ignored, revision marks are ignored, latest version of the text is what goes to the final .tex file).

However, caution about carriage returns in the Word file. "Enter" key in Word inserts a end-of-paragraph mark, which is translated by python into a blank line (which is a \par to tex, so everything is fine). However in some environments, we don't want those blank lines (for example, inside an equation environment, or other places where TeX doesn't expect a \par). We can avoid this by using Shift+Enter in Word, which inserts an end-of-line instead of an end-of-par. Those end-of-lines are translated by python to spaces.

My experiments with comments, revisions and math:

and the result after the script:

-
This is either great or completely weird. I can't decide, but however, it is incredible. –  Keks Dose Jul 4 '12 at 9:13
Funny, indeed. (+1) –  chl Jul 4 '12 at 20:18
I wonder if this could run either knitr or sweave to embed R computations within Word. –  R. Schumacher Jul 5 '12 at 2:06
It would be nice to have the script automatically know that style header 1 should be surrounded by the section command like: \section{#header1#}. This way, you could print a word document or share it with users who have no idea what tex is, and also have the option compile it for the quality version. Same thing for the lists. –  macmadness86 Aug 6 '12 at 13:55
I will not use it, but I like how you solved the problem. +1 –  rpax May 6 at 14:25

Given your remark:

I want the same master document. It irks my prof that all the Sticky Notes and Highlights that he makes in the review disappear after I compile a new document. Hence I wan't one place (preferably a .doc file) where all authors makes highlights and comments, I extract text out of it and then compile it.

Would it be an option for your professor/collaborators to use SVN?

I've recently been using LaTeX + SVN for collaboration and I think it is great combo (and much better than using Word's collaboration features, although I am a heavy Word user as well)

There would be no strict need for your collaborators to install LaTex, they would just have to update their svn working copy (using a GUI like Tortoise), open the .tex files in a text editor like notepad, and write their remarks either as comments (using %) or using a custom command (like \profremark{}), and commit the changed files.

This would keep all content and remarks in the same master document (or split over multiple ones if you want), and you'd easily be able to toggle the appearance of the remarks if you use a custom command.

You could aid your collaborators by

• keeping your latex source files as readable as possible in a simple text editor (e.g. through line breaks)
• writing some custom remark commands which you insert in the preamble (\collaborator1_comment{}, \collaborator2_comment{}) and that they can/have to use. You can give them some nice and distinctive appearance in the compiled pdf (like appear in the side margin, or in a specific color, etc.)
• using latexdiff to generate a clear PDF highlighting the changes (see my answer to my own question on latexdiff + svn not working with multiple files (flatten)) and commit the pdf to the repository as well. The collaborator comments could be shown in the PDF if you used a custom command.

For your final version, you just hide the appearance of the custom remark commands.

-
+1 for showing some version control to the rescue (I don't fully like SVN for collaboration, but SVN is still better than anything ad-hoc). However, I doubt that if LaTeX poses a problem, proper version control will work. –  Egon Sep 4 '12 at 16:21