%%%% Class file for Le Matematiche
%%% Author: Giuseppe Bilotta
%%% Email (work): bilotta@dmi.unict.it
%%% Email (home): giuseppe.bilotta@gmail.com

\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{lematema}[2007/02/08 v0.1
Class for articles in ``Le Matematiche'']

\newif\if@langsel
\@langselfalse
%%% Language options
\DeclareOption{english}{\PassOptionsToPackage{\CurrentOption}{babel}\@langseltrue}
\DeclareOption{french}{\PassOptionsToPackage{\CurrentOption}{babel}\@langseltrue}
\DeclareOption{italian}{\PassOptionsToPackage{\CurrentOption}{babel}\@langseltrue}

%%% draft/final
\DeclareOption{draft}{\PassOptionsToClass{\CurrentOption}{article}}
\DeclareOption{final}{\PassOptionsToClass{\CurrentOption}{article}}

\ProcessOptions\relax

\if@langsel\else
\ClassWarning{lematema}{No language specified, defaulting to english}
\PassOptionsToPackage{english}{babel}
\fi

%%% Class based on the standard article class
\LoadClass[10pt,twoside]{article}
%%% Font: times (and math stuff)
\usepackage{mathptmx}
%%% Package `babel' for language management
\RequirePackage{babel}
%%% Package `url' for urls and emails
\RequirePackage{url}
%% Define an \email{...} command
\DeclareUrlCommand\email{\urlstyle{tt}}

%%% AMS math environments and symbols
\RequirePackage{amsmath}
\RequirePackage{amssymb}
\RequirePackage{amsthm}

%%% Bibliography
\RequirePackage[square,numbers,sort&compress,sectionbib]{natbib}
\bibliographystyle{plain}
\def\bibhere{\bibliography{\jobname}}
\addto\captionsitalian{%
  \def\refname{Bibliografia}%
  \def\bibname{Bibliografia}%
}
\addto\captionsenglish{%
  \def\refname{References}%
  \def\bibname{References}%
}
\addto\captionsfrench{%
  \def\refname{Bibliographie}%
  \def\bibname{Bibliographie}%
}
%
\def\bibsection{%
  \par
  \vspace*{2cm}%
  \centering{\MakeUppercase{\refname}}%
  \par
}
\setlength{\bibsep}{3pt}

% %\magnification\magstephalf
%%% Page geometry, using the `geometry' package
\RequirePackage[paperwidth=17cm,%
		paperheight=24cm,%
		footskip=0pt,%
		headsep=0.5cm,%
		headheight=0.5cm,%
		width=12.2cm,%
		%height=18.5cm,% overspecification, comment out
		left=2cm,%
		top=2cm,%
		bottom=2.5cm]{geometry}

%%% Our default head/foot style

\renewcommand{\ps@plain}{%
  \renewcommand{\@oddhead}{\hfill{\footnotesize\@htitle}\hfill\thepage}%
  \renewcommand{\@evenhead}{\thepage\hfill{\footnotesize\@hauthor}\hfill}%
  \renewcommand{\@oddfoot}{}%
  \renewcommand{\@evenfoot}{}%
}
\pagestyle{plain}

\def\@htitle{\textbf{(no title)}}
\def\@hauthor{\textbf{(no author)}}

%%% Footnotes
\renewcommand\footnoterule{%
  \hrule\@width.4\columnwidth
  \kern2.6\p@}

%%% Section styles
\def\@seccntformat#1{\csname the#1\endcsname.\quad}
\renewcommand\section{\@startsection {section}{1}{\z@}%
                                   {-24pt \@plus -6pt \@minus -3pt}%
                                   {\medskipamount}%
                                   {\normalfont\bfseries}}

%%% Theorem environments
\addto\captionsitalian{%
  \def\theoremname{Teorema}%
  \def\corollaryname{Corollario}%
  \def\lemmaname{Lemma}%
  \def\propositionname{Proposizione}%
  \def\definitionname{Definizione}%
  \def\examplename{Esempio}%
  \def\remarkname{Osservazione}%
  \def\notename{Nota}%
}
\addto\captionsenglish{%
  \def\theoremname{Theorem}%
  \def\corollaryname{Corollary}%
  \def\lemmaname{Lemma}%
  \def\propositionname{Proposition}%
  \def\definitionname{Definition}%
  \def\examplename{Example}%
  \def\remarkname{Remark}%
  \def\notename{Note}%
}
\addto\captionsfrench{%
  \def\theoremname{Th\'eor\`eme}%
  \def\corollaryname{Corollaire}%
  \def\lemmaname{Lemma}%
  \def\propositionname{Proposition}%
  \def\definitionname{D\'efinition}%
  \def\examplename{Exemple}%
  \def\remarkname{Remarque}%
  \def\notename{Note}%
}

\theoremstyle{plain}
\newtheorem{thm}{\theoremname}[section]
\newtheorem{cor}{\corollaryname}[section]
\newtheorem{lemma}{\lemmaname}[section]
\newtheorem{prop}{\propositionname}[section]

\theoremstyle{definition}
\newtheorem{dfn}{\definitionname}[section]
\newtheorem{exa}{\examplename}[section]
\newtheorem{rem}{\remarkname}[section]
\newtheorem{note}{\notename}[section]

%%% Commands that define the article information
\let\originaltitlecmd\title
\def\title#1{\originaltitlecmd{#1}\titlemark{#1}}
\def\titlemark#1{\gdef\@htitle{\MakeUppercase{#1}}}
\def\titlenote#1{\expandafter\@titlenote\expandafter{\the\@titlenote\thanks{#1}}}
\newtoks\@titlenote

\def\authormark#1{\gdef\@hauthor{\MakeUppercase{#1}}}
\def\author#1{%
\if@authortoks
\expandafter\@authortoks\expandafter{\the\@authortoks{} - #1}%
\else
\@authortoks{#1}%
\@authortokstrue
\fi
\authormark{\the\@authortoks}%
\def\@author{\the\@authortoks}%
\@lastauthor{#1}%
}
\newtoks\@authortoks
\newif\if@authortoks\@authortoksfalse
\newtoks\@lastauthor

\def\address#1{%
\expandafter\expandafter\expandafter\@addresstoks\expandafter\expandafter\expandafter{%
\expandafter\the\expandafter\@addresstoks\expandafter\par
\expandafter\bigskip
\expandafter\MakeUppercase\expandafter{\the\@lastauthor}\\
#1}%
}
\newtoks\@addresstoks
\def\@address{\par\bigskip\begin{flushright}%
  \itshape
  \the\@addresstoks
\end{flushright}}
\AtEndDocument{\@address}

%% Mathematics Subject Classification
\def\MSC#1{\gdef\@MSC{#1}}
\def\@MSC{}
%% Keywords
\def\keywords#1{\gdef\@keywords{#1}}
\def\@keywords{}

%% Year/Volume/Number/First Page/Last Page
\def\LM@Year{????}
\def\LM@Vol{???}
\def\LM@Num{??}
\def\LM@FirstPg{??}
\def\LM@LastPg{??}

%% Redefine \date to parse a string yyyy/mm/dd, which will be used
%% as submission date
\newcounter{LM@year}
\newcounter{LM@month}
\newcounter{LM@day}
\def\date#1{\LM@parsedate#1\LM@enddate}
\def\LM@parsedate#1/#2/#3\LM@enddate{%
\setcounter{LM@year}{#1}%
\setcounter{LM@month}{#2}%
\setcounter{LM@day}{#3}%
}

%% turn a number/counter into an italian month
\def\@itmonth#1{\ifcase#1\or gennaio\or febbraio\or marzo\or aprile\or
maggio\or giugno\or luglio\or agosto\or settembre\or ottobre\or novembre\or
dicembre\else\textbf{??}\fi}
\def\itmonth#1{\expandafter\@itmonth\csname c@#1\endcsname}

\renewcommand{\maketitle}{%
  \begingroup
    \renewcommand\thefootnote{}%
    \def\@makefnmark{}%
    \long\def\@makefntext##1{\parindent 1em\noindent##1}%
    \newpage
    \global\@topnum\z@   % Prevents figures from going at top of page.
    \@maketitle
    \thispagestyle{empty}\@thanks
  \endgroup
  \setcounter{footnote}{0}%
  \global\let\thanks\relax
  \global\let\maketitle\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
  \global\let\MSC\relax
  \global\let\@MSC\relax
  \global\let\keywords\relax
  \global\let\@keywords\relax
  \global\let\titlenote\relax
  \global\let\@titlenote\relax
}

%% insert the article information in the top right corner
\def\@articleinfo{%
\noindent\footnotesize LE MATEMATICHE\\
Vol.~\LM@Vol~(\LM@Year)~--~Fasc.~\LM@Num,~pp.~\LM@FirstPg--\LM@LastPg
}

%% Make the title page
\def\@maketitle{%
  \newpage
  \@articleinfo
  %% Submission date (at the bottom)
  \thanks{%
    \hskip0.8cm Entrato in redazione \arabic{LM@day} \itmonth{LM@month} \arabic{LM@year}\\}%
  %% MSC, if present
  \ifx\@MSC\empty\else\thanks{%
  \textit{Subject Classification:} \@MSC
  }\fi%
  %% Keywords, if present
  \ifx\@keywords\empty\else\thanks{%
  \textit{Keywords:} \@keywords
  }\fi%
  \vskip 4.5truecm%
  \begin{center}%
  \let \footnote \thanks
    {\def\\{\protect\linebreak}%
    \Large\bfseries \MakeUppercase{\@title}
    %% Title note, if present
    \the\@titlenote
    \par}%
    \vskip 0.5truecm%
    {\Large
      \begin{tabular}[t]{c}%
        \MakeUppercase{\@author}
      \end{tabular}\par}%
  \end{center}%
  \par
  \vskip 0.5truecm}

\renewenvironment{abstract}{%
\small\quotation
}{%
\endquotation
}

% In case the author forgets it:
\AtBeginDocument{\maketitle}

\expandafter\IfFileExists\expandafter{\jobname.pub}{%
  \InputIfFileExists{\jobname.pub}{\typeout{Volume data loaded}}\relax%
  \c@page=\LM@FirstPg\relax
  \AtEndDocument{%
    \@bsphack
    \protected@write\@auxout{}%
      {\string\xdef\string\LM@LastPg{\thepage}}%
    \@esphack
  }%
}{}
