%%
%% This file contains the main style commands
%%
%% Some options can be set by changing the \loaddefaults command
%%

\usepackage{color}
\usepackage{marginnote}

\def\loaddefaults{
  \sectionstrue
  \subseqcountfalse
  \def\seqskip{\vskip1.5cm}
  \def\subseqskip{\vskip1cm}
  \resetpointattheofalse
  \parindent=0pt
  \def\indent{\hskip20pt}
}

% false if there are no sections
\newif\ifsections
% true if equation numbers should include the subsection number
\newif\ifsubseqcount
% true if there is a table of contents
\newif\iftoc
% true if point counting should reset at each theorem
\newif\ifresetpointattheo

% a prefix to put before the section number, e.g. A for appendices
\def\sectionprefix{}

\loaddefaults

%% style for the equation number
\def\eqnumstyle{}


%% define a command and write it to aux file
\def\outdef#1#2{%
% define command
\expandafter\xdef\csname #1\endcsname{\noexpand\hyperlink{page.\the\count0}{#2}}%
% write command to aux
\immediate\write\@auxout{\noexpand\expandafter\noexpand\gdef\noexpand\csname #1\endcsname{\noexpand\hyperlink{page.\thepage}{#2}}}%
% define ref with no link
\expandafter\xdef\csname #1@nol\endcsname{#2}%
% write command to aux
\immediate\write\@auxout{\noexpand\expandafter\noexpand\gdef\noexpand\csname #1@nol\endcsname{#2}}%
}

%% define a label for the latest tag
%% label defines a command containing the string stored in \tag
\AtBeginDocument{
  \def\label#1{\expandafter\outdef{#1}{\safe\tag}}
  \def\ref#1{\expandafter\safe\csname #1\endcsname}
}

%% counters
\newcounter{sectioncount}
\newcounter{subsectioncount}
\newcounter{pointcount}
\newcounter{subpointcount}
\newcounter{subsubpointcount}
\newcounter{seqcount}
\newcounter{figcount}
\newcounter{Theocount}
\newcounter{tocsectioncount}
\newcounter{tocsubsectioncount}

%% section command
\newlength\secnumwidth
\newlength\sectitlewidth
\def\section#1{%
% reset counters
\refstepcounter{sectioncount}%
\setcounter{subsectioncount}{0}%
\setcounter{pointcount}{0}%
\setcounter{subpointcount}{0}%
\setcounter{subsubpointcount}{0}%
\setcounter{figcount}{0}%
\setcounter{Theocount}{0}%
\setcounter{seqcount}{0}%
% space before section (if not first)
\ifnum\thesectioncount>1%
\seqskip%
\fi%
% get widths
\def\@secnum{{\bf\Large\sectionprefix\thesectioncount.\hskip10pt}}%
\settowidth\secnumwidth{\@secnum}%
\setlength\sectitlewidth\textwidth%
\addtolength\sectitlewidth{-\secnumwidth}%
% print name
\parbox{\textwidth}{%
\@secnum%
\parbox[t]{\sectitlewidth}{\Large\bf #1}%
\par\penalty10000%
\bigskip}\par\penalty10000%
% define tag (for \label)
\xdef\tag{\sectionprefix\thesectioncount}%
% write to table of contents
\iftoc%
\write\tocoutput{\noexpand\tocsection{#1}{\thepage}}%
\fi%
}

%% subsection
\def\subsection#1{
% counters
\refstepcounter{subsectioncount}%
\setcounter{pointcount}{0}%
\setcounter{subpointcount}{0}%
\setcounter{subsubpointcount}{0}%
\ifsubseqcount%
\setcounter{seqcount}0%
\fi%
% space before subsection (if not first)
\ifnum\thesubsectioncount>1%
\subseqskip%
\fi%
% get widths
\def\@secnum{{\bf\large\hskip.5cm\sectionprefix\thesectioncount.\thesubsectioncount.\hskip5pt}}%
\settowidth\secnumwidth{\@secnum}%
\setlength\sectitlewidth\textwidth%
\addtolength\sectitlewidth{-\secnumwidth}%
% print name
\parbox{\textwidth}{%
\@secnum%
\parbox[t]{\sectitlewidth}{\large\bf #1}%
\par\penalty10000%
\medskip}\par\penalty10000%
% define tag (for \label)
\xdef\tag{\sectionprefix\thesectioncount.\thesubsectioncount}%
% write to table of contents
\iftoc%
\write\tocoutput{\noexpand\tocsubsection{#1}{\the\count0}}%
\fi%
}

%% points
\def\point{
  \refstepcounter{pointcount}
  \setcounter{subpointcount}{0}
  \indent{\bf \thepointcount\ - }
  % define tag (for \label)
  \xdef\tag{\thepointcount}
}
\def\subpoint{
  \refstepcounter{subpointcount}
  \setcounter{subsubpointcount}0
  \indent\hskip.5cm{\bf \thepointcount-\thesubpointcount\ - }
  % define tag (for \label)
  \xdef\tag{\thepointcount-\thesubpointcount}
}
\def\subsubpoint{
  \refstepcounter{subsubpointcount}
  \indent\hskip1cm{\bf \thepointcount-\thesubpointcount-\thesubsubpointcount\ - }
  % define tag (for \label)
  \xdef\tag{\thepointcount-\thesubpointcount-\thesubsubpointcount}
}
% reset points
\def\resetpointcounter{
  \setcounter{pointcount}{0}
  \setcounter{subpointcount}{0}
  \setcounter{subsubpointcount}{0}
}

%% equation numbering
\def\seqcount{
  \refstepcounter{seqcount}
  % the output
  \edef\seqformat{\theseqcount}
  % add subsection number
  \ifsubseqcount
    \let\tmp\seqformat
    \edef\seqformat{\thesubsectioncount.\tmp}
  \fi
  % add section number
  \ifsections
    \let\tmp\seqformat
    \edef\seqformat{\sectionprefix\thesectioncount.\tmp}
  \fi
  % define tag (for \label)
  \xdef\tag{\seqformat}
  % write number
  \marginnote{\eqnumstyle\hfill(\seqformat)}
}
%% equation environment compatibility
\def\equation{$$\seqcount}
\def\endequation{$$\@ignoretrue}

%% figures
\newlength\figwidth
\setlength\figwidth\textwidth
\addtolength\figwidth{-2.5cm}

\def\figcount#1{%
\refstepcounter{figcount}%
% the number of the figure
\edef\figformat{\thefigcount}%
% add section number
\ifsections%
\let\tmp\figformat%
\edef\figformat{\sectionprefix\thesectioncount.\tmp}%
\fi%
% define tag (for \label)
\xdef\tag{\figformat}%
% write
\hfil fig \figformat: \parbox[t]{\figwidth}{\small#1}%
\par\bigskip%
}

%% environment
\def\figure{
  \vfill\hbox{}
}
\def\endfigure{
  \vfill\hbox{}
}
\let\caption\figcount

%% delimiters
\def\delimtitle#1{\par \leavevmode\raise.3em\hbox to\hsize{\lower0.3em\hbox{\vrule height0.3em}\hrulefill\ \lower.3em\hbox{#1}\ \hrulefill\lower0.3em\hbox{\vrule height0.3em}}\par\penalty10000}
\def\delim{\par\leavevmode\raise.3em\hbox to\hsize{\vrule height0.3em\hrulefill\vrule height0.3em}\par\penalty10000}
\def\enddelim{\par\penalty10000\leavevmode\raise.3em\hbox to\hsize{\vrule height0.3em\hrulefill\vrule height0.3em}\par}

%% theorem headers
\def\theo#1{
  \refstepcounter{Theocount}
  % reset points
  \ifresetpointattheo\resetpointcounter\fi
  % the number
  \def\formattheo{\theTheocount}
  % add section number
  \ifsections
    \let\tmp\formattheo
    \edef\formattheo{\sectionprefix\thesectioncount.\tmp}
  \fi
  % define tag (for \label)
  \xdef\tag{\formattheo}
  % write
  \delimtitle{\bf #1 \formattheo}
}
\let\endtheo\enddelim

%% start appendices
\def\appendix{%
\vfill
\pagebreak
% counter
\setcounter{sectioncount}0%
% prefix
\def\sectionprefix{A}%
% write
{\bf \LARGE Appendices}\par\penalty10000\bigskip\penalty10000%
% add a mention in the table of contents
\iftoc%
\write\tocoutput{\noexpand\tocappendices}\penalty10000%
\fi%
%% uncomment for new page for each appendix
%\def\seqskip{\vfill\pagebreak}
}

%% start references
\def\references{%
% write
{\bf \LARGE References}\par\penalty10000\bigskip\penalty10000%
% add a mention in the table of contents
\iftoc%
\write\tocoutput{\noexpand\tocreferences{\the\count0}}\penalty10000%
\fi%
}


%% table of contents
\newif\iftocopen
\def\tableofcontents{
{\bf \large Table of contents:}\par\penalty10000\bigskip\penalty10000%
% copy content from file
\IfFileExists{\jobname.toc}{\input{\jobname.toc}}{{\tt error: table of contents missing}}
% open new toc
\newwrite\tocoutput
\immediate\openout\tocoutput=\jobname.toc
\toctrue
}
%% close file
\AtEndDocument{
  % close toc
  \iftoc
    \immediate\closeout\tocoutput
  \fi
}


%% fill line with dots
\def\leaderfill{\leaders\hbox to 1em {\hss. \hss}\hfill}

%% same as sectionprefix
\def\tocsectionprefix{}

%% toc formats
\def\tocsection #1#2{
  \stepcounter{tocsectioncount}
  \setcounter{tocsubsectioncount}{0}
  % write
  \smallskip\hyperlink{page.#2}{{\bf \tocsectionprefix\thetocsectioncount}.\hskip5pt #1\leaderfill#2}\par
}
\def\tocsubsection #1#2{
  \stepcounter{tocsubsectioncount}
  % write
  {\hskip10pt\hyperlink{page.#2}{{\bf \thetocsubsectioncount}.\hskip5pt {\small #1}\leaderfill#2}}\par
}
\def\tocappendices{
  \medskip
  \setcounter{tocsectioncount}0
  {\bf Appendices}\par
  \smallskip
  \def\tocsectionprefix{A}
}
\def\tocreferences#1{
  \medskip
  {\hyperlink{page.#1}{{\bf References}\leaderfill#1}}\par
  \smallskip
}
