Workflow: automatically convert markdown to pdf (pandoc, latex, hazel)


In 2016 some very great and helpful users in the MPU community (back then on google+) helped me to realize the following workflow, which I unfortunately am not able to reimplement on my new MacBook Pro (Catalina 10.15.2):

I write personal notes in markdown and have hazel watching the folder where I save them. Once hazel notices the hashtag #finished in the content of my markdown file it runs the following script on the file:

pandoc $1 --template=mytemplate.tex --latex-engine=xelatex --variable mainfont=“Avenir” --variable linestretch=1.25 -o mdoutput.pdf

This converts my markdown file to a PDF file using the format “mytemplate.tex” I configured with the help of MPU Users - the main purpose of this own format is to put special words (also from the metadata of the markdown file) to the headers and footers of the PDF.

To implement this workflow I had to

  1. install pandoc
  2. install mactex
  3. do some other fine tuning I don’t remember exactly.

On my new MacBook I have installed 1) and 2) from above and I stored the “mytemplate.tex” file (copied from my old macbook) under /Users/rexwex/.pandoc/templates/mytemplate.tex (it was this way on my old macbook).

When I now run follwing in terminal to test it:

pandoc --template=mytemplate.tex --latex-engine=xelatex --variable mainfont=“Avenir” --variable linestretch=1.25 -o mdoutput.pdf

I get this result:

–latex-engine has been removed. Use --pdf-engine instead.
Try pandoc --help for more information.

So i use

pandoc --template=mytemplate.tex --pdf-engine=xelatex --variable mainfont=“Avenir” --variable linestretch=1.25 -o mdoutput.pdf


Error producing PDF.
! LaTeX Error: Missing \begin{document}.

See the LaTeX manual or LaTeX Companion for explanation.
Type H for immediate help.

l.2 1

Is there anyone to help me reimplement my workflow and even helps me to tweak my “format” concerning headers and footers of the PDF (of course i can share mytemplate.tex therefore with you).


Can’t help you debug the CLI commands, but fyi a friend uses an Alfred workflow. A quick googling found this, which may or may not be what he uses.

I don’t have much experience with latex, but the last error message you got has to do with your latex template. I would try doing as it says and adding \begin{document}

Nothing wrong with pandoc --template=mytemplate.tex --pdf-engine=xelatex --variable mainfont=“Avenir” --variable linestretch=1.25 -o mdoutput.pdf

Just tested it (haven’t been using pandoc for a while) and (after changing the font, since I don’t have Avenir), it produced the PDF.

Share your LaTeX template with us!

1 Like

hi all!

Thx for your answers - @Lars is it possible, that it was you in 2016, who helped me to set this great workflow up on my old macbook - because at that time also a “Lars E.” was great help!

Is it possible that pandoc doesnt find my template, so that i have to set the path up anywhere?

mytemplate looks like that:














\usepackage{fixltx2e} % provides \textsubscript

\ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex






\else % if luatex or xelatex



























% use upquote if available, for straight quotes in verbatim environments


% use microtype if available



\UseMicrotypeSet[protrusion]{basicmath} % disable protrusion for tt fonts







\PassOptionsToPackage{usenames,dvipsnames}{color} % color is loaded by hyperref










pdfkeywords={$for(keywords)$$keywords$$sep$; $endfor$},








pdfborder={0 0 0},



\urlstyle{same} % don't use monospace font for urls


\ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex



































\VerbatimFootnotes % allows verbatim text in footnotes











% Scale images if necessary, so that they will not overflow the page

% margins by default, and it is still possible to overwrite the defaults

% using explicit options in \includegraphics[width, height, ...]{}




% Make links footnotes instead of hotlinks:





% avoid problems with \sout in headers with hyperref:







}{% else


\setlength{\parskip}{6pt plus 2pt minus 1pt}



\setlength{\emergencystretch}{3em} % prevent overfull lines










% Redefines (sub)paragraphs to behave more like sections












% load bidi as late as possible as it modifies e.g. graphicx







\ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex


\newcommand{\RL}[1]{\beginR #1\endR}

\newcommand{\LR}[1]{\beginL #1\endL}
















\author{$for(author)$$author$$sep$ \and $endfor$}






\chead{ }










\rfoot{Seite \thepage}


















































it worked on my old macbook, that is why i guess anything else is wrong… i somehow remember that at that time i had to change some bash file or something like that - might that be something i have to look for?

thank you!

Yes, that was me.

I could reproduce the error “./root.tex:160: LaTeX Error: Missing \begin{document}.”

Replace mytemplate.tex with this:

This is now working on my Mac (Catalina, current MacTeX,…). If it works, let’s take it from there…if not…we`ll dig into error logs.


many thanks. It basically works (i haven’t tested in combination with hazel but i guess that should work once it works in terminal). At first i got following error:

kpathsea:make_tex: Invalid filename `“Avenir”', contains '?'
kpathsea:make_tex: Invalid filename `“Avenir”', contains '?'
Error producing PDF.
! Package fontspec Error: The font "“Avenir”" cannot be found.

For immediate help type H <return>.
l.20 \fi

Then I removed "–variable mainfont=“Avenir” from the command line and it worked. I have the font “Avenir” installed, so I don’t understand the error… is there anything else I have to add in the command Line for Avenir?

I have one remaining wish - is it possible to replace “Aktenvermerk” (in left HEADER) and “rexwex” (in left FOOTER) through two variables, which take content of the metadata of the markdown file - the same as the “date” (right HEADER) - therefore i start my markdown file with

date: MM.DD.YYYY

I would like to add

date: MM.TT.YYYY
case: CASEXY
author: rexwex

how can this be handled?


You are welcome. Something weird happened when transferring the template file and some wonky stuff was in there (line breaks which shouldn’t be there). I just cleaned it, removed excessive line breaks and you are good to go.

As for your fontspec problem: “ and ” and ". Are not the same. Try this: “Avenir” and “Avenir” are not the same. Zoom in and see the subtle difference. You have a keyboard with german layout? Then edit the command and enter is properly: The " you want is Shift+2. It seems that whatever editor you used, replaced them with typographic (but useless) ones.

As for your special request: yes. But I am hungry now. :smiley: Can you post a markdown file of a note you want to convert, so I can test it? Just fill it with blabla. I recall we created a md template but I have no idea what it looked like.


Of course you were right with your tipp regarding the fontspec problem - now it works.


here the file for my special request:


Like this? Page x of y is also fixed.

OMG, sensational … exactly what i am looking for … with one more space after the first number in the pagecount… :slight_smile: i guess the footer and header look the same on all pages (apart from pagecount), which would be perfect.

would you be so kind and share the tex file with me and tell me how the metadata of the md file has to look like? how can i express my gratitude? would like to give you an amazon voucher or something similar? how is it possible to send direct messages on this board?

Everything (CLI command, tex template, sample md) is here:

I added a stripped-down and commented version (notiztemplate.tex), so you can easily take it from there and do some modifications. Command is simpler (is in command.txt), since I removed some options (just font is left).

It has less features, but I think enough for text notes (pdf is written, pdf meta data is pulled from md and written, full page count, custom headers/footers)

you are a genius! thank you very much, I am deeply thankful and somehow feel bad that I cant give you (at least a little bit) back. is there some option to send you a private message though this board?


hmm… i now tried the new “shorter” file and got this error:

pandoc --template=notiztemplate.tex --pdf-engine=xelatex --variable mainfont="Avenir" -o output.pdf

Error producing PDF.

! Undefined control sequence.

l.49 \tightlist

not big of a deal because the other one works anyway…just wanted to tell you…

Easy fix…done…

1 Like

I am moving away from DropBox, so I moved all the files to GitHub.