%% Copyleft 2005-2007 Thomas Holder, Tobias Heide
%% Distributed under the terms of the GNU General Public License v2
%% $Id: $
%%
%% This LaTeX document class is optimized for scientific homework sheets and is
%% based on the KOMA-Script article class (scrartcl). Its main feature is the
%% \aufgabe[] macro and the appropriate credits summary table in the top right
%% corner of the first page.
%%
%% File names of form "xyz-number.tex" will have \blatt{} initialized with
%% <number> (for example statistics-5.tex -> blatt = 5).
%%
%% == Macro summary ==
%%
%% \fach{}
%%     Set the name of the subject (before \begin{document})
%% \blatt{}
%%     Set the sheet number (before \begin{document)}
%% \aufgabe[]
%%     Print ``Aufgabe (#1)'' in bold, argument is optional, starts with 1 and
%%     gets incrementet automatically
%% \teilaufgabe
%%     Print ``(#1)'' with automatically incremented lower case chars, gets
%%     reset after \aufgabe[]
%% \cv{}
%%     Column vector in math mode, separator is \\
%% \rv{}
%%     Row vector in math mode, separator is \\
%% \xor
%%     Small caps xor in math mode
%% \N \Z \Q \R \C
%%     Some number sets in math mode
%% \zz
%%     Italic zz (for ``zu zeigen'')
%% \[
%%     Begin equation array environment without numbering
%% \]
%%     End equation array environment without numbering
%% \en
%%     Switch to english language
%% \de
%%     Switch to german language
%% \translate{german}{english}
%%     Current language determines whether to use first or second text
%%
%%
%% == Class Options summary ==
%%
%% theide
%%     Alternative header layout designed by Tobias
%% notop
%%     No ``Punktetabelle'' (table of points)
%% numericalteilaufgabe
%%     Count ``\teilaufgabe'' with numbers instead of letters
%% rowvectors
%%     ``\xv{}'' macros makes rowvectors instead of columnvectors
%% morespace
%%     (still broken!!!) Less margin, more textwidth and -height
%%
\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{uebungsblatt}[2007/05/05 Copyleft Thomas Holder, Tobias Heide]

%% Load Packages

\RequirePackage[english,german]{babel}
\RequirePackage[latin1]{inputenc}
\RequirePackage{enumerate}
\RequirePackage{ifthen}
\RequirePackage{fancyhdr}
\RequirePackage{bbm}
\RequirePackage{amsmath}
\RequirePackage{amssymb}
\RequirePackage{amsthm}
\RequirePackage{lastpage}
\RequirePackage{graphicx}

%% Fancy tex programming

\def\getblattfromjobname{%
  \expandafter\@getblattfromfilename\jobname--\@nil%
}
\def\@getblattfromfilename#1-#2-#3\@nil{#2}

%% Initialise Variables

\def\morespace{0}
\def\theidestyle{0}
\def\xv{\cv}
\def\notop{0}
\def\numericteilaufgabe{0}
\def\thefach{}
\def\theblatt{\getblattfromjobname}
\def\therighthead{}
\def\theaufgabenlabel{\translate{Aufgabe}{Question}}

\newlength{\tmplength}

%% Process Options

\DeclareOption{morespace}{\def\morespace{1}}
\DeclareOption{theide}{\def\theidestyle{1}}
\DeclareOption{tholder}{\typeout{Option ``tholder'' is deprecated!}}
\DeclareOption{rowvectors}{\def\xv{\rv}}
\DeclareOption{notop}{\def\notop{1}}
\DeclareOption{numericteilaufgabe}{\def\numericteilaufgabe{1}}
\DeclareOption*{\PassOptionsToClass{\CurrentOption}{scrartcl}}
\ProcessOptions\relax

%% Load superclass

\LoadClass{scrartcl}

%% Commands

\newcommand{\fach}[1]{\def\thefach{#1}}
\newcommand{\blatt}[2][]{\def\theblatt{#2}%
	\def\next{#1}% ifx cannot evaluate #1 correct, but token, so give him one
	\ifx\next\empty \else\fach{#1}\fi}
\newcommand{\righthead}[1]{\def\therighthead{#1}}
\newcommand{\aufgabenlabel}[1]{\def\theaufgabenlabel{#1}}

%% Misc Macros

\newcommand{\cv}[1]{%
	\let\dots\vdots
	\begin{pmatrix}#1\end{pmatrix}%
}
\newcommand{\rv}[1]{%
	\let\\,
	\let\dots\ldots
	\left(#1\right)^T%
}

\newcommand{\xor}{\mbox{\small\textsc{ xor }}}
\newcommand{\N}{\mathbbm N}
\newcommand{\Z}{\mathbbm Z}
\newcommand{\Q}{\mathbbm Q}
\newcommand{\R}{\mathbbm R}
\newcommand{\C}{\mathbbm C}
\newcommand{\zz}{\textit{zz}}

\renewcommand{\[}{\begin{eqnarray*}}
\renewcommand{\]}{\end{eqnarray*}}

\newcommand{°}{\text{\textdegree}}

%% English/Deutsch

\def\mylang{de}
\newcommand{\de}{\def\mylang{de}\selectlanguage{german}}
\newcommand{\en}{\def\mylang{en}\selectlanguage{english}}
\newcommand
	{\translate}[2]
	{\ifthenelse{\equal{\mylang}{de}}{#1}{#2}}
\def\translatedheader{\translate
	{Blatt \theblatt{} -- Seite \thepage{} von \pageref{LastPage}}%
    {Assignment \theblatt{} -- Page \thepage{} of \pageref{LastPage}}}%

%% Punktetabelle

\newcounter{ptcc}
\newcommand
	{\ptloop}[1]
	{\ifthenelse{\value{ptcc}<\value{aufgabentotal}}
		{\stepcounter{ptcc}#1 \hspace{0.3cm} & \ptloop{#1}}{}%
}
\newcommand
	{\punktetabelle}
	{
	\newcounter{ptwidth}
	\setcounter{ptwidth}{\theaufgabentotal}
	\addtocounter{ptwidth}{-\theaufgabenstart}
	\ifnum\value{aufgabentotal}>0{
	 \begin{flushright}%
	   \textsc{\translate{Punkteverteilung}{Credits Summary}:}\\\vspace{0.5em}%
	   \begin{tabular}{*{\theptwidth}{c|}|c}%
	      \setcounter{ptcc}{\theaufgabenstart}\ptloop{\arabic{ptcc}}
	        $\Sigma$ \\\hline%
	      \setcounter{ptcc}{\theaufgabenstart}\ptloop{} \\%
	   \end{tabular}%
	 \end{flushright}%
	}\fi%
}

%% Aufgaben-Makro

\newcounter{aufgabenstart}
\setcounter{aufgabenstart}{0}
\newcounter{aufgabentotal}
\setcounter{aufgabentotal}{0}
\newcounter{aufgabe}
\setcounter{aufgabe}{0}
\newcommand
	{\insideteilaufgabe}
	{false}
\newcommand
	{\aufgabe}[1][]
	{\teilaufgabeClose\stepcounter{aufgabe}%
	\def\next{#1}% ifx cannot evaluate #1 correct, but be token!
	\ifx\next\empty \else
	  \ifnum\value{aufgabe}=1
		\setcounter{aufgabenstart}{#1}
		\addtocounter{aufgabenstart}{-1}
	  \fi%
	  \setcounter{aufgabe}{#1}%
	\fi%
	\ifnum\theidestyle=1{%
	  \medskip\vspace{2em} \textbf{\theaufgabenlabel\ (\arabic{aufgabe})}\vspace{1em}%
	}\else{%
	  \medskip\hspace{-1em}\textbf{\theaufgabenlabel\ (\arabic{aufgabe})}

	}\fi}
\newcommand
	{\teilaufgabeBeginEnumerate}
	{\ifnum\numericteilaufgabe=1
		\begin{enumerate}[(1)]%
	\else%
		\begin{enumerate}[(a)]%
	\fi}
\newcommand
	{\teilaufgabe}
	{\ifthenelse%
	    {\equal{\insideteilaufgabe}{false}}%
	    {\vspace{-1ex}\teilaufgabeBeginEnumerate\renewcommand{\insideteilaufgabe}{true}}{}%
	\item}
\newcommand
	{\teilaufgabeClose}
	{\ifthenelse%
	    {\equal{\insideteilaufgabe}{true}}%
	    {\end{enumerate}\renewcommand{\insideteilaufgabe}{false}}{}}
\newcommand
	{\uebungsblattende}
	{\teilaufgabeClose%
	\immediate\write\@auxout{%
		\string\setcounter{aufgabenstart}{\arabic{aufgabenstart}}%
		\string\setcounter{aufgabentotal}{\arabic{aufgabe}}%
	}%
}

%% Delayed handlers

\AtBeginDocument{%
  \ifnum\theidestyle=1{%
	\lhead {%
	  \ifx\@author\empty\else\textsc{\@author}\\\fi%
	  \ifx\thefach\empty\else\thefach\fi%
	}%
	\rhead {%
	  \ifx\therighthead\empty\else\therighthead\\\fi%
	  \today%
	}%
	\cfoot{Seite \thepage{} von \pageref{LastPage}}%
  }\else{%
	\settowidth{\tmplength}{\thefach{} -- \translatedheader00 \textsc{\@author}}
	\ifnum\tmplength>\textwidth{%
	  \lhead{\thefach{} \\ \translatedheader}%
	}\else{%
	  \lhead{\thefach{} -- \translatedheader}%
	}\fi%
	\rhead{\textsc{\@author}}%
	\cfoot{}%
  }\fi%
  \renewcommand{\headrulewidth}{0.5pt}
  \ifnum\notop=0{\punktetabelle}\fi%
}

\AtEndDocument{\uebungsblattende}

%% Style

\ifnum\morespace=1{%
	% unset margin stuff, use all space for body
	\oddsidemargin 0pt
	\marginparwidth 0pt
	\marginparsep 0pt
	\footskip 30pt
	\addtolength{\textheight}{70pt}
	\addtolength{\textwidth}{40pt}
}\fi%

\parindent 0pt
\parskip 1ex

\pagestyle{fancy}

% vi:ts=4:fenc=latin1:ft=tex


