%% PhD FEUP THESIS STYLE for LaTeX2e
%% Based on "feuptese.sty" by jcl and jcf at fe.up.pt
%%
%% Faculdade de Engenharia da Universidade do Porto
%% Programa Doutoral em Engenharia Biomedica
%% Mario Saenz Espinoza
%%
%% PLEASE send improvements to mario.espinoza at fe.up.pt
%%
%% Special thanks to Prof. Joao Correia Lopes, FEUP

%% LOG
	% March 20, 2013: 
		% Added "RequirePackage{tabularx}"
		% Added "RequirePackage{array}"
		% Moved "RequirePackage{longtable}" after "RequirePackage{array}" and "RequirePackage{tabularx}"
		% Added "RequirePackage{xtab}"
		% Added "RequirePackage{tabu}"
	% March 22, 2013:
		% Added "RequirePackage{siunitx}"
			% Setup comma as decimal separator
			% Setup minimum group of three
			% Setup font to match text
	% March 28, 2013:
		% Added \bstctlcite part of bibliography
	% April 2, 2013:
		% Added \RequirePackage{arydshln}
		% Added \RequirePackage{dashrule}
			% Didn't work, took it out
		% Moved `\modulolinenumbers[2]' from the required packages section (between siunitx and hyperref)
		% Removed \Requirepackage{tabu}
		% Added \RequirePackage{ltxtable}
	% April 3, 2013:
		% Added \RequirePackage{subcaption}
	% April 5, 2013:
		% Removing pdftex option (as described by: http://tex.stackexchange.com/questions/106159/why-i-shouldnt-load-pdftex-option-with-hyperref)
			% in graphicx package
			% in hyperref package
	% April 6, 2013:
		% Removed \RequirePackage{ltxtable}
	% April 10, 2013:
		% Added \RequirePackage{tikz}
	% April 11, 2013:
		% Removed \sisetup{detect-all}
	% April 12, 2013:
		% Added \RequirePackage{floats}
		% Added \RequirePackage{pdfpages}
	% April 22, 2013:
		% Added \RequirePackage{placeins}
		% Added option 'linenum' to include line numbers
		% Added \RequirePackage{grffile}
		% Removed option 'working'
	% April 28, 2013:
		% Added \RequirePackage{ltxtable}
		% Added \RequirePackage{adjustbox}
		% Removed \RequirePackage{ltxtable} (not working along with other packages)
		% Added \RequirePackage{wrapfig}
		% Removed \RequirePackage{wrapfig} (not treated as floats)
	% April 29, 2013:
		% Added \RequirePackage{amsmath}
	% May 1, 2013:
		% Added \RequirePackage{xcolor}
		% Added \RequirePackage{pgfgantt}
		% Added \RequirePackage{pdflscape}
	% May 3, 2013:
		% Added \usetikzlibrary{decorations.pathreplacing}
		% Added \newcommand{citeauthorlist}
		% Added \newcommand{citetlist}
	% May 7, 2013:
		% Added \RequirePackage{bookmark}
	% May 12, 2013:
		% Added \RequirePackage[inline]{enumitem}
	% May 15, 2013:
		% Added \newcommand*{\compressitem}{\@minipagetrue}
		% Added \RequirePackage{booktabs}
	% May 17, 2013:
		% Added \RequirePackage{tabuarly}
		% Removed \RequirePackage{arydshln} (incompatibility with tabulary)
	% May 24, 2013:
		% Added '~' on backrefs
	% May 28,2013:
		% Added fix for 'showframe' of geometry package
	% May 30, 2013:
		% Added \urlstyle{same}
	% May 31, 2013:
		% Added \usetikzlibrary{positioning,fit,backgrounds}
		% Added \usetikzlibrary{matrix}
	% June 12, 2013:
		% Added \RequirePackage{listings}	
	% June 13, 2013:
		% Added \RequirePackage{pgfplots}	
%%

\NeedsTeXFormat{LaTeX2e}
\def \filename {feupphdteses.sty}
\def \filedate {2013/06/13}
\def \fileversion {1.26}	
\ProvidesPackage {feupphdteses}[\filedate\space\fileversion\space]
\typeout {Document Style `FEUP PhD Teses' \fileversion\space <\filedate> by Mario Saenz Espinoza}


%%%%%%%%%%%%%%%%%%%%%%%%% Document Class %%%%%%%%%%%%%%%%%%%%%%%%%

%% Make sure that report has been loaded and pdf package is in place
\@ifundefined{chapter}{\@latexerr{The `feupphdteses' option should be used only with the `report' document style}{}}{}
\RequirePackage{ifpdf}
\ifpdf
	\relax
\else
  \@latexerr{The 'feupphdteses' package may only be used with pdflatex}{Please process this file with pdflatex.}
\fi


%%%%%%%%%%%%%%%%%%%%%%%%% Encoding %%%%%%%%%%%%%%%%%%%%%%%%%

%% Ensure correct font encoding
\RequirePackage[T1]{fontenc}
\RequirePackage[utf8]{inputenc}


%%%%%%%%%%%%%%%%%%%%%%%%% Declare General Input Options %%%%%%%%%%%%%%%%%%%%%%%%%

%% Option jury
\newif\if@jury
\@juryfalse
\DeclareOption{jury}{\@jurytrue}

%% Option final
\newif\if@final
\@finalfalse
\DeclareOption{final}{\@finaltrue}

%% Option backrefs
\newif\if@backrefs
\@backrefsfalse
\DeclareOption{backrefs}{\@backrefstrue}

%% Option for print on paper
\newif\if@onpaper
\@onpaperfalse
\DeclareOption{onpaper}{\@onpapertrue}

%% Option libre
\newif\if@libre
\@librefalse
\DeclareOption{libre}{\@libretrue}

%% Option linenum
\newif\if@linenum
\@linenumfalse
\DeclareOption{linenum}{\@linenumtrue}

%% Doctoral Programs at FEUP

	% Option PRODEB
	\newif\if@prodeb
	\@prodebfalse
	\DeclareOption{prodeb}{\@prodebtrue}

	% Option PRODEF
	\newif\if@prodef
	\@prodeffalse
	\DeclareOption{prodef}{\@prodeftrue}

	% Option PDEQB
	\newif\if@pdeqb
	\@pdeqbfalse
	\DeclareOption{pdeqb}{\@pdeqbtrue}
	
	% Option PDEEC
	\newif\if@pdeec
	\@pdeecfalse
	\DeclareOption{pdeec}{\@pdeectrue}


%%%%%%%%%%%%%%%%%%%%%%%%% Internal Options %%%%%%%%%%%%%%%%%%%%%%%%%

%% Internal option: committeepage
\newif\if@committeepage
\@committeepagefalse

%% Internal option: provisional
\newif\if@provisional
\@provisionalfalse

%% Internal option: uselineno
\newif\if@uselineno
\@uselinenofalse

\ProcessOptions\relax


%%%%%%%%%%%%%%%%%%%%%%%%% Global Packages of the document %%%%%%%%%%%%%%%%%%%%%%%%%

%% General packages
\RequirePackage[english]{babel}
\RequirePackage[outer=25mm,inner=35mm,vmargin=20mm,includehead,includefoot,headheight=15pt]{geometry}		% Geometry package, %showframe option
\RequirePackage{xcolor}																											% Different Colors
\RequirePackage{mathptmx}																										% Font:Times
\RequirePackage[scaled]{couriers}  																					% Font: Courier
\RequirePackage[scaled]{helvet}    																					% Font: Helvetica
\RequirePackage{graphicx}																										% Graphics
\RequirePackage{grffile}																										% larger range of file names (dots and spaces)
\RequirePackage{amsmath}																										% Math and equations
\RequirePackage{tikz}																												% To make graphics
\RequirePackage{pgfplots}																										% To import figures from Matlab
\RequirePackage{url}																												% To handle URLs
\RequirePackage{setspace}																										% Controls line spacing
\RequirePackage{makeidx}																										% Index
\RequirePackage[tableposition=top,figureposition=bottom]{caption}						% Tables' captions
\RequirePackage{multirow}																										% Tables with multiple rows
\RequirePackage{eurosym}																										% Currency: Euro
\RequirePackage{array}																											% Needed for 'tabularx'
\RequirePackage{tabularx}																										% Tables' width can be set
\RequirePackage{tabulary}																										% Columns' width can be set to length
\RequirePackage{longtable}																									% Tables can break into two pages
\RequirePackage[pagewise,switch*]{lineno}																		% Line numbers
\RequirePackage{siunitx}																										% SI units
\RequirePackage{float}																											% To be able to use the 'Here' specification
\RequirePackage{subcaption}																									% Subcaptions in subfigures
\RequirePackage{adjustbox}																									% Alignment of cells in tables
\RequirePackage[inline]{enumitem}																						% Itemize, enumerate, lists, etc.
\RequirePackage[section]{placeins}																					% To prevent floats going over different sections
\RequirePackage{booktabs}																										% To use toprule, midrule, bottomrule
\RequirePackage{pgfgantt}																										% Create Gantt diagrams
\RequirePackage{pdflscape}																									% Landscape mode
\RequirePackage{pdfpages}																										% To include .pdf (papers) at the end as appendix
\RequirePackage{listings}																										% To include code
\RequirePackage[pdfpagelabels,bookmarks,hyperindex,hyperfigures]{hyperref}	% Hyper References
\RequirePackage{bookmark}																										% For resetting bookmarks

%% Special declarations

	%Line Numbers
	\modulolinenumbers[2]		% Set counter multiple
	
	%Graphics
	\DeclareGraphicsExtensions{.pdf,.jpeg,.png,.jpg}		% Consider all these extensions as graphics
	
	%SI units
	\sisetup{output-decimal-marker = {.}} % Use comma as decimal point (as described by the SI)
	\sisetup{group-minimum-digits = 3}		% \num{1234} should be 1 234, not 1234 (as described by the SI)
	%\sisetup{separate-uncertainty}				% for using \pm in the uncertainty instead of ()
	
	%Tikz Calendar
	%\usetikzlibrary{calendar,shapes.geometric} (Podria llegar a ser util para calendarios)
	\usetikzlibrary{decorations.pathreplacing}
	\usetikzlibrary{positioning,fit,backgrounds}
	\usetikzlibrary{matrix}
	
	%Colors
	\definecolor{barblue}{RGB}{153,204,254}
	
	%URL
	\urlstyle{same}


%%%%%%%%%%%%%%%%%%%%%%%%% Title, Program, Degree, Date, Supervisor, etc. Spaces %%%%%%%%%%%%%%%%%%%%%%%%%

%% University
\gdef\@school{Faculdade de Engenharia da Universidade do Porto}
\def\school#1{\gdef\@school{#1}}

%% Thesis date
\gdef\@thesisdate{\today}
\def\thesisdate#1{\gdef\@thesisdate{#1}}

%% Program
	% Programa Doutoral em Engenharia Biomedica
	\if@prodeb
		\gdef\@program{Programa Doutoral em Engenharia Biom\'{e}dica}
		\gdef\@department{Biomedical Engineering}
	\fi

	% Programa Doutoral em Engenharia Fisica
	\if@prodef
		\gdef\@program{Programa Doutoral em Engenharia F\'{i}sica}
		\gdef\@department{Physics Engineering}
	\fi

	% Programa Doutoral em Engenharia Quimica e Biologica
	\if@pdeqb
		\gdef\@program{Programa Doutoral em Engenharia Qu\'{i}mica e Biol\'{o}gica}
		\gdef\@department{Chemical Engineering}
	\fi
	
	% Programa Doutoral em Engenharia Electrotecnica e de Computadores
	\if@pdeec
		\gdef\@program{Programa Doutoral em Engenharia Electrot\'{e}cnica e de Computadores}
		\gdef\@department{Electronic \& Computer Engineering}
	\fi

%% Degree
	\gdef\@degree{Doctor Philosophiae in \@department}
	\def\degree#1{\gdef\@degree{#1}}

%% Provisional text space
\gdef\@provisionaltext{\@empty}
\def\additionalfronttext#1{\gdef\@provisionaltext{#1}}

%% Committee text
\newbox\@vivatext 

	\def\committeetext#1{%
  \setbox\@vivatext\vbox
  {\Large\noindent #1
  \par\vskip 0.5\baselineskip}}
	
	% For more than 1 member
	\def\committeemember#1#2{%
  \setbox\@vivatext\vbox
  {\unvbox\@vivatext
  \vskip 0.5\baselineskip%
  \par%
  {\noindent\large #1: #2}}}
	
	%Space for signatures
	\def\signature{%
  \setbox\@vivatext\vbox
  {\unvbox\@vivatext
  \vskip 2\baselineskip%
  {\hfill \rule{0.75\linewidth}{0.2mm}}
  \vskip 2\baselineskip}}

%% Supervisor text
\newbox\@supervisorstext 

\def\supervisor#1#2{%
  \setbox\@supervisorstext\vbox%
  {\unvbox\@supervisorstext
  \vskip 0.5\baselineskip%
  \par%
  \center{\noindent\large #1: #2}}}
	

%%%%%%%%%%%%%%%%%%%%%%%%% Versions & Options Do %%%%%%%%%%%%%%%%%%%%%%%%%

%% Working version
\if@final
  \relax
\else
	\if@jury
		\relax
	\else
		\additionalfronttext{Working version}
	\fi
\fi

%% Jury version
\if@jury
	\def\committeetext#1{\gdef\@committeetext{#1}}
	\@committeepagetrue
	\additionalfronttext{For Jury Evaluation}
\fi

%% Final version
\if@final
	\additionalfronttext{}
	\@committeepagetrue
\fi

%% Libre version
\if@libre
	\@committeepagefalse
\fi

%% Backrefs version
\if@backrefs
	\RequirePackage[hyperpageref]{backref}
	\renewcommand*{\backref}[1]{}
	\renewcommand*{\backrefalt}[4]{%
			\ifcase #1 %
			\or
			{\footnotesize Cited on page~#2.}%
			\else
				{\footnotesize Cited on pages~#2.}%
			\fi
	}
\fi

%% Line number version
\if@linenum
	\@uselinenotrue
\fi

%% Onpaper version
\if@onpaper
  \hypersetup{%
    plainpages=false, 
    pdfpagelayout=SinglePage,
    bookmarksopen=false,
    bookmarksnumbered=true,
    breaklinks=true,
    linktocpage,
    colorlinks=false,
    pdfborder=0 0 0
		}
\else
  \hypersetup{%
    plainpages=false, 
    pdfpagelayout=SinglePage,
    bookmarksopen=false,
    bookmarksnumbered=true,
    breaklinks=true,
    linktocpage,
    colorlinks=true,
    linkcolor=blue,
    urlcolor=blue,
    citecolor=blue,
    anchorcolor=green
		}
\fi


%%%%%%%%%%%%%%%%%%%%%%%%% Copyright Info %%%%%%%%%%%%%%%%%%%%%%%%%

\gdef\@permission{\null}
\def\permission#1{\gdef\@permission{#1}}

%% define a copyrightnotice variable and initialize it
\gdef\@copyrightnotice{}
\def\copyrightnotice#1{\gdef\@copyrightnotice{\copyright\ #1\par\@permission}}

\copyrightnotice{\@author: \@thesisdate}


%%%%%%%%%%%%%%%%%%%%%%%%% Title %%%%%%%%%%%%%%%%%%%%%%%%%

\def\maketitle{%
\newbox\@crestbox

%% Title page
\begin{titlepage}

\hbox to \textwidth
{
	\setbox\@crestbox=\vbox{\includegraphics[scale=0.82]{uporto-feup.pdf}}	% University Logo
	%\setbox\@crestbox=\vbox{\includegraphics[width=90mm]{uporto-feup.pdf}}	% University Logo
	\box\@crestbox%
}
\vfill%

%\vspace*{20mm}%
{\def\baselinestretch{1.2}\Huge\bf \@title \par}	% Title
\vskip 25mm%

{\huge\bf \@author}																% Author
\vskip 10mm%


{\Large\textsc \@provisionaltext}									% Provisional text
\vfill

\unvbox\@supervisorstext													% Supervisor
\vskip 5mm

{\large \@program}																% Program
%\vskip 30mm
\vfill

\@thesisdate																			% Date

\end{titlepage}


%%%%%%%%%%%%%%%%%%%%%%%%% Copyright Page %%%%%%%%%%%%%%%%%%%%%%%%%

%% copyright page
\ifx\@copyrightnotice\@empty
  \relax
\else
  \vspace*{\fill}
  \par
  \begin{center}
    \@copyrightnotice
  \end{center}
\fi
\clearpage
} % maketitle


%%%%%%%%%%%%%%%%%%%%%%%%% Second Page %%%%%%%%%%%%%%%%%%%%%%%%%
%\newpage
\def\committeepage
{%
	\cleardoublepage
	\thispagestyle{empty}
	\begin{center}%
		\null\vskip 12mm
		{\Large\textnormal{\@school}}\\[16mm]
		{\LARGE\bf \@title}\\[16mm]
		{\Large\bf \@author}\\[16mm]
		%{\Large \@degree}%
	\end{center}
	\vspace*{\fill}
	
	\begin{center}
		Dissertation submitted to Faculdade de Engenharia da Universidade do Porto\\ to obtain the degree of
		\vskip 3mm
		\large{\textbf{Doctor Philosophiae in \@department}}
	\end{center}
	
	\unvbox\@supervisorstext
	\vspace*{\fill}
	
	\unvbox\@vivatext
	
	\begin{center}
		\@thesisdate
	\end{center}
}

%% Second Title Page Grooming 
\def\titlepage
{
	\cleardoublepage\centering
  \thispagestyle{empty}
  \parindent 0pt \parskip 10pt plus 1fil minus 1fil
  \def\baselinestretch{1}\@normalsize\vbox to \vsize\bgroup\vbox to 9in\bgroup
}

%% The \kern0pt pushes any depth into the height.  Thanks to Richard Stone.
\def\endtitlepage{\par\kern 0pt\egroup\vss\egroup\clearpage}


%%%%%%%%%%%%%%%%%%%%%%%%%% General Layout %%%%%%%%%%%%%%%%%%%%%%%%%

%% Avoid widows and orphans
\clubpenalty=6000
\widowpenalty=6000

%% Headers
\RequirePackage{fancyhdr}
\pagestyle{fancy}

%% Remember chapter title
\renewcommand{\chaptermark}[1]{\markboth{#1}{#1}}

%% Section number and title
\renewcommand{\sectionmark}[1]{\markright{{\ifnum \c@secnumdepth >\z@ \thesection\ \fi #1}}}

%% Two sided printinig
\if@twoside
  \lhead[\fancyplain{}{\rm\thepage}]{\fancyplain{}{\sl\rightmark}}
  \chead{}
  \rhead[\fancyplain{}{\sl\leftmark}]{\fancyplain{}{\rm\thepage}}
  \lfoot{}
  \cfoot{\fancyplain{\rm\thepage}{}}
  \rfoot{}
\else
  \lhead{\fancyplain{}{\sl\ifodd\c@page\rightmark\else\leftmark\fi}}
  \chead{}
  \rhead[]{\fancyplain{}{\rm\thepage}}
  \lfoot{}
  \cfoot{\fancyplain{\rm\thepage}{}}
  \rfoot{}
\fi

%% Renew some commands
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}
\addtolength{\headheight}{0.5pt}

%% changes to report.sty
\lineskip 1pt
\normallineskip 1pt
\onehalfspacing										% One and a half line spacing
\setcounter{secnumdepth}{3}      	% To number subsubsections
\setcounter{tocdepth}{2}         	% Sets the depth of sectional units listed in the toc
\setcounter{topnumber}{1}        	% Maximum number of floats at top of page
\renewcommand{\topfraction}{0.8} 	% Maximum fraction of the page occupied by floats

%% Fix pagenumbering so that it works even when the \pagenumbering command is in a file that has not been \include-d
\def\c@pagenumbering{arabic}
\def\@pagenumbering#1{\gdef\thepage{\csname @#1\endcsname \c@page}
\gdef\c@pagenumbering{#1}}
\def\pagenumbering#1{\global\c@page\@ne \@pagenumbering{#1}}


%%%%%%%%%%%%%%%%%%%%%%%%%% New Commands & Environments %%%%%%%%%%%%%%%%%%%%%%%%%

%% Some new commands
\newcommand{\Activate@lineno}{\if@uselineno\linenumbers\switchlinenumbers*\fi}
\newcommand{\@StartPrelim}{\cleardoublepage\pagenumbering{roman}\setcounter{page}{1}\pagestyle{fancy}}
\newcommand{\clearemptydoublepage}{\newpage{\pagestyle{empty}\cleardoublepage}}
\newcommand{\StartBody}{\cleardoublepage\pagenumbering{arabic}\setcounter{page}{1}\pagestyle{fancy}\Activate@lineno}
\newcommand{\@StartDoc}{\setcounter{page}{-3}\pagestyle{empty}\pdfbookmark[0]{Front Page}{init}}
\newcommand{\PrintIndex}{\cleardoublepage\phantomsection\addcontentsline{toc}{chapter}{Index}\begin{singlespace}\printindex\end{singlespace}}
\newcommand*{\compressitem}{\@minipagetrue}

%% Some new columns
\newcolumntype{P}[1]{>{\endgraf\vspace*{-\baselineskip}}p{#1}} % For itemize inside a longtable

%% Prolog
\newenvironment{Prolog}
{
	\maketitle
	\if@committeepage
		\committeepage
	\fi
	\@StartPrelim
	\begin{singlespace}
}
{\end{singlespace}}


%%%%%%%%%%%%%%%%%%%%%%%%% Bibbliography %%%%%%%%%%%%%%%%%%%%%%%%%

%% Bibliography Style
\RequirePackage[]{natbib}

\newcommand\MidSep{, }% separator for two elements, not the last two (for citeauthorlist and citetlist)
\newcommand\LastSep{ and }% separator for last two elements

%citeauthorlist
\newcommand\citeauthorlist[1]{%
  \let\last@elem\relax
  \let\last@sep\relax
  \@for\@list:=#1\do{%
    \ifx\last@elem\relax\else
      \ifx\last@sep\relax
        \def\last@sep{\LastSep}% the separator between the last two elements should is "and"
      \else\MidSep  % the separator between two elements (not the two last) is a comma
      \fi
      \citeauthor{\last@elem}%
    \fi
    \let\last@elem\@list
  }% the last element of the list:
  \ifx\last@elem\relax\else
    \last@sep\citeauthor{\last@elem}%
  \fi
}

%citetlist
\newcommand\citetlist[1]{%
  \let\last@elem\relax
  \let\last@sep\relax
  \@for\@list:=#1\do{%
    \ifx\last@elem\relax\else
      \ifx\last@sep\relax
        \def\last@sep{\LastSep}% the separator between the last two elements should is "and"
      \else\MidSep  % the separator between two elements (not the two last) is a comma
      \fi
      \citet{\last@elem}%
    \fi
    \let\last@elem\@list
  }% the last element of the list:
  \ifx\last@elem\relax\else
    \last@sep\citet{\last@elem}%
  \fi
}

%%%citeplist  %Still work in progress
%%\newcommand\citeplist[1]{%
  %%\let\last@elem\relax
  %%\let\last@sep\relax
  %%\@for\@list:=#1\do{%
    %%\ifx\last@elem\relax\else
      %%\ifx\last@sep\relax
        %%\def\last@sep{\LastSep}% the separator between the last two elements should is "and"
      %%\else\MidSep  % the separator between two elements (not the two last) is a comma
      %%\fi
      %%\citep{\last@elem}%
    %%\fi
    %%\let\last@elem\@list
  %%}% the last element of the list:
  %%\ifx\last@elem\relax\else
    %%\last@sep\citep{\last@elem}%
  %%\fi
%%}

\def\bstctlcite{\@ifnextchar[{\@bstctlcite}{\@bstctlcite[@auxout]}}
\def\@bstctlcite[#1]#2{\@bsphack
	\@for\@citeb:=#2\do{%
		\edef\@citeb{\expandafter\@firstofone\@citeb}%
		\if@filesw\immediate\write\csname #1\endcsname{\string\citation{\@citeb}}\fi}%
\@esphack}

%%%%%%%%%%%%%%%%%%%%%%%%% Fixes %%%%%%%%%%%%%%%%%%%%%%%%%

% 'showframe' of geometry package
\newcommand*{\gmshow@textheight}{\textheight}
\newdimen\gmshow@@textheight
\g@addto@macro\landscape{%
  \gmshow@@textheight=\hsize
  \renewcommand*{\gmshow@textheight}{\gmshow@@textheight}%
}
\def\Gm@vrule{%
  \vrule width 0.2pt height\gmshow@textheight depth\z@
}%

%%%%%%%%%%%%%%%%%%%%%%%%% Go, go, go! %%%%%%%%%%%%%%%%%%%%%%%%%
\AtBeginDocument{\@StartDoc}

\endinput