%%
%% This is the DocBook LaTeX style used by DB2LaTeX (http://db2latex.sf.net)
%%
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{db2latex}[2003/04/11 DB2LaTeX DocBook Style]

% The defined options
\DeclareOption{hyperlink}{ \def\DBKhyperlink{yes} }
\DeclareOption{nohyperlink}{ \def\DBKhyperlink{no} }

% Default values
\ExecuteOptions{nohyperlink}

% If defined, process the options
\ProcessOptions\relax

% Needed packages
\usepackage{ifthen}
\usepackage{float}       
\usepackage{footmisc} 
\usepackage{appendix} 

% Workaround to avoid troubles with accents in \appendixname used as hyperref
% label: force the label prefix to 'appendix' whatever the lang is
\let\orgresetsapp=\@resets@pp
\let\orgrestoreapp=\restoreapp
\let\restoreapp\relax
\def\@resets@pp{%
  \orgresetsapp
  \if@pphyper
    \def\Hy@chapapp{appendix}%
  \fi
  \orgrestoreapp
}

\newcommand{\DBKreleaseinfo}{Working Paper}

\usepackage{dbk_fonts}
\usepackage{dbk_hyper}
\usepackage{dbk_biblio}
\usepackage{dbk_table}
\usepackage{dbk_changebar}
\usepackage{dbk_graphic}
\usepackage{dbk_cover}
\usepackage{dbk_verbatim}
\usepackage{dbk_misc}
\usepackage{dbk_callout}
\usepackage{dbk_locale}
\usepackage{dbk_annot}

% For missing options in article
\@ifundefined{if@openright}
  {\let\if@openright\iftrue}{}

% LaTeX <othercredit> commands to define
%
% #1: mapped to <contrib>
% #2: mapped to <firstname> <surname>
%
\def\DBKinditem#1#2{
  #2  & #1 \tabularnewline
  \hline
}

\newenvironment{DBKindtable}{
  \begin{tabular}{ |l|l| }
  \hline
  \emph{Collaborator} & \emph{Contribution}
  \hline
}{
  \end{tabular}
}

%
% Latex <revhistory> table
%
\newenvironment{DBKrevtable}{
  \begin{longtable}{ |l|l|l|l| }
  \hline
  Revision number & Date & Release Info & Author \tabularnewline
  \hline
  \endhead
}{
  \end{longtable}
}

\newcommand{\DBKreference}{}
\newcommand{\DBKsite}{}
\newcommand{\DBKedition}{}
\newcommand{\DBKsubtitle}{}
\newcommand{\DBKdate}{\today}
\newcommand{\DBKpubdate}{}
\newcommand{\DBKindexation}{}
\newcommand{\DBKrevhistory}{}
\newcommand{\DBKlegalblock}{}
\newcommand{\DBKlegaltitle}{}
\newcommand{\DBKcopyright}{}

% Display ``DRAFT'' as a water mark:
\usepackage{ifpdf}

\newif\ifpdfengine
\ifpdf\pdfenginetrue
\else\ifxetex\pdfenginetrue
\fi\fi

\ifpdfengine
\usepackage{pdfpages}
\usepackage{color}
\def\showwatermark#1{%
  \AddToShipoutPicture{
  \AtTextCenter{%
  \makebox(0,0)[c]{\resizebox{\textwidth}{!}{%
  \rotatebox{45}{\textsf{\textbf{\color[gray]{0.8}#1}}}}}%
  }}
}
\else
\def\showwatermark#1{}%
\fi

% --------------------------
% Title and Bookinfo display
% --------------------------
\let\stdtitle=\title
\def\title#1{
  \def\DBKtitle{#1}
  \stdtitle{\bfseries #1%
    \ifthenelse{\equal{\DBKsubtitle}{}}{}{\\[12pt]\normalsize \DBKsubtitle}}
}

\let\stdauthor=\author
\def\author#1{
  \def\DBKauthor{#1}
  \stdauthor{\begin{minipage}{10cm}\centering #1\end{minipage}}%
}

\let\stdmaketitle=\maketitle
\def\maketitle{
  \stdmaketitle
  \noindent
  \textbf{\DBKtitle \\}
  \ifx\DBKauthor\empty\else{by \DBKauthor \\}\fi
  \hspace{0pt}\\
  \ifthenelse{\equal{\DBKedition}{}}{}{Edition \DBKedition \\}
  \ifthenelse{\equal{\DBKpubdate}{}}{}{Published \DBKpubdate \\}
  \ifthenelse{\equal{\DBKcopyright}{}}{}{\DBKcopyright \\}
  \hspace{0pt}\\
  % Now the legalnotice block
  \DBKlegalblock
  \newpage
}

\newenvironment{DBKlegalnotice}{
  \if@twocolumn
    \noindent\small\textit{\DBKlegaltitle}
    \/\bfseries---$\!$
  \else
    \noindent\begin{center}\small\bfseries\DBKlegaltitle
    \end{center}\begin{quote}\small
  \fi
}{
  \vspace{0.6em}\par\if@twocolumn\else\end{quote}\fi
}

% --------------------------------------------
% Check for PDFLaTeX/LaTeX 
% --------------------------------------------
% Default page setup if not already done by geometry
\@ifpackageloaded{geometry}{}{
\usepackage{anysize}
\marginsize{3cm}{2cm}{1.25cm}{1.25cm}
}

\makeatletter

% Redefine paragraph to put the title on a separate line
\renewcommand\paragraph{\@startsection{paragraph}{4}{\z@}%
                                     {-3.25ex\@plus -1ex \@minus -.2ex}%
                                     {1ex \@plus .2ex}%
                                     {\normalfont\normalsize\bfseries}}

% redefine the listoffigures and listoftables so that the name of the chapter
% is printed whenever there are figures or tables from that chapter. encourage
% pagebreak prior to the name of the chapter (discourage orphans).
\let\save@@chapter\@chapter
\let\save@@l@figure\l@figure
\let\the@l@figure@leader\relax
\def\@chapter[#1]#2{\save@@chapter[{#1}]{#2}%
\addtocontents{lof}{\protect\def\the@l@figure@leader{\protect\pagebreak[0]\protect\contentsline{chapter}{\protect\numberline{\thechapter}#1}{}{\thepage}}}%
\addtocontents{lot}{\protect\def\the@l@figure@leader{\protect\pagebreak[0]\protect\contentsline{chapter}{\protect\numberline{\thechapter}#1}{}{\thepage}}}%
}
\renewcommand*\l@figure{\the@l@figure@leader\let\the@l@figure@leader\relax\save@@l@figure}
\let\l@table\l@figure
\makeatother
\usepackage{fancyhdr}
\renewcommand{\headrulewidth}{0.4pt}
\renewcommand{\footrulewidth}{0.4pt}
% Safeguard against long headers.
\IfFileExists{truncate.sty}{
\usepackage{truncate}
% Use an ellipsis when text would be larger than x% of the text width.
% Preserve left/right text alignment using \hfill (works for English).
\fancyhead[ol]{\truncate{0.49\textwidth}{\sl\leftmark}}
\fancyhead[er]{\truncate{0.49\textwidth}{\hfill\sl\rightmark}}
\fancyhead[el]{\truncate{0.49\textwidth}{\sl\leftmark}}
\fancyhead[or]{\truncate{0.49\textwidth}{\hfill\sl\rightmark}}
}{\typeout{WARNING: truncate.sty wasn't available and functionality was skipped.}}
\pagestyle{fancy}

% Define front/main/back matter if not available
\makeatletter
\@ifundefined{frontmatter}{
  \def\frontmatter{\cleardoublepage\pagenumbering{roman}}
}{}
\@ifundefined{mainmatter}{
  \def\mainmatter{\cleardoublepage\pagenumbering{arabic}}
}{}
\@ifundefined{backmatter}{
  \def\backmatter{\if@openright\cleardoublepage\else\clearpage\fi}
}{}


\@ifundefined{abstract}{
\newenvironment{abstract}{
  \setlength\parskip{\medskipamount}%
  \setlength\parindent{0pt}%
}{}
}{
% Redefine abstract in avoid page count reset
\def\nocount#1#2{}
\let\stdsetcounter\setcounter

\let\stdabstract\abstract
\let\endstdabstract\endabstract

\renewenvironment{abstract}{%
\let\setcounter\nocount
\stdabstract
}{%
\endstdabstract
\let\setcounter\stdsetcounter
}
}

\makeatother

% ---------------------- 
% Most Common Packages   
% ---------------------- 
\usepackage{latexsym} 
% FIXME: enumitem is expected by the stylesheets
\usepackage{enumitem}
\usepackage{fancybox} 
\usepackage{ragged2e} 
\usepackage{fancyvrb} 
\makeatletter\@namedef{FV@fontfamily@default}{\def\FV@FontScanPrep{}\def\FV@FontFamily{}}\makeatother
\fvset{obeytabs=true,tabsize=3}
\makeatletter
\let\dblatex@center\center\let\dblatex@endcenter\endcenter
\def\dblatex@nolistI{\leftmargin\leftmargini\topsep\z@ \parsep\parskip \itemsep\z@}
\def\center{\let\@listi\dblatex@nolistI\@listi\dblatex@center\let\@listi\@listI\@listi}
\def\endcenter{\dblatex@endcenter}
\makeatother
\usepackage{rotating}         
\usepackage{subfigure}         
\usepackage{tabularx}         

% --------------------------------------------
% Math support                                
% --------------------------------------------
\usepackage{amsmath,amsthm,amsfonts,amsxtra,amsopn}

% ---------------
% Document Font  
% ---------------
\ifxetex\else
\usepackage{palatino}
\fi

% ----------------------------------------------
% Define a new LaTeX environment (adminipage)
% ----------------------------------------------
\newenvironment{admminipage}%
{ % this code corresponds to the \begin{adminipage} command
  \begin{Sbox}%
  \begin{minipage}%
} %done
{ % this code corresponds to the \end{adminipage} command
  \end{minipage}
  \end{Sbox}
  \fbox{\TheSbox}
} %done

% ----------------------------------------------
% Define a new LaTeX length (admlength)
% ----------------------------------------------
\newlength{\admlength}

% ----------------------------------------------
% Define a new LaTeX environment (admonition)
% With 2 parameters:
% #1 The image (e.g. note.pdf)
% #2 The caption
% ----------------------------------------------
\newenvironment{DBKadmonition}[2] {
  % this code corresponds to the \begin{admonition} command
  \hspace{0mm}\newline\hspace*\fill\newline
  \noindent
  \setlength{\fboxsep}{5pt}
  \setlength{\admlength}{\linewidth}
  \addtolength{\admlength}{-10\fboxsep}
  \addtolength{\admlength}{-10\fboxrule}
  \admminipage{\admlength}
  {\bfseries \sc\large{#2}} \newline
  ~\\[1mm]
  \sffamily
  \ifthenelse{\equal{#1}{}}{
    \def\admgraph{false}
  }{
    \def\admgraph{true}
    \includegraphics[width=1cm]{#1}
    \addtolength{\admlength}{-1cm}
    \addtolength{\admlength}{-20pt}
    \begin{minipage}[lt]{\admlength}
  }
  \parskip=0.5\baselineskip \advance\parskip by 0pt plus 2pt
} %done
{ % this code corresponds to the \end{admonition} command
  \vspace{5mm} 
  \ifthenelse{\equal{\admgraph}{false}}{}{
    \end{minipage}
  }
  \endadmminipage
  \vspace{.5em}
  \par
}

\newenvironment{sidebar}[1][]{
  \hspace{0mm}\newline%
  \noindent\begin{Sbox}\begin{minipage}{\linewidth-2\fboxsep-2\fboxrule-\shadowsize}
}{
  \end{minipage}\end{Sbox}\shadowbox{\TheSbox}%
}

% --------------------------------------------
% Commands to manage/style/create floats      
% figures, tables, algorithms, examples, eqn  
% --------------------------------------------
\makeatletter
\floatstyle{ruled}
\restylefloat{figure}
\floatstyle{ruled}
\restylefloat{table}
\@ifundefined{chapter}{
  \floatstyle{ruled}
  \newfloat{program}{ht}{lop}[section]
  \floatstyle{ruled}
  \newfloat{example}{ht}{loe}[section]
  \floatname{example}{\examplename}
  \floatstyle{ruled}
  \newfloat{dbequation}{ht}{lom}[section]
  \def\toclevel@dbequation{0}
  \floatname{dbequation}{\dbequationname}
  \floatstyle{boxed}
  \newfloat{algorithm}{ht}{loa}[section]
  \floatname{algorithm}{Algorithm}
}{
  \floatstyle{ruled}
  \newfloat{program}{ht}{lop}[chapter]
  \floatstyle{ruled}
  \newfloat{example}{ht}{loe}[chapter]
  \floatname{example}{\examplename}
  \floatstyle{ruled}
  \newfloat{dbequation}{ht}{lom}[chapter]
  \def\toclevel@dbequation{0}
  \floatname{dbequation}{\dbequationname}
  \floatstyle{boxed}
  \newfloat{algorithm}{ht}{loa}[chapter]
  \floatname{algorithm}{Algorithm}
}
\makeatother

% --------------------------------------------
% $latex.caption.swapskip enabled for $formal.title.placement support
%\newlength{\docbooktolatextempskip}
%\newcommand{\captionswapskip}{\setlength{\docbooktolatextempskip}{\abovecaptionskip}\setlength{\abovecaptionskip}{\belowcaptionskip}\setlength{\belowcaptionskip}{\docbooktolatextempskip}}
%% Guard against a problem with old package versions.
%\makeatletter
%\AtBeginDocument{
%\DeclareRobustCommand\ref{\@refstar}
%\DeclareRobustCommand\pageref{\@pagerefstar}
%}
%\makeatother
%% --------------------------------------------
\makeatletter
\newcommand{\dbz}{\penalty \z@}
\newcommand{\docbooktolatexpipe}{\ensuremath{|}\dbz}
\newskip\docbooktolatexoldparskip
\newcommand{\docbooktolatexnoparskip}{\docbooktolatexoldparskip=\parskip\parskip=0pt plus 1pt}
\newcommand{\docbooktolatexrestoreparskip}{\parskip=\docbooktolatexoldparskip}
\def\cleardoublepage{\clearpage\if@twoside \ifodd\c@page\else\hbox{}\thispagestyle{empty}\newpage\if@twocolumn\hbox{}\newpage\fi\fi\fi}

\ifx\dblatex@chaptersmark\@undefined\def\dblatex@chaptersmark#1{\markboth{\MakeUppercase{#1}}{}}\fi
\let\save@makeschapterhead\@makeschapterhead
\def\dblatex@makeschapterhead#1{\vspace*{-80pt}\save@makeschapterhead{#1}}
\def\@makeschapterhead#1{\dblatex@makeschapterhead{#1}\dblatex@chaptersmark{#1}}

			
\AtBeginDocument{\ifx\refname\@undefined\let\docbooktolatexbibname\bibname\def\docbooktolatexbibnamex{\bibname}\else\let\docbooktolatexbibname\refname\def\docbooktolatexbibnamex{\refname}\fi}
% Facilitate use of \cite with \label
\newcommand{\docbooktolatexbibaux}[2]{%
  \protected@write\@auxout{}{\string\global\string\@namedef{docbooktolatexcite@#1}{#2}}
}

%\usepackage{cite}
%\renewcommand\citeleft{(}  % parentheses around list
%\renewcommand\citeright{)} % parentheses around list
\newcommand{\docbooktolatexcite}[2]{%
  \@ifundefined{docbooktolatexcite@#1}%
  {\cite{#1}}%
  {\def\@docbooktolatextemp{#2}\ifx\@docbooktolatextemp\@empty%
   \cite{\@nameuse{docbooktolatexcite@#1}}%
   \else\cite[#2]{\@nameuse{docbooktolatexcite@#1}}%
   \fi%
  }%
}
\newcommand{\docbooktolatexbackcite}[1]{%
  \ifx\Hy@backout\@undefined\else%
    \@ifundefined{docbooktolatexcite@#1}{%
      % emit warning?
    }{%
      \ifBR@verbose%
        \PackageInfo{backref}{back cite \string`#1\string' as \string`\@nameuse{docbooktolatexcite@#1}\string'}%
      \fi%
      \Hy@backout{\@nameuse{docbooktolatexcite@#1}}%
    }%
  \fi%
}

% --------------------------------------------
% A way to honour <footnoteref>s
% Blame j-devenish (at) users.sourceforge.net
% In any other LaTeX context, this would probably go into a style file.
\newcommand{\docbooktolatexusefootnoteref}[1]{\@ifundefined{@fn@label@#1}%
  {\hbox{\@textsuperscript{\normalfont ?}}%
    \@latex@warning{Footnote label `#1' was not defined}}%
  {\@nameuse{@fn@label@#1}}}
\newcommand{\docbooktolatexmakefootnoteref}[1]{%
  \protected@write\@auxout{}%
    {\global\string\@namedef{@fn@label@#1}{\@makefnmark}}%
  \@namedef{@fn@label@#1}{\hbox{\@textsuperscript{\normalfont ?}}}%
  }

% index labeling helper
\let\dbtolatex@@theindex\theindex
\let\dbtolatex@@endtheindex\endtheindex
\renewenvironment{theindex}{
  \if@openright\cleardoublepage\else\clearpage\fi
  \dbtolatex@@theindex
  % To have the index in the TOC
  \@ifundefined{chapter}{
    \addcontentsline{toc}{section}{\numberline{}\indexname}
  }{
    \refstepcounter{chapter}
    \ifnum \value{part} >\z@
      \addcontentsline{toc}{part}{\protect\numberline{}\indexname}
    \else
      \addcontentsline{toc}{chapter}{\numberline{}\indexname}
    \fi
  }
}{
  \dbtolatex@@endtheindex%
}

\newlength\saveparskip \newlength\saveparindent
\newlength\tempparskip \newlength\tempparindent

% Prevent multiple openings of the same aux file
% (happens when backref is used with multiple bibliography environments)
\ifx\AfterBeginDocument\undefined\let\AfterBeginDocument\AtBeginDocument\fi
\AfterBeginDocument{
  \let\latex@@starttoc\@starttoc
  \def\@starttoc#1{%
    \@ifundefined{docbooktolatex@aux#1}{%
      \global\@namedef{docbooktolatex@aux#1}{}%
      \latex@@starttoc{#1}%
    }{}
  }
}

