% MJ's TeX Macro Collection
% (c) 1995--2004 Martin Mares <mj@ucw.cz>
% Freely distributable as long as the author's copyright is left intact

% History:
% 2.0 (05-11-95)  Itemization and list system completely rewritten. Hopefully
%		  it's usable now.
% 2.1 (11-05-96)  Added \fixfont.
% 2.2 (15-02-97)  Modified \fixfont not to leave extra spaces.
%		  Added \relax at the end of \tt.
%		  Added a very limited support for picture drawing.
% 2.3 (21-05-01)  Modified \fixfont to use \fntdesignsize,
%		  Added \fntmath(|s|ss)size and used it in \setmath
%		  Added \twelvepoint
% 2.4 (10-07-03)  Added \bo
% 2.5 (27-09-03)  Added \verblocaldefs
% 2.6 (21-03-04)  Added \itemspacingrules
% 2.7 (10-04-04)  There were collisions between our \[bB]ig and the same
%		  macro used in plain to implement \bigl et al. Now we rename
%		  the original \[bB]ig to \plain[bB]ig and re-define \[bB]ig[lrm]
%		  to call the renamed version instead.

% We'll use internal macros of plain TeX

\catcode`@=11

\newcount\mjtempcount

% Page size definition
% --------------------

% Standard A4 dimensions are used. For different set of dimensions, redefine the
% \setpagesize macro to set everything you want in true units. Don't forget to
% call the macro immediately to refresh current setting. \magnification can be
% set prior to inclusion of these macros.

\def\setpagesize{\hsize=7truein\vsize=9.2truein}
\def\m@g{\mag\count@\setpagesize\dimen\footins8truein}
\setpagesize

% Spacing
% -------

\parindent=1.4cm
\parskip=3pt plus 1pt minus 1pt

% We want to override \[bB]ig in plain, so save the original definition
% ---------------------------------------------------------------------

\let\plainbig=\big
\let\plainBig=\Big
\def\bigl{\mathopen\plainbig}
\def\bigm{\mathrel\plainbig}
\def\bigr{\mathclose\plainbig}
\def\Bigl{\mathopen\plainBig}
\def\Bigm{\mathrel\plainBig}
\def\Bigr{\mathclose\plainBig}

% Font selection system
% ---------------------

% The heart of the font selection system is the \setfont macro which selects
% the current font according to the following settings:
% \fntprefix - font name prefix (usually cm or cs)
% \fntdesignsize - font design size in points (normally 10)
% \fnttokprefix - prefix of control sequence the font is assigned to ('ten')
% \fntmag - font magnification factor
% \fnttoksuffix - control sequence suffix ('A' to 'G' according to mag)
% \fntstyle - font name suffix determining font style ('bx', 'r' etc.)

\def\fntprefix{cs}
\def\fntdesignsize{10}
\def\fnttokprefix{ten}
\def\fntmag{\magstep0}
\def\fnttoksuffix{A}
\def\fntstyle{r}

\def\setfont{%
\toks0=\expandafter{\csname\fnttokprefix\fntstyle\fnttoksuffix\endcsname}%
\expandafter\ifx\the\toks0\relax%
%\immediate\write-1{aa \the\toks0=\fntprefix\fntstyle\fntdesignsize\space scaled \fntmag}%
\global\expandafter\font\the\toks0=\fntprefix\fntstyle\fntdesignsize\space scaled \fntmag%
\fi%
\edef\currentfont{\the\toks0}\currentfont}

\setfont  % Initialize the whole mechanism

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

\def\fntmathsize{10}
\def\fntmathtok{ten}
\def\fntmathssize{7}
\def\fntmathstok{seven}
\def\fntmathsssize{5}
\def\fntmathsstok{five}

\def\setmath{%
\mathf@nt{\fntmathtok}{r}{\fntmathsize}{cs}\textfont0=\m@@f
\mathf@nt{\fntmathstok}{r}{\fntmathssize}{cs}\scriptfont0=\m@@f
\mathf@nt{\fntmathsstok}{r}{\fntmathsssize}{cs}\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}{cs}\textfont\itfam=\m@@f
\mathf@nt{\fntmathtok}{sl}{\fntmathsize}{cs}\textfont\slfam=\m@@f
\mathf@nt{\fntmathtok}{bx}{\fntmathsize}{cs}\textfont\bffam=\m@@f
\mathf@nt{\fntmathtok}{tt}{\fntmathsize}{cs}\hyphenchar\m@@f=-1\textfont\ttfam=\m@@f
}

% Call \fixfont to fix lineskip, math fonts and the strut box

\def\fixfont{\setfont\setmath
\mjtempcount=\fntdesignsize
\multiply\mjtempcount by \fntmag
\normalbaselineskip=0.12pt
\multiply\normalbaselineskip by \mjtempcount
\divide\normalbaselineskip by 100
\normallineskip=0.01pt
\multiply\normallineskip by \mjtempcount
\divide\normallineskip by 100
\normalbaselines
\dimen0=0.085pt
\multiply\dimen0 by \mjtempcount
\divide\dimen0 by 100
\dimen1=0.035pt
\multiply\dimen1 by \mjtempcount
\divide\dimen1 by 100
\setbox\strutbox=\hbox{\vrule height\dimen0 depth\dimen1 width\z@}}

% You can use the following macros to change the magnification:

\def\normal{\def\fntmag{\magstep0}\def\fnttoksuffix{A}\setfont}       % Normal size
\def\halflarge{\def\fntmag{\magstephalf}\def\fnttoksuffix{b}\setfont} % Magstephalf
\def\large{\def\fntmag{\magstep1}\def\fnttoksuffix{B}\setfont}        % Magstep 1
\def\Large{\def\fntmag{\magstep2}\def\fnttoksuffix{C}\setfont}
\def\big{\def\fntmag{\magstep3}\def\fnttoksuffix{D}\setfont}
\def\Big{\def\fntmag{\magstep4}\def\fnttoksuffix{E}\setfont}
\def\huge{\def\fntmag{\magstep5}\def\fnttoksuffix{F}\setfont}
\def\Huge{\def\fntmag{\magstep6}\def\fnttoksuffix{G}\setfont}         % Magstep 6
\def\fm#1{\def\fntmag{\magstep#1}\edef\fnttoksuffix{\ifcase #1 A\or B\or C%
\or D\or E\or F\or G\fi}\setfont}				      % User mag

% And to change the style:

\def\rm{\fam\z@\def\fntstyle{r}\setfont}	% Roman
\def\it{\fam\itfam\def\fntstyle{ti}\setfont}	% Italic
\def\bo{\fam\bffam\def\fntstyle{b}\setfont}	% Bold
\def\bf{\fam\bffam\def\fntstyle{bx}\setfont}	% Bold extended
\def\ss{\def\fntstyle{ss}\setfont}		% Sans serif
\def\tt{\fam\ttfam\def\fntstyle{tt}\setfont%
\hyphenchar\currentfont=-1\relax}		% Typewriter
\def\sc{\def\fntstyle{csc}\setfont}		% Caps and small caps
\def\ssi{\def\fntstyle{ssi}\setfont}		% Sans serif italic
\def\sl{\fam\slfam\def\fntstyle{sl}\setfont}	% Slanted
\def\ssbf{\def\fntstyle{ssbx}\setfont}		% Sans serif bold
\def\ssdc{\def\fntstyle{ssdc}\setfont}		% Sans serif demi condensed

% Switch to large font set

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

% Itemization
% -----------

\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\fi
\advance\leftskip by 2.5em}

\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\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$)}

\def\itm{\:}

% Additional math functions
% -------------------------

\def\mod{\;{\rm mod}\;}
\def\div{\;{\rm div}\;}
\def\xor{\;{\rm xor}\;}

\def\mathbox#1{  % Typeset math formula enclosed in a box.
$$\vbox{
\hrule
\hbox{\vrule height 10pt depth 5pt\hskip3mm\hbox{$#1$}\hskip3mm\vrule }
\hrule
}$$}

% Italic typesetting	(automatic \/)
% ------------------

\def\I{\it\aftergroup\/}

% 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}

% Typesetting of one verbatim word
% --------------------------------

\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

% Typesetting of verbatim text  (enclosed 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
]]

% Input file verbatim
% -------------------

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

% Pictures
% --------
% Allowed direction vectors are 0<=x<=6, -6<=y<=6, GCD(x,y)=1

\def\initlines{\font\lif=line10}
\def\linechar#1#2{\ifnum #1=0 \hbox to 0pt{\hss\vrule height 10pt\hss}\else
\ifnum #2=0 \vbox to 0pt{\vss\hrule width 10pt\vss}\else
{\count255=#1\multiply\count255 by 8
\ifnum #2>0 \advance\count255 by #2 \advance\count255 by -9
\else\advance\count255 by -#2 \advance\count255 by 55\fi
\lif\char\count255}\fi\fi}
\def\picture#1#2#3{\initlines\medskip\centerline{\vbox to #2{\hbox to #1{#3\hss}\vss}}\medskip}
\def\place#1#2#3{{\setbox0=\hbox to 0pt{#3 \hss}\ht0=0pt\dp0=0pt
\hbox to 0pt{\hskip #1\lower #2\box0\hss}}}
\def\pl@celine{\ifnum \count255>0 \place{\dimen0}{\dimen1}{\copy0}\advance\dimen0 by \wd0
\advance\dimen1 by -\ht0 \advance\count255 by -1 \pl@celine\fi}
\def\placeline#1#2#3#4#5{{\setbox0=\hbox{\linechar{#3}{#4}}\ifnum #4<0\setbox0=\hbox to -\wd0{\hss\box0}\fi
\dimen0=#1\dimen1=#2\count255=#5\pl@celine}}
\def\cplace#1#2#3{\place{#1}{#2}{\hss #3}}

% Let's hide all internal macros

\catcode`@=12
