% ====================[ classicthesis-lyx-ldpkg.sty        ]====================
%                     [ Time-stamp: "2009-04-18 19:29:36 leycec" ]
%
% --------------------( SYNOPSIS                           )--------------------
% This file is harvested, wholesale, from the original version of
% "classicthesis-ldpkg.sty" shipped with classicthesis. This file is meant to be
% included in the preamble of this classicthesis LyX port, and comes inspired by
% Nick Mariette's generous earlier classicthesis LyX port.
%
% --------------------( CHANGES                            )--------------------
% This file is substantially altered from the original version, since:
%
% * LyX implicitly loads several packages the original version also loaded. In
%   some cases (e.g., "babel") this is merely redundant; in other cases (e.g.,
%   "natbib") this actually induces a package clash with LyX. This file thus
%   delegates loading and configure of these packages to LyX:
%   * amsmath.
%   * babel.
%   * graphicx.
%   * inputinc.
%   * listings.
%   * natbib.
%
% ....................{ CLASSICTHESIS                      }....................
% ********************************************************************
% classicthesis-ldpkg.sty (formerly known as loadpackages.sty)
%
% Copyright (C) 2009 André Miede http://www.miede.de
%
% If you like the style then I would appreciate a postcard. My address 
% can be found in the file ClassicThesis.pdf. A collection of the 
% postcards I received so far is available online at 
% http://postcards.miede.de
%
% License:
% This program is free software; you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation; either version 2 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program; see the file COPYING.  If not, write to
% the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
% Boston, MA 02111-1307, USA.
%
% ********************************************************************  
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{classicthesis-lyx-ldpkg}[2009/05/09 v2.5.1 Package Loader for Classic Thesis Style]
 
\@ifpackageloaded{classicthesis}%
    {\PackageWarningNoLine{classicthesis-lyx-ldpkg}{Package "classicthesis" should be loaded AFTER "classicthesis-lyx-ldpkg".}}%
    {\relax}%    
 
% ********************************************************************
% Options
% ******************************************************************** 
\RequirePackage{ifthen}
    \newboolean{cld@nochapters} % disable all chapter-specific commands
    \newboolean{cld@biblatex} % enable BibLaTeX compatibility
\DeclareOption{nochapters}{\setboolean{cld@nochapters}{true}}
\DeclareOption{biblatex}{\setboolean{cld@biblatex}{true}}
\DeclareOption*{\PackageWarning{classicthesis-lyx-ldpkg}%
  {Unknown option ‘\CurrentOption’}}
\ProcessOptions\relax

% ******************************************************************** 
% Packages
% ******************************************************************** 
\RequirePackage[T1]{fontenc}                
\RequirePackage{textcomp}
\RequirePackage[dvipsnames,usenames]{xcolor}
\RequirePackage{xspace} % to get the spacing after macros right
\RequirePackage{tabularx} % better tables
\RequirePackage{mparhack} % get marginpar right
\RequirePackage{fixltx2e} % fixes some LaTeX stuff
%\RequirePackage{ellipsis} % ... ;-)                  
\RequirePackage{relsize} % scale font up or down                                        
\RequirePackage[smaller]{acronym} % nice macros for handling all acronyms in the thesis
\renewcommand{\bflabel}[1]{{#1}\hfill} % fix the list of acronyms

% ********************************************************************
% Using BibLaTeX:
% ********************************************************************
% Define a bibliography command for typesetting author citations. If no
% bibliography package has been loaded, this is changed to simply \relax,
% below.
\newcommand{\cauthor}[1]{\person{\citeauthor{#1}}}

% If enabling BibLaTeX, import the "biblatex" package and configure it. This
% should be done before importing the "hyperref" package. Also note that the
% absolute path for the bibliography file for this document must be explicitly
% specified in that document's preamble using the \bibliography{..} command.
%
% Otherwise, import the "natbib" package and configure that.
\ifthenelse{\boolean{cld@biblatex}}%
  {%FIXME: Consider enabling "refsection=part," "bibencoding=inputenc," and
   %\RequirePackage[style=alphabetic]{biblatex-chicago}%. If enabling
   %"refsection=part," note that this also probably requires use of the
   %"\bibbysection" command rather than "\printbibliography".
   \RequirePackage[%
     backref=true,   % Enable back references in the bibliography.
     hyperref=true,  % Enable clickable references in the bibliography.
     natbib=true,    % Define command aliases for "natbib" compatibility.
   ]{biblatex}%
   % Redefine the "bibintoc" bibliography heading style so as to produce a
   % classicthesis-formatted bibliography heading.
   \defbibheading{bibintoc}{%
     % Print the bibliography heading without numbering it, ensuring that the
     % header also prints the heading in small caps.
     \ifthenelse{\boolean{cld@nochapters}}%
       {\section*{\refname}}%
       {\chapter*{\refname}}%
     \manualmark%
     \markboth{\spacedlowsmallcaps{\refname}}{\spacedlowsmallcaps{\refname}}%
%    \@mkboth{\spacedlowsmallcaps{\refname}}{\spacedlowsmallcaps{\refname}}%
     % Increment the dummy counter so as to produce a properly linked entry in
     % the Table of Contents.
     \refstepcounter{dummy}%
     % Add a reference to the heading to the Table of Contents, vertically
     % delimited from the prior entry by whitespace.
     \addtocontents{toc}{\protect\vspace{\beforebibskip}}%
     \ifthenelse{\boolean{cld@nochapters}}%
       {\addcontentsline{toc}{section}{\refname}}%
       {\addcontentsline{toc}{chapter}{\refname}}%
     \label{app:bibliography}% 
%    \addcontentsline{toc}{chapter}{\tocEntry{\bibname}}
   }%
   % Define bibliography commands, now that "biblatex" is loaded.
   \newcommand{\professBibliography}{\printbibliography[heading=bibintoc]}
   % BibLaTeX's \bibliography command, unlike Natbib's \bibliography command,
   % does not actually print the bibliography; rather, it registers the path to
   % that bibliography. Thus, we can call this command at any time. We do so now
   % so as to immediately notify external tools (e.g., LyX) of this path.
   \bibliography{\myBibliography}%
  }%
  {% LyX already requires the "natbib" package, as needed.
   \@ifpackageloaded{natbib}{}{\RequirePackage{natbib}}%
   % Define bibliography commands, as "natbib" was loaded. See comments, above.
   \newcommand{\professBibliography}{%
     \manualmark%
     \markboth{\spacedlowsmallcaps{\bibname}}{\spacedlowsmallcaps{\bibname}}%
     \refstepcounter{dummy}%
     \addtocontents{toc}{\protect\vspace{\beforebibskip}}%
     \addcontentsline{toc}{chapter}{\tocEntry{\bibname}}%
     \bibliographystyle{plainnat}%
     \label{app:bibliography}%
     \bibliography{\myBibliography}%
  }}%

% ********************************************************************
% Commands
% ********************************************************************                
\newcounter{dummy} % necessary for correct hyperlinks (to index, bib, etc.)
\newcommand{\longpage}{\enlargethispage{3\baselineskip}}
\newcommand{\medpage}{\enlargethispage{2\baselineskip}}
\newcommand{\shortpage}{\enlargethispage{\baselineskip}}
\newcommand{\concskip}{\medskip}
\newcommand{\myPrimRef}[1]{\textit{#1}} % how primary index entries will be marked 
% ******************************************************************** 
\newcommand{\ie}{i.\,e.}
\newcommand{\Ie}{I.\,e.}
\newcommand{\eg}{e.\,g.}
\newcommand{\Eg}{E.\,g.}
\newcommand{\etAl}{et al.\xspace}
\newcommand{\twod}{2\textsc{d}\xspace}
\newcommand{\threed}{3\textsc{d}\xspace}
% ******************************************************************** 
\newcommand{\system}[1]{\emph{#1}}
\newcommand{\person}[1]{#1}
\newcommand{\term}[1]{\emph{#1}}
%FIXME: This command moved above.%\newcommand{\cauthor}[1]{\relax}
%\@ifpackageloaded{natbib}%
%  {\renewcommand{\cauthor}[1]{\person{\citeauthor{#1}}}}%
%  {\PackageWarningNoLine{classicthesis-ldpk}{Package "natbib" not loaded, %
%        command "cauthor" will not work.}}
\newcommand{\tableheadline}[1]{\multicolumn{1}{c}{\spacedlowsmallcaps{#1}}}
% ******************************************************************** 
\newcommand{\RA}{\ensuremath{\Rightarrow}}
\newcommand{\ra}{\ensuremath{\rightarrow}}
\newcommand{\lra}{\ensuremath{\leftrightarrow}}
\newcommand{\On}{\ensuremath{O(n)}\xspace}
\newcommand{\Ologn}{\ensuremath{O(\log n)}\xspace}
\newcommand{\Oone}{\ensuremath{O(1)}\xspace}

% ********************************************************************
% take care of the caption fonts and sizes (tables and figures)
\RequirePackage{caption}
    
% ********************************************************************
% fine typesetting of code listings
% LyX already requires the "listings" package, as needed.
\@ifpackageloaded{listings}{}{\RequirePackage{listings}}

% ********************************************************************
% caption numbering now w/o chapter number
\RequirePackage{remreset}
\ifthenelse{\boolean{cld@nochapters}}%
  {}%
  {\@removefromreset{table}{chapter}%
   \@removefromreset{figure}{chapter}%
   \@ifpackageloaded{listings}%
     {\@removefromreset{lstlisting}{chapter}}%
     {\relax}%
  }
\renewcommand{\thetable}{\arabic{table}}
\renewcommand{\thefigure}{\arabic{figure}}  
\@ifpackageloaded{listings}%
  {% Might cause an error with some TeX distributions.
   \AtBeginDocument{\renewcommand{\thelstlisting}{\arabic{lstlisting}}}%
  }%
  {\relax}

% ********************************************************************
% enable subfigures in figures
% LyX already requires the "subfig" package, as needed.
\@ifpackageloaded{subfig}{}{\RequirePackage{subfig}}

% ********************************************************************
% Using PDFLaTeX:
% ********************************************************************
% If enabling BibLaTeX, do not pass the "backref" or "pagebackref" package
% options to the "hyperref" package, as "backref" is incompatible with
% BibLaTeX; otherwise, do.
\ifthenelse{\boolean{ctl@biblatex}}%
  {}%
  {\PassOptionsToPackage{pagebackref=true}{biblatex}}

% LyX already requires the "hyperref" package, as needed.
\@ifpackageloaded{hyperref}%
  {}%
  {\ifpdf
     \RequirePackage[pdftex=true,pdfpagelabels=true]{hyperref}
   \else
     \RequirePackage[dvips=true]{hyperref}
   \fi
   \hypersetup{hyperfootnotes=false}
  }

\ifpdf
  \pdfcompresslevel=9
  \pdfadjustspacing=1 
\fi

% LyX already requires the "graphicx" package, as needed.
\@ifpackageloaded{graphicx}%
  {}%
  {\ifpdf
     \RequirePackage[pdftex]{graphicx}
   \else
     \RequirePackage[dvips]{graphicx}
   \fi
  }

% Enable use of MetaPost graphics with PDFLaTex.
\DeclareGraphicsRule{*}{mps}{*}{}

% ********************************************************************
% for German texts to get \autoref macros right
\renewcommand{\figureautorefname}{Figure}
\renewcommand{\tableautorefname}{Table}
\renewcommand{\partautorefname}{Part}
\renewcommand{\chapterautorefname}{Chapter}
\renewcommand{\sectionautorefname}{Section}
\renewcommand{\subsectionautorefname}{Section}
\renewcommand{\subsubsectionautorefname}{Section}
%\renewcommand{\paragraphautorefname}{Absatz}
%\renewcommand{\subparagraphautorefname}{Unterabsatz}
%\renewcommand{\footnoteautorefname}{Fu\"snote}
%\renewcommand{\FancyVerbLineautorefname}{Zeile}
%\renewcommand{\theoremautorefname}{Theorem}
%\renewcommand{\appendixautorefname}{Anhang}
%\renewcommand{\equationautorefname}{Gleichung}        
%\renewcommand{\itemautorefname}{Punkt}                
%\newcommand*{\subfigureautorefname}{Abbildung}

% ********************************************************************
% get the links to the figures and tables right
% ********************************************************************
%\RequirePackage[all]{hypcap} % to be loaded after hyperref package

% ********************************************************************
% setup the style of the backrefs from the bibliography
% ********************************************************************
\ifthenelse{\boolean{cld@biblatex}}%
  {%BibLaTeX-specific back references.
   \renewbibmacro*{pageref}{%
     \iflistundef{pageref}%
       {}%
       {(Cited on\space%
        \ifnum\value{pageref}>1\relax%
          \bibstring{pages}\ppspace%
        \else%
          \bibstring{page}\ppspace%
        \fi%
        \printlist[pageref][-\value{listtotal}]{pageref}.)}}%
  }%
  {%Natbib-specific back references via the "backref" package.
   \newcommand{\backrefnotcitedstring}{\relax}%(Not cited.)
   \newcommand{\backrefcitedsinglestring}[1]{(Cited on page~#1.)}%
   \newcommand{\backrefcitedmultistring}[1]{(Cited on pages~#1.)}%
   \RequirePackage[hyperpageref]{backref} % to be loaded after hyperref package 
      \renewcommand{\backreftwosep}{ and~} % seperate 2 pages
      \renewcommand{\backreflastsep}{, and~} % seperate last of longer list
      \renewcommand*{\backref}[1]{}  % Disable standard
      \renewcommand*{\backrefalt}[4]{% Detailed backref
         \ifcase #1 %
            \backrefnotcitedstring%
         \or%
            \backrefcitedsinglestring{#2}%
         \else%
            \backrefcitedmultistring{#2}%
         \fi%
      }%
  }
%   \hspace*{\fill}{\scriptsize\itshape\mbox{%
%   \ifnum#1=1 %
%     (Cited on page~%
%   \else
%     (Cited on pages~%
%   \fi
%   #2)}
%}       
