% $Id: dmucthesis.cls,v 1.8 2007/12/10 18:04:49 pedro Exp $
% Version 1
% Novembro de 2007 (Pedro Quaresma, Dep. Matematica, Universidade de Coimbra)
%
% based on the jasthesis.cls
%

\RequirePackage{graphicx}
\RequirePackage{ifpdf}

\typeout{ }
\typeout{You can probably use this in much earlier versions of LaTeX2e}
\NeedsTeXFormat{LaTeX2e}[2000/06/01]
\ProvidesClass{dmucthesis}[2007/11/23 v1 (www.mat.uc.pt/~pedro)]
\RequirePackage{ifthen}

\typeout{ }
\typeout{dmucthesis.cls 2007/11/23 v1 (www.mat.uc.pt/~pedro)}
\typeout{ }
\typeout{This LaTeX class conforms to the requirements of}
\typeout{a thesis to be submitted to the Department of Mathematics, University of Coimbra}
\typeout{ }

\typeout{ }
\typeout{Checking for option 'dmucheads' to have section numbering in the margin}
\newboolean{dmuc@heads} \setboolean{dmuc@heads}{false}
\DeclareOption{dmucheads}{
  \renewcommand{\@seccntformat}[1]{{
      \protect\makebox[0pt][r]{\@nameuse{the#1}.~}}}
  \setboolean{dmuc@heads}{true}}
\typeout{ }
\typeout{Checking for option 'dmuconeside' to force singlesided}
\newboolean{dmuc@oneside} \setboolean{dmuc@oneside}{false}
\DeclareOption{dmuconeside}{\setboolean{dmuc@oneside}{true}}

\typeout{ }
\typeout{Checking for option 'dmucref' to have references in brackets}
\typeout{and with page numbers if a forward reference or more than two pages before} 
\DeclareOption{dmucref}{
 \newcommand{\refdis}{2}
 \newcommand\pagerefname{p.}
 \let\dmuc@ref=\ref
 \newcounter{reftmp}
 \renewcommand{\ref}[1]{%
   \setcounter{reftmp}{\value{page}}%
   \addtocounter{reftmp}{-\refdis}%
  \ifthenelse{\pageref{#1}>\value{page} \or \pageref{#1} < \value{reftmp}}%
  {(\dmuc@ref{#1})(\pagerefname\pageref{#1})}{(\dmuc@ref{#1})}%
 }
}




\ProcessOptions*

\typeout{ }
\ifthenelse{\boolean{dmuc@oneside}}{
 \typeout{11pt text, A4 paper, singlesided}
 \LoadClass[11pt,a4paper,oneside]{book}
}
{
 \typeout{11pt text, A4 paper, twosided}
 \LoadClass[11pt,a4paper,twoside]{book}
}

\typeout{ }
\typeout{linespacing set at 1.5}
\renewcommand{\baselinestretch}{1.5}

\typeout{}
\typeout{Command 'sectionname' created so you can translate the oneside heading (like 'chaptername')}
\newcommand{\sectionname}{Section}

\typeout{ }
\typeout{Centre page number at bottom, left heading section, right heading chapter}
\typeout{Margins: Binding 40mm, others 20mm}
\let\dmuc@headings=\ps@headings
\newcommand{\ps@dmucthesis}{\dmuc@headings
 \ifthenelse{\boolean{dmuc@heads}}{
  \ifthenelse{\boolean{dmuc@oneside}}{
   \renewcommand{\@evenhead}{}
   \renewcommand{\@oddhead}{\ifthenelse{\value{chapter}=0}{}{
       \begin{minipage}{\textwidth}
         \normalfont\bfseries\sffamily\hfill%
         \ifthenelse{\equal{\@chapapp}{\appendixname}}
         {\appendixname~\thechapter~~\dmuc@eventext\\}         
         {\chaptername~\thechapter~~\dmuc@eventext\\}
         \normalfont\sffamily\mbox{}\hfill\sectionname~\thesection~~\dmuc@oddtext
         \\[-1em]\rule{0mm}{1.5em}\rule{\textwidth}{0.6pt}
       \end{minipage}}}
 }
 {
   \renewcommand{\@evenhead}{\ifthenelse{\value{chapter}=0}{}{
       \begin{minipage}{\textwidth}
         \normalfont\bfseries\sffamily%
         \ifthenelse{\equal{\@chapapp}{\appendixname}}%
         {\appendixname~\thechapter~~\dmuc@eventext\hfill
         \\[-1em]\rule{0mm}{1.5em}\rule{\textwidth}{0.6pt}}%
         {\chaptername~\thechapter~~\dmuc@eventext\hfill
         \\[-1em]\rule{0mm}{1.5em}\rule{\textwidth}{0.6pt}}
       \end{minipage}}}
   \renewcommand{\@oddhead}{\ifthenelse{\value{chapter}=0}{}{
       \begin{minipage}{\textwidth}
         \normalfont\bfseries\sffamily\hfill\thesection~~\dmuc@oddtext
         \\[-1em]\rule{0mm}{1.5em}\rule{\textwidth}{0.6pt}
       \end{minipage}}}
 }
}
{
  \renewcommand{\@evenhead}{\leftmark\hfill}
  \renewcommand{\@oddhead}{\hfill\rightmark}
}
\renewcommand{\@evenfoot}{\hfill\thepage\hfill}
\renewcommand{\@oddfoot}{\@evenfoot}

\setlength{\voffset}{-1in}
\setlength{\hoffset}{-1in}
\setlength{\oddsidemargin}{45mm}
\setlength{\evensidemargin}{20mm}
\ifthenelse{\boolean{dmuc@heads}}{
  \addtolength{\evensidemargin}{5mm}
  \ifthenelse{\boolean{dmuc@oneside}}{\setlength{\headheight}{2em}}{\setlength{\headheight}{1em}}
  \setlength{\headsep}{2em}
}{}
\setlength{\topmargin}{\evensidemargin}\addtolength{\topmargin}{-1cm}
\setlength{\textwidth}{\paperwidth} \addtolength{\textwidth}{-\oddsidemargin}
\addtolength{\textwidth}{-\evensidemargin}
\setlength{\textheight}{\paperheight} \addtolength{\textheight}{-\topmargin}
\addtolength{\textheight}{-25mm} % space for page number
\addtolength{\textheight}{-\headheight}
\addtolength{\textheight}{-\headsep}
}

% O contador numcap dá-nos o número de capítulos que já existem e
% serve como forma de colocar a zero e em numeração árabe o contador
% das páginas assim que se entra no primeiro capítulo.
\newcounter{numcap}
\setcounter{numcap}{0}

\newif\ifcoorientador
\coorientadorfalse % assume-se à partida que não há nenhum co-orientador

\newif\ifvogais
\vogaisfalse % assume-se à partida que só há um vogal

\newcommand{\ps@dmuctitlepage}{\dmuc@headings
 \renewcommand{\@evenhead}{}
 \renewcommand{\@oddhead}{}
 \renewcommand{\@evenfoot}{}
 \renewcommand{\@oddfoot}{}

 \setlength{\headheight}{0mm}
 \setlength{\headsep}{\footskip}
 \setlength{\voffset}{-1in}
 \setlength{\hoffset}{-1in}
 \setlength{\oddsidemargin}{45mm}
 \setlength{\evensidemargin}{20mm}
 \setlength{\topmargin}{\evensidemargin}
 \setlength{\textwidth}{\paperwidth} \addtolength{\textwidth}{-\oddsidemargin}
                                     \addtolength{\textwidth}{-\evensidemargin}
 \setlength{\textheight}{\paperheight} \addtolength{\textheight}{-2\topmargin}
                                       \addtolength{\textheight}{-\headheight}
                                       \addtolength{\textheight}{-\headsep}
 \renewcommand{\baselinestretch}{1}
}



\newcommand{\dmuc@eventext}{}
\newcommand{\dmuc@oddtext}{}

\pagestyle{dmucthesis}

\newcommand{\dmucendofchapter}{\ifthenelse{\isodd{\value{page}}}{\newpage\thispagestyle{dmuctitlepage}}{}}

\newcommand{\dmuc@titulo}{}
\newcommand{\thesistitulo}[1]{\renewcommand{\dmuc@titulo}{#1}}

\newcommand{\dmuc@autor}{}
\newcommand{\thesisautor}[1]{\renewcommand{\dmuc@autor}{#1}}

\newcommand{\dmuc@grau}{}
\newcommand{\thesisgrau}[1]{\renewcommand{\dmuc@grau}{#1}}

\newcommand{\dmuc@datarealizacao}{}
\newcommand{\thesisdatarealizacao}[1]{\renewcommand{\dmuc@datarealizacao}{#1}}

\newcommand{\dmuc@presidente}{}
\newcommand{\thesispresidente}[1]{\renewcommand{\dmuc@presidente}{#1}}

\newcommand{\dmuc@orientador}{}   
\newcommand{\thesisorientador}[1]{\coorientadorfalse\renewcommand{\dmuc@orientador}{#1}}

\newcommand{\dmuc@coorientadorA}{}
\newcommand{\thesiscoorientadorA}[1]{\coorientadortrue\renewcommand{\dmuc@coorientadorA}{#1}}

\newcommand{\dmuc@coorientadorB}{}
\newcommand{\thesiscoorientadorB}[1]{\coorientadortrue\renewcommand{\dmuc@coorientadorB}{#1}}

\newcommand{\dmuc@vogalA}{}
\newcommand{\thesisvogalA}[1]{\vogaisfalse\renewcommand{\dmuc@vogalA}{#1}}

\newcommand{\dmuc@vogalB}{}
\newcommand{\thesisvogalB}[1]{\vogaistrue\renewcommand{\dmuc@vogalB}{#1}}


\newcommand{\thesismaketitle}{
   \thispagestyle{dmuctitlepage}\addtocounter{page}{-4}
   \mbox{}\begin{minipage}[0mm]{\textwidth}
     {\ }
     \vspace*{3.5cm}

     \hspace{.5cm}{%
     \setlength{\unitlength}{1cm}
     \begin{picture}(10,6)
       \put(0,0){\makebox(10,6){
           \begin{minipage}{10cm}
             \centering{\Large\sffamily\bfseries\dmuc@titulo}\linebreak{\ }\linebreak
             \centering{\large\sffamily\dmuc@autor}
           \end{minipage}}}      
     \end{picture}
   }
   \end{minipage}
   \newpage\thispagestyle{dmuctitlepage}\mbox{\ }\pagebreak

  \thispagestyle{dmuctitlepage}
  \mbox{}\begin{minipage}[0mm]{\textwidth}
    \ifpdf
    \centerline{\includegraphics[width=\textwidth]{cabecalho_final.jpg}}
    \else
    \centerline{\includegraphics[width=\textwidth]{cabecalho_final.eps}}
    \fi
    % Para considerar o caso de títulos muito grandes vai-se colocar o
    % mesmo numa minipage
    \vspace{5cm}
    \begin{minipage}{\textwidth}
      \centering{\huge\sffamily\bfseries\dmuc@titulo}
    \end{minipage}
  \end{minipage}

  \vfill
  \begin{minipage}{\textwidth}
    \centerline{\Large\sffamily\dmuc@autor}
  \end{minipage}

  \vfill
  \begin{minipage}{\textwidth}
    \centerline{Disserta{\c c}{\~a}o para a obten{\c c}{\~a}o do Grau
      de {\bfseries Mestre em  Matem{\'a}tica}}
    \centerline{{\'A}rea de Especializa{\c c}{\~a}o em \bfseries\dmuc@grau}
  \end{minipage}

  \vfill
  \mbox{}
  \begin{minipage}{\textwidth}
    \begin{center}
      \begin{tabular}{ll}
        \multicolumn{2}{p\textwidth}{\centerline{\large\bfseries J{\'u}ri}}\\
        {\bfseries Presidente:} & \dmuc@presidente\\
        \ifcoorientador
          {\bfseries Co-Orientador:} & \dmuc@coorientadorA\\
          {\bfseries Co-Orientador:} & \dmuc@coorientadorB\\
        \else
          {\bfseries Orientador:} & \dmuc@orientador\\
        \fi
        \ifvogais % dois vogais
          {\bfseries Vogais:} & \dmuc@vogalA\\
                              & \dmuc@vogalB\\
        \else
          {\bfseries Vogal:} & \dmuc@vogalA\\
        \fi
      \end{tabular}
    \end{center}
    \centerline{\large\bfseries\dmuc@datarealizacao}
  \end{minipage}
  \newpage\thispagestyle{dmuctitlepage}\mbox{}\pagebreak
}

\newcommand{\dmuc@resumo}{}
\newcommand{\thesisresumo}[1]{\renewcommand{\dmuc@resumo}{#1}}

\newcommand{\dmuc@palavraschave}{}
\newcommand{\thesispalavraschave}[1]{\renewcommand{\dmuc@palavraschave}{#1}}

\newcommand{\dmuc@abstract}{}
\newcommand{\thesisabstract}[1]{\renewcommand{\dmuc@abstract}{#1}}

\newcommand{\dmuc@keywords}{}
\newcommand{\thesiskeywords}[1]{\renewcommand{\dmuc@keywords}{#1}}

\newcommand{\thesismakeabstract}{
  \cleardoublepage
  \pagenumbering{roman}
  \renewcommand{\baselinestretch}{1}\normalfont
  \mbox{}\vspace{6em}\\
  {\Huge\bfseries Resumo}
 \begin{quotation}\dmuc@resumo\end{quotation}
 \mbox{}\vspace{1em}
 {\bfseries Palavras Chave:} \dmuc@palavraschave
 \mbox{}\vspace{3em}\\
 {\Huge\bfseries Abstract}
 \begin{quotation}\dmuc@abstract\end{quotation}
 \mbox{}\vspace{1em}
 {\bfseries Keywords:} \dmuc@keywords
 \par\renewcommand{\baselinestretch}{1.5}\normalfont
 \newpage\thispagestyle{dmuctitlepage}\mbox{}\pagebreak
}


\newcommand{\thesisdedication}[1]{
 \cleardoublepage
 \renewcommand{\baselinestretch}{1}\normalfont
 \mbox{}\vspace{6em}\\
 {\Huge\bfseries Dedicat{\'o}ria}
 \begin{quotation}\slshape#1\end{quotation}
 \par\renewcommand{\baselinestretch}{1.5}\normalfont
 \newpage\thispagestyle{dmuctitlepage}\mbox{}\pagebreak
}

\newcommand{\thesisagradecimentos}[1]{
 \cleardoublepage
 \renewcommand{\baselinestretch}{1}\normalfont
 \mbox{}\vspace{6em}\\
 {\Huge\bfseries Agradecimentos}
 \begin{quotation}\slshape#1\end{quotation}
 \par\renewcommand{\baselinestretch}{1.5}\normalfont
 \newpage\thispagestyle{dmuctitlepage}\mbox{}\pagebreak
}

\typeout{ }
\typeout{Ensuring contents page/list of figures/list of tables/titles is singlespaced}
\typeout{I'm afraid I can't see anyway of making the contents pages singleside}
% put \addtocontents{toc}{\protect\cleardoublepage} in the section where you want the page break

\let\dmuc@contents=\tableofcontents
\renewcommand{\tableofcontents}{
 \par\renewcommand{\baselinestretch}{1}\normalfont
 \dmuc@contents
 \par\renewcommand{\baselinestretch}{1.5}\normalfont
 \dmucendofchapter
}

\let\dmuc@figures=\listoffigures
\renewcommand{\listoffigures}{
 \par\renewcommand{\baselinestretch}{1}\normalfont
 \dmuc@figures
 \par\renewcommand{\baselinestretch}{1.5}\normalfont
}

\let\dmuc@tables=\listoftables
\renewcommand{\listoftables}{
 \par\renewcommand{\baselinestretch}{1}\normalfont
 \dmuc@tables
 \par\renewcommand{\baselinestretch}{1.5}\normalfont
}

% Edited from book.cls
%----------------------------------------------------------------------------

\def\@chapter[#1]#2{\ifnum\thenumcap=0
  \pagenumbering{arabic}
  \addtocounter{numcap}{1}
  \fi
  \ifnum \c@secnumdepth >\m@ne
  \if@mainmatter
  \refstepcounter{chapter}%
  \typeout{\@chapapp\space\thechapter.}%
  \addcontentsline{toc}{chapter}%
  {\protect\numberline{\thechapter}#1}%
  \else
  \addcontentsline{toc}{chapter}{#1}%
  \fi
  \else
  \addcontentsline{toc}{chapter}{#1}%
  \fi
  \chaptermark{#1}%
  \renewcommand{\dmuc@eventext}{#1}
  \addtocontents{lof}{\protect\addvspace{10\p@}}%
  \addtocontents{lot}{\protect\addvspace{10\p@}}%
  \addtocontents{lod}{\protect\addvspace{10\p@}}%
  \addcontentsline{lod}{chapter}{\protect\numberline{\thechapter}#1}%
  \if@twocolumn
  \@topnewpage[\@makechapterhead{#2}]%
  \else
  \@makechapterhead{#2}%
  \@afterheading
  \fi}

\def\@makechapterhead#1{%
  \vspace*{5\p@}%
  {\parindent \z@ \raggedright \normalfont
    \ifnum \c@secnumdepth >\m@ne
    \if@mainmatter
    \par\renewcommand{\baselinestretch}{1}\normalfont
    \centering\huge\bfseries \@chapapp\space \thechapter
    \par\nobreak\renewcommand{\baselinestretch}{1.5}\normalfont
    \vskip 20\p@
    \fi
    \fi
    \interlinepenalty\@M
    \par\renewcommand{\baselinestretch}{1}\normalfont
    \centering\Huge \bfseries #1\par\nobreak\renewcommand{\baselinestretch}{1}\normalfont
    \vskip 40\p@
  }}

\renewcommand\section[2][]{\par\renewcommand{\baselinestretch}{1}\normalfont
  \ifthenelse{\equal{#1}{}}{\renewcommand{\dmuc@oddtext}{#2}
    \@startsection {section}{1}{\z@}%
    {-3.5ex \@plus -1ex \@minus -.2ex}%
    {2.3ex \@plus.2ex}%
    {\normalfont\Large\bfseries}{#2}}
  {\renewcommand{\dmuc@oddtext}{#1}
    \@startsection {section}{1}{\z@}%
    {-3.5ex \@plus -1ex \@minus -.2ex}%
    {2.3ex \@plus.2ex}%
    {\normalfont\Large\bfseries}[#1]{#2}}
  \par\nobreak\renewcommand{\baselinestretch}{1.5}\normalfont}

\renewcommand\subsection[2][]{\par\renewcommand{\baselinestretch}{1}\normalfont
  \ifthenelse{\equal{#1}{}}{%
    \@startsection{subsection}{2}{\z@}%
    {-3.25ex\@plus -1ex \@minus -.2ex}%
    {1.5ex \@plus .2ex}%
    {\normalfont\large\bfseries}{#2}}
  {\@startsection{subsection}{2}{\z@}%
    {-3.25ex\@plus -1ex \@minus -.2ex}%
    {1.5ex \@plus .2ex}%
    {\normalfont\large\bfseries}[#1]{#2}}
  \par\nobreak\renewcommand{\baselinestretch}{1.5}\normalfont}

\renewcommand\subsubsection[2][]{\par\renewcommand{\baselinestretch}{1}\normalfont
  \ifthenelse{\equal{#1}{}}{%
    \@startsection{subsubsection}{3}{\z@}%
    {-3.25ex\@plus -1ex \@minus -.2ex}%
    {1.5ex \@plus .2ex}%
    {\normalfont\normalsize\bfseries}{#2}}
  {\@startsection{subsubsection}{3}{\z@}%
    {-3.25ex\@plus -1ex \@minus -.2ex}%
    {1.5ex \@plus .2ex}%
    {\normalfont\normalsize\bfseries}[#1]{#2}}
  \par\nobreak\renewcommand{\baselinestretch}{1.5}\normalfont}
% ----------------------------------------------------------------------------

% Do a clever table of definitions

% Set up external file and what \defn does
\def\ext@defn{lod}
\newcommand{\defn}[2][]{%
  \ifthenelse{\equal{#1}{}}%
  {\emph{#2}\addcontentsline{lod}{defn}{#2}}%
  {\emph{#2}\addcontentsline{lod}{defn}{#1}}%
}
\newcommand\listdefnname{List of Definitions}

% Define command for producing defn list
\newcommand\listofdefn{%
  \setcounter{chapter}{0}
  \par\renewcommand{\baselinestretch}{1}\normalfont
  \if@twocolumn
  \@restonecoltrue\onecolumn
  \else
  \@restonecolfalse
  \fi
  \chapter*{\listdefnname
    \@mkboth{\MakeUppercase\listdefnname}%
    {\MakeUppercase\listdefnname}}%
  \addcontentsline{toc}{chapter}{\listdefnname}
  \@starttoc{lod}%
  \if@restonecol\twocolumn\fi
  \par\renewcommand{\baselinestretch}{1.5}\normalfont
  \dmucendofchapter
}
\newcommand*\l@defn{\@dottedtocline{2}{3.8em}{3.2em}}

\typeout{ }
