How to mix plain markdown and latex and get beautiful results while writing human stuff. (with results being PDF, XHTML, HTML5, docx, EPUB, LaTex and many more).

Pandoc allows you to get the best of the two worlds without compromises: readable plain text files with latex expressions and everything offered by latex rendering and packages.

You can write code-blocks with specific language syntax highlight, normal markdown and LaTex parts.

To place something written in TeX in mathmode, use $ signs to enclose the math you want to display, exactly what you would do in a normal LaTex file. The same for symbols in textmode.

Some useful links:

In this article I’ll present my workflow with Sublime Text 3 on Debian, don’t worry if you use another editor or another OS, you should be able to adapt this easily.


Tools and dependencies

You will need Pandoc and texlive packages. apt-get install pandoc texlive-latex-base on debian-based distros.

Now, on Sublime Text

We’ll use the MarkdownEditing package on Sublime Text, which provides better syntax and better color schemes. Additionaly, I’m using this theme for this package, which provides both coloured and text-style preview for Markdown.

Now set every markdown file to be opened with this theme: Open all with current extension as > MarkdownEditing > Markdown (Markdown GFM if you want the codeblock syntax highlight while editing).

At this point, a markdown file should look like this in Sublime Text: Screenshot1

Automate building

On Sublime Text: Tools > Build System > New Build System and paste this:

    "shell_cmd": "pandoc $ -o  $file_base_name.pdf"

Select this build with Tools > Build System and tick Save all on build. Now, every time you press CTRL+B on a markdown file, pandoc will build, in the same directory, a PDF from that file.

Additionaly, you can enable syntax-highlighting in the output PDF adding --highlight-style tango in the shell command above. This will automatically select the language specified after the opening of codeboxes (exactly how GitHub renders markdown files):

this codeblock will be syntax-highlighted as "lua"s code by pandoc, 
using the color scheme "tango"

You can even specify your LaTex template with your favourite packages and LaTex settings:

    "shell_cmd": "pandoc $  --template='/home/avivace/template.tex' --highlight-style tango -o  $file_base_name.pdf"

Here’s the template.tex I’m using, based on the pandoc default one.