% Macros for creating documents
% Written by Marcel Goh, unless otherwise specified.

% OLD FONT NAMES. These are not used anymore (as of 29 Jul 2020),
% but left here for compatibility purposes.
\font\bigbold=cmbx12
\font\bigsans=cmss12
\font\smallheader=cmssbx10 % for section headers

% Roman fonts
\font\eightpt=cmr8
\font\ninept=cmr9
\font\twelvept=cmr12

% Bold fonts
\font\eightbold=cmbx8
\font\ninebold=cmbx9
\font\mathbold=cmmib10

% NEW FONT NAMES FOR PRELOADED PLAIN FONTS (naming mostly consistent with the TeXbook)
\font\ninerm=cmr9     \font\eightrm=cmr8   \font\sixrm=cmr6      % ROMAN
\font\ninei=cmmi9     \font\eighti=cmmi8   \font\sixi=cmmi6      % MATH ITALIC
\font\ninesy=cmsy9    \font\eightsy=cmsy8  \font\sixsy=cmsy6     % SYMBOLS
\font\ninebf=cmbx9    \font\eightbf=cmbx8  \font\sixbf=cmbx6     % BOLD EXTENDED
\font\ninett=cmtt9    \font\eighttt=cmtt8                        % TYPEWRITER
\font\nineit=cmti9    \font\eightit=cmti8  \font\sevenit=cmti7   % TEXT ITALIC
\font\ninesl=cmsl9    \font\eightsl=cmsl8                        % SLANTED

\font\tensc=cmcsc10   \font\ninesc=cmcsc9  \font\eightsc=cmcsc8  % SMALL CAPS
\font\tenss=cmss10    \font\eightss=cmss8    % SANS SERIF
\font\tenssi=cmssi10  \font\eightssi=cmssi8  % SANS SERIF ITALIC

\font\eightssq=cmssq8  \font\eightssqi=cmssqi8  % SANS SERIFS FOR QUOTES
\font\tensltt=cmsltt10 % SLANTED TYPEWRITER
\font\tenu=cmu10       % UNSLANTED TEXT ITALIC

\font\tenbi=cmmib10    % BOLD MATH ITALIC
\font\tenbsy=cmbsy10   % BOLD MATH SYMBOLS

\font\tenssbx=cmssbx10 % SANS SERIF BOLD EXTENDED

% MISC. OTHER FONTS
\font\twelverm=cmr12  \font\twelvebf=cmbx12
\font\eightfib=cmfib8  \font\tenfib=cmfib8 at 10pt
\def\sc{\tensc}  \def\mc{\ninerm}

% CYRILLIC
% \input cyracc.def
%     \font\tencyr=wncyr10   \font\ninecyr=wncyr9   \font\eightcyr=wncyr8
%     \font\tencyri=wncyi10  \font\ninecyri=wncyi9  \font\eightcyri=wncyi8
%     \def\cyr{\tencyr\cyracc} \def\cyri{\tencyri\cyracc}

% FONT SIZES (ADAPTED FROM THE TEXBOOK).
% CHANGES MADE:
% - Edited \sc and added \mc.  (1)
% - Cyrillic support built in. (2)
\newskip\ttglue
\def\tenpoint{\def\rm{\fam0\tenrm}%
  \textfont0=\tenrm \scriptfont0=\sevenrm \scriptscriptfont0=\fiverm
  \textfont1=\teni  \scriptfont1=\seveni  \scriptscriptfont1=\fivei
  \textfont2=\tensy \scriptfont2=\sevensy \scriptscriptfont2=\fivesy
  \textfont3=\tenex \scriptfont3=\tenex   \scriptscriptfont3=\tenex
  \textfont\itfam=\tenit  \def\it{\fam\itfam\tenit}%
  \textfont\slfam=\tensl  \def\sl{\fam\slfam\tensl}%
  \textfont\ttfam=\tentt  \def\tt{\fam\ttfam\tentt}%
  \textfont\bffam=\tenbf  \scriptfont\bffam=\sevenbf
   \scriptscriptfont\bffam=\fivebf \def\bf{\fam\bffam\tenbf}%
  \tt \ttglue=.5em plus.25em minus.15em
  \normalbaselineskip=12pt
  \setbox\strutbox=\hbox{\vrule height8.5pt depth3.5pt width0pt}%
  \let\sc=\tensc \let\mc=\ninerm  % (1)
  \def\cyr{\tencyr\cyracc}\def\cyri{\tencyri\cyracc}% (2)
  \let\big=\tenbig  \normalbaselines\rm}

\def\ninepoint{\def\rm{\fam0\ninerm}%
\textfont0=\ninerm  \scriptfont0=\sixrm  \scriptscriptfont0=\fiverm
\textfont1=\ninei   \scriptfont1=\sixi   \scriptscriptfont1=\fivei
\textfont2=\ninesy  \scriptfont2=\sixsy  \scriptscriptfont2=\fivesy
\textfont3=\tenex   \scriptfont3=\tenex  \scriptscriptfont3=\tenex
\textfont\itfam=\nineit  \def\it{\fam\itfam\nineit}%
\textfont\slfam=\ninesl  \def\sl{\fam\slfam\ninesl}%
\textfont\ttfam=\ninett  \def\tt{\fam\ttfam\ninett}%
\textfont\bffam=\ninebf  \scriptfont\bffam=\sixbf
\scriptscriptfont\bffam=\fivebf\def\bf{\fam\bffam\ninebf}%
\tt\ttglue=.5em plus.25em minus.15em
\normalbaselineskip=11pt
\setbox\strutbox=\hbox{\vrule height8pt depth3pt width0pt}%
\let\sc=\ninesc\let\mc=\eightrm%(1)
\def\cyr{\ninecyr\cyracc}\def\cyri{\ninecyri\cyracc}%(2)
\let\big=\ninebig\normalbaselines\rm}

\def\eightpoint{\def\rm{\fam0\eightrm}%
  \textfont0=\eightrm \scriptfont0=\sixrm \scriptscriptfont0=\fiverm
  \textfont1=\eighti  \scriptfont1=\sixi  \scriptscriptfont1=\fivei
  \textfont2=\eightsy \scriptfont2=\sixsy \scriptscriptfont2=\fivesy
  \textfont3=\tenex   \scriptfont3=\tenex \scriptscriptfont3=\tenex
  \textfont\itfam=\eightit  \def\it{\fam\itfam\eightit}%
  \textfont\slfam=\eightsl  \def\sl{\fam\slfam\eightsl}%
  \textfont\ttfam=\eighttt  \def\tt{\fam\ttfam\eighttt}%
  \textfont\bffam=\eightbf  \scriptfont\bffam=\sixbf
  %\scriptscriptfont\bffam=\fivebf  \def\bf{\fam\bffam\eightbf}
  % \tt \ttglue=.5em plus.25em minus.15em
  \normalbaselineskip=9pt
  %\setbox\strutbox=\hbox{\vrule height7pt depth2pt width0pt}
  \let\sc=\eightsc \let\mc=\sevenrm  % (1)
  \def\cyr{\eightcyr\cyracc}\def\cyri{\eightcyri\cyracc}% (2)
  \let\big=\eightbig  \normalbaselines\rm}%
\def\nospace{\nulldelimiterspace0pt\mathsurround0pt}%
\def\tenbig#1{{\hbox{$\left#1\vbox to8.5pt{}\right.\nospace$}}}%
\def\ninebig#1{{\hbox{$\textfont0=\tenrm\textfont2=\tensy
  \left#1\vbox to7.25pt{}\right.\nospace$}}}%
\def\eightbig#1{{\hbox{$\textfont0=\ninerm\textfont2=\ninesy
  \left#1\vbox to6.5pt{}\right.\nospace$}}}%

% USE NON-EXTENDED VERSIONS OF BOLD FONT
\def\nonextendedbold{
  \font\fiveb=cmb10 at 5pt
  \font\sixb=cmb10 at 6pt
  \font\sevenb=cmb10 at 7pt
  \font\eightb=cmb10 at 8pt
  \font\nineb=cmb10 at 9pt
  \font\tenb=cmb10
  \font\twelveb=cmb10 at 12pt
  \let\fivebf=\fiveb
  \let\sixbf=\sixb
  \let\sevenbf=\sevenb
  \let\eightbf=\eightb
  \let\ninebf=\nineb
  \let\tenbf=\tenb
  \let\twelvebf=\twelveb
}

% Create header on non-first pages
\def\leftrighttop#1#2{
  \headline{\ifnum\pageno=1\hfil\else{\ninept #1 \hfil #2}\fi}
}

% No page numbers on first page
\def\firstnopagenum{
  \footline{\ifnum\pageno=1 \hfil \else \hfil{\rm \number\pageno}\hfil\fi}
}

% Create title, subtitle, name, and date
\def\maketitle#1#2#3#4{
  \centerline {\titlefont #1}
  \medskip
  \centerline {\eightpt #2}
  \medskip
  \centerline {\tensc #3}
  \medskip
  \centerline {\tensc #4}
  \bigskip
}
% Two-author version
\def\maketitletwoauthor#1#2#3#4#5{
  \centerline {\titlefont #1}
  \medskip
  \centerline {\eightpt #2}
  \medskip
  \centerline{\tensc #3\enspace \eightpt and\enspace \tensc #4}
  \medskip
  \centerline {\tensc #5}
  \bigskip
}

% Create title, subtitle, and name
\def\maketitlenodate#1#2#3{
  \centerline {\titlefont #1}
  \medskip
  \centerline {\eightpt #2}
  \medskip
  \centerline {\tensc #3}
  \bigskip
}

% Create floating text box with specified width in inches.
% Useful for abstracts (6 true inches is good):
% \floattext 6 Abstract. text...
\outer\def\floattext#1 #2. #3\par{
  $$
  \vbox{
    \hsize #1 true in
    \noindent{\bf #2.}\enskip #3
  }
  $$
}

% Math version of floattext, no bold label
\def\mathfloattext#1#2{
  \vbox{
    \hsize #1 true in
    \noindent #2
  }
}

% Section header in heavy sans-serif font
% Separate two of these in a row with \vskip -\medskipamount
\def\lsection#1\par{
  \bigskip\vskip\parskip
  \leftline{\sectionfont#1}\nobreak\medskip\noindent
}

% Centred version
\def\csection#1\par{
  \bigskip\vskip\parskip
  \centerline{\sectionfont#1}\nobreak\medskip\noindent
}

% Right-justified version
\def\rsection#1\par{
  \bigskip\vskip\parskip
  \rightline{\sectionfont#1}\nobreak\medskip\noindent
}
\def\section{\lsection}

% New subsection
\def\boldlabel#1. {\noindent{\bf #1.\enspace}}
\def\subsection#1. {\medskip\noindent{\bf #1.\enspace}}

% Block of left-justified text, in "poetry-mode"
\def\begintext{\par\begingroup\nobreak\medskip \obeylines}
\def\endtext{\endgroup\medbreak\noindent}

% Block of left-justified text, in "poetry-mode"
\def\begincode{\par\begingroup\nobreak\medskip \obeylines\obeyspaces\frenchspacing\tt}
\def\endcode{\endgroup\medbreak\noindent}
\def\tab{\ \ \ \ }

% Fraktur font -- needs some extra tuning to work with sizes other than \tenpoint
\font\tenfrak=eufm10
\font\sevenfrak=eufm7
\font\fivefrak=eufm5
\newfam\frakfam
\textfont\frakfam=\tenfrak
\scriptfont\frakfam=\sevenfrak
\scriptscriptfont\frakfam=\fivefrak
\def\frak#1{{\fam\frakfam #1}}

% A janky small-caps
\def\janksc#1#2 {#1{\eightpt#2}}
\def\jankscsp#1#2 {#1{\eightpt#2}\ }
\def\scproclaim#1.#2\par{\noindent\jankscsp #1.\enspace{\it#2\par}}

% References
\def\bref#1{[#1]}
\def\ref#1{[#1]}

\def\textbackslash{{\char`\\}}

% Quotes -- adapted from manmac
\def\quote{
  \begingroup
    \baselineskip 10pt
    \parfillskip 0pt
    \interlinepenalty 10000
    \leftskip 0pt plus 40pc minus \parindent
    \let\rm=\quoterm\let\sl=\quotesl\everypar{\sl}
    \obeylines
}
% This is used to end a \quote block. (I'm not sure why the second \rm is needed.)
\def\author#1(#2){\nobreak\smallskip\rm--- \rm#1\unskip\enspace(#2)\par\endgroup}

% DEFAULT SETTINGS
\def\titlefont{\twelvebf}
\def\sectionfont{\tenssbx}
\def\quoterm{\eightssq}
\def\quotesl{\eightssqi}

% CLASSIC MODE:
% Section headers are bold-serif; bolds and sans-serifs are more condensed
\def\classicmode{
  \nonextendedbold
  \font\elevenb=cmb10 at 11pt
  \def\sectionfont{\elevenb}
  \def\quoterm{\eightss}
  \def\quotesl{\eightssi}
  % \def\titlefont{\tenfib} Slightly wider and thinner strokes
  \tenpoint
}

% WIDE MARGINS
\def\widemargins{
  \magnification=\magstephalf\hoffset=40pt \voffset=28pt
  \hsize=29pc  \vsize=45pc  \maxdepth=2.2pt  \parindent=19pt
}
\def\bookheader#1#2{
  \nopagenumbers
  \def\leftheadline{{\rm\folio}\hfil{\eightpoint#1}\hfil}
  \def\rightheadline{\hfil{\eightpoint#2}\hfil{\rm\folio}}
  \headline{\ifodd\pageno{\ifnum\pageno<2\hfil\else\rightheadline\fi}\else\leftheadline\fi}
}

\tenpoint
