% misccorr package
% Based on `russcorr' by Serge Lvovski and other sources.
% This package should be used IN ADDITION to BABEL's `russian' option.
% Mail bug reports, questions, comments, suggestions to vvv@vsu.ru
\ProvidesPackage{misccorr}[1999/09/23 v0.1
  misc corrections for russian typesetting]

\newif\if@longtoc
\DeclareOption{longtoc}{\@longtoctrue}
\DeclareOption{shorttoc}{\@longtocfalse}
\newif\if@indentheadings
\DeclareOption{indentheadings}{\@indentheadingstrue}
\DeclareOption{noindentheadings}{\@indentheadingsfalse}
% Note that the default LaTeX behavior is \@longtocfalse
\ExecuteOptions{longtoc,noindentheadings}
\ProcessOptions

% \varkappa, \leqslant, \geqslant, etc (in addition to \varepsilon, \varphi)
\RequirePackage{amssymb}

\RequirePackage{enumerate}[1999/03/05]
\g@addto@macro\@enhook{%
  \def\@tempb{\IeC{\CYRA}}%
  \def\@tempc{\IeC{\cyra}}%
  \ifx\@tempb\@entemp\def\@tempa{\@enLabel\Asbuk}\fi
  \ifx\@tempc\@entemp\def\@tempa{\@enLabel\asbuk}\fi}

% lists
% Теперь "русифицируем" окружение enumerate
\def\labelenumi{\theenumi)}
\def\theenumii{\asbuk{enumii}}
\def\labelenumii{\theenumii)}
\def\p@enumii{\theenumi}
\def\labelenumiii{{\bf--}}
\let\theenumiii\relax
\def\p@enumiii{\theenumi\theenumii}
%
% И еще запретим вкладывать четыре enumerate друг в друга ...
%
\def\enumerate{\ifnum \@enumdepth >2% было 3, а не 2
\@toodeep\else
      \advance\@enumdepth \@ne
      \edef\@enumctr{enum\romannumeral\the\@enumdepth}\list
      {\csname label\@enumctr\endcsname}{\usecounter
        {\@enumctr}\def\makelabel##1{\hss\llap{##1}}}\fi}
%
% Теперь определим окружение rlist
% для создания списков, в которых
% \item'ы нумеруются русскими буквами
% (строчными для rlist, прописными для rlist[u])
%
\@definecounter{rlistctr}
\newif\if@rlistsnested\@rlistsnestedfalse
\def\rlist{\@ifnextchar[{\@rlist}{\@rlist[l]}}
\def\@rlist[#1]{\if u#1\def\@tempa{A}\else\def\@tempa{a}\fi
                \if@rlistsnested\@toodeep\else
                \@rlistsnestedtrue
                \edef\therlistctr{\expandafter\noexpand\csname
                                @\@tempa sbuk\endcsname\noexpand\c@rlistctr}%
                \list{\labelrlist}{\usecounter
                   {rlistctr}}\fi}
\let\endrlist\endlist
\def\labelrlist{\therlistctr)}

% Unfortunately, \@seccntformat is not reflected in toc.
% Hence, we had to hack up the definition of \@sect a little.
% In the LaTeX definition of |\@sect| we changed in two places
% |\numberline{\csname the#1\endcsname}| to
% |\numberline{\@tocseccntformat{#1}\relax}|
% and |#7| to |\if@longtoc#8\else#7\fi|.
% We preferred not to change |\numberline| e.g.
% |\def\numberline#1{\hb@xt@\@tempdima{#1.\hfil}}|
\def\@sect#1#2#3#4#5#6[#7]#8{%
  \ifnum #2>\c@secnumdepth
    \let\@svsec\@empty
  \else
    \refstepcounter{#1}%
    \protected@edef\@svsec{\@seccntformat{#1}\relax}%
  \fi
  \@tempskipa #5\relax
  \ifdim \@tempskipa>\z@
    \begingroup
      #6{%
        \@hangfrom{\hskip #3\relax\@svsec}%
          \interlinepenalty \@M #8\@@par}%
    \endgroup
    \csname #1mark\endcsname{#7}%
    \addcontentsline{toc}{#1}{%
      \ifnum #2>\c@secnumdepth \else
        \protect\numberline{\@tocseccntformat{#1}\relax}%
      \fi
      \if@longtoc#8\else#7\fi}%
  \else
    \def\@svsechd{%
      #6{\hskip #3\relax
      \@svsec #8}%
      \csname #1mark\endcsname{#7}%
      \addcontentsline{toc}{#1}{%
        \ifnum #2>\c@secnumdepth \else
          \protect\numberline{\@tocseccntformat{#1}\relax}%
        \fi
        \if@longtoc#8\else#7\fi}}%
  \fi
  \@xsect{#5}}
\def\@seccntformat#1{\csname the#1\endcsname.\enskip}
\def\@tocseccntformat#1{\csname the#1\endcsname.}

\if@indentheadings
\def\section{\@startsection {section}{1}{\parindent}%
                                   {3.5ex \@plus 1ex \@minus .2ex}%
                                   {2.3ex \@plus.2ex}%
                                   {\reset@font\Large\bfseries}}
\def\subsection{\@startsection{subsection}{2}{\parindent}%
                                     {3.25ex\@plus 1ex \@minus .2ex}%
                                     {1.5ex \@plus .2ex}%
                                     {\reset@font\large\bfseries}}
\def\subsubsection{\@startsection{subsubsection}{3}{\parindent}%
                                     {3.25ex\@plus 1ex \@minus .2ex}%
                                     {1.5ex \@plus .2ex}%
                                     {\reset@font\normalsize\bfseries}}
\fi

\@ifundefined{chapter}{}{%
\def\postchapter{.}
\def\chapter{\if@openright\cleardoublepage\else\clearpage\fi
                    \thispagestyle{plain}%
                    \global\@topnum\z@
                    \if@indentheadings\@afterindenttrue
                        \else\@afterindentfalse
                    \fi
                    \secdef\@chapter\@schapter}
\def\@chapter[#1]#2{\ifnum \c@secnumdepth >\m@ne
                       \if@mainmatter
                         \refstepcounter{chapter}%
                         \typeout{\@chapapp\space\thechapter.}%
                         \addcontentsline{toc}{chapter}%
                                   {\protect\numberline{\thechapter
                                         \postchapter}%
                        \if@longtoc#2\else#1\fi}%
                       \else
                         \addcontentsline{toc}{chapter}%
                       {\if@longtoc#2\else#1\fi}\fi
                    \else
                      \addcontentsline{toc}{chapter}{\if@longtoc#2\else#1\fi}
                    \fi
                    \chaptermark{#1}%
                    \addtocontents{lof}{\protect\addvspace{10\p@}}%
                    \addtocontents{lot}{\protect\addvspace{10\p@}}%
                    \if@twocolumn
                      \@topnewpage[\@makechapterhead{#2}]%
                    \else
                      \@makechapterhead{#2}%
                      \@afterheading
                    \fi}
}% end of conditional
\@ifundefined{chapter}{}{%
\def\@makechapterhead#1{%
  \vspace*{50\p@}%
  {\reset@font \@tempdima\parindent \raggedright
     \if@indentheadings\advance\leftskip\@tempdima\fi
    \ifnum \c@secnumdepth >\m@ne
       \if@mainmatter
         \huge\bfseries \@chapapp{} \thechapter\postchapter
         \par\nobreak
         \vskip 20\p@
       \fi
       \fi
    \interlinepenalty\@M
    \Huge \bfseries #1\par
    \nobreak
    \vskip 40\p@
  }}%
\def\@schapter#1{\if@twocolumn
                   \@topnewpage[\@makeschapterhead{#1}]%
                 \else
                   \@makeschapterhead{#1}%
                   \@afterheading
                 \fi}
\def\@makeschapterhead#1{%
  \vspace*{50\p@}%
  {\reset@font \@tempdima\parindent \raggedright
     \if@indentheadings\advance\leftskip\@tempdima\fi
    \interlinepenalty\@M
    \Huge \bfseries  #1\par\nobreak
    \nobreak
    \vskip 40\p@
  }}
}% end of conditional

% captions
\long\def\@makecaption#1#2{%
  \vskip\abovecaptionskip
  \sbox\@tempboxa{#1. #2}%
  \ifdim \wd\@tempboxa >\hsize
    #1. #2\par
%   \unhbox\@tempboxa\par
  \else
    \global \@minipagefalse
    \hb@xt@\hsize{\hfil\box\@tempboxa\hfil}%
  \fi
  \vskip\belowcaptionskip}

\long\def\@caption#1[#2]#3{\par\addcontentsline{\csname
  ext@#1\endcsname}{#1}{\protect\numberline{\csname
  the#1\endcsname.}{\ignorespaces #2}}\begingroup
    \@parboxrestore
    \normalsize
    \@makecaption{\csname fnum@#1\endcsname}{\ignorespaces #3}\par
  \endgroup}

% theorems
\def\afterthmseparator{.}
\expandafter\ifx\csname theoremstyle\endcsname\relax
  \def\@begintheorem#1#2{\trivlist
     \item[\hskip \labelsep{\bfseries #1\ #2\unskip\afterthmseparator
  }]\itshape}
  \def\@opargbegintheorem#1#2#3{\trivlist
        \item[\hskip \labelsep{\bfseries #1\ #2\ (#3)\afterthmseparator
  }]\itshape}
\fi

% class-dependent redefinitions: appendix and running heads
\@ifundefined{chapter}{% article
\renewcommand\appendix{\par
  \setcounter{section}{0}%
  \setcounter{subsection}{0}%
  \gdef\thesection{\@Asbuk\c@section}}
% вставлено ".\@aftersepkern" после номера
\if@twoside
  \def\ps@headings{%
      \let\@oddfoot\@empty\let\@evenfoot\@empty
      \def\@evenhead{\thepage\hfil\slshape\leftmark}%
      \def\@oddhead{{\slshape\rightmark}\hfil\thepage}%
      \let\@mkboth\markboth
    \def\sectionmark##1{%
      \markboth {\MakeUppercase{%
        \ifnum \c@secnumdepth >\z@
          \thesection\postsection\quad
        \fi
        ##1}}{}}%
    \def\subsectionmark##1{%
      \markright {%
        \ifnum \c@secnumdepth >\@ne
          \thesubsection\postsubsection\quad
        \fi
        ##1}}}
\else
  \def\ps@headings{%
    \let\@oddfoot\@empty
    \def\@oddhead{{\slshape\rightmark}\hfil\thepage}%
    \let\@mkboth\markboth
    \def\sectionmark##1{%
      \markright {\MakeUppercase{%
        \ifnum \c@secnumdepth >\m@ne
          \thesection\postsection\quad
        \fi
        ##1}}}}
\fi
}{% not article
\renewcommand\appendix{\par
  \setcounter{chapter}{0}%
  \setcounter{section}{0}%
  \gdef\@chapapp{\appendixname}%
  \gdef\thechapter{\@Asbuk\c@chapter}}
\@ifundefined{abstractname}{}{\let\if@mainmatter\iftrue}% report
% два пробела после точки заменены на один
\if@twoside
  \def\ps@headings{%
      \let\@oddfoot\@empty\let\@evenfoot\@empty
      \def\@evenhead{\thepage\hfil\slshape\leftmark}%
      \def\@oddhead{{\slshape\rightmark}\hfil\thepage}%
      \let\@mkboth\markboth
    \def\chaptermark##1{%
      \markboth {\MakeUppercase{%
        \ifnum \c@secnumdepth >\m@ne
          \if@mainmatter
            \@chapapp\ \thechapter. %
          \fi
        \fi
        ##1}}{}}%
    \def\sectionmark##1{%
      \markright {\MakeUppercase{%
        \ifnum \c@secnumdepth >\z@
          \thesection. %
        \fi
        ##1}}}}
\else
  \def\ps@headings{%
    \let\@oddfoot\@empty
    \def\@oddhead{{\slshape\rightmark}\hfil\thepage}%
    \let\@mkboth\markboth
    \def\chaptermark##1{%
      \markright {\MakeUppercase{%
        \ifnum \c@secnumdepth >\m@ne
          \if@mainmatter
            \@chapapp\ \thechapter. %
          \fi
        \fi
        ##1}}}}
\fi
}

% \cleardoublepage
% добавил \ps@empty (и заодно заменил \hbox{} на \null)
\def\cleardoublepage{\clearpage\if@twoside \ifodd\c@page\else
  {\null\ps@empty\newpage}\if@twocolumn{\null\ps@empty\newpage}\fi\fi\fi}

\AtBeginDocument{\ifdim\hsize<\@cclv\p@\pretolerance\m@ne\fi}
\endinput
