Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

There are many guides to convert .doc to .tex with text editor softwares (mostly under Windows); and this popularity has made it hard to search for command-based converting methods.

I am looking for a command line to convert MS Word to LaTeX. This will be part of a PHP script running on Linux server. I mean a shell command to be executed by PHP.

share|improve this question
3  
Pandoc is a good place to start, although I'm not sure how well it understands .doc. You should be prepared; conversion between the two is notoriously patchy (whatever the tool), so you're going to have big problems beyond the most basic of documents. –  qubyte Feb 27 '12 at 18:25
4  
You may also try converting doc to html first, maybe clean it up a bit (Tidy) and then running one of the html2latex convertors. There are plenty of those. Hope it helps. –  Ondrej Feb 27 '12 at 18:37
    
@MarkS.Everitt I understand what you mean. I hope to find a way to ignore messy markups instead of converting them. Converting a very basic structure is better than a bunch of messy markups. –  All Feb 27 '12 at 18:39
    
@Ondrej I tried this before (actually my first choice), but conversion of doc to html is not very clean. Do you know a good tool to do so? –  All Feb 27 '12 at 18:42
    
As the maintainer of rtf2latex2e I suggest that the best way is to use Word to save the document as .rtf and use rtf2latex2e (rtf2latex2e.sf.net). All the MathType equations get translated to more-or-less normal LaTeX. All the figures get extracted and the proper \includegraphics{file.pdf} mark-up added. Of course, if you don't have equations then all the other suggestions are just fine. –  Scott Prahl Mar 25 '12 at 6:38
show 1 more comment

6 Answers

up vote 20 down vote accepted

Antiword is going to do reasonable good job converting .doc to .tex files . It makes every effort to preserve not only the content but formating as well. It is well suited for batch processing that you want to do.

Edit: Several people asked me privately about LaTeX switch in Antiword and the latest version of Antiword. The latest version is indeed 0.37. As of LaTeX output I think I mixed up things a bit. I used Antiword for formated ASCII output. I think it is capable of PostScript output but not of LaTeX output. As Jon observed you can use pandoc to convert well formated ASCII into LaTeX. However, wvWare (wv and wv2) are capable of outputting LaTeX. A bit of warning. wvWare is depreciated in favor of AbiWord but can be used for batch processing (I have no clue if AbiWord can be used from the command line). It is still a bit younger program (dormant since 2006) than Antiword(dormant since 2004).

Finally there is a tool called catdoc which is great for batch processing but will not preserve format (great for extracting content though and supports MS Excel format).

share|improve this answer
    
I second that: antiword myfile.doc > myfile.txt ; pandoc -o myfile.tex myfile.txt works surprisingly well. –  jon Feb 27 '12 at 19:49
    
@jon Antiword can convert directly into .tex format. Please refer to man pages. –  Predrag Punosevac Feb 27 '12 at 20:03
    
Maybe in newer versions, but the version that I have (0.37) makes no mention of (La)TeX. –  jon Feb 27 '12 at 20:12
    
Thanks for sharing this useful tool; but I expect to save basic formatting. I do not expect to convert all MS Word markups (and I do not need), but I hope to keep basic ones such as Bold and Italic –  All Feb 27 '12 at 20:15
2  
Is there a newer version than 0.37? That's the only version I can find, and I don't see any options for (La)TeX either. –  LianTze Lim Feb 28 '12 at 9:41
show 3 more comments

This answer is specific to OS X...

Command line utility textutil

There's a nice command line utility called textutil included in OS X that will convert among common document formats:

Word docx to txt

$ textutil -convert txt worddoc.docx

txt to Word docx

$ textutil -convert docx mytextdoc.txt

txt to Word, using Times New Roman 12pt

$ textutil -convert docx -font "Times New Roman" -fontsize 12 blah.txt

Also works with html, rtf, doc, odt, and others...

word2latex and latex2word by using textutil with Pandoc

If you use Pandoc in combination with textutil you have can have a decent Word-to-LaTeX and LaTeX-to-Word roundtrip. For docx support you need the latest version of Pandoc (1.9+).

word2latex

$ textutil -convert html worddoc.docx -stdout | pandoc -s -f html -t latex -o latexdoc.tex

latex2word

$ pandoc -t docx -f latex -o backtoword.docx latexdoc.tex
share|improve this answer
    
So much pity that I'm on Linux ;) –  All Feb 27 '12 at 21:47
1  
I just saw this amazing post! I would like to mention that pandoc runs on Mac, Linux, and Windows! see johnmacfarlane.net/pandoc/installing.html and tex.stackexchange.com/questions/44236/… –  macmadness86 Aug 6 '12 at 14:21
1  
@macmadness86 -- I think the point is that textutil is for OS X. Pandoc is indeed cross-platform and very useful. –  jon Aug 10 '12 at 1:03
add comment

A lot depends on how complicated the Word document formatting is. I have had very good success with rtf2latex2e, which converts RTF formatted text to LaTeX. It has various levels of matching the RTF formatting. I have mainly used its "minimal LaTeX markup mode", which is ideal for a document that will be subsequently hand-edited (which I understand is not the same conditions as you require.)

share|improve this answer
add comment

A multiplatform solution is in the works. Rob Oakes is implementing this feature in LyX. Once implemented, I think that using it on the commandline would be straightforward. However, (1) I'm not sure you want to install a somewhat large application for just this conversion and (2) the work is in alpha stages right now. Rob is looking for testers. He's mainly looking for .doc files to test on. He just gave an update on the features that his tool supports: http://marc.info/?l=lyx-users&m=133070969217214&w=2 Here is a list that of features already implemented (copied from the url):

  1. Translating Word paragraph and character styles to LyX paragraph and character styles. In the case of character styles that aren't defined, it will write entries for them into the local layout (including basic LaTeX commands).
  2. Importing Word tables, including those with merged rows or columns. It will also do its best with the table borders.
  3. Enumerated and itemized lists.
  4. Importing images from the Word document. (It skips over embedded objects, such as charts from Excel.)
  5. The use of custom templates, which allows you to fine tune importing your documents. I've created templates for article.cls and book.cls. I'll also probably create one for memoir.cls as well.
share|improve this answer
add comment

I was dismayed that the Linux user above expressed envy of textutil on Mac OS X. Although I am on Mac OS X, I don't use textutil. As far as I know, it is not capable of reading any formatting information from files anyway. I believe it just extracts text. There are many utilities for that purpose. For instance antiword does it for pre-docx MSWord files and is cross platform. catdoc, mentioned above, works better on some files. I'm sure I'm forgetting a few good ones ...

Instead of textutil, I use a shell script I inherited (I don't know who posted it originally or where) to decode .docx files. Here it is:

#!/bin/sh
unzip -p "$1" word/document.xml \
| sed -e 's/<\/w:p>/\
/g' -e 's/<[^>]*>//g'

I named it docx, put it in /usr/local/bin and said sudo chmod 700 docx to make it executable. Then I say

docx mystupidlittlewordfile.docx | less

to read the contents of any .docx file quickly and conveniently.

All it does is three things one per line (not counting the first line that tells which shell to use). First, it unzips the xml document containing text. This xml document is currently usually word/document.xml in the .docx file which, if you have not guessed, is a structured .zip file. If this script does not work, the first thing I do is to say

unzip -l mystupidwordfile.docx

so I can see where the text is. You should look inside word/document.xml anyway, to understand the next two lines of the shell script above. The second line pipes the output to a sed script that replaces MSWord's symbol for newline with an actual newline. The third line pipes this output to another sed script that sends everything to stdout except whatever is between angle brackets.

There are actually a number of other useful items between angle brackets, such as table layouts, that would be easy to add with intermediate sed scripts. You could replace many of these useful items, for instance, with markdown equivalents. I just haven't gotten around to it. The main reason I use this script is so that I can see the content of an average .docx file on a network volume to see if I can understand its content without having to run MSWord to read it. Nine out of ten times, this is all I need to read and understand .docx files. Formatting in average .docx files is usually decorative rather than meaningful.

Presumably, Microsoft will change these angle bracket directives with successive versions of MSWord to make it harder to use any non-Microsoft tools on them. I have heard that Microsoft's original attempt at an open format was essentially a MSWord document surrounded by angle brackets.

Nevertheless, as long as public pressure remains for them to use XML, at least some of the directives can be read and modified from version to version. My view is that the end user should be able to modify the tool when it fails because intentional incompatibility has been introduced by the monopolist. I'm not sure how to do that. My solution requires that the user know sed and regular expressions and be less lazy than, evidently, I am. This will only fit a small subset of people who would like, perhaps for ethical reasons, to avoid owning a copy of MSWord.

share|improve this answer
1  
I like your comment a lot! As you observed docx is a glorified xml file + few style files which are zipped. A simple sed script is perfectly capable of dealing with it reasonable well. For people who look more serious tool I recommend docx2txt (sourceforge.net/projects/docx2txt ) written in Perl. –  Predrag Punosevac Aug 13 '12 at 22:00
add comment

I have had a lot of success with an XSL stylesheet called WordML2Latex. It does an excellent of converting formatting to sensible equivalents. Much more than your one-liner. You first save your Word doc as word XML2003, then apply the stylesheet to it.

Being XSL it is cross platform and you can use it from the command line :-). It is also open source ad modifiable.

It's on CTAN http://www.ctan.org/tex-archive/support/WordML2LaTeX

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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