% This package is designed to work with beamer
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{csed}

\RequirePackage{xkeyval}
\RequirePackage{ifthen}
\RequirePackage{tikz}
\usetikzlibrary{arrows}

\newenvironment{cmddef}{%
  \par\vskip\baselineskip\noindent
  \begin{ttfamily}\ignorespacesafterend\begin{tabular}{@{}l@{}}}{%
  \end{tabular}\end{ttfamily}\par\noindent
  \vskip\baselineskip
}

\newcommand*{\term}[1]{\emph{#1}}
\newcommand*{\dash}{\,---\,}
\newcommand*{\cs}[1]{\texttt{\char`\\#1}}
\newcommand*{\marg}[1]{\char`\{#1\char`\}}
\newcommand*{\oarg}[1]{\texttt{[#1]}}
\newcommand*{\meta}[1]{\emph{#1}}
\newcommand*{\cls}[1]{\texttt{#1}}
\newcommand*{\sty}[1]{\texttt{#1}}
\newcommand*{\env}[1]{\texttt{#1}}
\newcommand*{\clsopt}[1]{\texttt{#1}}
\newcommand*{\app}[1]{\texttt{#1}}
\newcommand*{\bst}[1]{\texttt{#1}}

% Put a frame around the text on the given overlays
% Syntax: \boxtext<overlay specs>{colour}{text}
\newcommand<>{\boxtext}[2]{%
  \alt#3{{\color{#1}\fbox{\usebeamercolor[fg]{normal text}#2}}}{#2}%
}

% Define keys \annote:

% The offset is the distance from the text to the annotation.
\define@key{annote}{offset}{\setlength\annoteoffset{#1}}
\newlength{\annoteoffset}
% Set the default:
\setlength{\annoteoffset}{0.5in}

% The voffset is the vertical distance at the end of the arrow
\define@key{annote}{voffset}{\setlength\annotevoffset{#1}}
\newlength{\annotevoffset}
% Set the default:
\setlength{\annotevoffset}{0.5cm}

% The position can be a combination of l (left) or r (right) and one of:
% t (top), c (centre) or b (bottom).
\define@key{annote}{pos}{\def\annote@pos{#1}}
% The default is tr:
\def\annote@pos{tr}

% The foreground colour is the colour of the annotated text:
\define@key{annote}{fg}{\def\annote@fg{#1}}
% The default foreground colour is black:
\def\annote@fg{black}

% The background colour is the colour of the box containing the
% annoted text and the colour of the arrow.
\define@key{annote}{bg}{\def\annote@bg{#1}}
% The default background colour is light grey
\def\annote@bg{lightgray}

% The font style for the annotated text (must be a declaration):
\define@key{annote}{font}{\def\annote@font}
% The default font is small Roman:
\def\annote@font{\rmfamily\small}

% If smash, apply \smash to the annotation.
\define@boolkey{annote}[annote]{smash}[true]{}
\annotesmashfalse

\newlength\annotewidth
\setlength{\annotewidth}{1in}

\def\endobeyspaces{\catcode`\ =10}
\def\endobeylines{\catcode`\^^M=5}

% syntax: \annote[options]{text}{notes}
\newcommand<>{\annote}[3][]{%
\bgroup\endobeyspaces\endobeylines\setkeys{annote}{#1}%
  \@ifundefined{@\annote@pos @annote}%
  {\PackageError{csed}{Undefined position '\annote@pos'}{}}%
  {%
    \def\@annote{%
      \@nameuse{@\annote@pos @annote}%
       {\color{\annote@fg}\annote@font\shortstack[l]{#3}}%
       {#2}{#4}%
    }%
    \ifannotesmash
      \settoheight{\annotelength}{#2}\rule{0pt}{\annotelength}%
      \smash{\@annote}%
    \else
      \@annote
    \fi
  }%
\egroup
}

\newlength\annotelength

\newcommand*{\@tr@annote}[3]{%
\settowidth{\annotelength}{#2}%
\makebox[\annotelength][l]{%
  \alt#3{%
    \begin{tikzpicture}[baseline=(x.base)]%
      \path (0,0) node[fill=\annote@bg,inner sep=0pt] (x) {#2};
      \path (x.north) ++(\annoteoffset,\annotevoffset) node[fill=\annote@bg,anchor=west] (y) {#1};
      \draw[<-,rounded corners,thick,draw=\annote@bg] (x.north) |- (y);
    \end{tikzpicture}%
  }{%
    \begin{tikzpicture}[baseline=(x.base)]%
      \path (0,0) node[,inner sep=0pt] (x) {#2};
      \path (x.north) ++(\annoteoffset,\annotevoffset) node[anchor=west] (y) {\phantom{#1}};
      \path[<-,rounded corners,thick] (x.north) |- (y);
    \end{tikzpicture}%
  }%
}%
}

\newcommand*{\@rt@annote}{\@tr@annote}
\newcommand*{\@t@annote}{\@tr@annote}

\newcommand*{\@br@annote}[3]{%
\settowidth{\annotelength}{#2}%
\makebox[\annotelength][l]{%
  \alt#3{%
    \begin{tikzpicture}[baseline=(x.base)]%
      \path (0,0) node[fill=\annote@bg,inner sep=0pt] (x) {#2};
      \path (x.south) ++(\annoteoffset,-\annotevoffset) node[fill=\annote@bg,anchor=west] (y) {#1};
      \draw[<-,rounded corners,thick,draw=\annote@bg] (x) |- (y);
    \end{tikzpicture}%
  }{%
    \begin{tikzpicture}[baseline=(x.base)]%
      \path (0,0) node[inner sep=0pt] (x) {#2};
      \path (x.south) ++(\annoteoffset,-\annotevoffset) node[anchor=west] (y) {\phantom{#1}};
      \path[<-,rounded corners,thick] (x) |- (y);
    \end{tikzpicture}%
  }%
}%
}

\newcommand*{\@rb@annote}{\@br@annote}
\newcommand*{\@b@annote}{\@br@annote}

\newcommand*{\@tl@annote}[3]{%
\settowidth{\annotelength}{#2}%
\makebox[\annotelength][r]{%
  \alt#3{%
    \begin{tikzpicture}[baseline=(x.base)]%
      \path (0,0) node[fill=\annote@bg,inner sep=0pt] (x) {#2};
      \path (x.north) ++(-\annoteoffset,\annotevoffset) node[fill=\annote@bg,anchor=east] (y) {#1};
      \draw[<-,rounded corners,thick,draw=\annote@bg] (x) |- (y);
    \end{tikzpicture}%
  }{%
    \begin{tikzpicture}[baseline=(x.base)]%
      \path (0,0) node[inner sep=0pt] (x) {#2};
      \path (x.north) ++(-\annoteoffset,\annotevoffset) node[anchor=east] (y) {\phantom{#1}};
      \path[<-,rounded corners,thick] (x) |- (y);
    \end{tikzpicture}%
  }%
}%
}

\newcommand*{\@lt@annote}{\@tl@annote}

\newcommand*{\@bl@annote}[3]{%
\settowidth{\annotelength}{#2}%
\makebox[\annotelength][r]{%
  \alt#3{%
    \begin{tikzpicture}[baseline=(x.base)]%
      \path (0,0) node[fill=\annote@bg,inner sep=0pt] (x) {#2};
      \path (x.south) ++(-\annoteoffset,-\annotevoffset) node[fill=\annote@bg,anchor=east] (y) {#1};
      \draw[<-,rounded corners,thick,draw=\annote@bg] (x) |- (y);
    \end{tikzpicture}%
  }{%
    \begin{tikzpicture}[baseline=(x.base)]%
      \path (0,0) node[inner sep=0pt] (x) {#2};
      \path (x.south) ++(-\annoteoffset,-\annotevoffset) node[anchor=east] (y) {\phantom{#1}};
      \path[<-,rounded corners,thick] (x) |- (y);
    \end{tikzpicture}%
  }%
}%
}

\newcommand*{\@lb@annote}{\@bl@annote}

\newcommand*{\@cr@annote}[3]{%
\settowidth{\annotelength}{#2}%
\makebox[\annotelength][l]{%
  \alt#3{%
    \begin{tikzpicture}[baseline=(x.base)]%
      \path (0,0) node[fill=\annote@bg,inner sep=0pt] (x) {#2} 
        (x.east) ++(\annoteoffset,0) node[fill=\annote@bg,anchor=west] (y) {#1};
      \draw[<-,thick,draw=\annote@bg] (x.east) -- (y.west);
    \end{tikzpicture}%
  }{%
    \begin{tikzpicture}[baseline=(x.base)]%
      \path (0,0) node[inner sep=0pt] (x) {#2} 
        (x.east) ++(\annoteoffset,0) node[anchor=west] (y) {\phantom{#1}};
      \path[<-,thick] (x.east) -- (y.west);
    \end{tikzpicture}%
  }%
}%
}

\newcommand*{\@rc@annote}{\@cr@annote}
\newcommand*{\@c@annote}{\@cr@annote}

\newcommand*{\@cl@annote}[3]{%
\settowidth{\annotelength}{#2}%
\makebox[\annotelength][r]{%
  \alt#3{%
    \begin{tikzpicture}[baseline=(x.base)]%
      \path (0,0) node[fill=\annote@bg,inner sep=0pt] (x) {#2} 
        (x.west) --(\annoteoffset,0) node[fill=\annote@bg,anchor=east] (y) {#1};
      \draw[<-,thick,draw=\annote@bg] (x.west) -- (y.east);
    \end{tikzpicture}%
  }{%
    \begin{tikzpicture}[baseline=(x.base)]%
      \path (0,0) node[inner sep=0pt] (x) {#2} 
        (x.west) --(\annoteoffset,0) node[anchor=east] (y) {\phantom{#1}};
      \path[<-,thick] (x.west) -- (y.east);
    \end{tikzpicture}%
  }%
}%
}

\newcommand*{\@lc@annote}{\@cl@annote}

\newcommand*{\@ru@annote}[3]{%
\settowidth{\annotelength}{#2}%
\makebox[\annotelength][l]{%
  \alt#3{%
    \begin{tikzpicture}[baseline=(x.base)]%
      \path (0,0) node[fill=\annote@bg,inner sep=0pt,anchor=south west] (x) {#2};
      \path (x.east) ++(\annoteoffset,\annotevoffset) node[fill=\annote@bg] (y) {#1};
      \draw[<-,rounded corners,thick,draw=\annote@bg] (x) -| (y);
    \end{tikzpicture}%
  }{%
    \begin{tikzpicture}[baseline=(x.base)]%
      \path (0,0) node[inner sep=0pt] (x) {#2};
      \path (x.east) ++(\annoteoffset,\annotevoffset) node (y) {\phantom{#1}};
      \path[<-,rounded corners,thick] (x) -| (y);
    \end{tikzpicture}%
  }%
}%
}

\newcommand*{\@lu@annote}[3]{%
\settowidth{\annotelength}{#2}%
\makebox[\annotelength][r]{%
  \alt#3{%
    \begin{tikzpicture}[baseline=(x.base)]%
      \path (0,0) node[fill=\annote@bg,inner sep=0pt,anchor=south west] (x) {#2};
      \path (x.west) ++(-\annoteoffset,\annotevoffset) node[fill=\annote@bg] (y) {#1};
      \draw[<-,rounded corners,thick,draw=\annote@bg] (x) -| (y);
    \end{tikzpicture}%
  }{%
    \begin{tikzpicture}[baseline=(x.base)]%
      \path (0,0) node[inner sep=0pt] (x) {#2};
      \path (x.west) ++(-\annoteoffset,\annotevoffset) node (y) {\phantom{#1}};
      \path[<-,rounded corners,thick] (x) -| (y);
    \end{tikzpicture}%
  }%
}%
}

\newcommand*{\@ld@annote}[3]{%
\settowidth{\annotelength}{#2}%
\makebox[\annotelength][r]{%
  \alt#3{%
    \begin{tikzpicture}[baseline=(x.base)]%
      \path (0,0) node[fill=\annote@bg,inner sep=0pt] (x) {#2};
      \path (x.west) ++(-\annoteoffset,-\annotevoffset) node[fill=\annote@bg] (y) {#1};
      \draw[<-,rounded corners,thick,draw=\annote@bg] (x) -| (y);
    \end{tikzpicture}%
  }{%
    \begin{tikzpicture}[baseline=(x.base)]%
      \path (0,0) node[inner sep=0pt] (x) {#2};
      \path (x.west) ++(-\annoteoffset,-\annotevoffset) node (y) {\phantom{#1}};
      \path[<-,rounded corners,thick] (x) -| (y);
    \end{tikzpicture}%
  }%
}%
}

\newcommand*{\@rd@annote}[3]{%
\settowidth{\annotelength}{#2}%
\makebox[\annotelength][l]{%
  \alt#3{%
    \begin{tikzpicture}[baseline=(x.base)]%
      \path (0,0) node[fill=\annote@bg,inner sep=0pt] (x) {#2};
      \path (x.east) ++(\annoteoffset,-\annotevoffset) node[fill=\annote@bg] (y) {#1};
      \draw[<-,rounded corners,thick,draw=\annote@bg] (x) -| (y);
    \end{tikzpicture}%
  }{%
    \begin{tikzpicture}[baseline=(x.base)]%
      \path (0,0) node[inner sep=0pt] (x) {#2};
      \path (x.east) ++(\annoteoffset,-\annotevoffset) node (y) {\phantom{#1}};
      \path[<-,rounded corners,thick] (x) -| (y);
    \end{tikzpicture}%
  }%
}%
}

\endinput
