# How can I convert a TeX file to Microsoft Word or LibreOffice format?

I’m trying to convert a report from LaTeX to OpenDocument (.odt) or Office Open XML (.docx) formats. But the programs give errors and stop, so in the end neither formats are written.

First I use Pandoc:

pandoc -f latex -t docx -o file.docx file.tex


But then it outputs the message:

Error at "file.tex" (line 124, column 20):
expecting \end{figure}
\textsf{\large{}\includegraphics{pasted1}}{\large\par}


I have revised the code and there is the corresponding \par, so I tried to erase these tags, but it isn’t compiled. It says

unexpected {
\par\end{centering}


Again I revised it, but the centerings are written as \begin{centering} and \par\end{centering} and all are paired, so I can't locate the mismatch

• Option 2: mk4ht Used the following command

mk4ht oolatex file.tex

It starts compiling, shows a list of libraries and files, but it stops and outputs:

?
! Emergency stop.
<inserted text>
\par
l.25 \babel@aux{spanish}{}

Output written on file.dvi (53 pages, 15708 bytes).
Transcript written on file.log.
--- error --- failed to execute command


And the dvi file can't be opened.

At starting, Pandoc was saying that the file isn't in UTF-8, and manually change the encoding. The TeX file is a large one, so perhaps isn’t for loading here, and I don't know what parts could be relevant to post.

So how can it be tweaked to get something converted to these formats?

MWE (this file is a portion of the main TeX file)

\documentclass[12pt,spanish]{article}
\usepackage[sc]{mathpazo}
\usepackage{helvet}
\usepackage{courier}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}
\usepackage[letterpaper]{geometry}
\geometry{verbose,tmargin=1.5cm,bmargin=1.5cm,lmargin=1.5cm,rmargin=1.5cm}
\setcounter{tocdepth}{2}
\usepackage{color}
\usepackage{babel}

\usepackage{float}
\usepackage{graphicx}
\usepackage{setspace}
\usepackage[unicode=true,
bookmarks=true,bookmarksnumbered=true,bookmarksopen=true,bookmarksopenlevel=2,
{hyperref}
\hypersetup{pdftitle={Title},
pdfauthor={Some_name},
pdfsubject={opamp, feedback},
pdfkeywords={opamp, feedback},
linkcolor=black, citecolor=black, urlcolor=blue, filecolor=blue, pdfpagelayout=OneColumn, pdfnewwindow=true, pdfstartview=XYZ, plainpages=false}

\makeatletter
\pagenumbering{roman}
\let\myTOC\tableofcontents
\renewcommand\tableofcontents{%
\pdfbookmark[1]{\contentsname}{}
\myTOC
\cleardoublepage
\pagenumbering{arabic} }

\makeatother

\begin{document}
\listoffigures

\title{\textsf{\large{} Title}}
\author{\textsf{Bowie}\thanks{\textsf{\large{}\protect\href{mailto:1@1.com}{1@1.com}}}}

\maketitle
\textsf{\large{}\tableofcontents{}}{\large\par}

\textsf{\large{}}\footnote{\textsf{\large{}footnote}}\textsf{\large{}.}{\large\par}

\textsf{\large{}Some text}{\large\par}

\textsf{\large{}More text }{\large\par}

\textsf{\large{}and more text}{\large\par}

\textsf{\large{}Again text}{\large\par}

\textsf{\large{}Bla... }{\large\par}

\subsubsection{\textsf{\large{}Notación}}

\textsf{\large{}
%here starts the problems
%the ouput is
%Error at "file.tex" (line 107, column 20):
%expecting \end{figure}
%\textsf{\large{}\includegraphics{pasted1}}{\large\par}
%
\begin{center}
\textsf{\large{}}
\begin{figure}[H]
{\centering}
\textsf{\large{}\includegraphics{pasted1}}{\large\par}
\par\end{centering}
\textsf{\large{}\caption{Caption of the figure}
\par}
\end{figure}
{\large\par}
\par\end{center}

\end{document}


### UPDATE

The source is compiled with LaTeX and a PDF file is generated, but reviewing the log I have found the next information (the parts I think are relevant):

...
restricted \write18 enabled.
%&-line parsing enabled.
...
... no UTF-8 mapping file for font encoding PU
...
Package geometry Warning: The marginal notes overrun the paper.
Add 11.32088pt and more to the right margin.
...
Output written on Intro.pdf (45 pages, 731901 bytes).
PDF statistics:
565 PDF objects out of 1000 (max. 8388607)
...


And amid the compilations says something about missing $s, } unwanted or no$, and absent glyphs (this is in the terminal and I can’t save it, and it isn’t in the logs).

I have tested with other TeX files and the conversion went well.

UPDATE 2

I have reviewed the figures and I have the following code (of the part of the figure)

\begin{center}
\textsf{\large{}}
\begin{figure}[H]
\begin{centering}
\textsf{\large{}\includegraphics{pasted1}}{\large\par}
\par\end{centering}
\textsf{\large{}\caption{Caption for the figure.}
}{\large\par}
\end{figure}
{\large\par}
\par\end{center}


The centering and center are in pairs. So I don’t know why at the MWE is different, but even in this way the output is the same.

### UPDATE 3

I have revamped the code and modified the figure sets, and now the document is well converted to .odt and .docx. The only thing is the figures lost their numeration and alignment.

• Before compiling your document with pandoc, maybe first assure that you can compile it with LaTeX. I get a number of errors, first about the encoding (I guess \usepackage[latin9]{inputenc} may not be your best choice if you use utf-8), but also you have troubles in your figure: there is a \end{centering} but no \begin{centering}... and centering is not an environment. Also, I'm not sure to see why you need that many \par. For instance the \par right after the caption break the latex compilation. Sep 21 at 20:50
• Then, once it compiles, you can try to see what disturbs pandoc. For instance, pandoc asks for \textsf to be inside the caption and not outside (which does make sense). Fixing that seems to solve the problem for me. To fix your whole document, I guess you can use search&replace tools, or sed in linux. Sep 21 at 20:54
• @tobiasBora,Im going to make a revision on the source, I was advised that $\par$ need to be used to correctly align the figure environments. It indeed compiles on LaTex, but I wasn't aware of the warnings Sep 21 at 21:37
• centering is not an environment, use \centering I can not guess the intention of lines such as \textsf{\large{}} or {\large\par} you should delete all such lines. It is not surprising that pandoc can not convert this document, the latex markup is rather strange. \textsf{\large{}\includegraphics{pasted1}}{\large\par} shoule be \includegraphics\{pasted1} you can not apply font commands to an image. Sep 21 at 22:15
• really it's surprisng that latex makes a pdf from this at all, only by lack of error checking, it is ompossible to even guess the intention of most of the lines here, it is certainly far from any normal latex markup. why is every line wrapped in \textsf for example??? Sep 21 at 22:20

# Converting to ODT with make4ht

Today make4ht is the best tool for this purpose. Write your LaTeX file as usual, with the proviso that you avoid exotic syntax and remove all packages that are not strictly necessary just to process the file (i.e., to run latex without errors). Forget about fancy formatting; keep it simple.

Then you do as follows:

latexmk -pdf file
make4ht -f odt file


You can use LibreOffice to save the resulting ODT file in DOCX format. (You can actually do it on the command line in some setups.)

## Alternative (better for HTML): lwarp

Another option is lwarp, which works wonderfully to generate HTML (see its documentation for how to do it, it's pretty simple). After opening the html file in a browser you can copy from the browser window and paste into LibreOffice and the results are passable. The unusual handling of footnotes in this setup were a dealbreaker for me, though.

# But it's harder in Spanish

Even once I rewrote your sample document in correct and reasonable LaTeX code, I did run into problems because using Spanish in babel interacts badly with a lot of other packages.

• I found that adding the es-sloppy option cleared up most of the problems by disabling some of the "bonus" features enabled in this Babel configuration. But still, when converting with make4ht, the table of contents included unwanted junk code written into the ODT output.
• Removing the contents lists solved the problems, and perhaps you can do without them in the ODT output. (Libreoffice does have its own ways of generating contents lists.)

A separate question about babel Spanish and make4ht is warranted.

The following document I think captures most of what you were trying to do, and both compiles to PDF and converts to ODT with no problems.

\documentclass[letterpaper, 12pt]{article}
\usepackage[T1]{fontenc}

% Because seems like you want a sans-serif typeface
\renewcommand*{\familydefault}{\sfdefault}

% Please note that the last option is necessary for conversion to work in
% Spanish
\usepackage[spanish,es-sloppy]{babel}

\usepackage{graphicx}
\usepackage{hyperref}

\title{Artículo}
\author{Bowie\thanks{\href{mailto:1@1.com}{1@1.com}}}

\begin{document}
\maketitle
% I can't get the contents lists to work in Spanish with make4ht
%\tableofcontents
%\listoffigures
Algunas palabras.\footnote{Una nota.}
Más palabras.
Y más.
\subsubsection{Notación}
Añado algo más (vease figura \ref{fig:pasted1}).
\begin{figure}
\label{fig:pasted1}
%    \includegraphics{pasted1} % I don't have the image to include
\caption{Descripción de la imágen}
\end{figure}
\end{document}

• I have tried this way and it tells me the same error eveb when I have depurated it. Sep 22 at 2:37
• @corie See my edits. These show more standard coding practices for what I think you are trying to write, and also how to deal with converting a Spanish document. Sep 22 at 14:00
• Thanks for your valuable information, and support. Indeed, there are trouble with the spanish babel pack. Sometimes I have to write in it, and usually some warnings and disabled features appears. I will look for the question. Thanks a lot again, you are very kind. Sep 22 at 14:50

The easiest way, I think, to convert a tex, PDF-file, to word or libreoffice format (.docx) is to use this online convertor https://pdf2docx.com

• The tool you have mentioned sucked so badly back in 2019 or something. Oct 12 at 8:13

I successfully LyX as a tool to this end. Keep the formatting of LaTeX as simple as possible. Launch LyX and import the file. Then export as "Open Document (pandoc)". Then, use LibreOffice to convert to doc if you like.

MS Word does a pretty good of importing PDF documents and converting them to .docx. It will not do well with advanced graphics and fancy headers though. You might save some time by simply opening the PDF output of LaTex in MS Word, and saving it as .docx.