Markdown & MathJax to Word with embedded images

When I’m writing question banks I like to use markdown and a program called Typora. I can type and format without ever needing to take my hands off the keyboard to reach for my mouse.

Normally I’ll export the markdown file as a docx file and then export from that to rtf using Word. Word gets added because it works better for me to keep fonts straight. Typora exports monospaced text as Consolas and I prefer Courier New for my question banks.

And it works pretty well, with one exception. Whenever I need to embed a math equation into a question or answer. Normally I would use MathJax to build the equation, but those carry over to Word as a Microsoft Equation and then exports to the RTF as MathJax where it would be better as an image.

MathJax?

If you’re not a markdown person, you may not have heard of MathJax. It’s an extension of LaTex where you enter a specifically formatted string and it’s converted into an equation.

For example, the string $$d = \sqrt{(x_2 - x_1)^2 + (y_2 - y_1)^2}$$ would convert to this.

It’s really handy for equations, but where it really gets a lot of use is when I need to show a matrix.

I can display the matrix and even highlight specific cells.

Word and the Equation Editor

Catch is, when I export from Typora to a Word file the equation comes in to Word as an Equation Editor object. That would work if Word was my final target, but eventually I need the file in RTF.

It needs to be an image.

The Script

A discussion on Google Groups got me to the right place.

The trick is to first convert the markdown file to HTML, which creates the images, and then to docx. From the command line it looks like this.

pandoc filename.md --webtex -t html | pandoc -f html -o filename.docx

And it worked. I created the markdown file in Typora and then ran that command against the markdown file and a docx was created. Opened it up in word and all the equations were images.

95% of the way there. The other 5% is me remembering the command. Instead I kicked out a quick batch file named md2docx.bat and dropped it in my path.

@echo off
for %%f in (*.md) do (
echo Converting %%~nf.md to %%~nf.docx
pandoc "%%~nf.md" --webtex -t html | pandoc -f html -o "%%~nf.docx"
)

It goes through every markdown file in a folder and converts to docx along with creating images along the way.

Next Step?

I’d really like to be able to skip the step of going to a Word file and go straight from markdown to rich text.

Problem is that monospaced fonts in the markdown get set as Courier if I go straight to RTF. Really needs to be Courier New so it’s scalable. If I go to docx first it uses Consolas. Fortunately Word makes it pretty easy to find and replace everything by font. But it’s an extra step.