\ProvidesPackage{gimac}
% Misc. macros for Gautam Iyer.
\newif\ifexam
\@ifclassloaded{exam}{\examtrue}{\examfalse}


\RequirePackage[T1]{fontenc}
\RequirePackage{amsmath,amssymb,amsopn,amsthm,bm,mathrsfs}
\RequirePackage{IEEEtrantools,cite,indentfirst}
\RequirePackage{indentfirst}
\RequirePackage{enumerate,multicol,verbatim,xspace,centernot}
\RequirePackage{mathtools,forloop,paralist,suffix,xcolor,fancybox}
\ifexam\else\RequirePackage{framed}\fi
\RequirePackage[normalem]{ulem}

\providecommand*{\showkeyslabelformat}[1]{% Should be done before loading showkeys
  \fontfamily{\rmdefault}\itshape%
  %\fontsize{3.5pt}{4.2pt}\selectfont%
  \fontsize{4.5pt}{5.4pt}\selectfont%
  \color{blue}%
  \ifSK@equation%
    \llap{\raisebox{-1em}{\pbox[t]{25ex}{\raggedleft\seqsplit{#1}}\hspace{-\marginparsep}\normalfont\hspace{-3.5em}}}%
  \else%
    \fbox{\pbox[t]{\marginparwidth}{\raggedleft\seqsplit{#1}}}%
  \fi
}
\ifexam\else
  \newif\ifGI@draft
  \GI@draftfalse
  \DeclareOption{draft}{\GI@drafttrue}

  \newif\ifTwoColumn
  \TwoColumnfalse
  \DeclareOption{twocolumn}{\TwoColumntrue}

  \ProcessOptions

  \ifTwoColumn
    % 2 Column mode to save trees. Margins chosen so that linewidth here is the
    % same as amsart, one column with default margins on US letter paper, so
    % that no overfull hboxes are created / destroyed.
    \setlength{\columnsep}{24.98999pt} % amsart,  10pt, letter (lw=360pt)
    %\setlength{\columnsep}{34.98999pt} % article, 10pt, letter (lw=345pt)
    \RequirePackage[landscape,includehead,headheight=12pt,headsep=10pt,left=\columnsep,right=\columnsep,top=12.98999pt,bottom=\columnsep]{geometry}
    \twocolumn%Lowercase. Tex/Latex internal command, not my defined if.
  \else
    %\usepackage[body={16.65cm,22.65cm}]{geometry}
    \ifGI@draft
      \RequirePackage{fix-cm}% Arbitrary font sizes.
      \RequirePackage{seqsplit}% Split without hyphenation
      \RequirePackage{pbox}% Variable width parboxes
      \RequirePackage[notref,notcite]{showkeys}
    \fi
  \fi
\fi

\RequirePackage{hyperref}% put this last...
%\usepackage{wrapfig}

% Link colors
\definecolor{link1}{rgb}{0,0,.7}
\definecolor{link2}{rgb}{0,0.25,0.5}
\hypersetup{colorlinks,allcolors=link1,citecolor=link2}

% So that we don't get terrible overfull hboxes in the references.
%\newcommand{\eprint}[1]{\texttt{#1}}


%
% Commands to create author notes.
%
\@mparswitchfalse%Put all sidenotes on the right.

\newcommand{\sidenote}[1]{%
  \ifGI@draft
    \ifTwoColumn
      \footnote{#1}
    \else
      \marginpar[\raggedleft\tiny #1]{\raggedright\tiny #1}
    \fi
  \else
    \warning{Sidenote "#1" omitted in final version.}
  \fi
}
%\newcommand{\sidenote}[1]{}

\newcommand{\warning}[1]{\typeout{}\typeout{WARNING: #1 at line \the\inputlineno}\typeout{}}
\newenvironment{todo}[1][TODO]{%
    \ifGI@draft\else\warning{TODO still present in final version}\fi
    \def\FrameCommand{\vrule width 1.5pt \hspace{10pt}}%
    \MakeFramed{\advance\hsize-\width \FrameRestore}\textbf{#1. }}%
    {\endMakeFramed}
\ifexam\else
\newenvironment{note}[1][Note]{%
    \begin{todo}[#1]}%
    {\end{todo}}
\fi


%
% Add / remove / replace text
%
\newenvironment{removeblock}{\color{red}}{}
\newenvironment{addblock}{\color{blue}}{}
\newcommand{\Remove}[2][red]{\textcolor{#1}{\sout{#2}}}
\newcommand{\remove}[2][red]{%
  \ifmmode\text{\Remove[#1]{\ensuremath{#2}}}%
  \else\Remove[#1]{#2}\fi}
\newcommand{\Add}[2][blue]{\textcolor{#1}{\uwave{#2}}}
\newcommand{\add}[2][blue]{%
  \ifmmode\text{\Add[#1]{\ensuremath{#2}}}%
  \else\Add[#1]{#2}\fi}%
\newcommand{\Replace}[2]{\Remove{#1} \Add{#2}}
\newcommand{\replace}[2]{\remove{#1} \add{#2}}

%
% Highlight text
%
\newlength{\hbwidth}
\newcommand{\hbcolor}{yellow}
\newenvironment{highlightblock}[1][yellow]{%
  \renewcommand{\hbcolor}{#1}%
  \setlength{\hbwidth}{\linewidth}%
  \addtolength{\hbwidth}{-2\fboxsep}%
  \noindent\begin{Sbox}\begin{minipage}{\hbwidth}%
}{%
  \end{minipage}\end{Sbox}%
  \colorbox{\hbcolor}{\TheSbox}%
}
\newcommand{\Highlight}[2][yellow]{\bgroup\markoverwith{\textcolor{#1}{\rule[-.2em]{2pt}{1.2em}}}\ULon{#2}}
\newcommand{\highlight}[2][yellow]{%
  \ifmmode\text{\Highlight[#1]{\ensuremath{#2}}}%
  \else\Highlight[#1]{#2}\fi}%

%
% Equation environments
%
\newenvironment{beqn}[1][:C?s]%
    {\left\{\begin{IEEEeqnarraybox}[\relax][c]{#1}}%
    {\end{IEEEeqnarraybox}\right.}

\newenvironment{step}[1]%
  {\par\emph{#1\addpunct}}%
  {\par}

%
% Symbols and notation
%
\newcommand{\lap}{\Delta}
\newcommand{\laplacian}{\lap}
\newcommand{\inv}{^{-1}}
\newcommand{\transpose}{^*}
\newcommand{\grad}{\nabla}
\newcommand{\gradt}{\grad\transpose}
\newcommand{\gradperp}{\grad^\perp}
\newcommand{\dv}{\grad \cdot}
\newcommand{\curl}{\grad \times}
\newcommand{\loc}{_\text{loc}}
\newcommand{\varmin}{\wedge}
\newcommand{\varmax}{\vee}
\newcommand{\as}{\text{a.s.}}
\newcommand{\E}{\bm{E}}
\renewcommand{\P}{\bm{P}}
\newcommand{\mc}{\mathcal}
\newcommand{\AND}{\;\&\;}

\DeclareMathOperator{\erf}{erf}
\DeclareMathOperator{\sign}{sign}
\DeclareMathOperator{\var}{Var}
\DeclareMathOperator{\diam}{diam}
\DeclareMathOperator{\trace}{tr}
\DeclareMathOperator{\Lip}{Lip}
\DeclareMathOperator{\supp}{supp}

\newcommand{\eps}{\varepsilon}
\renewcommand{\epsilon}{\eps}
\renewcommand{\leq}{\leqslant}
\renewcommand{\geq}{\geqslant}

% Indicator functions
\newcommand{\Chi}[1]{\Chi*{\{#1\}}}
\WithSuffix\newcommand\Chi*[1]{\chi_{\raise-.5ex\hbox{$\scriptstyle#1$}}}

%
% Spaces & sets
%
\newcommand{\R}{\mathbb{R}}
\newcommand{\C}{\mathbb{C}}
\newcommand{\N}{\mathbb{N}}
\newcommand{\Z}{\mathbb{Z}}
\newcommand{\Q}{\mathbb{Q}}
\newcommand{\T}{\mathbb{T}}


%
% Test if we're in a display or not
%
\newif\iftextstyle
\textstyletrue
\everydisplay\expandafter{\the\everydisplay\textstylefalse}

%
% Norms and parenthesis
%
\DeclarePairedDelimiter{\paren}{(}{)}
\DeclarePairedDelimiter{\brak}{[}{]}
\DeclarePairedDelimiter{\set}{\{}{\}}
\DeclarePairedDelimiter{\floor}{\lfloor}{\rfloor}

\DeclarePairedDelimiter{\abs}{\lvert}{\rvert}
\DeclarePairedDelimiter{\norm}{\lVert}{\rVert}
\DeclarePairedDelimiter{\qv}{\langle}{\rangle}
\DeclarePairedDelimiter{\av}{\langle}{\rangle}

\newcommand{\ip}[3][]{\av[#1]{#2,#3}}
\WithSuffix\newcommand\ip*[2]{\av{#1,#2}}
\newcommand{\given}[1][\big]{\;\iftextstyle|\else#1|\fi\;}
\WithSuffix\newcommand\given*{\;\middle|\;}
\newcommand{\st}[1][\big]{\given[#1]}
\WithSuffix\newcommand\st*{\given*}
\newcommand{\at}[2][\Bigr]{\iftextstyle|\else#1|_{#2}\fi}
\WithSuffix\newcommand\at*[1]{\middle|_{#1}}

\newcommand{\defeq}{\stackrel{\scriptscriptstyle\text{def}}{=}}

\providecommand{\cites}[1]{\cite{#1}}
%
% Styles etc.
%
%\renewcommand{\figurename}{\scriptsize Fig.}
\allowdisplaybreaks

%
% Theorems, Lemmas etc.
%
\newif\ifnumwithin
\numwithinfalse
\ifcsname c@section\endcsname\numwithintrue\fi
\ifexam\numwithinfalse\fi

\ifnumwithin
  \numberwithin{equation}{section}
  \newtheorem{theorem}{Theorem}[section]
\else
  \newtheorem{theorem}{Theorem}
\fi
\newtheorem{lemma}[theorem]{Lemma}
\newtheorem{proposition}[theorem]{Proposition}
\newtheorem{corollary}[theorem]{Corollary}
\newtheorem{conjecture}[theorem]{Conjecture}
%\@ifclassloaded{exam}{}{\newtheorem{question}[theorem]{Question}}

\newtheorem*{theorem*}{Theorem}
\newtheorem*{lemma*}{Lemma}
\newtheorem*{proposition*}{Proposition}
\newtheorem*{corollary*}{Corollary}


\theoremstyle{definition}
\newtheorem{definition}[theorem]{Definition}

\theoremstyle{remark}
\newtheorem{remark}[theorem]{Remark}
\newtheorem*{remark*}{Remark}
\newtheorem{example}[theorem]{Example}

\newtheoremstyle{noparen}% name
  {}%      Space above, empty = `usual value'
  {}%      Space below
  {\itshape}% Body font
  {}%         Indent amount (empty = no indent, \parindent = para indent)
  {\bfseries}% Thm head font
  {.}%        Punctuation after thm head
  { }%     Space after thm head: " " = normal interword space; \newline = linebreak
  {\thmname{#1}\thmnumber{ #2}\thmnote{ #3}}% Thm head spec
  %{\thmnote{#1 #2 {\normalfont\emph{#3}}}}% Thm head spec
\theoremstyle{noparen}
\newtheorem{rtheorem}[theorem]{Theorem}
\newtheorem{rlemma}[theorem]{Lemma}
\newtheorem{rproposition}[theorem]{Proposition}

\newtheoremstyle{misc}
  {}%      Space above, empty = `usual value'
  {}%      Space below
  {\itshape}% Body font
  {}%         Indent amount (empty = no indent, \parindent = para indent)
  {\bfseries}% Thm head font
  {.}%        Punctuation after thm head
  { }%     Space after thm head: " " = normal interword space; \newline = linebreak
  {\thmname{#3}\thmnumber{ #2}}% Thm head spec
\theoremstyle{misc}
\newtheorem{miscthm}[theorem]{Misc}
\newtheorem*{miscthm*}{Misc}

%
% References.
%
\RequirePackage{cleveref}% Put this even laster than hyperref.
\crefname{lemma}{Lemma}{Lemmas}
\crefname{corollary}{Corollary}{Corollaries}

% Override default use of Eq. or Equation in equation references.
\crefformat{equation}{equation~(#2#1#3)}
\crefrangeformat{equation}{equations~(#3#1#4)--(#5#2#6)}
\crefmultiformat{equation}
    {equations~(#2#1#3)}%
    { and~(#2#1#3)}
    {, (#2#1#3)}
    { and~(#2#1#3)}

%
% Paper specific constants / equations / etc.
%
\newcommand{\pe}{\text{Pe}}
\newcommand{\linf}{L^\infty}
\newcommand{\eff}{_\text{eff}}
\newcommand{\avg}{_\text{avg}}
\newcommand{\el}{_\text{el}}
\newcommand{\bulk}{_\text{bulk}}
\newcommand{\LP}{\mathcal P}
