% Layout and Pagraph Format for Unified Process Methodology
%
% Copyright (c) 2006-2009 Stephane GALLAND <galland@arakhne.org>
% 
% This program is free library; you can redistribute it and/or modify
% it under the terms of the GNU Lesser General Public License as
% published by the Free Software Foundation; either version 3 of the
% License, or any later version.
%
% This library is distributed in the hope that it will be useful, but
% WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
% Lesser General Public License for more details.
%
% You should have received a copy of the GNU Lesser General Public
% License along with this library; see the file COPYING.  If not,
% write to the Free Software Foundation, Inc., 59 Temple Place - Suite
% 330, Boston, MA 02111-1307, USA.
%
% Creation date: 2006-04-06
% Modifications (lastest first):
%   2009-11-02   Bug fix: footnote macros are fixed.
%                Add macros: \prname, \drname, \phdname, \scdname,
%                \mdname, \pename, \iename.
%   2009-10-30   Add support for book and report classes.
%   2009-10-28   Add \parskip.
%                Rename macros to avoid conflicts: \st->\ust, \nd->\und,
%		 \rd->\urd, \th->\uth
%   2009-10-27   Add framedminipage environment.
%   2009-06-25   Add \tablenote in mtable environment.
%   2009-06-23   Add upmcaution, upminfo and upmquestion environments.
%   2009-05-08   Remove CR character
%                Add \arakhneorg command
%   2009-04-29   Include package pifont (texlive distribution)
%   2007-07-07   Override the bibliography functions.
%   2007-07-06   Bug fix: use varioref for the footnote references.
%   2007-07-05   Add people name formatting function.
%   2007-07-02   Add exponent/indice commands and st/nd/rd/th symbols.
%   2007-06-27   Add inline enumeration.
%   2007-03-19   Add date fonctions.
%                Add localization.
%   2006-04-19   Add version number.
%                Add mtabular and mtable.
%   2006-04-20   Bug fix: invalid position of the tables.
%                Bug fix: centering table's headers.
%

\global\edef\upm@package@fmt@ver{2009/11/01}

\NeedsTeXFormat{LaTeX2e}[1995/12/01]
\ProvidesPackage{upmethodology-fmt}[\upm@package@fmt@ver]

\RequirePackage{upmethodology-p-common}

\gdef\upm@fmt@force@single@spacing#1{{\begin{singlespace}#1\end{singlespace}}}
\gdef\upm@date@head#1/#2\@nil{#1}
\gdef\upm@date@tail#1/#2\@nil{#2}
\gdef\upm@date@first#1{{\expandafter\upm@date@head#1\expandafter\@nil}}
\gdef\upm@date@second#1{{\expandafter\expandafter\expandafter\upm@date@head\expandafter\upm@date@tail#1\expandafter\@nil\expandafter\expandafter\expandafter\@nil}}
\gdef\upm@date@third#1{{\expandafter\expandafter\expandafter\upm@date@tail\expandafter\upm@date@tail#1\expandafter\@nil\expandafter\expandafter\expandafter\@nil}}

%----------------------------------------
% LOCALES
%----------------------------------------
\def\upm@format@lang@english{%
  \gdef\upm@lang@@{\message{**** upmethodology-fmt is using English language ****}}%
  \global\renewcommand{\upm@format@lang@makedate}[3]{##3\string/\two@digits{##2}\string/\two@digits{##1}}%
  \global\let\upm@format@lang@extractyear\upm@date@first%
  \global\let\upm@format@lang@extractmonth\upm@date@second%
  \gdef\upm@format@lang@professor{Pr.}%
  \gdef\upm@format@lang@doctor{Dr.}%
  \gdef\upm@format@lang@phdoctor{Ph.D.}%
  \gdef\upm@format@lang@scdoctor{Sc.D.}%
  \gdef\upm@format@lang@mdoctor{M.D.}%
  \gdef\upm@format@lang@professionalengineer{CEng.}%
  \gdef\upm@format@lang@incorporatedengineer{IEng.}%
}
  \global\let\upm@format@lang@extractyear\upm@date@first%
\def\upm@format@lang@french{%
  \gdef\upm@lang@@{\message{**** upmethodology-fmt is using French language ****}}%
  \global\renewcommand{\upm@format@lang@makedate}[3]{\two@digits{##1}\string/\two@digits{##2}\string/##3}%
  \global\let\upm@format@lang@extractyear\upm@date@third%
  \global\let\upm@format@lang@extractmonth\upm@date@second%
  \global\let\upm@format@lang@extractday\upm@date@first%
  \gdef\upm@format@lang@professor{Pr.}%
  \gdef\upm@format@lang@doctor{Dr.}%
  \gdef\upm@format@lang@phdoctor{Ph.D.}%
  \gdef\upm@format@lang@scdoctor{Sc.D.}%
  \gdef\upm@format@lang@mdoctor{M.D.}%
  \gdef\upm@format@lang@professionalengineer{Ing.}%
  \gdef\upm@format@lang@incorporatedengineer{Ing.}%
}
\global\providecommand{\upm@format@lang@makedate}[3]{}%

%----------------------------------------
% OPTIONS
%----------------------------------------
\DeclareOption{french}{%
  \upm@format@lang@french
  \PassOptionsToPackage{french}{varioref}
}
\DeclareOption{francais}{%
  \upm@format@lang@french
  \PassOptionsToPackage{french}{varioref}
}
\DeclareOption{english}{%
  \upm@format@lang@english
  \PassOptionsToPackage{english}{varioref}
}
\ExecuteOptions{english}
\ProcessOptions
\upm@lang@@


\RequirePackage[final]{graphicx}
\RequirePackage{subfigure}
\RequirePackage{tabularx}
\RequirePackage{multicol}
\RequirePackage{colortbl}
\RequirePackage{picinpar}
\RequirePackage{amsmath}
\RequirePackage{pifont}
\RequirePackage{setspace}
\RequirePackage{varioref}
\RequirePackage{txfonts}

%----------------------------------------
% Exponent and indice
%----------------------------------------
% Exponent
%\newcommand{\textsup}[1]{\ensuremath{^{\text{#1}}}\xspace}
\newcommand{\textsup}[1]{\upm@textsuperscript{#1}\xspace}
% Indice
%\newcommand{\textsub}[1]{\ensuremath{_{\text{#1}}}\xspace}
\newcommand{\textsub}[1]{\upm@textsubscript{#1}\xspace}

%----------------------------------------
% SYMBOLS
%----------------------------------------
\let\upm@format@copyright\copyright
\renewcommand{\copyright}{\upm@format@copyright\xspace}
\newcommand{\trademark}{\Pisymbol{psy}{228}\xspace}
\newcommand{\regmark}{\Pisymbol{psy}{226}\xspace}
\newcommand{\smalltrade}{{\tiny\trademark}\xspace}
\newcommand{\smallreg}{{\tiny\regmark}\xspace}
\newcommand{\smallcopy}{{\tiny\copyright}\xspace}
\newcommand{\ust}{\textsup{st}}
\newcommand{\und}{\textsup{nd}}
\newcommand{\urd}{\textsup{rd}}
\newcommand{\uth}{\textsup{th}}


%----------------------------------------
% FIGURES
%----------------------------------------

\newcommand{\upm@mfigure}[5][ht]{
	\begin{figure}[#1]%
		\begin{center}%
			\includegraphics[#2]{#3}%
			\caption{#4}%
			\label{fig:#5}%
		\end{center}%
	\end{figure}%
}
\newcommand{\upm@mfigurestar}[5][ht]{
	\begin{figure*}[#1]%
		\begin{center}%
			\includegraphics[#2]{#3}%
			\caption{#4}%
			\label{fig:#5}%
		\end{center}%
	\end{figure*}%
}
%-----
%\mfigure[position]{options}{filename}{caption}{label}
\def\mfigure{\@ifstar\upm@mfigurestar\upm@mfigure}
%-----
%\figref{label}
\newcommand{\figref}[1]{\ref{fig:#1}}
%-----
%\figpageref{label}
\newcommand{\figpageref}[1]{\pageref{fig:#1}}

%-----
% Multi-figures

\newcounter{upm@subfigure@count}

\newcommand{\upm@beginsubfigure}[4]{
	\let\upm@mfiguresaved\mfigure
	%options,filename,caption,label
	\renewcommand{\mfigure}[5][]{
		\xdef\upm@tmp@subfigure@label{\alph{upm@subfigure@count}}%
                \subfigure[##4]{%
			\includegraphics[##2]{##3}%
			\label{fig:##5}%
			\label{fig:#4:\upm@tmp@subfigure@label}%
		}%
		\addtocounter{upm@subfigure@count}{1}%
	}
	%options,filename,caption
	\newcommand{\msubfigure}[3]{
		\xdef\upm@tmp@subfigure@label{\alph{upm@subfigure@count}}%
                \subfigure[##3]{%
			\includegraphics[##1]{##2}%
			\label{fig:#4:\upm@tmp@subfigure@label}%
		}%
		\addtocounter{upm@subfigure@count}{1}%
	}
	\setcounter{upm@subfigure@count}{1}
	\gdef\upm@mfigurescaption{#3}%
	\gdef\upm@mfigureslabel{fig:#4}%
	\begin{#1}[#2]\centering %
}

\newcommand{\upm@endsubfigure}[1]{
		\caption{\upm@mfigurescaption}%
		\label{\upm@mfigureslabel}%
	\end{#1}%
	\let\mfigure\upm@mfiguresaved
}

%-----
%\mfigures[position]{caption}{label}
\newenvironment{mfigures}[3][ht]{
	\upm@beginsubfigure{figure}{#1}{#2}{#3}%
}{%
	\upm@endsubfigure{figure}%
}
%-----
%\mfigures*[position]{caption}{label}
\newenvironment{mfigures*}[3][ht]{
	\upm@beginsubfigure{figure*}{#1}{#2}{#3}%
}{%
	\upm@endsubfigure{figure*}%
}

%----------------------------------------
% IMAGES INCLUDING TeX EXPRESSIONS
%----------------------------------------

\gdef\upm@figtex@remove@width@param#1{#1}
\gdef\upm@figtex@dyncaption@remove{}
\gdef\upm@figtex@figremove#1{%
	\global\expandafter\let\csname FIG#1\endcsname\relax%
}
\gdef\upm@figtex@restore{%
	\upm@figtex@dyncaption@remove%
	\gdef\upm@figtex@dyncaption@remove{}
}
%-----
%\figmath{id}{content}
\def\figmath#1#2{%
	\expandafter\gdef\csname FIG#1\endcsname{\ensuremath{#2}}%
	\global\protected@edef\upm@figtex@dyncaption@remove{\upm@figtex@dyncaption@remove\protect\upm@figtex@figremove{#1}}%
}
%-----
%\figtext{id}{content}
\def\figtext#1#2{%
	\expandafter\gdef\csname FIG#1\endcsname{#2}%
	\global\protected@edef\upm@figtex@dyncaption@remove{\upm@figtex@dyncaption@remove\protect\upm@figtex@figremove{#1}}%
}

\newcommand{\upm@mfigurewtex}[5][ht]{
	\begin{figure}[#1]%
		\begin{center}%
			\resizebox{\upm@figtex@remove@width@param{#2}}{!}{\input{#3.pstex_t}}%
			\caption{#4}%
			\label{fig:#5}%
		\end{center}%
	\end{figure}%
	\upm@figtex@restore%
}
\newcommand{\upm@mfigurewtexstar}[5][ht]{
	\begin{figure*}[#1]%
		\begin{center}%
			\resizebox{\upm@figtex@remove@width@param{#2}}{!}{\input{#3.pstex_t}}%
			\caption{#4}%
			\label{fig:#5}%
		\end{center}%
	\end{figure*}%
	\upm@figtex@restore%
}
%-----
%\mfigurewtex[position]{width}{filename}{caption}{label}
\def\mfigurewtex{\@ifstar\upm@mfigurewtexstar\upm@mfigurewtex}

%----------------------------------------
% TABLES
%----------------------------------------

%title of a table
\def\upm@fmt@table@title#1{\color{fronttableheader}{\bfseries #1}}
%title of columns
\def\upm@fmt@table@column@title#1{\mbox{}\hfil{\itshape \mbox{\color{fronttableheader}{#1}}}\hfil\mbox{}}
%table's colors
\definecolor{backtableheader}{rgb}{0.92,0.94,1}
\definecolor{fronttableheader}{rgb}{0.23,0.33,0.48}

%List of functions able to build an header
\newif\ifupm@havetitle
\def\upm@hline{\\\hline}
\expandafter\def\csname upm@tableheader@b@1\endcsname#1{\protect\upm@fmt@table@column@title{#1} \protect\\}
\expandafter\protected@edef\csname upm@tableheader@b@2\endcsname#1#2{%
	\protect\upm@fmt@table@column@title{#1} & \csname upm@tableheader@b@1\endcsname{#2}}
\expandafter\protected@edef\csname upm@tableheader@b@3\endcsname#1#2#3{%
	\protect\upm@fmt@table@column@title{#1} & \csname upm@tableheader@b@2\endcsname{#2}{#3}}
\expandafter\protected@edef\csname upm@tableheader@b@4\endcsname#1#2#3#4{%
	\protect\upm@fmt@table@column@title{#1} & \csname upm@tableheader@b@3\endcsname{#2}{#3}{#4}}
\expandafter\protected@edef\csname upm@tableheader@b@5\endcsname#1#2#3#4#5{%
	\protect\upm@fmt@table@column@title{#1} & \csname upm@tableheader@b@4\endcsname{#2}{#3}{#4}{#5}}
\expandafter\protected@edef\csname upm@tableheader@b@6\endcsname#1#2#3#4#5#6{%
	\protect\upm@fmt@table@column@title{#1} & \csname upm@tableheader@b@5\endcsname{#2}{#3}{#4}{#5}{#6}}
\expandafter\protected@edef\csname upm@tableheader@b@7\endcsname#1#2#3#4#5#6#7{%
	\protect\upm@fmt@table@column@title{#1} & \csname upm@tableheader@b@6\endcsname{#2}{#3}{#4}{#5}{#6}{#7}}
\expandafter\protected@edef\csname upm@tableheader@b@8\endcsname#1#2#3#4#5#6#7#8{%
	\protect\upm@fmt@table@column@title{#1} & \csname upm@tableheader@b@7\endcsname{#2}{#3}{#4}{#5}{#6}{#7}{#8}}
\expandafter\protected@edef\csname upm@tableheader@b@9\endcsname#1#2#3#4#5#6#7#8#9{%
	\protect\upm@fmt@table@column@title{#1} & \csname upm@tableheader@b@8\endcsname{#2}{#3}{#4}{#5}{#6}{#7}{#8}{#9}}

\expandafter\def\csname upm@tableheader@c@1\endcsname#1{\ifupm@havetitle\else\hline\fi\rowcolor{backtableheader}\csname upm@tableheader@b@1\endcsname{#1}}
\expandafter\def\csname upm@tableheader@c@2\endcsname#1#2{\ifupm@havetitle\else\hline\fi\rowcolor{backtableheader}\csname upm@tableheader@b@2\endcsname{#1}{#2}}
\expandafter\def\csname upm@tableheader@c@3\endcsname#1#2#3{\ifupm@havetitle\else\hline\fi\rowcolor{backtableheader}\csname upm@tableheader@b@3\endcsname{#1}{#2}{#3}}
\expandafter\def\csname upm@tableheader@c@4\endcsname#1#2#3#4{\ifupm@havetitle\else\hline\fi\rowcolor{backtableheader}\csname upm@tableheader@b@4\endcsname{#1}{#2}{#3}{#4}}
\expandafter\def\csname upm@tableheader@c@5\endcsname#1#2#3#4#5{\ifupm@havetitle\else\hline\fi\rowcolor{backtableheader}\csname upm@tableheader@b@5\endcsname{#1}{#2}{#3}{#4}{#5}}
\expandafter\def\csname upm@tableheader@c@6\endcsname#1#2#3#4#5#6{\ifupm@havetitle\else\hline\fi\rowcolor{backtableheader}\csname upm@tableheader@b@6\endcsname{#1}{#2}{#3}{#4}{#5}{#6}}
\expandafter\def\csname upm@tableheader@c@7\endcsname#1#2#3#4#5#6#7{\ifupm@havetitle\else\hline\fi\csname upm@tableheader@b@7\endcsname{#1}{#2}{#3}{#4}{#5}{#6}{#7}}
\expandafter\def\csname upm@tableheader@c@8\endcsname#1#2#3#4#5#6#7#8{\ifupm@havetitle\else\hline\fi\csname upm@tableheader@b@8\endcsname{#1}{#2}{#3}{#4}{#5}{#6}{#7}{#8}}
\expandafter\def\csname upm@tableheader@c@9\endcsname#1#2#3#4#5#6#7#8#9{\ifupm@havetitle\else\hline\fi\rowcolor{backtableheader}\csname upm@tableheader@b@9\endcsname{#1}{#2}{#3}{#4}{#5}{#6}{#7}{#8}{#9}}

%-----
%\mtabular[width]{ncolumns}{columns}
\newenvironment{mtabular}[3][\linewidth]{%
	\upm@havetitlefalse
	\newcommand{\tabulartitle}[1]{\hline\multicolumn{#2}{>{\columncolor{backtableheader}}c}{\upm@fmt@table@title{##1}}\upm@havetitletrue \upm@hline}%
	\expandafter\let\expandafter\tabularheader\csname upm@tableheader@c@#2\endcsname%
	\tabularx{#1}{#3}%
}{%
	\endtabularx%
}

%-----
%\mtable[position]{width}{ncolumns}{columns}{caption}{label}
\newenvironment{mtable}[6][ht]{%
	\table[#1]\center%
	\global\protected@edef\upm@table@caption{#5}%
	\global\protected@edef\upm@table@label{tab:#6}%
	\gdef\upm@table@note{}%
	\newcommand{\captionastitle}{\tabulartitle{\upm@table@caption}}%
	\newcommand{\tablenote}[1]{\gdef\upm@table@note{##1}}%
	\mtabular[#2]{#3}{#4}%
}{%
	\endmtabular%
	\caption{\upm@table@caption}%
	\label{\upm@table@label}%
	\endcenter%
	{\upm@table@note}%
	\endtable%
	\let\upm@table@caption\relax%
	\let\upm@table@label\relax%
	\let\upm@table@note\relax%
}

%-----
%\tabref{label}
\newcommand{\tabref}[1]{\ref{tab:#1}}
%-----
%\tabpageref{label}
\newcommand{\tabpageref}[1]{\pageref{tab:#1}}

%----------------------------------------
% PARAGRAPHS
%----------------------------------------
\setlength{\parindent}{0pt}
\setlength{\parskip}{.2cm}

%----------------------------------------
% COLORIZED SECTION'S TITLES
%----------------------------------------

% PRIVATE COLORS
\definecolor{sectiontitlecolor}{rgb}{0.93,0.41,0}
\definecolor{chaptertitlecolor}{rgb}{0.24,0.33,0.48}
\definecolor{parttitlecolor}{rgb}{0.24,0.33,0.48}

% PRIVATE FORMATTING MACROS
\newcounter{upm@format@section@sectionlevel}
\setcounter{upm@format@section@sectionlevel}{0}

\gdef\upm@format@partnum#1{\textcolor{parttitlecolor}{\expandafter\fontsize{40pt}{60pt}\selectfont#1\normalfont}}
\gdef\upm@format@parttitle#1{\textcolor{parttitlecolor}{\huge\scshape #1}}
\gdef\upm@format@chapternum#1{\textcolor{chaptertitlecolor}{\expandafter\fontsize{40pt}{60pt}\selectfont#1\normalfont}}
\gdef\upm@format@chaptertitle#1{\textcolor{chaptertitlecolor}{\Huge\scshape #1}}
\gdef\upm@format@sectionnum#1#2{\textcolor{sectiontitlecolor}{#2}}
\gdef\upm@format@sectiontitle#1#2{\textcolor{sectiontitlecolor}{#2}}

% PART
\ifupmbookformat
	\gdef\@part[#1]#2{%
	    \ifnum \c@secnumdepth >-2\relax
	      \refstepcounter{part}%
	      \addcontentsline{toc}{part}{\thepart\hspace{1em}#1}%
	    \else
	      \addcontentsline{toc}{part}{#1}%
	    \fi
	    \markboth{}{}%
	    {\centering
	     \interlinepenalty \@M
	     \normalfont
	     \ifnum \c@secnumdepth >-2\relax
	       \upm@format@partnum{\thepart}
	       \par
	       \vskip 20\p@
	     \fi
	     \upm@format@parttitle{#2}\par}%
	    \@endpart}
	\gdef\@spart#1{%
	    {\centering
	     \interlinepenalty \@M
	     \normalfont
	     \upm@format@parttitle{#1}\par}%
	    \@endpart}
\fi

% CHAPTER
\ifupmarticleformat\else
	\newcommand{\upm@format@makechapterhead}[2]{%
		  %\vspace*{50\p@}%
		  {\parindent \z@ \raggedleft \normalfont
		    \ifx\@empty#1\ifnum \c@secnumdepth >\m@ne
			\upm@format@chapternum{\thechapter}
			\par\nobreak
			\vskip 20\p@
		    \fi\fi
		    \interlinepenalty\@M
		    \ifupmbookformat\doublespacing\fi
		    \upm@format@chaptertitle{#2}\par\nobreak
		    \vskip 80\p@
		  }
	}
	%internal implementation of \chapter
	\gdef\@makechapterhead#1{\upm@format@makechapterhead{\@empty}{#1}}
	%internal implementation of \chapter*
	\gdef\@makeschapterhead#1{\upm@format@makechapterhead{\relax}{#1}}
\fi

% SECTIONS
\let\upm@format@sect@old\@sect
\let\upm@format@ssect@old\@ssect

% public implementation of sections.
\renewcommand{\section}{\setcounter{upm@format@section@sectionlevel}{1}\@startsection{section}{1}{\z@}{-3.5ex \@plus -1ex \@minus -.2ex}{2.3ex \@plus.2ex}{\normalfont\Large\scshape}}
\renewcommand{\subsection}{\setcounter{upm@format@section@sectionlevel}{2}\@startsection{subsection}{2}{\z@}{-3.25ex\@plus -1ex \@minus -.2ex}{1.5ex \@plus .2ex}{\normalfont\large\scshape}}
\renewcommand{\subsubsection}{\setcounter{upm@format@section@sectionlevel}{3}\@startsection{subsubsection}{3}{\z@}{-3.25ex\@plus -1ex \@minus -.2ex}{1.5ex \@plus .2ex}{\normalfont\normalsize\scshape}}

% internal implementations for sections.
\gdef\@sect#1#2#3#4#5#6[#7]#8{\upm@format@sect@old{#1}{#2}{#3}{#4}{#5}{#6}[#7]{\upm@format@sectiontitle{\theupm@format@section@sectionlevel}{#8}}}
\gdef\@ssect#1#2#3#4#5{\upm@format@ssect@old{#1}{#2}{#3}{#4}{\upm@format@sectiontitle{\theupm@format@section@sectionlevel}{#5}}}
\gdef\@seccntformat#1{\upm@format@sectionnum{\theupm@format@section@sectionlevel}{\csname the#1\endcsname/}\quad}

%----------------------------------------
% PAGE LAYOUT
%----------------------------------------

% Update the format of the saved pages and sections
\renewcommand{\sectionmark}[1]{\markright{\thesection\ \textsc{#1}}}
\ifupmarticleformat\else
\renewcommand{\chaptermark}[1]{\markboth{\textsc{#1}}{}}
\renewcommand{\@mkboth}[2]{%
    \let\upm@doc@MakeUppercase\MakeUppercase%
    \gdef\MakeUppercase##1{##1}%
    \chaptermark{#1}%
    \let\MakeUppercase\upm@doc@MakeUppercase%
    \let\upm@doc@MakeUppercase\relax%
}
\fi
% Make sure that the page before a part or
% a chapter title was empty
\renewcommand{\cleardoublepage}{%
  \clearpage%
  \if@twoside\ifodd\c@page%
  \else%
  \thispagestyle{facingtochapter}%
  \hbox{}\newpage%
  \if@twocolumn\hbox{}\newpage%
  \fi\fi\fi%
}
\newcommand{\ps@facingtochapter}{\ps@empty}

%----------------------------------------
% ADDITIONAL SECTIONS
%----------------------------------------

\gdef\upm@format@newsection@alignment{}

%-----
% Part without number but inside the TOC
%\parttoc [toctitle]{title} % left-alignment inside TOC
%\parttoc*[toctitle]{title} % right-alignment inside TOC
\def\parttoc{\@ifstar%
	{\gdef\upm@format@newsection@alignment{\protect\numberline{}}\expandafter\upm@format@newsection@part}%
	{\gdef\upm@format@newsection@alignment{}\expandafter\upm@format@newsection@part}}
\def\upm@format@newsection@part{\@ifnextchar [%
	{\expandafter\upm@format@newsection@part@opt}%
	{\expandafter\upm@format@newsection@part@nopt}}
\def\upm@format@newsection@part@opt[#1]#2{%
	\part*{#2\addcontentsline{toc}{part}{\upm@format@newsection@alignment{#1}}}%
	\chaptermark{}%
}
\def\upm@format@newsection@part@nopt#1{%
	\part*{#1\addcontentsline{toc}{part}{\upm@format@newsection@alignment{#1}}}%
	\chaptermark{}%
}

%-----
% Chapter without number but inside the TOC
%\chaptertoc [toctitle]{title} % left-alignment inside TOC
%\chaptertoc*[toctitle]{title} % right-alignment inside TOC
\def\chaptertoc{\@ifstar%
	{\gdef\upm@format@newsection@alignment{\protect\numberline{}}\expandafter\upm@format@newsection@chapter}%
	{\gdef\upm@format@newsection@alignment{}\expandafter\upm@format@newsection@chapter}}
\def\upm@format@newsection@chapter{\@ifnextchar [%
	{\expandafter\upm@format@newsection@chapter@opt}%
	{\expandafter\upm@format@newsection@chapter@nopt}}
\def\upm@format@newsection@chapter@opt[#1]#2{%
	\@schapter{#2\addcontentsline{toc}{chapter}{\upm@format@newsection@alignment{#1}}}%
	\chaptermark{#1}%
}
\def\upm@format@newsection@chapter@nopt#1{%
	\@schapter{#1\addcontentsline{toc}{chapter}{\upm@format@newsection@alignment{#1}}}%
	\chaptermark{#1}%
}

%-----
% Section without number but inside the TOC
%\sectiontoc [toctitle]{title} % left-alignment inside TOC
%\sectiontoc*[toctitle]{title} % right-alignment inside TOC
\def\sectiontoc{\@ifstar%
	{\gdef\upm@format@newsection@alignment{\protect\numberline{}}\expandafter\upm@format@newsection@section}%
	{\gdef\upm@format@newsection@alignment{}\expandafter\upm@format@newsection@section}}
\def\upm@format@newsection@section{\@ifnextchar [%
	{\expandafter\upm@format@newsection@section@opt}%
	{\expandafter\upm@format@newsection@section@nopt}}
\def\upm@format@newsection@section@opt[#1]#2{%
	\section*{#2\addcontentsline{toc}{section}{\upm@format@newsection@alignment{#1}}}%
}
\def\upm@format@newsection@section@nopt#1{%
	\section*{#1\addcontentsline{toc}{section}{\upm@format@newsection@alignment{#1}}}%
}

%-----
% Subsection without number but inside the TOC
%\subsectiontoc [toctitle]{title} % left-alignment inside TOC
%\subsectiontoc*[toctitle]{title} % right-alignment inside TOC
\def\subsectiontoc{\@ifstar%
	{\gdef\upm@format@newsection@alignment{\protect\numberline{}}\expandafter\upm@format@newsection@subsection}%
	{\gdef\upm@format@newsection@alignment{}\expandafter\upm@format@newsection@subsection}}
\def\upm@format@newsection@subsection{\@ifnextchar [%
	{\expandafter\upm@format@newsection@subsection@opt}%
	{\expandafter\upm@format@newsection@subsection@nopt}}
\def\upm@format@newsection@subsection@opt[#1]#2{%
	\subsection*{#2\addcontentsline{toc}{subsection}{\upm@format@newsection@alignment{#1}}}%
}
\def\upm@format@newsection@subsection@nopt#1{%
	\subsection*{#1\addcontentsline{toc}{subsection}{\upm@format@newsection@alignment{#1}}}%
}

%-----
% Subsubsection without number but inside the TOC
%\subsubsectiontoc [toctitle]{title} % left-alignment inside TOC
%\subsubsectiontoc*[toctitle]{title} % right-alignment inside TOC
\def\subsubsectiontoc{\@ifstar%
	{\gdef\upm@format@newsection@alignment{\protect\numberline{}}\expandafter\upm@format@newsection@subsubsection}%
	{\gdef\upm@format@newsection@alignment{}\expandafter\upm@format@newsection@subsubsection}}
\def\upm@format@newsection@subsubsection{\@ifnextchar [%
	{\expandafter\upm@format@newsection@subsubsection@opt}%
	{\expandafter\upm@format@newsection@subsubsection@nopt}}
\def\upm@format@newsection@subsubsection@opt[#1]#2{%
	\subsubsection*{#2\addcontentsline{toc}{subsubsection}{\upm@format@newsection@alignment{#1}}}%
}
\def\upm@format@newsection@subsubsection@nopt#1{%
	\subsubsection*{#1\addcontentsline{toc}{subsubsection}{\upm@format@newsection@alignment{#1}}}%
}

%----------------------------------------
% BIBLIOGRAPHY
%----------------------------------------

%-----
%\bibsize{size}
\newcommand{\bibsize}[1]{\gdef\upm@bibsize{#1}}

\gdef\upm@bibsize{\small}

\gdef\@biblabel#1{{\upm@bibsize{[#1]}}}

\gdef\@lbibitem[#1]#2{\item[\@biblabel{#1}\hfill]\upm@bibsize\if@filesw{%
	\let\protect\noexpand\immediate\write\@auxout{\string\bibcite{#2}{#1}}}\fi\ignorespaces}

\gdef\@bibitem#1{\item\upm@bibsize\if@filesw%
	\immediate\write\@auxout{\string\bibcite{#1}{\the\value{\@listctr}}}\fi\ignorespaces}

%-----
\let\upm@bibliographystyle\bibliographystyle
\let\upm@bibliography\bibliography
\gdef\upm@bibstyle{abbr}
\renewcommand{\bibliographystyle}[1]{\gdef\upm@bibstyle{#1}}
\renewcommand{\bibliography}[1]{%
	\upm@bibliographystyle{\upm@bibstyle}%
	\upm@bibliography{#1}%
}

%----------------------------------------
% TABLE OF CONTENT
%----------------------------------------
\setcounter{secnumdepth}{3}
\setcounter{tocdepth}{3}

%----------------------------------------
% ENUMERATIONS
%----------------------------------------
\newcounter{upm@fmt@savedcounter}

\newcommand{\savecounter}[1]{%
	\setcounter{upm@fmt@savedcounter}{\value{#1}}%
}
\newcommand{\restorecounter}[1]{%
	\setcounter{#1}{\theupm@fmt@savedcounter}%
}
\newcommand{\saveenumcounter}{\savecounter{\@enumctr}}
\newcommand{\restoreenumcounter}{\restorecounter{\@enumctr}}
\newcommand{\setenumcounter}[1]{\setcounter{\@enumctr}{#1}\addtocounter{\@enumctr}{-1}}
\newcommand{\getenumcounter}{\value{\@enumctr}}

%----------------------------------------
% FOOTNOTE
%----------------------------------------

\gdef\upm@footnoteref#1{\upm@textsuperscript{#1}}
\gdef\upm@footnotepageref#1#2{\upm@textsuperscript{{#1\tiny(#2)}}}

\def\upm@savefootnote#1#2{%
	\footnote{#1\label{\upm@protect{footnote:#2}}}%
	\upm@nameundef{footnote:#2:cmd}%
}
\def\upm@savefootnotestar#1#2{%
	\upm@namedef{footnote:#2:cmd}{\upm@savefootnote{#1}{#2}}%
}

\DeclareRobustCommand\savefootnote{\@ifstar\upm@savefootnotestar\upm@savefootnote}

\def\upm@reffootnote#1{%
	\upm@ifdefinedname{footnote:#1:cmd}{%
		\upm@nameuse{footnote:#1:cmd}}{%
		\upm@footnoteref{\upm@getref{\upm@protect{footnote:#1}}}%
	}%
	\xspace%
} 
\def\upm@reffootnotestar#1{%
	\upm@ifdefinedname{footnote:#1:cmd}{%
		\upm@nameuse{footnote:#1:cmd}}{%
		\xdef\upm@tmp@tmp{\upm@protect{footnote:#1}}%
		\upm@footnotepageref{\expandafter\upm@getref{\upm@tmp@tmp}}{\expandafter\upm@getpageref{\upm@tmp@tmp}}%
		\global\let\upm@tmp@tmp\relax%
	}%
	\xspace%
} 

\DeclareRobustCommand\reffootnote{\@ifstar\upm@reffootnotestar\upm@reffootnote}

%----------------------------------------
% IMAGES IN PARAGRAPHS
%----------------------------------------

\newenvironment{umlinpar}[2][width=.45\linewidth]{%
\begin{window}[0,r,{\mbox{\hspace{.5cm}\includegraphics[#1]{#2}\vspace{.5cm}}},{}]
}{%
\end{window}}

%----------------------------------------
% DATE
%----------------------------------------

%Build a date this a supported format
%\makedate{day}{month}{year}
\let\makedate\upm@format@lang@makedate

%-----
%Replies the year corresponding to the given supported date
%\extractyear{date}
\let\extractyear\upm@format@lang@extractyear

%-----
%Replies the month corresponding to the given supported date
%\extractmonth{date}
\let\extractmonth\upm@format@lang@extractmonth

%-----
%Replies the day corresponding to the given supported date
%\extractday{date}
\let\extractday\upm@format@lang@extractday

% Redefine the today function
\AtBeginDocument{\global\edef\today{\makedate{\the\day}{\the\month}{\the\year}}}

%----------------------------------------
% PEOPLE NAME
%----------------------------------------

%\upmmakename[von]{firstname}{lastname}{separator}
\newcommand{\upmmakename}[4][\relax]{%
	\ifx#1\relax%
		{\mbox{\small #2}#4\textsc{#3}}%
	\else%
		{\mbox{\small #2}#4\mbox{\small #1}#4\textsc{#3}}%
	\fi%
}

%\makename[von]{firstname}{lastname}
\newcommand{\makename}[3][\relax]{%
	\upmmakename[#1]{#2}{#3}{\ }%
}

\gdef\upm@format@people@title#1{\textsc{#1}}

%\prname[von]{firstname}{lastname}
%\prname*[von]{firstname}{lastname}
\def\prname{\@ifstar\upm@prnamestar\upm@prname}
\newcommand{\upm@prname}[3][\relax]{%
	\upm@format@people@title{\upm@format@lang@professor}~\upmmakename[#1]{#2}{#3}{\ }%
}
\newcommand{\upm@prnamestar}[3][\relax]{%
	\upmmakename[#1]{#2}{#3}{\ }, \upm@format@people@title{\upm@format@lang@professor}%
}

%\drname[von]{firstname}{lastname}
%\drname*[von]{firstname}{lastname}
\def\drname{\@ifstar\upm@drnamestar\upm@drname}
\newcommand{\upm@drname}[3][\relax]{%
	\upm@format@people@title{\upm@format@lang@doctor}~\upmmakename[#1]{#2}{#3}{\ }%
}
\newcommand{\upm@drnamestar}[3][\relax]{%
	\upmmakename[#1]{#2}{#3}{\ }, \upm@format@people@title{\upm@format@lang@doctor}%
}

%\phdname[von]{firstname}{lastname}
%\phdname*[von]{firstname}{lastname}
\def\phdname{\@ifstar\upm@phdnamestar\upm@phdname}
\newcommand{\upm@phdname}[3][\relax]{%
	\upm@format@people@title{\upm@format@lang@phdoctor}~\upmmakename[#1]{#2}{#3}{\ }%
}
\newcommand{\upm@phdnamestar}[3][\relax]{%
	\upmmakename[#1]{#2}{#3}{\ }, \upm@format@people@title{\upm@format@lang@phdoctor}%
}

%\scdname[von]{firstname}{lastname}
%\scdname*[von]{firstname}{lastname}
\def\scdname{\@ifstar\upm@scdnamestar\upm@scdname}
\newcommand{\upm@scdname}[3][\relax]{%
	\upm@format@people@title{\upm@format@lang@scdoctor}~\upmmakename[#1]{#2}{#3}{\ }%
}
\newcommand{\upm@scdnamestar}[3][\relax]{%
	\upmmakename[#1]{#2}{#3}{\ }, \upm@format@people@title{\upm@format@lang@scdoctor}%
}

%\mdname[von]{firstname}{lastname}
%\mdname*[von]{firstname}{lastname}
\def\mdname{\@ifstar\upm@mdnamestar\upm@mdname}
\newcommand{\upm@mdname}[3][\relax]{%
	\upm@format@people@title{\upm@format@lang@mdoctor}~\upmmakename[#1]{#2}{#3}{\ }%
}
\newcommand{\upm@mdnamestar}[3][\relax]{%
	\upmmakename[#1]{#2}{#3}{\ }, \upm@format@people@title{\upm@format@lang@mdoctor}%
}

%\pengname[von]{firstname}{lastname}
%\pengname*[von]{firstname}{lastname}
\def\pengname{\@ifstar\upm@pengnamestar\upm@pengname}
\newcommand{\upm@pengname}[3][\relax]{%
	\upm@format@people@title{\upm@format@lang@professionalengineer}~\upmmakename[#1]{#2}{#3}{\ }%
}
\newcommand{\upm@pengnamestar}[3][\relax]{%
	\upmmakename[#1]{#2}{#3}{\ }, \upm@format@people@title{\upm@format@lang@professionalengineer}%
}

%\iengname[von]{firstname}{lastname}
%\iengname*[von]{firstname}{lastname}
\def\iengname{\@ifstar\upm@iengnamestar\upm@iengname}
\newcommand{\upm@iengname}[3][\relax]{%
	\upm@format@people@title{\upm@format@lang@incorporatedengineer}~\upmmakename[#1]{#2}{#3}{\ }%
}
\newcommand{\upm@iengnamestar}[3][\relax]{%
	\upmmakename[#1]{#2}{#3}{\ }, \upm@format@people@title{\upm@format@lang@incorporatedengineer}%
}

%----------------------------------------
% INLINE ENUMERATION
%----------------------------------------

\newcounter{@@upm@fmt@inlineenumeration}
\newcommand{\inlineenumerationlabel}[1]{(#1)}
\let\upm@fmt@inlineenum@numberformat\roman
\newenvironment{inlineenumeration}{%
	\begingroup%
	\let\upm@fmt@inlineenum@currentlabel\@currentlabel%
	\renewcommand{\item}{\xdef\@currentlabel{\upm@fmt@inlineenum@numberformat{@@upm@fmt@inlineenumeration}}{\inlineenumerationlabel{\@currentlabel}{\addtocounter{@@upm@fmt@inlineenumeration}{1}}}~}%
	\setcounter{@@upm@fmt@inlineenumeration}{1}%
}{%
	\global\let\@currentlabel\upm@fmt@inlineenum@currentlabel%
	\endgroup%
}

%----------------------------------------
% SIZE MANAGEMENT
%----------------------------------------
\newenvironment{upmfontsize}[1]{%
	\begingroup%
	\let\upm@Huge\Huge%
	\let\upm@huge\huge%
	\let\upm@normalsize\normalsize%
	\let\upm@small\small%
	\let\upm@scriptsize\scriptsize%
	\let\upm@footnotesize\footnotesize%
	\let\upm@tiny\tiny%
	%
	\ifx#1\Huge%
		\let\Huge\upm@Huge%
		\let\huge\upm@Huge%
		\let\small\upm@huge%
		\let\scriptsize\upm@normalsize%
		\let\footnotesize\upm@small%
		\let\tiny\upm@scriptsize%
	\else\ifx#1\huge%
		\let\Huge\upm@Huge%
		\let\huge\upm@Huge%
		\let\small\upm@normalsize%
		\let\scriptsize\upm@small%
		\let\footnotesize\upm@scriptsize%
		\let\tiny\upm@footnotesize%
	\else\ifx#1\small%
		\let\Huge\upm@huge%
		\let\huge\upm@normalsize%
		\let\small\upm@scriptsize%
		\let\scriptsize\upm@footnotesize%
		\let\footnotesize\upm@tiny%
		\let\tiny\upm@tiny%
	\else\ifx#1\scriptsize%
		\let\Huge\upm@normalsize%
		\let\huge\upm@small%
		\let\small\upm@footnotesize%
		\let\scriptsize\upm@tiny%
		\let\footnotesize\upm@tiny%
		\let\tiny\upm@tiny%
	\else\ifx#1\footnotesize%
		\let\Huge\upm@small%
		\let\huge\upm@scriptsize%
		\let\small\upm@tiny%
		\let\scriptsize\upm@tiny%
		\let\footnotesize\upm@tiny%
		\let\tiny\upm@tiny%
	\else\ifx#1\tiny%
		\let\Huge\upm@scriptsize%
		\let\huge\upm@footnotesize%
		\let\small\upm@tiny%
		\let\scriptsize\upm@tiny%
		\let\footnotesize\upm@tiny%
		\let\tiny\upm@tiny%
	\fi%
	\fi%
	\fi%
	\fi%
	\fi%
	\fi%
	#1%
}{\endgroup}

%----------------------------------------
% FRAMED MINIPAGE
%----------------------------------------

\newsavebox{\upm@framed@minipage}
%\begin{framedminipage}{width}...\end{frameedminipage}
\newenvironment{framedminipage}[1]{%
	\begin{lrbox}{\upm@framed@minipage}\begin{minipage}{#1}%
	}{%
	\end{minipage}\end{lrbox}\fbox{\usebox{\upm@framed@minipage}}}

%----------------------------------------
% HIGHLIGH BOXES
%----------------------------------------

\newsavebox{\upm@highlight@box@save}

\newenvironment{upm@highligh@box}[2]{%
	\par
	\vspace{.5cm}
	\begin{tabular}{|p{#1}|}
	\hline
	\begin{window}[0,l,{\mbox{\includegraphics[width=1cm]{#2}}},{}]
}{%
	\end{window}\\ \hline \end{tabular}
	\vspace{.5cm}
	\par
}

\newenvironment{upmcaution}[1][\linewidth]{%
	\upm@highligh@box{#1}{caution.png}%
}{%
	\endupm@highligh@box%
}

\newenvironment{upminfo}[1][\linewidth]{%
	\upm@highligh@box{#1}{info.png}%
}{%
	\endupm@highligh@box%
}

\newenvironment{upmquestion}[1][\linewidth]{%
	\upm@highligh@box{#1}{question.png}%
}{%
	\endupm@highligh@box%
}

%----------------------------------------
% PROVIDE URL MACROS, WHICH WILL BE
% OVERRIDDEN BY THE DOCUMENT CLASS
%----------------------------------------

\newcommand{\url}[2][]{\texttt{#2}}
\newcommand{\href}[3][]{\texttt{#3}}

\endinput