%
%      Some new commands for math documents
%
%				By: Gabriel Zachmann
%
% Should be included after amsmath.sty (if any)
%


\RequirePackage{amsmath}
\RequirePackage{amssymb}
\@ifclassloaded{egpubl}{}{
  \RequirePackage{array}		% doesn't work with egpubl.cls
}

\RequirePackage{xspace}         % for \deg macro
\RequirePackage{etoolbox}



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                            Special Symbols
%  
%  Most symbols work inside and outside math mode.
%
%  Number sets: \R \Aff \E \N
%  Vectors: \vc v
%  Subscripts, superscript:  \trp \deg \sbr \spr
%  Slash fraction: \sfrac[\mathrm]{v}{3}
%  Short cdots: \scdots
%  Big-Oh: \bigo{ f(n) }
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%  Sets
\ifx\R\undefined \newcommand{\R}{\ensuremath{\mathbb{R}}} \else\fi
\ifx\Aff\undefined \newcommand{\Aff}{\ensuremath{\R^3}} \else\fi
\ifx\E\undefined \newcommand{\E}{\ensuremath{\mathbb{E}}} \else\fi
\ifx\N\undefined \newcommand{\N}{\ensuremath{\mathbb{N}}} \else\fi
\ifx\Z\undefined \newcommand{\Z}{\ensuremath{\mathbb{Z}}} \else\fi
\ifx\Q\undefined \newcommand{\Q}{\ensuremath{\mathbb{Q}}} \else\fi

%  \d = upright d for differential d 
%  \i = upright i for imaginary unit 
\ifx\d\undefined \newcommand{\d}{\mathrm{d}} \else\fi
\ifx\i\undefined \newcommand{\i}{{\rm i}} \else\fi

%  vectors
%\newcommand{\vc}[1]{\ensuremath{\mathbf{#1}}}
\newcommand{\vc}[1]{\ensuremath{%
\ifx \sansmath \undefined
  \mathbf{#1}%
\else
    \ifstrequal{#1}{l}{
      \mathitbf{l}% use italics "ell" to distinguish it from "I"
    }{
      \mathbf{#1}%
   }
\fi
}}

%  transpose
\newcommand{\trp}{\ensuremath{^{\mathsf{T}}} }

%  degree symbol
\renewcommand{\deg}{\ensuremath{^{\circ}}\xspace}

%  fraction using slash (plucked from c.t.t and modified by me)
\DeclareRobustCommand{\sfrac}[3][\mathit]{%
  \smash[b]{%
  \hspace{0.1em}\raisebox{0.4ex}{$#1{#2}$}%
  \hspace{-0.1em}/\hspace{-0.07em}%
  \mbox{$#1{#3}$}}}

%  Super- or subscript in roman
\newcommand{\sbr}[1]{\ensuremath{_{\mathrm{#1}}}}
\newcommand{\spr}[1]{\ensuremath{^{\mathrm{#1}}}}

%  "Short" cdots (only 2 dots)
\newcommand{\scdots}{\hspace*{-0.5ex}\cdotp\cdotp\hspace*{-0.5ex}}

%  Big-Oh with slightly larger parentheses
\newcommand{\bigo}[1]{ O\bigl( #1 \bigr) }

%  For "area of triangle"
%  Example: \triarea{P Q R}
\newcommand{\triarea}[1]{ \mathcal{A}\bigl( \Delta #1 \bigr) }

% \if-operator fuer die CG1-Folien
\DeclareMathOperator{\vecchoose}{choose}

% Volume & Area
\DeclareMathOperator{\vol}{Vol}
\DeclareMathOperator{\area}{Area}

% Convex Hull
\DeclareMathOperator{\ch}{\kern 0.04em CH}

% Math characters with serifs - useful when sansmath.sty is being used
% Uppercase I = identity matrix
\ifx\I\undefined \newcommand{\I}{\mathnormal{I}} \else\fi

% Math font \mathrmbf for typesetting in serif and bold
% useful when sansmath.sty is being used
\DeclareMathAlphabet{\mathrmbf}{\encodingdefault}{\rmdefault}{bx}{n}

% Math font \mathitbf for typesetting in serif, italics, and bold
% useful when sansmath.sty is being used
\DeclareMathAlphabet{\mathitbf}{\encodingdefault}{\rmdefault}{bx}{it}

% lowercase l (ell) = angular momentum, in serif font and bold (= vector)
% useful when sansmath.sty is being used
\ifx\LL\undefined \newcommand{\LL}{\mathitbf{l}} \else\fi

% lerp- & slrep-operator fuer die CG1-Folien
\DeclareMathOperator{\lerp}{lerp}
\DeclareMathOperator{\slerp}{slerp}

%
%  \shortintertext
%
%  Tighter intertext for short interjections, like ``and'', ``with'', etc.
%  Copied from amsmath.sty
%  Uses \{above|below}displayshortskip instead of \{above|below}displayskip
%  Donald Arsenau suggests to use abovedisplayshortskip for both above
%  and below.
%
% Sollte vielleicht eigene spacing parameter nehmen,
% da \*displayshortskip von latex zurueck gesetzt werden!

\def\shortintertext#1{%
\ifvmode\else\\\@empty\fi
\noalign{%
  \penalty\postdisplaypenalty\vskip\aboveshortintertextskip
  \vbox{\normalbaselines
	\ifdim\linewidth=\columnwidth
	\else \parshape\@ne \@totalleftmargin \linewidth
	\fi
	\noindent#1\par}%
  \penalty\predisplaypenalty\vskip\belowshortintertextskip%
}%
}

\newskip \aboveshortintertextskip
\aboveshortintertextskip 0.4\baselineskip plus 0.3\baselineskip minus 0.2\baselineskip
\newskip \belowshortintertextskip
\belowshortintertextskip 0.4\baselineskip plus 0.3\baselineskip minus 0.2\baselineskip



%
%  Square root symbol with little hook at the end
%  (from comp.text.tex)
%

\let\originalsqrt\sqrt
\def\varsqrt #1#2{{\setbox0=\hbox{$#1\originalsqrt{#2\hskip2pt}$}%
                    \dimen0=\ht0 \advance\dimen0-.5pt
    \dimen2=\dimen0
         \advance\dimen2 -1.5pt
         \ifdim \dimen2 >10pt \advance\dimen2-.5pt \fi
         \ifdim \dimen2 >15pt \advance\dimen2 -1pt \fi
    \box0 \kern-.4pt
    \vrule width.4pt height\dimen0 depth -\dimen2 \kern.8pt
  }}

\def\Sqrt {\mathpalette \varsqrt }
\let\sqrt\Sqrt



%
%    Increase vertical spacing in amsmath's displayed equation environments
%    (like align), without having to write \\[0.5\baselineskip after every
%    line. Usage like this:
%        \begin{spread}{29pt}
%        \begin{align}
%        a&=b+c-d\\
%        a'&=e\sqrt{\frac{n-1}{n}}
%        \end{align}
%        \end{spread} 
%    Plucked from comp.text.tex (Michael J Downes)
%

\newenvironment{spread}[1]{%
  \advance\jot#1% indeed
}{%
  \ignorespacesafterend
}



%
%    Decrease space around \cdot a little bit
%

\let\oldcdot=\cdot
\def\cdot{\negthinspace\oldcdot\negthinspace}


%
%    Decrease space before \left\right pair
%    Otherwise, the space in $K\left(\frac{x-p}{h}\right)$
%    after the K is too wide.
%    Alternatives: use \big[lr] etc., or use nath.sty
%

\AtBeginDocument{% 
	\let\oldleft\left 
	\def\left{\mathopen{}\oldleft}
}

