%----------------------------------------------------------------------
% Packages
%\usepackage[inline]{showlabels}
%\usepackage[latin1]{inputenc}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[english]{babel}
\usepackage[font=it,labelfont=normalfont]{caption}
\usepackage[colorlinks,urlcolor=blue,linkcolor=red,citecolor=Maroon]{hyperref}
\usepackage[dvipsnames]{xcolor}
\usepackage[super]{nth}
% \usepackage[retainorgcmds]{IEEEtrantools}
\usepackage[noae]{Sweave}
\usepackage[ae,hyper]{Rd}
\usepackage{makeidx,floatflt,amsmath,amsfonts,amsbsy,enumitem,dcolumn,needspace}
\usepackage{ifthen,calc,eso-pic,everyshi,pdfcomment,framed}
\usepackage{booktabs,longtable,rotating,graphicx,subfig}
\usepackage{pdfpages,verbatim,fancyhdr,datetime,afterpage,setspace}
\usepackage[abspath]{currfile}
% \usepackage{times}
\renewcommand{\textfraction}{0.0}
\renewcommand{\topfraction}{1.0}
\renewcommand{\bottomfraction}{1.0}
\renewcommand{\floatpagefraction}{0.9}
\definecolor{blaa}{RGB}{99,99,255}
\DeclareGraphicsExtensions{.png,.pdf,.jpg}
% Make the Sweave output nicer (slightly mor compact)
\DefineVerbatimEnvironment{Sinput}{Verbatim}{fontsize=\small,fontshape=sl,formatcom=\color{BlueViolet}}
\DefineVerbatimEnvironment{Soutput}{Verbatim}{fontsize=\small,formatcom=\color{Sepia},xleftmargin=0em}
\DefineVerbatimEnvironment{Scode}{Verbatim}{fontsize=\small}
\fvset{listparameters={\setlength{\topsep}{-0.1ex}}}
\renewenvironment{Schunk}%
{\renewcommand{\baselinestretch}{0.87} \vspace{\topsep}}%
{\renewcommand{\baselinestretch}{1.00} \vspace{\topsep}}
% \renewenvironment{knitrout}
% {\renewcommand{\baselinestretch}{0.87}}
% {\renewcommand{\baselinestretch}{1.00}}
\input{useful}

%----------------------------------------------------------------------
% Set up layout of pages
\oddsidemargin 1mm
\evensidemargin 1mm
\topmargin -10mm
\headheight 8mm
\headsep 5mm
\textheight 240mm
\textwidth 165mm
%\footheight 5mm
\footskip 15mm
\renewcommand{\topfraction}{0.9}
\renewcommand{\bottomfraction}{0.9}
\renewcommand{\textfraction}{0.1}
\renewcommand{\floatpagefraction}{0.9}
\renewcommand{\headrulewidth}{0.1pt}
\setcounter{secnumdepth}{2}
\setcounter{tocdepth}{3}

%----------------------------------------------------------------------
% How to insert a figure in a .rnw file
\newcommand{\rwpre}{./graph/gr}
\newcommand{\insfig}[3]{
\begin{figure}[h]
  \centering
  \includegraphics[width=#2\textwidth]{\rwpre-#1}
% \caption{#3}
  \caption{#3\hfill\mbox{\footnotesize \textrm{\tt \rwpre-#1}}}
  \label{fig:#1}
% \afterpage{\clearpage}
\end{figure}}
\newcommand{\linput}[1]{
% \clearpage 
\afterpage{\hfill \ldots now input from \texttt{#1.tex}\\} 
\fancyfoot[OR,EL]{\footnotesize \texttt{#1.tex}} 
\input{#1}}

%----------------------------------------------------------------------
% modifying the leftbar
\renewenvironment{leftbar}[2][\hsize]
{ \def\FrameCommand
    {
        {\color{#2}\vrule width 1pt}
        \hspace{-2pt}
    }
    \MakeFramed{\hsize#1\advance\hsize-\width\FrameRestore}
}
{\endMakeFramed}
\newcommand\blob{\,\,\rule{1.2ex}{1.2ex}}
% defining the expl environment with a leftbar
\newenvironment{expl}{% 
\vspace*{-1em}
\begin{leftbar}{gray}%
  \noindent\textcolor{gray}{\textsc{code explained:}\!\!} 
}{%\blob
\end{leftbar}\ \\[1ex]}

%----------------------------------------------------------------------
% Here is the document starting with the titlepage
\begin{document}

%----------------------------------------------------------------------
% The title page
\setcounter{page}{1}
\pagenumbering{roman}
\pagestyle{plain}
\thispagestyle{empty}
% \vspace*{0.05\textheight}
\flushright
% The blank below here is necessary in order not to muck up the
% linespacing in title if it has more than 2 lines
{\Huge \bfseries \Title

}\ \\[-1.5ex]
\noindent\textcolor{blaa}{\rule[-1ex]{\textwidth}{5pt}}\\[2.5ex]
\large
\Where \\
\Dates \\
\Homepage \\
\Version \\[1em]
\normalsize
Compiled \today,\ \currenttime\\
from: \texttt{\currfileabspath}\\[1em]
% \input{wordcount}
\normalsize
\vfill
\Faculty
% End of titlepage
\newpage

%----------------------------------------------------------------------
% Table of contents
\tableofcontents
% \listoftables
% \listoffigures
\clearpage
% \begingroup
% \let\clearpage\relax
% \listoftables
% \listoffigures
% \endgroup

%----------------------------------------------------------------------
% General text layout
\raggedright
\parindent 1em
\parskip 0ex
\cleardoublepage

%----------------------------------------------------------------------
% General page style
\pagenumbering{arabic}
\setcounter{page}{1}
\pagestyle{fancy}
\renewcommand{\chaptermark}[1]{\markboth{\textsl{#1}}{}}
\renewcommand{\sectionmark}[1]{\markright{\thesection\ \textsl{#1}}{}}
\fancyhead[EL]{\bf \thepage \quad \rm \rightmark}
\fancyhead[ER]{\rm \Tit}
\fancyhead[OL]{\rm \leftmark}
\fancyhead[OR]{\rm \rightmark \quad \bf \thepage}
\fancyfoot{}
