% The TAME Programming Language LaTeX style and macros
%
%  Copyright (C) 2021 Ryan Specialty, LLC.
%
%  Licensed under the Creative Commons Attribution-ShareAlike 4.0
%  International License.
%%

%%
% Package Imports
%%
\usepackage[letterpaper]{geometry}       % US Letter paper
\usepackage[amsfonts,amssymb]{concmath}  % Fonts used by the text
\usepackage{euler}                       %   Concrete Mathematics
\usepackage{makeidx}                     % Indexing
\usepackage[backend=biber]{biblatex}     % BibTeX replacement
\usepackage[colorlinks=true,
            linkcolor=href,
            citecolor=cite,
            urlcolor=href,
            draft=false]{hyperref}       % Hyperlinks (even in draft mode)
\usepackage{xcolor}                      % Colors (for hyperlinks)
\usepackage{amsmath,amsthm}              % AMS macros, including theorems
\usepackage{suffix}                      % To aid in defining star macros
\usepackage{marginnote}                  % Notes in the margin
\usepackage{ccicons}                     % CC license icons
\usepackage{manfnt}                      % Dangerous Bend symbols
\usepackage{rotating}                    % Rotating objects

\addbibresource{tpl.bib}



%%
% Colors
%
% Colors from Tango Icon Theme
% https://en.wikipedia.org/wiki/Tango_Desktop_Project
\definecolor{href}{HTML}{204a87}
\definecolor{cite}{HTML}{4e9a06}



%%
% Common typesetting
%%
% TAME
\newcommand\tame{\textsc{Tame}}
\newcommand\tamer{\textsc{Tamer}}
\newcommand\tameparam[1]{\texttt{#1}}
\newcommand\tameclass[1]{\texttt{#1}}
\newcommand\tameconst[1]{\texttt{#1}}
\newcommand\tametrue{\tameconst{TRUE}}
\newcommand\tamefalse{\tameconst{FALSE}}
\let\tamepkg\texttt

% XML
\let\xml\texttt
\newcommand\xmlnl{\\[-3mm]}
\newcommand\xmlnll{\\[-1mm]}
\newcommand\xpath[1]{\texttt{#1}}
\newcommand\xmlnode[1]{\texttt{#1}}
\newcommand\xmlattr[1]{{@\texttt{#1}}}



%%
% Mathematics
%%
% Boolean
\newcommand\true{\ensuremath\top}
\newcommand\false{\ensuremath\bot}
\newcommand\Bool{\ensuremath{\mathbb{B}}}

% Numbers
\newcommand\Int{\ensuremath{\mathbb{Z}}}
\newcommand\Real{\ensuremath{\mathbb{R}}}

% Sets and families
\newcommand\Set[1]{\ensuremath{\left\{#1\right\}}}
\newcommand\Fam[3]{\ensuremath{\left\{#1_{#2}\right\}_{#2\in #3}}}
\newcommand\len[1]{\ensuremath{\left|#1\right|}}
\newcommand\rank[1]{\ensuremath{\left\|#1\right\|}}
\newcommand\PSet[1]{\mathcal{P}\left(#1\right)}
\newcommand\PSetcard[2]{\mathcal{P}_{#1}\left(#2\right)}
\let\union\cup
\let\Union\bigcup
\let\intersect\cap

% Vectors and matrices with family notations
\newcommand\Vectors{\ensuremath{\mathcal{V}}}
\newcommand\Vector[1]{\ensuremath{\left\langle#1\right\rangle}}
\newcommand\VFam[3]{\ensuremath{\Vector{#1_{#2}}_{#2\in #3}}}
\newcommand\Matrices{\ensuremath{\Vectors^{\Vectors^\Real}}}
\newcommand\MFam[5]{\ensuremath{%
  \Vector{{#1_{#2}}_#4}_{\underset{#4\in {#5_#2}}{#2\in #3}}
}}
\newcommand\eleundef{\boxtimes}

% Variable subscripts
\let\varsubscript\imath
\newcommand\varsub[1]{{#1}_{\varsubscript}}

% Logic
\let\infer\vdash
\newcommand\Forall{\@ifstar\@Forallstar\@Forall}
\newcommand\@Forall[2]{\forall #1\left(#2\right)}
\newcommand\@Forallstar[2]{\forall #1 #2}
\newcommand\Exists{\@ifstar\@Existsstar\@Exists}
\newcommand\@Exists[2]{\exists #1\left(#2\right)}
\newcommand\@Existsstar[2]{\exists #1 #2}

% Equivalent, but some syntatic inference involved (e.g. equivalent by
% index, as in classification matches).
\newcommand\equivish{\equiv_{\varsubscript}}

% Group theory
\newcommand\Monoid[3]{\left({#1},{#2},{#3}\right)}
\let\monoidop\bullet
\newcommand\monoidops{\monoidop\cdots\monoidop}

% Closed binary function
\newcommand\cbif[1]{#1\times#1\rightarrow#1}

% Binary function composition
\newcommand\bicomp[1]{{#1}^\circ}

% Grammar
\let\emptystr\epsilon

% TAME Classification
\DeclareMathOperator*\Classify{%
  \mathchoice{\vcenter{\hbox{\huge$\mathfrak{C}$}}}
             {\vcenter{\hbox{\Large$\mathfrak{C}$}}}
             {\frak{C}}
             {\frak{C}}
}
% Convenience variants
\DeclareMathOperator*\Classifyland{%
  \mathchoice{\vcenter{\hbox{\huge$\mathfrak{C}^\land$}}}
             {\vcenter{\hbox{\Large$\mathfrak{C}\land$}}}
             {\frak{C^\land}}
             {\frak{C^\land}}
}
\DeclareMathOperator*\Classifylor{%
  \mathchoice{\vcenter{\hbox{\huge$\mathfrak{C}^\lor$}}}
             {\vcenter{\hbox{\Large$\mathfrak{C}\lor$}}}
             {\frak{C^\lor}}
             {\frak{C^\lor}}
}



%%
% Theorem environments
%%
\numberwithin{equation}{section}   % number by section

% Create a new theorem environment along with a set of label/ref/pref
% commands.
\newcommand\newtheoremwithlabel[3]{%
  \newtheorem{#1}{#2}[section]
  \expandafter\def\csname #1autorefname\endcsname{#2}
  \expandafter\newcommand\csname #3label\endcsname[1]{\label{#1:##1}}
  \expandafter\newcommand\csname #3ref\endcsname[1]{\autoref{#1:##1}}
  \expandafter\newcommand\csname #3pref\endcsname[1]{\spref{#1:##1}}
}

\theoremstyle{definition}
\newtheoremwithlabel{definition}{Definition}{dfn}
\newtheoremwithlabel{axiom}{Axiom}{axm}

\theoremstyle{plain}
\newtheoremwithlabel{corollary}{Corollary}{cor}
\newtheoremwithlabel{lemma}{Lemma}{lem}
\newtheoremwithlabel{theorem}{Theorem}{thm}

\theoremstyle{remark}
\newtheoremwithlabel{remark}{Remark}{rem}


%%
% Optional page reference
%%
% "Smart" page reference changing the text depending on what page the
% reference appears relative to the current.
\newcount\@sprefn
\newcommand\spref[1]{%
  % We use labels to get the current page from the previous run, since
  % \thepage can be inaccurate except right after a \newpage.
  \advance\@sprefn by 1\relax%
  \label{__:#1SELF\the\@sprefn}%
  \xdef\@@realp{\getpagerefnumber{__:#1SELF\the\@sprefn}}%
  \autoref{#1}%
  \xdef\@pref{\getpagerefnumber{#1}}%
  \ifnum\@pref=\@@realp
    % Output nothing; it'd read really awkwardly, for example, if we output
    % a figure using [h] and then say "Figure X on this page" in the
    % paragraph that's _right_ below it.  Even if we didn't use [h].  I
    % noticed this awkward effect in Specifying Systems.
  \else
    \space
    \newcount\@p\@p=\@@realp
    \advance\@p by 1\relax
    \ifnum\@p=\@pref
      on the next page%
    \else
      \advance\@p by -2\relax
      \ifnum\@p=\@pref
        on the previous page%
      \else
        on \autopageref{#1}%
      \fi
    \fi
  \fi
}



%%
% General typesetting
%%
\newcommand\pref[1]{\ref{#1} on page~\pageref{#1}}
\newcommand\seclabel[1]{\label{sec:#1}}
\newcommand\secref[1]{Section~\ref{sec:#1}}
\newcommand\secpref[1]{Section~\pref{sec:#1}}

% Definitions (introduction of terms)
\let\dfn\textsl
\newcommand\todo[1]{%
  \marginnote{\underline{\textsc{Todo:}}\\
  \textsl{#1}}%
}
\newcommand\mremark[1]{\marginnote{\textsl{#1}}}

% A really obnoxious notice making clear to the reader that this portion of
% the work is unfinished, to the point where it's probably even
% incorrect.  Uses dangerous bend symbol from manfnt, which is admittedly a
% misuse given that it's often used to represent difficult problems.  Though
% I suppose an unfinished work is a difficult problem.
\newcommand\INCOMPLETE[1]{%
  \bigskip
  \par
  \todo{This is incomplete!}
  \hfill\textdbend\textbf{\textsl{#1}}\textdbend\hfill
  \bigskip
}



%%
% Indexing
%%
% Symbols appear at the beginning of the index
\newcommand\indexsym[2]{\index{__sym_#2@{\ensuremath{#1}}|see {#2}}}



%%
% Dynamic Configuration
%%
\newif\iftplappendix

\InputIfFileExists{./conf.tex}%
  {\message{Loaded `./conf.tex'.}}%
  {\message{`./conf.tex' not found (did you run `./configure'?).
            Using defaults.}%
}
