%% $Id: rusphd.cls,v 1.2 2002/07/18 13:39:58 andrei Exp $
%% This is a LaTeX class for Russian dissertation
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%  Copyright 1998-2002 by Andrei G. Demekhov, andrei@appl.sci-nnov.ru
%%  Institute of Applied Physics
%%  603950 Ulyanov st., Nizhny Novgorod, Russia
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Distribution Policy: GNU General Public Licence

\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{rusphd}
              [2002/07/18 v0.9\space A class for Russian dissertation]

\newif\if@mymain \@mymaintrue
\newif\if@avtoref \@avtoreffalse
\newif\if@doctor \@doctorfalse

\DeclareOption{main}{\def\ProcessClass{%
   \LoadClass[12pt,a4paper,titlepage,onecolumn]{report}}%
   \AtEndOfClass{%

\def\@makechapterhead#1{%
  \vspace*{50\p@}%
  {\parindent \z@ \raggedright \normalfont
    \singlespacing
    \ifnum \c@secnumdepth >\m@ne
        \huge\bfseries \@chapapp\space \thechapter
        \par\nobreak
        \vskip 20\p@
    \fi
    \interlinepenalty\@M
    \Huge \bfseries #1\par\nobreak
    \vskip 40\p@
  }}
      \renewcommand\section{\@startsection {section}{1}{\z@}%
                                   {-3.5ex \@plus -1ex \@minus -.2ex}%
                                   {2.3ex \@plus.2ex}%
                                   {\reset@font\Large\bfseries\raggedright\singlespacing}}
      \renewcommand\subsection{\@startsection{subsection}{2}{\z@}%
                                   {-3.25ex\@plus -1ex \@minus -.2ex}%
                                   {1.5ex \@plus .2ex}%
                                   {\reset@font\large\bfseries\raggedright}}
      \renewcommand\subsubsection{\@startsection{subsubsection}{3}{\z@}%
                             {-3.25ex\@plus -1ex \@minus -.2ex}%
                             {1.5ex \@plus .2ex}%
                             {\reset@font\normalsize\bfseries\raggedright}}
      \setstretch{1.7}
   }
}

\DeclareOption{selfref}{\def\ProcessClass{%
   \LoadClass[11pt,a4paper,onecolumn]{article}}%
   \@avtoreftrue
   \@mymainfalse
}

\DeclareOption{zakluch}{\def\ProcessClass{%
   \LoadClass[12pt,a4paper,onecolumn,oneside]{article}}%
   \@mymainfalse
   \AtBeginDocument{\thispagestyle{empty}}
   \AtEndOfClass{%
       \renewcommand\section{\@startsection {section}{1}{\z@}%
                                   {-3.5ex \@plus -1ex \@minus -.2ex}%
                                   {2.3ex \@plus.2ex}%
                                   {\reset@font\normalsize\sf\em\centering}}
       \renewcommand{\refname}{\begin{flushleft}
         Основные результаты диссертации опубликованы в работах:
         \end{flushleft}}
       \setstretch{1.4}
   }
}

\DeclareOption{doctor}{\AtBeginDocument{\def\@doctor{\cyrd\cyro\cyrk\cyrt\cyro\cyrr}\@doctortrue}}
\DeclareOption{candidate}{\AtBeginDocument{\def\@doctor{\cyrk\cyra\cyrn\cyrd\cyri\cyrd\cyra\cyrt}\@doctorfalse}}

\DeclareOption*{\PassOptionsToClass{\CurrentOption}{article}}

\ExecuteOptions{main,candidate}
\ProcessOptions
\ProcessClass
\let\ProcessClass\relax

\RequirePackage{russian,setspace}

\if@mymain
  \newcommand{\main}[1]{#1}
  \newcommand{\nomain}[1]{\relax}
\else
  \newcommand{\main}[1]{\relax}
  \newcommand{\nomain}[1]{#1}
\fi

\if@avtoref
  \newcommand{\avtoref}[1]{#1}
  \newcommand{\noavtoref}[1]{\relax}
  \setlength{\textwidth}{11.5cm}
  \setlength{\textheight}{17.5cm}
  \addtolength{\textheight}{-\footskip}
  \def\titleskip{\hskip 0.3\textwidth}
  \def\titlepar#1{\titleskip\parbox[t]{0.69\textwidth}{\parskip 0\p@ #1}}
  \renewcommand\maketitle{%
  \let\footnotesize\small
  \let\footnoterule\relax
  %\null
  \pagestyle{empty}
  \begin{center}\institute\end{center}
  \vskip 0.05\textheight
  \hskip 0.64\textwidth\parbox[t]{0.35\textwidth}%
                                 {\itshape На правах рукописи}\par
  \vfil
    \begin{center}{%
     \lineskip .75em%
      \begin{tabular}[t]{c}%
        \@author
      \end{tabular}\par}%
  \vskip 0.1\textheight
    {\bfseries \@title \par}%
    \vfil%
    {\SPEC \par}%   
  \vfil
  \vfil
  А~в~т~о~р~е~ф~е~р~а~т\par диссертации на соискание ученой степени\par
  \noindent \@doctor{}а \nauk{} наук\par
  \vfil
  \city\space\avtoref{--\space}\number\phdyear
  \end{center}
  \@thanks
%\vfil
%\null
  \setcounter{footnote}{0}%
  \newpage{\parindent 0\p@ \parskip 1\baselineskip
  Работа выполнена в \institutee{}, г.~\city\par 
  \vfil
  Научный руководитель:\par\titlepar{\advisor}\par
  Официальные оппоненты:\par\titlepar{\oppA}\par\titlepar{\oppB}\par
  Ведущая организация:\par\titlepar{\VedOrg}\par
  \vfil
  Защита состоится \whenphd{} \wherephd\par
  С диссертацией можно ознакомиться в библиотеке \institutea\par
  \vfil
  \parskip 0.5\baselineskip Автореферат разослан \whenavtoref\par
  \vfil
  Ученый секретарь специализированного совета\par%
  \UchSekr\newpage}\pagestyle{plain}
  \let\thanks\relax\let\maketitle\relax
  \let\oppA\relax  \let\oppB\relax  \let\VedOrg\relax
  \let\whenphd\relax  \let\institutea\relax  \let\whenavtoref\relax
  \let\UchSekr\relax%  \gdef\@thanks{}\gdef\@author{}\gdef\@title{}
  } %%% \maketitle
   \AtEndOfClass{\renewcommand{\refname}{Цитированная литература}%
     \setstretch{1}\pagestyle{plain}
     \makeauthorrefs}
   \AtBeginDocument{
     \typein[\phdfile]{Main file name (without extension):}\makeatletter
     \@input{\phdfile.aux}\makeatother\let\phdfile\relax
     \setcounter{page}{1}\sloppy}
\else
  \newcommand{\avtoref}[1]{\relax}
  \newcommand{\noavtoref}[1]{#1}
  \setlength{\topmargin}{-2cm}
  \setlength{\textwidth}{16cm}
  \setlength{\textheight}{24cm}
  \setlength{\evensidemargin}{0.5cm}
  \setlength{\oddsidemargin}{0.5cm}
  \pagestyle{myheadings}
  \markright{\thepage}
  \renewcommand{\rightmark}{\hfill--}
  \renewcommand\maketitle{\main{\begin{titlepage}}%
  \let\footnotesize\small
  \let\footnoterule\relax
  %\null
  \begin{singlespace}\begin{center}\institute\end{center}
  \vskip 0.05\textheight
  \hskip 0.68\textwidth\parbox[t]{0.3\textwidth}{\itshape На правах рукописи}\par
  \vfil
    \begin{center}{\large
     \lineskip .75em%
      \begin{tabular}[t]{c}%
        \@author
      \end{tabular}\par}%
  \vskip 0.1\textheight
    {\Large \@title \par}%
    \vfil%
    {\SPEC \par}%   
  \end{center}
  \vfil
  Диссертация на соискание ученой степени\par
  \noindent \@doctor{}а \nauk{} наук\par
  \vfil
  \hskip 0.5\textwidth\parbox[t]{0.45\textwidth}{\parindent 0\p@ Научный
    \if@doctor{консультант}\else{руководитель}\fi:\\
    \advisor}
  \vfil
  \centerline{\city\space\avtoref{--\space}\number\phdyear}
  \end{singlespace}\par
  \@thanks
  %\vfil
  %\null
  \end{titlepage}%
  \setcounter{footnote}{0}%
  \let\thanks\relax\let\maketitle\relax
  \gdef\@thanks{}\gdef\@author{}\gdef\@title{}}
\fi %%% \@avtoref

% We define 'enumerate' environment so as items look like usual paragraphs;
% here patches from LTLISTS.DTX and from THEAPA.STY by Ruy Young are used
%
\def\enumerate{%
    \ifnum \@enumdepth >3 \@toodeep\else
      \advance\@enumdepth \@ne
      \edef\@enumctr{enum\romannumeral\the\@enumdepth}
    \setbox0\hbox{1. }%
    \list{\csname label\@enumctr\endcsname}{\usecounter{\@enumctr}
    \labelwidth\z@\labelsep\z@\leftmargin\z@\parsep0.5\parskip
    \rightmargin\z@\itemsep\parskip\topsep\z@\partopsep\z@
    \setlength{\itemindent}{\parindent}\advance\itemindent by\wd0
    \def\makelabel##1{\hss\llap{##1 }}}\fi}
\let\endenumerate=\endlist

\let\Huge\LARGE
\let\huge\LARGE
\setstretch{1.7}
\reversemarginpar
\setcounter{secnumdepth}{3}


\renewcommand{\bibname}{Литература}
\renewcommand{\@biblabel}[1]{#1.}  %% No brackets in Reference list

%%% Commands to handle author's own bibliography as a separate list

\newif\if@myref \@myreffalse
\def\makeauthorrefs{\if@filesw \newwrite\@myrefaux \newwrite \@tempaux
  \immediate\openout\@myrefaux myrefs.aux \@myreftrue
  \typeout {Writing AUX file for author refs: MYREFS.AUX}\fi}
\def\mybibliography#1{\if@myref
     \if@filesw \immediate\write\@myrefaux{\string\bibdata{#1}}\fi%
       \InputIfFileExists{myrefs.bbl}{\if@mymain\newpage\label{myrefs}\fi%
        \let\mytemp\refname%
        \renewcommand{\refname}{\cyrS\cyrp\cyri\cyro\cyrk \cyrr\cyra\cyrb\cyro\cyrt \cyra\cyrv\cyrt\cyro\cyrr\cyra \cyrp\cyro \cyrt\cyre\cyrm\cyre \cyrd\cyri\cyrs\cyrs\cyre\cyrr\cyrt\cyra\cyrc\cyri\cyri}%
       }{\typeout{No file myrefs.bbl}}%
       \let\refname\mytemp \let\mytemp\relax
     \fi}
\def\mybibliographystyle#1{\if@myref\if@filesw\immediate\write\@myrefaux%
    {\string\bibstyle{#1}}\fi \fi}
\def\mycite#1{%
   \if@myref \let\@tempaux\@auxout \let\@auxout\@myrefaux
      \avtoref{\nocite{#1}}\main{\emph{\cite{#1}}\myrefnote}
      \let\@auxout\@tempaux
   \else \myrefwarn\cite{#1}\fi
   \let\myrefwarn\relax \let\myrefnote\relax}

\AtBeginDocument{%
   \if@myref
        \def\myrefnote{\footnote{Ссылки на работы автора по теме диссертации
        выделены курсивом, см.\ список работ на с.~\pageref{myrefs}}}
   \else
        \def\myrefwarn{\PackageWarningNoLine{rusphd}{%
                Command `\protect\makeauthorrefs' not 
                found in preamble,\MessageBreak
                author references will be placed\MessageBreak
                in the main Reference list}}
   \fi%
}

% Библиография
\renewenvironment{thebibliography}[1]
     {\section*{\refname
        \@mkboth{\uppercase{\refname}}{\uppercase{\refname}}}%
      \begingroup\avtoref{\small}
      \list{\@biblabel{\arabic{enumiv}}}%
           {\settowidth\labelwidth{\@biblabel{#1}}%
            \leftmargin\labelwidth
            \advance\leftmargin\labelsep\itemsep \z@
            \if@openbib
              \advance\leftmargin\bibindent
              \itemindent -\bibindent
              \listparindent \itemindent
              \parsep \z@
            \fi
            \usecounter{enumiv}%
            \let\p@enumiv\@empty
            \renewcommand\theenumiv{\arabic{enumiv}}}%
      \if@openbib
        \renewcommand\newblock{\par}
      \else
        \renewcommand\newblock{\hskip .11em \@plus.33em \@minus.07em}%
      \fi
      \sloppy\clubpenalty4000\widowpenalty4000%
      \sfcode`\.=\@m}
     {\def\@noitemerr
       {\@latex@warning{Empty `thebibliography' environment}}%
      \endlist\endgroup}
\newcommand{\institute}{Организация,\\ где выполнена работа}
\newcommand{\institutee}{Организация в дат.\ падеже}
\newcommand{\institutea}{Организация в род. падеже}
\newcommand{\city}{город}
\newcommand{\phdyear}{\year}
\newcommand{\SPEC}{Специальность}
\newcommand{\nauk}{каких-то там}
\newcommand{\advisor}{ОЧЕНЬ хороший человек!}
\newcommand{\oppA}{1-й оппонент --- Замечательный Человек!}
\newcommand{\oppB}{и 2-й --- Чудеснейший Человек!}
\newcommand{\oppC}{а уж 3-й --- Просто Уникальной Души Человек!}
\newcommand{\VedOrg}{передовое, ведущее предприятие!}
\newcommand{\whenphd}{Когда?}
\newcommand{\wherephd}{Где??}
\newcommand{\whenavtoref}{Когда??}
\newcommand{\UchSekr}{Достойнейший \hfill человек!}
\newcommand{\mytemp}{\relax}

\newcommand{\chapres}[1]{%
  \section{Основные результаты главы \ref{chp:#1}}
  \label{sec:#1-res}
  \InputIfFileExists{#1-res}{}{\typeout{No file #1-res.tex}}
}

%% Дополнительные определения
% варианты для 2-х и 1- колонки
\providecommand\iftwocol[2]{\if@twocolumn{#1}\else {#2}\fi}