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

Hi!

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 test.md --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 test.md --template=mytemplate.tex --pdf-engine=xelatex --variable mainfont=“Avenir” --variable linestretch=1.25 -o mdoutput.pdf

THEN I GET FOLLWING RESULT

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).

THANKS A LOT!

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 test.md --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:

\documentclass[11pt,a4paper,oneside]{scrartcl}

\KOMAoptions{DIV=15}

$if(fontfamily)$

\usepackage[$for(fontfamilyoptions)$$fontfamilyoptions$$sep$,$endfor$]{$fontfamily$}

$else$

\usepackage{lmodern}

$endif$

$if(linestretch)$

\usepackage{setspace}

\setstretch{$linestretch$}

$endif$

\usepackage{amssymb,amsmath}

\usepackage{ifxetex,ifluatex}

\usepackage{fixltx2e} % provides \textsubscript

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

\usepackage[$if(fontenc)$$fontenc$$else$T1$endif$]{fontenc}

\usepackage[utf8]{inputenc}

$if(euro)$

\usepackage{eurosym}

$endif$

\else % if luatex or xelatex

\ifxetex

\usepackage{mathspec}

\else

\usepackage{fontspec}

\fi

\defaultfontfeatures{Ligatures=TeX,Scale=MatchLowercase}

$if(euro)$

\newcommand{\euro}{€}

$endif$

$if(mainfont)$

\setmainfont[$for(mainfontoptions)$$mainfontoptions$$sep$,$endfor$]{$mainfont$}

$endif$

$if(sansfont)$

\setsansfont[$for(sansfontoptions)$$sansfontoptions$$sep$,$endfor$]{$sansfont$}

$endif$

$if(monofont)$

\setmonofont[Mapping=tex-ansi$if(monofontoptions)$,$for(monofontoptions)$$monofontoptions$$sep$,$endfor$$endif$]{$monofont$}

$endif$

$if(mathfont)$

\setmathfont(Digits,Latin,Greek)[$for(mathfontoptions)$$mathfontoptions$$sep$,$endfor$]{$mathfont$}

$endif$

$if(CJKmainfont)$

\usepackage{xeCJK}

\setCJKmainfont[$for(CJKoptions)$$CJKoptions$$sep$,$endfor$]{$CJKmainfont$}

$endif$

\fi

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

\IfFileExists{upquote.sty}{\usepackage{upquote}}{}

% use microtype if available

\IfFileExists{microtype.sty}{%

\usepackage{microtype}

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

}{}

$if(geometry)$

\usepackage[$for(geometry)$$geometry$$sep$,$endfor$]{geometry}

$endif$

\usepackage{hyperref}

$if(colorlinks)$

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

$endif$

\hypersetup{unicode=true,

$if(title-meta)$

pdftitle={$title-meta$},

$endif$

$if(author-meta)$

pdfauthor={$author-meta$},

$endif$

$if(keywords)$

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

$endif$

$if(colorlinks)$

colorlinks=true,

linkcolor=$if(linkcolor)$$linkcolor$$else$Maroon$endif$,

citecolor=$if(citecolor)$$citecolor$$else$Blue$endif$,

urlcolor=$if(urlcolor)$$urlcolor$$else$Blue$endif$,

$else$

pdfborder={0 0 0},

$endif$

breaklinks=true}

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

$if(lang)$

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

\usepackage[shorthands=off,$for(babel-otherlangs)$$babel-otherlangs$,$endfor$main=$babel-lang$]{babel}

$if(babel-newcommands)$

$babel-newcommands$

$endif$

\else

\usepackage{polyglossia}

\setmainlanguage[$polyglossia-lang.options$]{$polyglossia-lang.name$}

$for(polyglossia-otherlangs)$

\setotherlanguage[$polyglossia-otherlangs.options$]{$polyglossia-otherlangs.name$}

$endfor$

\fi

$endif$

$if(natbib)$

\usepackage{natbib}

\bibliographystyle{$if(biblio-style)$$biblio-style$$else$plainnat$endif$}

$endif$

$if(biblatex)$

\usepackage$if(biblio-style)$[style=$biblio-style$]$endif${biblatex}

$if(biblatexoptions)$\ExecuteBibliographyOptions{$for(biblatexoptions)$$biblatexoptions$$sep$,$endfor$}$endif$

$for(bibliography)$

\addbibresource{$bibliography$}

$endfor$

$endif$

$if(listings)$

\usepackage{listings}

$endif$

$if(lhs)$

\lstnewenvironment{code}{\lstset{language=Haskell,basicstyle=\small\ttfamily}}{}

$endif$

$if(highlighting-macros)$

$highlighting-macros$

$endif$

$if(verbatim-in-note)$

\usepackage{fancyvrb}

\VerbatimFootnotes % allows verbatim text in footnotes

$endif$

$if(tables)$

\usepackage{longtable,booktabs}

$endif$

$if(graphics)$

\usepackage{graphicx,grffile}

\makeatletter

\def\maxwidth{\ifdim\Gin@nat@width>\linewidth\linewidth\else\Gin@nat@width\fi}

\def\maxheight{\ifdim\Gin@nat@height>\textheight\textheight\else\Gin@nat@height\fi}

\makeatother

% 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, ...]{}

\setkeys{Gin}{width=\maxwidth,height=\maxheight,keepaspectratio}

$endif$

$if(links-as-notes)$

% Make links footnotes instead of hotlinks:

\renewcommand{\href}[2]{#2\footnote{\url{#1}}}

$endif$

$if(strikeout)$

\usepackage[normalem]{ulem}

% avoid problems with \sout in headers with hyperref:

\pdfstringdefDisableCommands{\renewcommand{\sout}{}}

$endif$

$if(indent)$

$else$

\IfFileExists{parskip.sty}{%

\usepackage{parskip}

}{% else

\setlength{\parindent}{0pt}

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

}

$endif$

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

\providecommand{\tightlist}{%

\setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}

$if(numbersections)$

\setcounter{secnumdepth}{5}

$else$

\setcounter{secnumdepth}{0}

$endif$

$if(subparagraph)$

$else$

% Redefines (sub)paragraphs to behave more like sections

\ifx\paragraph\undefined\else

\let\oldparagraph\paragraph

\renewcommand{\paragraph}[1]{\oldparagraph{#1}\mbox{}}

\fi

\ifx\subparagraph\undefined\else

\let\oldsubparagraph\subparagraph

\renewcommand{\subparagraph}[1]{\oldsubparagraph{#1}\mbox{}}

\fi

$endif$

$if(dir)$

\ifxetex

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

$if(latex-dir-rtl)$

\usepackage[RTLdocument]{bidi}

$else$

\usepackage{bidi}

$endif$

\fi

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

\TeXXeTstate=1

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

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

\newenvironment{RTL}{\beginR}{\endR}

\newenvironment{LTR}{\beginL}{\endL}

\fi

$endif$

$for(header-includes)$

$header-includes$

$endfor$

$if(title)$

\title{$title$$if(thanks)$\thanks{$thanks$}$endif$}

$endif$

$if(subtitle)$

\providecommand{\subtitle}[1]{}

\subtitle{$subtitle$}

$endif$

$if(author)$

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

$endif$

\date{$date$}

\usepackage{fancyhdr}

\pagestyle{fancy}

\lhead{Aktenvermerk}

\chead{ }

\rhead{

$if(date)$

$date$

$else$

\today

$endif$

}

\lfoot{rexwex}

\cfoot{}

\rfoot{Seite \thepage}

\renewcommand{\headrulewidth}{1pt}

\renewcommand{\footrulewidth}{1pt}

\begin{document}

$if(title)$

\maketitle

$endif$

$if(abstract)$

\begin{abstract}

$abstract$

\end{abstract}

$endif$

$for(include-before)$

$include-before$

$endfor$

$if(toc)$

{

$if(colorlinks)$

\hypersetup{linkcolor=$if(toccolor)$$toccolor$$else$black$endif$}

$endif$

\setcounter{tocdepth}{$toc-depth$}

\tableofcontents

}

$endif$

$if(lot)$

\listoftables

$endif$

$if(lof)$

\listoffigures

$endif$

$body$

$if(natbib)$

$if(bibliography)$

$if(biblio-title)$

$if(book-class)$

\renewcommand\bibname{$biblio-title$}

$else$

\renewcommand\refname{$biblio-title$}

$endif$

$endif$

\bibliography{$for(bibliography)$$bibliography$$sep$,$endfor$}

$endif$

$endif$

$if(biblatex)$

\printbibliography$if(biblio-title)$[title=$biblio-title$]$endif$

$endif$

$for(include-after)$

$include-after$

$endfor$

\end{document}

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: https://www.dropbox.com/s/4x6fdyti7dktasc/mytemplate.tex?dl=0

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.

Hi,

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?

THX!!!

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.

hi!

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

THX.

here the file for my special request: https://www.dropbox.com/s/4izqqn29r3os77m/test.md?dl=0

MERCI

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: https://www.dropbox.com/sh/f554b0quj74gdtp/AADInKDz5hoTNcgnEQ8FAgCra?dl=0

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?

THX

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


pandoc test.md --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