Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandoc skip latex environment

I'm writing mainly in LaTeX, but some co-authors prefer MS Word. To facilitate their work a bit, I would like to convert the .tex file (or the .pdf) to a .docx. The formatting does not need to be perfect, but all of the text, equations, figures etc should be perfectly readable.

I'm currently thinking to take the .tex document, replace all the essential stuff and then let Pandoc do it's magic. For this I would preferably implement my additions as a Pandoc filter. E.g., my tikz pictures would be converted to png using the tikz.py filter provided with Pandoc. The problem I'm facing with this approach is that Pandoc tries to interpret the tikz environment upon conversion from tex into it's internal language and the filters take this internal language as an input. The result is that the tikz code is lost. Is there a way to tell Pandoc to leave any tikzpicture environments alone?

Edit: See the MWE below:

MWE.tex contents:

\documentclass{article}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
    \draw (0,0) -- (2,2);
\end{tikzpicture}
\end{document}

Output of pandoc -t native MWE.tex

[Para [Str "(0,0)",Space,Str "\8211",Space,Str "(2,2);"]]

The \draw command has completely disappeared as you can see.

like image 223
Octaviour Avatar asked Nov 09 '22 00:11

Octaviour


1 Answers

I found that pandoc does not skip code encapsulated in \iffalse ... \fi, so you can redefine the tikpicture environment as such (or in any other way you might like):

\documentclass{article}
\usepackage{tikz}

\iffalse
    \renewenvironment{tikzpicture}%
        {\par---start tikzpicture---\\}%
        {\\---end tikzpicture---\par}
    \renewcommand{\node}{node:}
\fi

\begin{document}

\begin{tikzpicture}
\node {foo};
\end{tikzpicture}

\end{document}

With pandoc 2.5 this results in a docx file containing:

—start tikzpicture—
node:foo;
—end tikzpicture—

This feels very wrong, and I wish I knew a nicer way.

like image 180
Roel Avatar answered Dec 09 '22 12:12

Roel