%!TEX TS-program = xelatex
\documentclass[11pt]{scrartcl}  
%\documentclass[11pt,twocolumn]{scrartcl}  

% Note that Kieran Healy uses the swank 'Memoir' class, 
% \documentclass[11pt,article,oneside]{memoir}
% which might be worth a look. 

% -- We are in swanky unicode, XeTeX land, and must now import these packages:
% Characters
\usepackage{fontspec}
%,xltxtra,xunicode}
\usepackage[french]{babel}

% Math formules
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{geometry}

% En-tête et pied de page
\usepackage{fancyhdr}
\usepackage{hyperref}

% Police
\usepackage{libertine}
\usepackage{longtable}
\usepackage{booktabs}

% Mise en forme 
%  couleurs
\usepackage[svgnames]{xcolor}
%  page
\usepackage{a4wide}

\setlength{\parindent}{0pt}
\setlength{\parskip}{2pt plus 1pt minus 1pt}

% Définition en-tête et pied de page
\pagestyle{fancy}
\fancyhf{}
\rhead{\rightmark}
\lhead{\leftmark}
\lfoot{$title$}
\rfoot{Page \thepage}

\renewcommand{\footrulewidth}{1pt}

% Format des différents niveaux de titre
% Taille de l'espacement vertical
\RedeclareSectionCommand[beforeskip=0.1\baselineskip,afterskip=0.1\baselineskip]{section}
\RedeclareSectionCommand[beforeskip=0.1\baselineskip,afterskip=0.1\baselineskip]{subsection}
\RedeclareSectionCommand[beforeskip=0.1\baselineskip,afterskip=0.1\baselineskip]{subsubsection}

% Couleur
\makeatletter
\renewcommand{\sectionlinesformat}[4]{%
%  \@tempswafalse
  \ifstr{#1}{section}{%
    \colorbox{Black}{%
      \color{white}
      \parbox{\dimexpr\linewidth-2\fboxrule-2\fboxsep-#2}{%
        \raggedsection
        \@hangfrom{#3}{#4}%
      }%
    }%
  }{%
    \ifstr{#1}{subsection}{%
      \colorbox{Orange}{%
        \parbox{\dimexpr\linewidth-2\fboxrule-2\fboxsep-#2}{%
          \raggedsection
          \@hangfrom{#3}{#4}%
        }%
      }%
    }{%
      \ifstr{#1}{subsubsection}{%
%        \colorbox{Yellow}{%
          \color{Brown}
          \parbox{\dimexpr\linewidth-2\fboxrule-2\fboxsep-#2}{%
            \raggedsection
            \@hangfrom{#3}{#4}%
            }%
%          }%
        }
      {%
      \@hangfrom{\hskip #2#3}{#4}%
      }%
    }%
  }%
}
\makeatother

\setlength{\emergencystretch}{3em}  % prevent overfull lines
\providecommand{\tightlist}{%
  \setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}

% Paramètres tableaux
$if(tables)$
\usepackage{array}
% -- This is needed because raggedright in table elements redefines \\:
\newcommand{\PreserveBackslash}[1]{\let\temp=\\#1\let\\=\temp}
\let\PBS=\PreserveBackslash
$endif$

$if(subscript)$
\newcommand{\textsubscr}[1]{\ensuremath{_{\scriptsize\textrm{#1}}}}
$endif$

% Gestion des images
$if(graphics)$
\usepackage{graphicx}
% -- We will generate all images so they have a width \maxwidth. This means
% -- that they will get their normal width if they fit onto the page, but
% -- are scaled down if they would overflow the margins.
\makeatletter
\def\maxwidth{\ifdim\Gin@nat@width>\linewidth\linewidth
\else\Gin@nat@width\fi}
\makeatother
\let\Oldincludegraphics\includegraphics
\renewcommand{\includegraphics}[1]{\Oldincludegraphics[width=\maxwidth]{#1}}
$endif$

% Red color bold text
%\let\oldtextbf\textbf
%\renewcommand{\textbf}[1]{\textcolor{red}{\oldtextbf{#1}}}

% Section numbering.  
% Here again is a variable you can specify on the commandline
% `markdown2pdf my.txt --number-sections --xetex --template=/wherever/this/is -o my.pdf`
$if(numbersections)$
$else$
\setcounter{secnumdepth}{0}
$endif$

% Title, authors, date.
% If you specified title authors and date at the start of 
% your pandoc-markdown file, pandoc knows the 'values' of the
% variables: title authors date and fills them in.
\addtokomafont{title}{\color{Brown}}
\subject{\vspace{-4\baselineskip}}
\publishers{\vspace{-1.5\baselineskip}}
$if(title)$
\title{$title$}
$endif$
$if(author)$
\author{$author$}
$endif$
$if(date)$
\date{$date$}
$endif$

% Début du document proprement dit
\begin{document}

$if(title)$
\maketitle
\thispagestyle{empty}
$endif$

$if(toc)$
\tableofcontents
$endif$

$body$

\end{document}
