Multimarkdown to PDF

As I currently write my diploma thesis, there is this need to do other things than writing the actual thesis. This and the fact that I really like the idea of markdown resulted in writing my thesis in markdown. Actually I write it in multimarkdown, an extension of it. As the name implies markdown is something contrary to markup in that is deemphasizes the markdown. All mardown files are actually plain text files. But they have a markup which makes plain sense. But as there is a definition behind your writing style you can convert that plain text in a number of markup languages, including html and latex. Some generators even support the open document format or rtf.

So what I do is the following:

  1. writing in a number of markdown files,
  2. putting them together to one large markdown file,
  3. convert that into a latex file,
  4. include that into my main latex file
  5. and mklatex the hell out of that one.

How that works out? Pretty well so far, I have to say. Sometimes I have to use standard latex, which you can embed in your document. I always have the option to go back to writing standard latex if I want to by running my files through the converter so I get my normal latex files.

Source code

So if you want to do the same, here is the code that I use. Stuff it into a .sh file in your main directory. This is my file structure:

  • content/
    • (contains a list all the .md-files for sorting)
    • your other .md files
  • main-document.tex

Keep in mind that this is on a Mac, so on other unix systems you might have to adopt your code a little bit. For windows … well you can get the idea :)

# this script takes everything that is in content-markdown and turns it into one pdf file.
# it also stops everytime it encouters an error in any of the steps.

# make sure we start in this directory (needed for running it from finder)
cd "`dirname "$0"`" 

# multimarkdown source, concatenate files given in
cd content/
perl ~/Library/Application\ Support/MultiMarkdown/Utilities/ > ../
if [[ $rc != 0 ]] ; then
	echo " exited with status code $rc"
    exit $rc

cd ../

# generate latex code from that one markdown file. this uses multimarkdown
multimarkdown -t latex -o "main-document-content.tex" ""
if [[ $rc != 0 ]] ; then
	echo "multimarkdown compilation exited with status code $rc"
    exit $rc
	echo "multimarkdown compilation done!"

# change all \autoref commands to vref. This makes the references use pagenumbers as well (i.e. section 2.1 on page 4)
sed -ie 's/\\autoref/\\vref/g' "main-document-content.tex"

# compile pdf from latex source:
# we need bibtex and pdf output option + use xelatex instead of pdflatex (so we get nice otf & aat support)
latexmk -bibtex -pdf main-document -e '$pdflatex=q/xelatex %O %S/'
if [[ $rc != 0 ]] ; then
	echo "latexmk run #1 exited with status code $rc"
    exit $rc

# remove temporary files
latexmk -c main-document

# open file in
open main-document.pdf

exit 0

Andere Artikel