Converting LaTeX to Word using pandoc and R

September 17, 2022    pandoc

A few weeks ago I needed to convert an academic paper written using LaTeX into a Microsoft Word document. My preferred way of doing this would be to use pandoc.

In the past I have used pandoc in the command line. On checking my (reasonably) new laptop, it appeared that I didn’t have access to it on the command line but I knew it had to be installed as part of the RStudio install.

This led me to check if there were any pandoc() functions within any of the packages and I found that, as I suspected, there are a bunch of them, mostly in the rmarkdown package. To demo, we can use it to check with pandoc is installed

library(rmarkdown)
pandoc_available()
[1] TRUE

and check which version is available.

pandoc_version()
[1] '3.1.1'

Now that we’re happy we can access pandoc, we can use pandoc_convert() to do the actual conversion. My LaTeX document also used a BibTeX file and so the conversion could be a little more complicated. The command line syntax is explained nicely here and we can implement it in R even more easily as we don’t actually need to specify the BibTeX file (it is picked up from the LaTeX file).

pandoc_convert(input = "my_paper.tex", 
               output = "my_paper_converted.docx", 
               citeproc = TRUE)

In my case, the conversion worked really well and I will be trying this in the future to allow quick conversions between LaTeX (my preferred option for writing papers) to help with collaboration.