github twitter linkedin email
Markdown for flexible reporting

Transparency, multiple devices, multiple operating systems, paper/electronic,… when we write documents, we often need to adjust to different constrains.

This is the reason why I think that the dissociation between the information and the presentation of the said information is an important step that allows you on the long run to become more efficient. This is clearly has no immediate benefit, but on the long I really started to see the advantage.

I have started some time ago to use the combination of pandoc and its Markdown. You write your text in a regular text file, following extended Markdown syntax and then you use pandoc’s incredible power to convert it into : pdf, word, openoffice, html, … I found that using this strategy in combination with a proper library management system that can export to bib file format is rather efficient in my hands.

The major bottleneck for me has been to get the conversion and the templates right to automate the whole process and this what I am sharing below. Nothing fancy, just clean and efficient.

The global idea is to have your markdown document (doc.md) in a folder together with the style sheet for html (style.css), the xelatex template file (template.tex), your bibliography (doc.bib),… and then running the makefile or the batch file depending on your system.

It requires of course a full install of pandoc and LaTeX distribution that I won’t cover but multiple resources can be found on the web.

Makefile (for *nix)

PANDOC = pandoc
 
%.html: %.md style.css Makefile
    $(PANDOC) -c style.css -s -f markdown \
    -t html --standalone \
    --bibliography=doc.bib -o $@ $<
 
%.odt: %.md Makefile
    $(PANDOC) --standalone -f markdown \
    -t odt --bibliography=doc.bib -o $@ $<
 
%.epub: %.md Makefile
    $(PANDOC) -o $@ $<
 
%.pdf: %.md Makefile
    $(PANDOC) --bibliography=doc.bib --latex-engine xelatex --template=template.tex \
    -V lang=english \
    -V mainfont='Ubuntu' \
    -V date='' \
    -V fontsize='11pt' \
    -o $@ $<
 
%.tex: %.md Makefile
    $(PANDOC) --bibliography=doc.bib --latex-engine xelatex \
    -V lang=english \
    -V mainfont='Ubuntu' \
    -V date='' \
    -V fontsize='11pt' \
    -o $@ $<
 
all: doc.html doc.epub doc.odt doc.pdf
 
clean:
    rm -f *.{html,odt,epub,pdf,tex}

Makefiles.bat (for Windows)

pandoc -f markdown --bibliography=doc.bib --latex-engine=xelatex --template=templateMK.tex --toc -o doc.pdf doc.md

pandoc -f markdown --bibliography=doc.bib --latex-engine=xelatex --template=templateMK.tex --toc -o doc.tex doc.md

pandoc -f markdown --bibliography=doc.bib -V mainfont=Ubuntu -c style.css --toc -o doc.html doc.md

pandoc -f markdown --bibliography=doc.bib --toc -V mainfont=Ubuntu -t odt -o doc.odt doc.md

pandoc -f markdown --bibliography=doc.bib --toc -V mainfont=Ubuntu -t docx -o doc.docx doc.md

XeLatex template :

%\documentclass[$if(fontsize)$$fontsize$,$endif$$if(lang)$$lang$,$endif$$if(papersize)$$papersize$,$endif$$for(classoption)$$classoption$$sep$,$endfor$]{$documentclass$}
\documentclass[a4paper,12pt,tocbasic]{scrreprt}
\usepackage[english]{babel}
%\KOMAoptions{abstract=true}
\usepackage{graphicx, float, array, fancybox, tikz} % For images, tables & floats
\usepackage{titlesec}
\usepackage{amsmath, amssymb} % For math
\usepackage{verbatim, color, xcolor, wasysym}
\usepackage{lastpage, booktabs}
\usepackage{enumitem}
\usepackage{crop,NAR-natbib}
\newcommand{\tightlist}{\vspace*{-1ex}%
  \setlength{\itemsep}{0pt}
  \setlength{\parskip}{0pt}
  }
\PassOptionsToPackage{cmyk}{xcolor}
%\usepackage[pdftitle={TEST}, pdfauthor={That is You}, pdfkeywords={}, unicode, pdfencoding=auto]{hyperref}
%\hypersetup{colorlinks, citecolor=blue, filecolor=blue, linkcolor=black, urlcolor=blue}
%\usepackage{cite}
\usepackage{html,makeidx}
\usepackage{pgfplotstable, makecell, colortbl}
% Fix for acrobat reader, cmyk and transparancy + XELATEX
\usepackage{type1cm}
\usepackage{eso-pic}
\AddToShipoutPicture{%
\makeatletter%
\special{pdf: put @thispage <</Group << /S /Transparency /I true /CS /DeviceRGB>> >>}%
\makeatother%
}

%framed mini box
\newenvironment{fminipage}%
{\setlength{\fboxsep}{10pt}\begin{Sbox}\begin{minipage}}%
{\end{minipage}\end{Sbox}%
\begin{center}\vspace{5mm}%
  \fbox{\TheSbox}%
\vspace{5mm}\end{center}}

%defining Pasteur official colors
\definecolor{BlueP}{cmyk}{1,0.5,0,0.2}
\definecolor{YellowP}{cmyk}{0,0.35,1,0.1}

%creme
\definecolor{creamy}{cmyk}{0.03,0,0.07,0.05}
%Pink
% RGB 242 56 90
\definecolor{pinky}{cmyk}{0,0.77,0.63,0.05}
%yellowish
% RGB 245 165 3
\definecolor{yellowy}{cmyk}{0,0.33,0.99,0.04}
%Blue
%RGB 54 177 191
\definecolor{bluey}{cmyk}{0.72,0.07,0,0.25}
%darker blue
% RGB 74 217 217
\definecolor{dbluey}{cmyk}{0.66,0,0,0.15}


\titleformat{\chapter}[display]
{\normalfont\huge\bfseries\color{BlueP}}{\chaptertitlename\ \thechapter}{20pt}{\Huge}
\titleformat*{\section}{\normalfont\Large\bfseries\color{BlueP}}
\titleformat*{\subsection}{\normalfont\large\bfseries\color{BlueP}}

\usepackage{chngcntr}
\counterwithout{figure}{chapter}

%\titleformat*{\subsubsection}{\normalfont\normalsize\bfseries\color{BlueP}}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{amssymb,amsmath}
\usepackage{ifxetex,ifluatex}
\usepackage{fixltx2e} % provides \textsubscript
% use upquote if available, for straight quotes in verbatim environments
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
\ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex
  \usepackage[utf8]{inputenc}
$if(euro)$
  \usepackage{eurosym}
$endif$
\else % if luatex or xelatex
  \ifxetex
    \usepackage{mathspec}
    \usepackage{xltxtra,xunicode}
    \setmainfont{Times New Roman}
  \else
    \usepackage{fontspec}
  \fi
  \defaultfontfeatures{Mapping=tex-text,Scale=MatchLowercase}
  \newcommand{\euro}{€}
$if(mainfont)$
    \setmainfont{$mainfont$}
$endif$
$if(sansfont)$
    \setsansfont{$sansfont$}
$endif$
$if(monofont)$
    \setmonofont[Mapping=tex-ansi]{$monofont$}
$endif$
$if(mathfont)$
    \setmathfont(Digits,Latin,Greek){$mathfont$}
$endif$
\fi
% use microtype if available
\IfFileExists{microtype.sty}{\usepackage{microtype}}{}
$if(geometry)$
\usepackage[$for(geometry)$$geometry$$sep$,$endfor$]{geometry}
$endif$
$if(natbib)$
\usepackage{natbib}
\bibliographystyle{$if(biblio-style)$$biblio-style$$else$plainnat$endif$}
$endif$
$if(biblatex)$
\usepackage{biblatex}
$if(biblio-files)$
\bibliography{$biblio-files$}
$endif$
$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}
$endif$
$if(tables)$
\usepackage{longtable,booktabs}
$endif$
$if(graphics)$
\usepackage{graphicx}
% Redefine \includegraphics so that, unless explicit options are
% given, the image width will not exceed the width of the page.
% Images get their normal width if they fit onto the page, but
% are scaled down if they would overflow the margins.
\makeatletter
\def\ScaleIfNeeded{%
  \ifdim\Gin@nat@width>\linewidth
    0.7\linewidth
  \else
    \Gin@nat@width
  \fi
}
\makeatother
\let\Oldincludegraphics\includegraphics
{%
 \catcode`\@=11\relax%
 \gdef\includegraphics{\@ifnextchar[{\Oldincludegraphics}{\Oldincludegraphics[width=\ScaleIfNeeded]}}%
}%
$endif$
\ifxetex
  \usepackage[setpagesize=false, % page size defined by xetex
              unicode=false, % unicode breaks when used with xetex
              xetex]{hyperref}
\else
  \usepackage[unicode=true]{hyperref}
\fi
\hypersetup{breaklinks=true,
            bookmarks=true,
            pdfauthor={$author-meta$},
            pdftitle={$title-meta$},
            colorlinks=true,
            citecolor=$if(citecolor)$$citecolor$$else$blue$endif$,
            urlcolor=$if(urlcolor)$$urlcolor$$else$blue$endif$,
            linkcolor=$if(linkcolor)$$linkcolor$$else$magenta$endif$,
            pdfborder={0 0 0}}
\urlstyle{same}  % don't use monospace font for urls
$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$
\setlength{\parindent}{0pt}
\setlength{\parskip}{6pt plus 2pt minus 1pt}
\setlength{\emergencystretch}{3em}  % prevent overfull lines
$if(numbersections)$
\setcounter{secnumdepth}{5}
$else$
\setcounter{secnumdepth}{0}
$endif$
$if(verbatim-in-note)$
\VerbatimFootnotes % allows verbatim text in footnotes
$endif$
$if(lang)$
\ifxetex
  \usepackage{polyglossia}
  \setmainlanguage{$mainlang$}
\else
  \usepackage[$lang$]{babel}
\fi
$endif$

$if(title)$
\title{$title$}
$endif$
$if(subtitle)$
\subtitle{$subtitle$}
$endif$
\author{$for(author)$$author$$sep$ \and $endfor$}
\date{$date$}
$for(header-includes)$
$header-includes$
$endfor$

\begin{document}
$if(title)$
\maketitle
$endif$
$if(abstract)$
\begin{abstract}
$abstract$
\end{abstract}
$endif$

$for(include-before)$
$include-before$

$endfor$
$if(toc)$
{
\newpage
\hypersetup{linkcolor=black}
\setcounter{tocdepth}{$toc-depth$}
\tableofcontents
\newpage
}
$endif$
$body$

$if(natbib)$
$if(biblio-files)$
$if(biblio-title)$
$if(book-class)$
\renewcommand\bibname{$biblio-title$}
$else$
\renewcommand\refname{$biblio-title$}
$endif$
$endif$
\bibliography{$biblio-files$}

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

$endif$
$for(include-after)$
$include-after$

$endfor$
\end{document}

CSS template (style.css) :

/*
** HTML elements
*/
body {
  width:600px;
  margin:0 auto;
  padding: 0;
  color: #000;
  background-color: #fff;
  font: 76% Verdana, Arial, Helvetica, sans-serif;
}
tr.dark td, tr.light td {
  padding: 0.3em;
}
h1, h2, h3, h4, h5, h6 {
  margin-bottom: 0.5em;
  color: #005D9C;
}
h1 {
  font-size: 1.3em;
}
h2 {
  font-size: 1.2em;
}
h3, h4, h5, h6 {
  font-size: 1.1em;
}
p {
  margin-top: 0.5em;
  margin-bottom: 0.9em;
}
a {
  text-decoration: none;
  font-weight: bold;
}
a:link {
  color: #005D9C;
}
a:visited {
  color: #005D9C;
}
a:hover {
  color: #005D9C;
  text-decoration: underline;
}

.title, .author {
  text-align: center;
}

table {
  margin-left:auto;
  margin-right:auto;
}

th {
  font-size: 13px;
  font-weight: bold;
  padding: 8px;
  border-bottom: 1px solid #fff;
  color: #005D9C;
}
td {
  padding: 8px; 
  border-bottom: 1px solid #fff;
  color: #000;
  border-top: 1px solid transparent;
}
tbody tr:hover td {
  color: #fff;
  background: #005D9C;
}

img {
  width: 70%;
  height: auto;
}