As you might know, I write for a living. And, as you might know, I do a lot of my writing using Markdown. One limitation of Markdown is that the stock converters can only morph it into HTML. HTML is fine, but sometimes I need to take what I’ve written and formatted using Markdown and convert it to other formats like LaTeX or PDF.
When I need flexibility when converting from Markdown to other formats, I turn to Pandoc.
Yes, Pandoc. It’s a set of libraries and a command line tool to convert between various markup languages. Among other languages, Pandoc can deal with:
And, of course, Markdown.
Pandoc is very powerful and flexible. It’s also a command line tool, but don’t let that scare you.
While Pandoc is not standard kit with most Linux distributions, it is available in the software repositories for many major distros. Check your package manager. You can also download it.
I usually use Pandoc with files formatted with Markdown. So, let’s use that as our starting point.
Crack open a terminal window and navigate to the directory that contains the file(s) that you want to convert.
At it’s most basic, Pandoc’s syntax is fairly simple. You run the command pandoc followed by -o fileName followed by the file that you want to convert. For example:
pandoc -s -o myFile.tex myFile.md
That converts a Markdown file to LaTeX. Notice the -s option? on the command line. When converting to another markup language, that option surrounds your text with the proper header and footer. You add that when you’re creating documents that will exist on their own. If you plan to combine a bunch of, say, LaTeX or HTML documents you can leave the -s option out.
Let’s look at a real example. A few years ago, I wrote about my experiences with a Chromebook. One of my coaching clients at the time was interested in those experiences, so I decided to convert that post into three formats:
Let’s take quick look at how. First up, LaTeX. I run the following command:
pandoc -s -o chromebook.tex chromebook.md
The resulting file looks like this in a text editor:
I can modify the header to use whatever LaTeX class (which determines the look and feel of the output) that I prefer.
When I want to go PDF, I need to have a functioning TeX system installed on my computer. That’s standard kit for most Linux distros so you shouldn’t have a problem. If I’m running version 1.9 of Pandoc or newer, I use this command:
pandoc chromebook.md -o chromebook.pdf
The results look like this:
It’s not the prettiest document, but it works. I use this conversion mainly for archiving my writing.
Finally, I want to convert the file to ODT (the format for such applications as LibreOffice Writer and AbiWord). To do that, I run the following command:
pandoc -o chromebook.odt chromebook.md
Here’s what the results look like in LibreOffice Writer:
Obviously, Pandoc is for much more than converting files formatted with Markdown to other formats. It really is a swiss-army knife. But, for now, it’s most useful to me as a Markdown coverter.
I’ll be looking at some of Pandoc’s other features in a future article in this space. Stay tuned.