[Tex/LaTex] Convert LaTeX file to word including a lot of tikzpictures

conversionmswordtikz-pgf

I need to convert a latex file to MS word which contains a lot of tikz-pictures. To make the conversion more easy, the converter schould automatically replace the tikzpicture blocks with an includegraphics command to to corresponding pdf of the picture and convert before conversion.

Is there any script or converter which can do this?

Best Answer

You can use tex4ht to convert LaTeX to OpenDocument format, which can be then easily converted to Word using LibreOffice. It supports TikZ externalization, so you save your drawings in suitable format first and use them in the conversion process afterwards. It seems that the most suitable image format to use is png. A vector format like svg or pdf would be better, but they are unsupported in the odt format, so no luck here, we must stick with bitmaps.

You haven't provided a mwe, so here is mine:

\documentclass[a4paper, 12pt, ngerman]{scrartcl}

\usepackage{graphicx}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\usetikzlibrary{external}    
\tikzset{
 external/system call/.add={}                                                
 {;  rungs -dSAFER -dBATCH -dNOPAUSE -sDEVICE=pngalpha -dGraphicsAlphaBits=4 -r110 -sOutputFile=\image.png \image.pdf }
}
\makeatletter
\@ifpackageloaded{tex4ht}{
}{
    \tikzexternalize
}
\makeatother

\begin{document}

\begin{figure}[htb]
\centering
\begin{tikzpicture}
\begin{axis}[width=0.8\textwidth,
height=0.3\textheight,
scale only axis,
xmin=0,
xmax=1,
xlabel={Dimensionless number $\zeta$ (-)},
xlabel style={fill=white,fill opacity=0.9},
ymin=0,
ymax=2000,
ylabel={Resulting pressure $\Sigma p$ (bar)},
ylabel style={fill=white,fill opacity=0.9},
axis background/.style={fill=white},
legend style={at={(0.71,0.96)},anchor=north west,draw=black,fill=white,legend cell align=left}
]
%% Formula 1
\addplot[blue, domain=0:1, samples=101]
{1998*(1 - x^2)};
\addlegendentry{Formula 1};
\end{axis}
\end{tikzpicture}
\caption{Resulting pressure $\Sigma p$ as a function of $\zeta$ \label{fig:spz}}
\end{figure}

\begin{tikzpicture}
  \draw (2,2) ellipse (3cm and 1cm);
  \node (2,2) {Hello};
\end{tikzpicture}

\end{document}

You can compile the document using:

pdflatex -shell-escape filename.tex    

It will create two png images.

The interesting bits in this TeX file are these

\usetikzlibrary{external}    
\tikzset{
 external/system call/.add={}                                                
 {;  rungs -dSAFER -dBATCH -dNOPAUSE -sDEVICE=pngalpha -dGraphicsAlphaBits=4 -r110 -sOutputFile=\image.png \image.pdf }
} 

This is configuration for TikZ externalization. rungs command is version of GhostScript shipped with TeX distributions, so it should work everywhere. You may want to play with the parameters, in order to get the best result.

\@ifpackageloaded{tex4ht}{
}{
    \tikzexternalize
} 

This piece will ensure that the configuration from the above is used only when the document is not compiled using tex4ht, because we need to use special configuration in that case.

The configuration for tex4ht is provided in a file with .cfg extension, like mycfg.cfg:

\tikzset{
    tex4ht inc/.style={
        /pgf/images/include external/.code={%
            \includegraphics[]{##1.png}%
        }

    }
}
\tikzexternalize[mode=only graphics]  
\tikzset{tex4ht inc}
\Preamble{xhtml,mathml}
\begin{document}
\EndPreamble

This configuration basically tells TikZ not to generate the images for figures, but to include the png images created in the previous step instead.

You can compile the document using tex4ht now. The command is following:

mk4ht oolatex filename.tex mycfg 

with this result:

enter image description here

Related Question