% TeX macros for typesetting my PhD thesis
% (c) 2008 Martin Mares <mj@ucw.cz>

\input epsf.tex
\input btxmac.tex
\input amssym.tex

\catcode`@=11

%%% Basic parameters %%%

% A4 with 1in margins, leaving 20pt for page numbering at the bottom
\hsize=159mm
\vsize=246mm
\advance\vsize by -20pt
\parindent=0.8cm
\parskip=3pt plus 1pt minus 1pt

% Parameters for final typesetting (bigger inner margins etc.)
\newif\iffinal
\finaltrue
\newdimen\hwobble
\advance\hsize by -10mm

\iffinal
\hwobble=10mm
\else
\hwobble=0mm
\fi

% Glue
\smallskipamount=4pt plus 1.2pt minus 1.2pt
\medskipamount=7pt plus 2.5pt minus 2pt
\bigskipamount=15pt plus 5pt minus 4pt

\abovedisplayskip=15pt plus 4pt minus 10pt
\abovedisplayshortskip=0pt plus 3pt
\belowdisplayskip=15pt plus 4pt minus 10pt
\belowdisplayshortskip=8.5pt plus 3.5pt minus 4.5pt

%%% Miscellanea %%%

\def\em#1{{\it #1\/}}
\def\df#1{{\it #1\/}}	% when we define something
\def\O{{\cal O}}
\def\<#1>{\ifmmode
    \mathchoice{\hbox{\it #1\/}}{\hbox{\it #1\/}}{\hbox{\eightti #1\/}}{\hbox{\sixti #1\/}}%
  \else\leavevmode\hbox{\it #1\/}\fi}
\let\>=\noindent
\def\qed{{\parfillskip=0pt\allowbreak\hfill\nobreak $\spadesuit$\par}}
\def\qeditem{{\parfillskip=0pt\hfill\rlap{\hskip\rightskip\llap{$\spadesuit$}}\par}}
\def\qedmath{\eqno{\spadesuit}}
\def\FIXME#1{\>{\bo TODO:} #1}
\def\symdiff{\mathbin{\Delta}}
\def\rack#1#2{\setbox0=\hbox{#1}\hbox to \wd0{#2}}
\def\o#1{\accent23 #1}
\def\mst{\mathop{\rm mst}}
\def\msf{\mathop{\rm msf}}
\def\deg{\mathop{\rm deg}}
\def\timesalpha{\mskip2mu\alpha}
\def\timesbeta{\mskip2mu\beta}
\def\tower{\mathbin\uparrow}
\def\Forb{{\rm Forb}}
\def\minorof{\preccurlyeq}
\def\per{\mathop{\rm per}}
\def\poly{\mathop{\rm poly}}
\def\E{{\bb E}}
\def\crpt{\mathbin{\Uparrow}}
\def\C{{\cal C}}

\def\brk{\hfil\break}

% Bit strings
\def\0{{\bf 0}}
\def\1{{\bf 1}}
\def\(#1){\mathord{\left<#1\right>}}

% Bitwise operations
\def\shl{\mathbin{<\!<}}
\def\shr{\mathbin{>\!>}}
\def\bop#1{\mathbin{\hbox{\sc #1}}}
\def\band{\bop{and}}
\def\bor{\bop{or}}
\def\bxor{\bop{xor}}
\def\bnot{\mathop{\hbox{\sc not}}}

% A reversed version of \ddots with extra space at the top to get good alignment of exponents.
\def\rddots{\mathinner{\mkern1mu\raise\p@\vbox{\kern7\p@\hbox{.}}\mkern2mu
    \raise4\p@\hbox{.}\mkern2mu\raise7\p@\hbox{.}\raise11\p@\hbox{}\mkern1mu}}

% Footnotes
\newcount\footcnt
\footcnt=0
\def\foot#1{\global\advance\footcnt by 1{\parindent=0.25in\parskip=0pt\footnote{$^{\bf\the\footcnt}$}{#1}}}

%%% Fonts %%%

\font\twelverm=cmr12
\font\twelveit=cmti12
\font\twelvebf=cmbx12

%\font\twelverm=pplr8z at 12pt
%\font\twelveit=pplri8z at 12pt
%\font\twelvebf=pplb8z at 12pt

\font\twelvesl=cmsl12
\font\twelveb=cmb12
\font\twelvesc=cmcsc12

\def\mathf@nt#1#2#3#4{%
\toks0=\expandafter{\csname#1#2\endcsname}%
\expandafter\ifx\the\toks0\relax%
%\immediate\write-1{bb \the\toks0=#4#2#3}%
\global\expandafter\font\the\toks0=#4#2#3%
\fi%
\edef\m@@f{\the\toks0}}

\def\fntmathsize{12}
\def\fntmathtok{twelve}
\def\fntmathssize{8}
\def\fntmathstok{eight}
\def\fntmathsssize{6}
\def\fntmathsstok{six}

\mathf@nt{\fntmathtok}{r}{\fntmathsize}{cm}\textfont0=\m@@f
\mathf@nt{\fntmathstok}{r}{\fntmathssize}{cm}\scriptfont0=\m@@f
\mathf@nt{\fntmathsstok}{r}{\fntmathsssize}{cm}\scriptscriptfont0=\m@@f
\mathf@nt{\fntmathtok}{mi}{\fntmathsize}{cm}\skewchar\m@@f='177\textfont1=\m@@f
\mathf@nt{\fntmathstok}{mi}{\fntmathssize}{cm}\skewchar\m@@f='177\scriptfont1=\m@@f
\mathf@nt{\fntmathsstok}{mi}{\fntmathsssize}{cm}\skewchar\m@@f='177\scriptscriptfont1=\m@@f
\mathf@nt{\fntmathtok}{sy}{\fntmathsize}{cm}\skewchar\m@@f='60\textfont2=\m@@f
\mathf@nt{\fntmathstok}{sy}{\fntmathssize}{cm}\skewchar\m@@f='60\scriptfont2=\m@@f
\mathf@nt{\fntmathsstok}{sy}{\fntmathsssize}{cm}\skewchar\m@@f='60\scriptscriptfont2=\m@@f
\mathf@nt{\fntmathtok}{ex}{\fntmathsize}{cm}\textfont3=\m@@f\scriptfont3=\m@@f\scriptscriptfont3=\m@@f
\mathf@nt{\fntmathtok}{ti}{\fntmathsize}{cm}\textfont\itfam=\m@@f
\mathf@nt{\fntmathstok}{ti}{\fntmathssize}{cm}\scriptfont\itfam=\m@@f
\mathf@nt{\fntmathsstok}{ti}{\fntmathssize}{cm}\scriptscriptfont\itfam=\m@@f
\mathf@nt{\fntmathtok}{sl}{\fntmathsize}{cm}\textfont\slfam=\m@@f
\mathf@nt{\fntmathtok}{bx}{\fntmathsize}{cm}\textfont\bffam=\m@@f
\mathf@nt{\fntmathtok}{tt}{\fntmathsize}{cm}\hyphenchar\m@@f=-1\textfont\ttfam=\m@@f
\mathf@nt{\fntmathtok}{msam}{\fntmathsize}{}\textfont\msafam=\m@@f
\mathf@nt{\fntmathstok}{msam}{\fntmathssize}{}\scriptfont\msafam=\m@@f
\mathf@nt{\fntmathsstok}{msam}{\fntmathsssize}{}\scriptscriptfont\msafam=\m@@f
\mathf@nt{\fntmathtok}{msbm}{\fntmathsize}{}\textfont\msbfam=\m@@f
\mathf@nt{\fntmathstok}{msbm}{\fntmathssize}{}\scriptfont\msbfam=\m@@f
\mathf@nt{\fntmathsstok}{msbm}{\fntmathsssize}{}\scriptscriptfont\msbfam=\m@@f

% Blackboard bold font
\newfam\bbfam
\font\bbtwelve=bbm12
\font\bbeight=bbm8
\font\bbsix=bbm6
\textfont\bbfam=\bbtwelve
\scriptfont\bbfam=\bbeight
\scriptscriptfont\bbfam=\bbsix
\def\bb{\bbtwelve\fam\bbfam}

% Font switches
\def\rm{\fam\z@\twelverm}
\def\it{\fam\itfam\twelveit}
\def\bo{\fam\bffam\twelveb}
\def\bf{\fam\bffam\twelvebf}
\def\tt{\fam\ttfam\twelvett\hyphenchar\twelvett=-1\relax}
\def\sc{\twelvesc}
\def\sl{\fam\slfam\twelvesl}

% Switch to 12pt baselines
\rm
\baselineskip=14.4pt
\lineskip=1.2pt
\setbox\strutbox=\hbox{\vrule height 10.2pt depth 4.2pt width 0pt}

% Other fonts
\font\chapfont=csssdc17 scaled \magstep1
\font\secfont=csb14
\font\secitfont=csbxti14

%%% FIXME
\footline={\hss\twelverm\folio\hss}

% We have to redefine \big and friends as we are using 12pt symbols
\def\big#1{{\hbox{$\left#1\vbox to11.5\p@{}\right.\n@space$}}}
\def\Big#1{{\hbox{$\left#1\vbox to14.5\p@{}\right.\n@space$}}}
\def\bigg#1{{\hbox{$\left#1\vbox to17.5\p@{}\right.\n@space$}}}
\def\Bigg#1{{\hbox{$\left#1\vbox to20.5\p@{}\right.\n@space$}}}

%%% Enumerated lists %%%

\newif\ifitem\itemtrue
\newbox\itembox
\newcount\itemcount

\def\interlistskip{\vskip 3pt plus 2pt minus 1pt}

\def\itemspacingrules{\ifitem\advance\leftskip by \parindent\parindent=0pt\itemfalse\else
\advance\leftskip by 2.5em\fi\rightskip=\leftskip}

\def\inititemize{\interlistskip\begingroup
\parskip=1pt plus 0.3pt minus 0.1pt
\itemspacingrules}

\def\itemize#1{\inititemize\setbox\itembox\llap{#1\hskip 0.4em}%
\let\:=\singleitem}

\def\singleitem{\par\leavevmode\copy\itembox}

\def\endlist{\interlistskip\endgroup}

\def\ibull{\raise0.2ex\hbox{$\bullet$}} % Signs frequently used for \itemize
\def\icirc{\raise0.2ex\hbox{$\circ$}} % Signs frequently used for \itemize
\def\idot{\raise0.2ex\hbox{$\cdot$}}
\def\istar{\raise0.2ex\hbox{$\ast$}}

\def\numlist#1{\inititemize\itemcount=0\let\:=\numbereditem
\let\itemnumbering=#1}

\def\numbereditem{\par\leavevmode\advance\itemcount by 1
\llap{\itemnumbering\hskip 0.4em}}

\def\nnorm{\the\itemcount}
\def\ndotted{\nnorm.}
\def\nparen{\nnorm)}
\def\nroman{\romannumeral\itemcount}
\def\nromanp{\nroman)}
\def\nalpha{\count@=96\advance\count@ by\itemcount\char\count@)}
\def\nAlpha{\count@=64\advance\count@ by\itemcount\char\count@)}
\def\ngreek{$\ifcase\itemcount\or\alpha\or\beta\or\gamma\or\delta\or\epsilon\or
\zeta\or\eta\or\theta\or\iota\or\kappa\or\lambda\or\mu\or\nu\or\xi\or\pi\or\rho
\or\sigma\or\tau\or\upsilon\or\phi\or\chi\or\psi\or\omega\fi$)}

% New \frenchspacing, which doesn't leave unwanted spaces in text.
\def\frenchsp@cing{\sfcode`\.\@m \sfcode`\?\@m \sfcode`\!\@m%
\sfcode`\:\@m \sfcode`\;\@m \sfcode`\,\@m}

%%% Local verbatim mode %%%

\def\make@ther#1{\catcode`#1=12\relax}
\def\setverb@tim{\tt\frenchsp@cing\catcode`\`=13\catcode`\'=13\n@ligs%
\let\do=\make@ther\dospecials\obeyspaces}

{\catcode`'=13
\catcode``=13
\gdef\n@ligs{\def`{\relax\lq}\def'{\relax\rq}}}

{\obeyspaces
\global\chardef\sp@ce=32
\gdef\showspaces{\let =\sp@ce}
\global\let =\ }

\catcode124=\active % Make | active
\def\ttst@rt{\begingroup\setverb@tim\let|=\endgroup}
\let|=\ttst@rt

%%% Verbatim mode for text in { } %%%

\newskip\verbglue\newskip\verblineglue
\verbglue=3pt plus 1pt minus 0.3pt
\verblineglue=0pt plus 1.5pt
\def\verblocaldefs{}

\newdimen\tabwidth
\newcount\tabsize
\tabsize=8
\newdimen\t@b

\newcount\vercnt
\def\v@rbend{\par\endgroup\vskip\verbglue}
\def\v@rbl{\ifnum\vercnt>0\{\fi\global\advance\vercnt by 1\relax}
\def\v@rbr{\ifnum\vercnt>1\}\else\v@rbend\fi\global\advance\vercnt by -1\relax}
\def\v@rbparams{\setverb@tim\parskip=\verblineglue\parindent=0pt\raggedbottom%
\rightskip\z@ plus1fil\pretolerance=10000\tolerance=10000\vercnt=0%
\catcode124=12\setupt@bs\vskip\verbglue\defb@x\obeylines}

\def\startb@x{\setbox0=\hbox\bgroup}
\def\defb@x{\def\par{\leavevmode\egroup\box0\endgraf}%
\everypar{\startb@x}}

{\catcode`\^^I=\active
\gdef\setupt@bs{\catcode`\^^I=\active
\setbox0=\hbox{\tt\ }\tabwidth=\wd0\multiply\tabwidth by \tabsize
\def^^I{\leavevmode\egroup\t@b=\wd0\divide\t@b by \tabwidth%
\multiply\t@b by \tabwidth%
\advance\t@b by \tabwidth\advance\t@b by -\wd0\box0\hbox to \t@b{}\startb@x}
}}

{
\catcode`[=1\catcode`]=2\catcode123=\active\catcode125=\active
\gdef\verbatim[\begingroup\v@rbparams\catcode123=\active\catcode125=\active%
\let{=\v@rbl\let}=\v@rbr\chardef\{=123\chardef\}=125\verblocaldefs
]]

\def\verbinput#1{\begingroup\v@rbparams\input #1\endgroup}

%%% Algorithms %%%

%	\algo{jmeno algoritmu}
%	\algin popis vstupu
%	\:krok
%	\:krok
%	\::vnoreny krok
%	\algout popis vystupu
%	\endalgo

\def\algo{
\interlistskip
\begingroup
\let\:=\algoitem
\parskip=1pt plus 1pt minus 0.3pt
\rightskip=2em
\itemcount=0
\smallskip
}
\def\endalgo{\interlistskip\endgroup}
\def\algopar{\par
\parindent=2em
\hangindent=4em
\hangafter=1
\leavevmode
}
\def\algoitem{
\algopar\advance\itemcount by 1
\hbox to 2em{\hss \the\itemcount. }%
\futurelet\next\algoitemh}
\def\algoitemh{\ifx\next:\let\next=\algohang\else\let\next=\relax\fi\next}
\def\algohang:{\advance\hangindent by 2em \hskip 2em\futurelet\next\algoitemh}
\def\algin{\par{\sl Input:\/} }
\def\algout{\par{\sl Output:\/} }

%%% Constructs used in algorithms %%%

\def\={\leftarrow}
\def\cmt#1{~~{\sl (#1)}}

%%% Chapters, sections and proclamations %%%

\newcount\chapcount
\newcount\seccount
\newcount\thmcount
\newcount\tmpcount
\chapcount=0
\seccount=0
\thmcount=0
\def\currentid{??}
\def\currentchap{??}

\def\appendices{\chapcount=99}

\def\rawchapter#1{\vfill\supereject
\iffinal
  \ifodd\pageno
  \else
    \hglue 1in \vfill\supereject
  \fi
\fi
\leftline{\chapfont #1}
\bigskip
}

\def\unchapter#1{
\rawchapter{#1}
\writetoc{unchap}{#1}
}

\def\chapter#1{\advance\chapcount by 1
\seccount=0
\thmcount=0
\footcnt=0
% Chapter name
  \ifnum\chapcount>99
    \tmpcount=\chapcount
    \advance\tmpcount by -100
    \edef\currentchap{\ifcase\tmpcount A\or B\or C\or D\or E\or ZZZ\fi}
  \else
    \edef\currentchap{\the\chapcount}
  \fi
\edef\currentid{\currentchap}
\rawchapter{\currentid. #1}
\writetoc{chap}{#1}
}

\def\section#1{\bigskip
\advance\seccount by 1
\thmcount=0
\edef\currentid{\currentchap.\the\seccount}
\leftline{\secfont\currentid. #1}
\nobreak
\medskip
\nobreak
\writetoc{sec}{#1}
}

\def\para{\advance\thmcount by 1
\edef\currentid{\currentchap.\the\seccount.\the\thmcount}
\noindent {\bo \currentid.\enspace}}

\def\proclaim#1{\para {\bo #1.\enspace}}

\def\thm{\proclaim{Theorem}}
\def\lemma{\proclaim{Lemma}}
\def\defn{\proclaim{Definition}}
\def\problem{\proclaim{Problem}}
\def\obs{\proclaim{Observation}}
\def\rem{\proclaim{Remark}}
\def\alg{\proclaim{Algorithm}}
\def\impl{\proclaim{Implementation}}
\def\cor{\proclaim{Corollary}}
\def\nota{\proclaim{Notation}}
\def\example{\proclaim{Example}}

\def\label#1{{\sl (#1)\/}\enspace}
\def\labelx#1{\label{#1}\hfil\break\kern 0pt}	% Eat spaces

\def\thmn{\thm\labelx}
\def\lemman{\lemma\labelx}
\def\defnn{\defn\labelx}
\def\corn{\cor\labelx}
\def\algn{\alg\label}
\def\notan{\nota\labelx}
\def\examplen{\example\labelx}
\def\problemn{\problem\labelx}
\def\remn{\rem\labelx}

\def\paran#1{\para {\sl #1.\/}\enspace\kern 0pt}

\def\proof{\noindent {\sl Proof.}\enspace}
\def\proofsketch{\noindent {\sl Proof sketch.}\enspace}

%%% Table of contents %%%

\newwrite\toc
\immediate\openout\toc=\jobname.toc
\newif\iftoc
\tocfalse

\def\writetoc#1#2{
  \toctrue
  \toks0={\the\pageno}
  \edef\tocaux{\write\toc{\noexpand\expandafter\noexpand\string\noexpand\csname toc#1\noexpand\endcsname{\currentid}{#2}{\the\toks0}}}
  \tocaux
  \tocfalse
}

\newwrite\tok
\def\includetoc{
\immediate\openin\tok=\jobname.tok
\ifeof\tok
\else
\input \jobname.tok
\fi
\immediate\closein\tok
}

\def\tocchap#1#2#3{\smallskip\line{\bo #1.~~#2 \dotfill ~#3}}
\def\tocsec#1#2#3{\line{#1.~~#2 \dotfill ~#3}}
\def\tocunchap#1#2#3{\smallskip\line{\bo #2 \dotfill ~#3}}

%%% References %%%

\newwrite\ids
\def\writeid#1#2{\immediate\write\ids{\string\def\expandafter\string\csname id#1\endcsname{#2}}}

\immediate\openin\ids=\jobname.ids
\ifeof\ids
\else
\input \jobname.ids
\fi
\immediate\closein\ids
\immediate\openout\ids=\jobname.ids

\def\ref#1{\expandafter\ifx\csname id#1\endcsname\relax
{\bo ??}%
\immediate\write16{*** Warning: Reference #1 undefined ***}%
\else
\csname id#1\endcsname
\fi
}

\def\id#1{\writeid{#1}{\currentid}}

%%% Bibliography %%%

%\bibliographystyle{abbrv}
\bibliographystyle{alpha}
\def\dumpbib{
  \def\bblhook{\parskip=2pt plus 1pt minus 0.5pt}
  \bibliography{biblio}
}

%%% Figures %%%

\def\float#1#2{\midinsert
\medskip
\centerline{#1}
\medskip
\smallskip
\centerline{#2}
\endinsert}

\def\figure#1#2#3{\float{\epsfxsize=#2\epsfbox{pic/#1}}{#3}}

\def\multicap#1{
\let\\=\break
\vbox{\hsize=0.7\hsize
\parindent=0pt
\leftskip=0pt plus 0.3\hsize
\rightskip=\leftskip
\parfillskip=0pt
#1
}}

%%% Stand-alone chapters %%%

\def\endpart{
  \section{Bibliography}
  \dumpbib
  \vfill\supereject\end
}

%%% Hyphenation %%%

\hyphenation{Ra-ma-chan-dran}

%%% Output routine %%%

\output{\sagaoutput}

\def\sagaoutput{\ifodd\pageno \hoffset=\hwobble \else \hoffset=0pt \fi
\plainoutput}

\def\makefootline{\baselineskip30pt\lineskiplimit 0pt\line{\the\footline}}

\footline={\ifodd\pageno
  {\hfil\twelvemi\the\pageno}%
\else
  {\twelvemi\the\pageno\hfil}%
\fi
}

%%% The End %%%

\catcode`@=12
