\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{artdocupv}[2018/03/22 Editorial de la Universitat Politècnica de València]

% ---------------------------------------------------------------------

\RequirePackage{ifthen}

\newboolean{valencia}\setboolean{valencia}{false}
\newboolean{castellano}\setboolean{castellano}{false}
\newboolean{english}\setboolean{english}{false}


% ---------------------------------------------------------------------

\newcommand{\distanciesEquacions}{1}
\DeclareOption{nomathskip}{\renewcommand{\distanciesEquacions}{0}}

\DeclareOption{valencia}{%
	\setboolean{valencia}{true}
	\setboolean{castellano}{false}
	\setboolean{english}{false}
	}
\DeclareOption{castellano}{%
	\setboolean{valencia}{false}
	\setboolean{castellano}{true}
	\setboolean{english}{false}
	}
\DeclareOption{english}{%
	\setboolean{valencia}{false}
	\setboolean{castellano}{false}
	\setboolean{english}{true}
	}
	
% ---------------------------------------------------------------------

\DeclareOption*{\ClassWarning{artdocupv}{No podem processar l'opci\'{o}: ?\CurrentOption?}}

% ---------------------------------------------------------------------

\ExecuteOptions{castellano}
\ProcessOptions\relax

% ---------------------------------------------------------------------

\LoadClass[10pt]{article}


% ---------------------------------------------------------------------
% Paquets que necessitem

\RequirePackage{tikz}
\RequirePackage{colortbl}

\RequirePackage{graphicx}
\RequirePackage{calc}
\RequirePackage{lastpage}
\RequirePackage{xcolor}

\colorlet{BlauArticlesUPV}{blue!30!black}

% ---------------------------------------------------------------------
% Portada

  \renewcommand\maketitle{
  \begin{titlepage}%
  \let\footnotesize\small
  \let\footnoterule\relax
  \let \footnote \thanks
  \null\vfil
  \begin{center}%
  \vskip -75\p@
  {\includegraphics[width=6.0cm]{logo_UPV_horitzontal}}
  \vskip 150\p@
%	\rule{\textwidth}{1pt}\par
%    \vskip 1.5em%
    {\Huge\bfseries\color{BlauArticlesUPV} \@title \par}%
%    \vskip 1.5em%
%	\rule{\textwidth}{1pt}\par    
    {\normalsize
     \lineskip .75em%
	  \vskip 175\p@     
      \begin{tabular}[t]{c}%
        \@author\par
      \end{tabular}\par}%
	  \vskip 60\p@     
%    {\large \@date \par}%       % Set date in \large size.
%  \@thanks
  \vfil\null
  \end{center}\par
  \end{titlepage}%
  \setcounter{footnote}{0}%
  \global\let\thanks\relax
  \global\let\maketitle\relax
  \global\let\@thanks\@empty
  \global\let\@author\@empty
  \global\let\@date\@empty
  \global\let\@title\@empty
  \global\let\title\relax
  \global\let\author\relax
  \global\let\date\relax
  \global\let\and\relax
}

% ---------------------------------------------------------------------
% Tipus de lletra

\renewcommand{\normalsize}{\fontsize{11pt}{13pt}\selectfont}

\renewcommand{\familydefault}{\sfdefault}	

% ---------------------------------------------------------------------
% Format de pàgina

\RequirePackage{geometry}

	
	\geometry{ 
		a4paper, twoside=off,       % A4 a una cara
		hmargin = {3.0cm, 3.0cm}, 	% {Izquierdo, Derecho}
		vmargin = {1.25cm, 1.75cm}, % {Superior, Inferior} 
		includehead, includefoot, 	% Incluyendo encabezado y pie
		headsep = 1.75cm,          	% Separaci?n entre el encabezado y el texto
		footskip = 0.0cm,        	% Separaci?n entre el texto y el pie
		footnotesep = 1cm,
		}


% ---------------------------------------------------------------------
% Capaçaleres i peus de pàgina

\RequirePackage{fancyhdr}
\pagestyle{fancy}

\fancyhead{} % Esborra la configuració de la capçalera
\fancyfoot{} % Esborra la configuració del peu


\fancyhead[L]{\includegraphics[height=1cm]{logo_UPV_horitzontal}}
\fancyfoot[R]{\ifvalencia Pàgina\else Página\fi\ \textbf{\thepage} de \textbf{\pageref*{LastPage}}}
\renewcommand{\headrulewidth}{0.0pt}
\renewcommand{\footrulewidth}{0.0pt}


% ---------------------------------------------------------------------
% Format de paràgraf i maquetació

\setlength{\parskip}{2ex}
\setlength{\parindent}{0pt}

\linespread{1.0}

\setlength{\widowpenalty}{10000pt}
\setlength{\clubpenalty}{10000pt}

\raggedbottom


% ---------------------------------------------------------------------
% Millora els títols de les figures y taules

\RequirePackage{caption}
\renewcommand{\captionlabelfont}{\bfseries\small}
\renewcommand{\captionfont}{\small}

% ---------------------------------------------------------------------
% Formato de las secciones

\RequirePackage[
	raggedright,
	compact,
	nobottomtitles*, % Evita que queden títols solts al final de la pàgina
	clearempty, % Modifica \cleardoublepage per a que els fulls parells buides siguen blanques
	]{titlesec}

% ---------------------------------------------------------------------

	\titleformat{\section}
		[hang]
		{\vspace{2ex}\raggedright\tolerance=10000\hyphenpenalty=10000}
		{\tikz[overlay]\node[draw=none, line width=0pt, above left, inner sep=0pt] at (-1.25em,0) 
			{\Large\bfseries\color{BlauArticlesUPV}\thesection};}
		{0em}
		{\Large\bfseries\color{BlauArticlesUPV}}
		
	\titleformat{\subsection}
		[hang]
		{\vspace{1.5ex}\raggedright\tolerance=10000\hyphenpenalty=10000}
		{\large\bfseries\color{BlauArticlesUPV}\thesubsection}
		{1em}
		{\large\bfseries\color{BlauArticlesUPV}}
		[\vspace{-1ex}]

    \titleformat{\subsubsection}
    	[hang]
    	{\vspace{2ex}\raggedright\tolerance=10000\hyphenpenalty=10000}
    	{\normalsize\itshape\bfseries\color{BlauArticlesUPV}\thesubsubsection}
    	{1em}
    	{\normalsize\itshape\bfseries\color{BlauArticlesUPV}}
    	[\vspace{-0.75ex}]


% ---------------------------------------------------------------------
% Per controlar la numeració i el format de la taula de continguts

\RequirePackage{enumitem}
\setlist[1]{partopsep=-1ex,parsep=\parskip,itemsep=0\parskip}
\setlist[2]{partopsep=-1ex,parsep=\parskip,itemsep=0\parskip}


% ---------------------------------------------------------------------
% ---------------------------------------------------------------------
% ---------------------------------------------------------------------
% ---------------------------------------------------------------------
% ---------------------------------------------------------------------

% authblk.sty

%\RequirePackage[blocks]{authblk}


\ifcastellano
	\newcommand{\etiquetaApellidos}{Apellidos y nombre}
	\newcommand{\etiquetaDepartamento}{Departamento/Centro}
\fi	
\ifvalencia
	\newcommand{\etiquetaApellidos}{Cognoms i nom}
	\newcommand{\etiquetaDepartamento}{Departament/Centre}
\fi	
\ifenglish
	\newcommand{\etiquetaApellidos}{Surname and first name}
	\newcommand{\etiquetaDepartamento}{Department}
\fi	


\newcommand\Authsep{\\[0.5ex] } 
\newcommand\Authand{\\[0.5ex] } 
\newcommand\Authands{\\[0.5ex] }

\newcommand\Authfont{\normalfont}
\newcommand\Affilfont{\normalfont}

\newlength{\affilsep}\setlength{\affilsep}{1em}
\newlength{\@affilsep}
\newcounter{Maxaffil}
\setcounter{Maxaffil}{2}

\newcounter{authors}
\newcounter{affil}
\newif\ifnewaffil \newaffiltrue
\newcommand\AB@authnote[1]{\textsuperscript{\normalfont#1}}
\newcommand\AB@affilnote[1]{\textsuperscript{\normalfont#1}}
\providecommand\textsuperscript[1]{$^{#1}$}
\newcommand\AB@blk@and{\protect\Authfont\protect\AB@setsep}
\newcommand\AB@pand{\protect\and \protect\Authfont \protect\AB@setsep}
\@namedef{@sep1}{}
\@namedef{@sep2}{\Authand}
\newcommand\AB@affilsep{\protect\Affilfont}
%\newcommand\AB@affilsepx{\protect\\\protect\Affilfont}
\newcommand\AB@affilsepx{\protect\\[2ex]}
\newcommand\AB@setsep{\setlength{\@affilsep}{\affilsep}}
\newcommand\AB@resetsep{\setlength{\@affilsep}{\z@}}
\newcommand\AB@authlist{}
\newcommand\AB@affillist{}
\newcommand\AB@centlist{}
\newcommand\AB@authors{}
\newcommand\AB@empty{}
\xdef\AB@author{\noexpand\AB@blk@and\@author}
\renewcommand\author[2][]%
      {\ifnewaffil\addtocounter{affil}{1}%
       \edef\AB@thenote{\arabic{affil}}\fi
      \if\relax#1\relax\def\AB@note{\AB@thenote}\else\def\AB@note{#1}%
        \setcounter{Maxaffil}{0}\fi
      \ifnum\value{authors}>1\relax
      \@namedef{@sep\number\c@authors}{\Authsep}\fi
      \addtocounter{authors}{1}%
      \begingroup
          \let\protect\@unexpandable@protect \let\and\AB@pand
          \def\thanks{\protect\thanks}\def\footnote{\protect\footnote}%
         \@temptokena=\expandafter{\AB@authors}%
         {\def\\{\protect\\[\@affilsep]\protect\Affilfont
              \protect\AB@resetsep}%
              \xdef\AB@author{\AB@blk@and#2}%
       \ifnewaffil\gdef\AB@las{}\gdef\AB@lasx{\protect\Authand}\gdef\AB@as{}%
           \xdef\AB@authors{\the\@temptokena\AB@blk@and}%
       \else
          \xdef\AB@authors{\the\@temptokena\AB@as\AB@au@str}%
          \global\let\AB@las\AB@lasx\gdef\AB@lasx{\protect\Authands}%
          \gdef\AB@as{\Authsep}%
       \fi
       \gdef\AB@au@str{#2}}%
         \@temptokena=\expandafter{\AB@authlist}%
         \let\\=\authorcr
         \xdef\AB@authlist{\the\@temptokena
           \protect\@nameuse{@sep\number\c@authors}%
           \protect\Authfont#2\AB@authnote{\AB@note}}%
      \endgroup
      \ifnum\value{authors}>2\relax
      \@namedef{@sep\number\c@authors}{\Authands}\fi
      \newaffilfalse
}
\newcommand\authorcr{\protect\\ \protect\Authfont \protect\AB@setsep}%
\newcommand\affil[2][]%
   {\newaffiltrue\let\AB@blk@and\AB@pand
      \if\relax#1\relax\def\AB@note{\AB@thenote}\else\def\AB@note{#1}%
        \setcounter{Maxaffil}{0}\fi
      \begingroup
        \let\protect\@unexpandable@protect
        \def\thanks{\protect\thanks}\def\footnote{\protect\footnote}%
        \@temptokena=\expandafter{\AB@authors}%
        {\def\\{\protect\\\protect\Affilfont}\xdef\AB@temp{#2}}%
         \xdef\AB@authors{\the\@temptokena\AB@las\AB@au@str
         \protect\\[\affilsep]\protect\Affilfont\AB@temp}%
         \gdef\AB@las{}\gdef\AB@au@str{}%
        {\def\\{\par \ignorespaces}\xdef\AB@temp{#2}}%
        \@temptokena=\expandafter{\AB@affillist}%
        \xdef\AB@affillist{\the\@temptokena \AB@affilsep
          \AB@affilnote{\AB@note}\protect\Affilfont\AB@temp}%
      \endgroup
       \let\AB@affilsep\AB@affilsepx
}
\def\@author{}
\renewcommand\@author{\ifx\AB@affillist\AB@empty\AB@author\else
      \ifnum\value{affil}>\value{Maxaffil}\def\rlap##1{##1}%
    \mbox{\parbox[t]{4.5cm}{\hfill\bfseries\etiquetaApellidos:}\hspace{1em}%
    	\parbox[t]{11.5cm}{\AB@authlist}}\\[\affilsep]%
    \mbox{\parbox[t]{4.5cm}{\hfill\bfseries\etiquetaDepartamento:}\hspace{1em}%
    	\parbox[t]{11.5cm}{\AB@affillist}}
    \else  
    \AB@authors
%    \mbox{\parbox[t]{4.25cm}{\mdseries Apellidos y nombre:}\hspace{1em}%
%    	\parbox[t]{11cm}{\AB@authlist}}\\[\affilsep]%
%    \mbox{\parbox[t]{4.25cm}{\mdseries Departamento/Centro:}\hspace{1em}%
%    	\parbox[t]{11cm}{\AB@affillist}}
    \fi\fi}
\let\AB@maketitle=\maketitle
\def\maketitle
  {{\renewenvironment{tabular}[2][]{\begin{flushright}}
                                   {\end{flushright}}
  \AB@maketitle}}



% ---------------------------------------------------------------------
% ---------------------------------------------------------------------
% ---------------------------------------------------------------------
% ---------------------------------------------------------------------
% ---------------------------------------------------------------------
% ---------------------------------------------------------------------


\RequirePackage{mathtools}

\ifnum\distanciesEquacions=1
\AtBeginDocument
	{
	% ---------------------------------------------------
	% Dist?ncies de les equacions al text
	
	% Per a les equacions normals
	\abovedisplayshortskip = -1.0ex plus 0ex minus 0.25ex
	\belowdisplayshortskip = 2.0ex plus 1ex minus 0.0ex
	
	% Per a les equacions en varies línies
	\abovedisplayskip = -1.0ex plus 0ex minus 0.25ex
	\belowdisplayskip = 2.0ex plus 1ex minus 0.0ex
	}
\fi

% ------------------------------------------------------------------------
% ------------------------------------------------------------------------
% ------------------------------------------------------------------------
