% System stylesheet for the new LaTeX writer, newlatex2e.

% Major parts of the rendering are done in this stylesheet and not in the
% Python module.

% For development notes, see notes.txt.

% User documentation (in the stylesheet for now; that may change though):

% Naming conventions:
% All uppercase letters in macro names have a specific meaning.
% \D...: All macros introduced by the Docutils LaTeX writer start with "D".
% \DS<name>: Setup function (called at the bottom of this stylesheet).
% \DN<nodename>{<contents>}: Handler for Docutils document tree node `node`; called by
%                            the Python module.
% \DEV<name>: External variable, set by the Python module.
% \DEC<name>: External command.  It is called by the Python module and must be
%             defined in this stylesheet.
% \DN<nodename>A<attribute>{<number>}{<attribute>}{<value>}{<nodename>}{<contents>}:
%     Attribute handler for `attribute` set on nodes of type `nodename`.
%     See below for a discussion of attribute handlers.
% \DA<attribute>{<number>}{<attribute>}{<value>}{<nodename>}{<contents>}:
%     Attribute handler for all `attribute`.  Called only when no specific
%     \DN<nodename>A<attribute> handler is defined.
% \DN<nodename>C<class>{<contents>}:
%     Handler for `class`, when set on nodes of type `nodename`.
% \DC<class>{<contents>}:
%     Handler for `class`.  Called only when no specific \DN<nodename>C<class>
%     handler is defined.
% \D<name>: Generic variable or function.

% Attribute handlers:
% TODO

% ---------------------------------------------------------------------------

% Having to intersperse code with \makeatletter-\makeatother pairs is very
% annoying, so we call \makeatletter at the top and \makeatother at the
% bottom.   Just be aware that you cannot use "@" as a text character inside
% this stylesheet.
\makeatletter

% Print-mode (as opposed to online mode e.g. with Adobe Reader).
% This causes for example blue hyperlinks.
\providecommand{\Dprinting}{false}

% \DSearly is called right after \documentclass.
\providecommand{\DSearly}{}
% \DSlate is called at the end of the stylesheet (right before the document
% tree).
\providecommand{\DSlate}{}

% Use the KOMA script article class.
\providecommand{\Ddocumentclass}{scrartcl}
\providecommand{\Ddocumentoptions}{a4paper}
\providecommand{\DSdocumentclass}{
  \documentclass[\Ddocumentoptions]{\Ddocumentclass} }

% Todo: This should be movable to the bottom, but it isn't as long as
% we use \usepackage commands at the top level of this stylesheet
% (which we shouldn't).
\DSdocumentclass

\providecommand{\DSpackages}{
  % Load miscellaneous packages.
  % Note 1: Many of the packages loaded here are used throughout this stylesheet.
  % If one of these packages does not work on your system or in your scenario,
  % please let us know, so we can consider making the package optional.
  % Note 2: It would appear cleaner to load packages where they are used.
  % However, since using a wrong package loading order can lead to *very*
  % subtle bugs, we centralize the loading of most packages here.
  \DSfontencoding % load font encoding packages
  \DSlanguage % load babel
  % Using \ifthenelse conditionals.
  \usepackage{ifthen} % before hyperref (really!)
  % There is not support for *not* using hyperref because it's used in many
  % places.  If this is a problem (e.g. because hyperref doesn't work on your
  % system), please let us know.
  \usepackage[colorlinks=false,pdfborder={0 0 0}]{hyperref}
  % Get color, e.g. for links and system messages.
  \usepackage{color}
  % Get \textnhtt macro (non-hyphenating type writer).
  \usepackage{hyphenat}
  % For sidebars.
  \usepackage{picins}
  % We use longtable to create tables.
  \usepackage{longtable}
  % Images.
  \usepackage{graphicx}
  % These packages might be useful (some just add magic pixie dust), so
  % evaluate them:
  %\usepackage{fixmath}
  %\usepackage{amsmath}
  % Add some missing symbols like \textonehalf.
  \usepackage{textcomp}
}

\providecommand{\DSfontencoding}{
  % Set up font encoding.  Called by \DSpackages.
  % AE is a T1 emulation.  It provides mostly the same characters and
  % features as T1-encoded fonts but doesn't use bitmap fonts (which are
  % unsuitable for online reading and subtle for printers).
  \usepackage{ae}
  % Provide the characters not contained in AE from EC bitmap fonts.
  \usepackage{aecompl}
  % Guillemets ("<<", ">>") in AE.
  \usepackage{aeguill}
}

\providecommand{\DSsymbols}{%
  % Fix up symbols.
  % The Euro symbol in Computer Modern looks, um, funny.  Let's get a
  % proper Euro symbol.
  \usepackage{eurosym}%
  \renewcommand{\texteuro}{\euro}%
}

% Taken from
% <http://groups.google.de/groups?selm=1i0n5tgtplti420e1omp4pctlv19jpuhbb%404ax.com>
% and modified.  Used with permission.
\providecommand{\Dprovidelength}[2]{%
  \begingroup%
    \escapechar\m@ne%
    \xdef\@gtempa{{\string#1}}%
  \endgroup%
  \expandafter\@ifundefined\@gtempa%
  {\newlength{#1}\setlength{#1}{#2}}%
  {}%
}

\providecommand{\Dprovidecounter}[2]{%
  % Like \newcounter except that it doesn't crash if the counter
  % already exists.
  \@ifundefined{c@#1}{\newcounter{#1}\setcounter{#1}{#2}}{}
}

\Dprovidelength{\Dboxparindent}{\parindent}

\providecommand{\Dmakebox}[1]{%
  % Make a centered, frameless box.  Useful e.g. for block quotes.
  % Do not use minipages here, but create pseudo-lists to allow
  % page-breaking.  (Don't use KOMA-script's addmargin environment
  % because it messes up bullet lists.)
  \Dmakelistenvironment{}{}{%
    \setlength{\parskip}{0pt}%
    \setlength{\parindent}{\Dboxparindent}%
    \item{#1}%
  }%
}

\providecommand{\Dmakefbox}[1]{%
  % Make a centered, framed box.  Useful e.g. for admonitions.
  \vspace{0.4\baselineskip}%
  \begin{center}%
    \fbox{%
      \begin{minipage}[t]{0.9\linewidth}%
        \setlength{\parindent}{\Dboxparindent}%
        #1%
      \end{minipage}%
    }%
  \end{center}%
  \vspace{0.4\baselineskip}%
}

% We do not currently recognize the difference between an end-sentence and a
% mid-sentence period (".  " vs. ". " in plain text).  So \frenchspacing is
% appropriate.
\providecommand{\DSfrenchspacing}{\frenchspacing}


\Dprovidelength{\Dblocklevelvspace}{%
  % Space between block-level elements other than paragraphs.
  0.7\baselineskip plus 0.3\baselineskip minus 0.2\baselineskip%
}
\providecommand{\DECauxiliaryspace}{%
  \ifthenelse{\equal{\Dneedvspace}{true}}{\vspace{\Dblocklevelvspace}}{}%
  \par\noindent%
}
\providecommand{\DECparagraphspace}{\par}
\providecommand{\Dneedvspace}{true}

\providecommand{\DSlanguage}{%
  % Set up babel.
  \usepackage[\DEVlanguagebabel]{babel}
}

\providecommand{\Difdefined}[3]{\@ifundefined{#1}{#3}{#2}}

% Handler for 'classes' attribute (called for each class attribute).
\providecommand{\DAclasses}[5]{%
  % Dispatch to \DN<nodename>C<class>.
  \Difdefined{DN#4C#3}{%
    % Pass only contents, nothing else!
    \csname DN#4C#3\endcsname{#5}%
  }{%
    % Otherwise, dispatch to \DC<class>.
    \Difdefined{DC#3}{%
      \csname DC#3\endcsname{#5}%
    }{%
      #5%
    }%
  }%
}

\providecommand{\DECattr}[5]{%
  % Global attribute dispatcher, called inside the document tree.
  % Parameters:
  % 1. Attribute number.
  % 2. Attribute name.
  % 3. Attribute value.
  % 4. Node name.
  % 5. Node contents.
  \Difdefined{DN#4A#2}{%
    % Dispatch to \DN<nodename>A<attribute>.
    \csname DN#4A#2\endcsname{#1}{#2}{#3}{#4}{#5}%
  }{\Difdefined{DA#2}{%
    % Otherwise dispatch to \DA<attribute>.
    \csname DA#2\endcsname{#1}{#2}{#3}{#4}{#5}%
  }{%
    % Otherwise simply run the contents without calling a handler.
    #5%
  }}%
}

% ---------- Link handling ----------
% Targets and references.

\providecommand{\Draisedlink}[1]{%
  % Anchors are placed on the base line by default.  This is a bad thing for
  % inline context, so we raise the anchor (normally by \baselineskip).
  \Hy@raisedlink{#1}%
}

% References.
% We're assuming here that the "refid" and "refuri" attributes occur
% only in inline context (in TextElements).
\providecommand{\DArefid}[5]{%
  \ifthenelse{\equal{#4}{reference}}{%
    \Dexplicitreference{\##3}{#5}%
  }{%
    % If this is not a target node (targets with refids are
    % uninteresting and should be silently dropped).
    \ifthenelse{\not\equal{#4}{target}}{%
      % If this is a footnote reference, call special macro.
      \ifthenelse{\equal{#4}{footnotereference}}{%
        \Dimplicitfootnotereference{\##3}{#5}%
      }{%
        \ifthenelse{\equal{#4}{citationreference}}{%
          \Dimplicitcitationreference{\##3}{#5}%
        }{%
          \Dimplicitreference{\##3}{#5}%
        }%
      }%
    }{}%
  }%
}
\providecommand{\DArefuri}[5]{%
  \ifthenelse{\equal{#4}{target}}{%
    % The node name is 'target', so this is a hyperlink target, like this:
    % .. _mytarget: URI
    % Hyperlink targets are ignored because they are invisible.
  }{%
    % If a non-target node has a refuri attribute, it must be an explicit URI
    % reference (i.e. node name is 'reference').
    \Durireference{#3}{#5}%
  }%
}
% Targets.
\providecommand{\DAids}[5]{%
  \label{#3}%
  \ifthenelse{\equal{#4}{footnotereference}}{%
    {%
      \renewcommand{\HyperRaiseLinkDefault}{%
        % Dirty hack to make backrefs to footnote references work.
        % For some reason, \baselineskip is 0pt in fn references.
        0.5\Doriginalbaselineskip%
      }%
      \Draisedlink{\hypertarget{#3}{}}#5%
    }%
  }{%
    \Draisedlink{\hypertarget{#3}{}}#5%
  }%
}
\providecommand{\Dimplicitreference}[2]{%
  % Create implicit reference to ID.  Implicit references occur
  % e.g. in TOC-backlinks of section titles.  Parameters:
  % 1. Target.
  % 2. Link text.
  \href{#1}{#2}%
}
\providecommand{\Dimplicitfootnotereference}[2]{%
  % Ditto, but for the special case of footnotes.
  % We want them to be rendered like explicit references.
  \Dexplicitreference{#1}{#2}%
}
\providecommand{\Dimplicitcitationreference}[2]{%
  % Ditto for citation references.
  \Dimplicitfootnotereference{#1}{#2}%
}
\providecommand{\Dcolorexplicitreference}{%
  \ifthenelse{\equal{\Dprinting}{true}}{\color{black}}{\color{blue}}%
}
\providecommand{\Dexplicitreference}[2]{%
  % Create explicit reference to ID, e.g. created with "foo_".
  % Parameters:
  % 1. Target.
  % 2. Link text.
  \href{#1}{{\Dcolorexplicitreference#2}}%
}
\providecommand{\Dcolorurireference}{\Dcolorexplicitreference}
\providecommand{\Durireference}[2]{%
  % Create reference to URI.  Parameters:
  % 1. Target.
  % 2. Link text.
  \href{#1}{{\Dcolorurireference#2}}%
}

\Dprovidecounter{Dpdfbookmarkid}{0}%
\providecommand{\Dpdfbookmark}[1]{%
  % Temporarily decrement Desctionlevel counter.
  \addtocounter{Dsectionlevel}{-1}%
  %\typeout{\arabic{Dsectionlevel}}%
  %\typeout{#1}%
  %\typeout{docutils\roman{Dpdfbookmarkid}}%
  %\typeout{}%
  \pdfbookmark[\arabic{Dsectionlevel}]{#1}{docutils\arabic{Dpdfbookmarkid}}%
  \addtocounter{Dsectionlevel}{1}%
  \addtocounter{Dpdfbookmarkid}{1}%
}
% ---------- End of Link Handling ----------

\providecommand{\DNparagraph}[1]{%
  \ifthenelse{\equal{\DEVparagraphindented}{true}}{\indent}{\noindent}%
  #1%
}
\providecommand{\Dformatboxtitle}[1]{{\Large\textbf{#1}}}
\providecommand{\Dformatboxsubtitle}[1]{{\large\textbf{#1}}}
\providecommand{\Dtopictitle}[1]{%
  \Difinsidetoc{\vspace{1em}\par}{}%
  \noindent\Dformatboxtitle{#1}%
  \ifthenelse{\equal{\DEVhassubtitle}{false}}{\vspace{1em}}{\vspace{0.5em}}%
  \par%
}
\providecommand{\Dadmonitiontitle}[1]{%
  \Dtopictitle{#1}%
}
\providecommand{\Dtopicsubtitle}[1]{%
  \noindent\Dformatboxsubtitle{#1}%
  \vspace{1em}%
  \par%
}
\providecommand{\Dsidebartitle}[1]{\Dtopictitle{#1}}
\providecommand{\Dsidebarsubtitle}[1]{\Dtopicsubtitle{#1}}
\providecommand{\Ddocumenttitle}[1]{%
  \begin{center}{\Huge#1}\end{center}%
  \ifthenelse{\equal{\DEVhassubtitle}{true}}{\vspace{0.1cm}}{\vspace{1cm}}%
}
\providecommand{\Ddocumentsubtitle}[1]{%
  \begin{center}{\huge#1}\end{center}%
  \vspace{1cm}%
}
% Can be overwritten by user stylesheet.
\providecommand{\Dformatsectiontitle}[1]{#1}
\providecommand{\Dformatsectionsubtitle}[1]{\Dformatsectiontitle{#1}}
\providecommand{\Dbookmarksectiontitle}[1]{%
  % Return text suitable for use in \section*, \subsection*, etc.,
  % containing a PDF bookmark.  Parameter:  The title (as node tree).
  \Draisedlink{\Dpdfbookmark{\DEVtitleastext}}%
  #1%
}
\providecommand{\Dsectiontitlehook}[1]{#1}
\providecommand{\Dsectiontitle}[1]{%
  \Dsectiontitlehook{%
    \Ddispatchsectiontitle{\Dbookmarksectiontitle{\Dformatsectiontitle{#1}}}%
  }%
}
\providecommand{\Ddispatchsectiontitle}[1]{%
  \@ifundefined{Dsectiontitle\roman{Dsectionlevel}}{%
    \Ddeepsectiontitle{#1}%
  }{%
    \csname Dsectiontitle\roman{Dsectionlevel}\endcsname{#1}%
  }%
}
\providecommand{\Ddispatchsectionsubtitle}[1]{%
  \Ddispatchsectiontitle{#1}%
}
\providecommand{\Dsectiontitlei}[1]{\section*{#1}}
\providecommand{\Dsectiontitleii}[1]{\subsection*{#1}}
\providecommand{\Ddeepsectiontitle}[1]{%
  % Anything below \subsubsection (like \paragraph or \subparagraph)
  % is useless because it uses the same font.  The only way to
  % (visually) distinguish such deeply nested sections is to use
  % section numbering.
  \subsubsection*{#1}%
}
\providecommand{\Dsectionsubtitlehook}[1]{#1}
\Dprovidelength{\Dsectionsubtitleraisedistance}{0.7em}
\providecommand{\Dsectionsubtitlescaling}{0.85}
\providecommand{\Dsectionsubtitle}[1]{%
  \Dsectionsubtitlehook{%
    % Move the subtitle nearer to the title.
    \vspace{-\Dsectionsubtitleraisedistance}%
    % Don't create a PDF bookmark.
    \Ddispatchsectionsubtitle{%
      \Dformatsectionsubtitle{\scalebox{\Dsectionsubtitlescaling}{#1}}%
    }%
  }%
}
\providecommand{\DNtitle}[1]{%
  % Dispatch to \D<parent>title.
  \csname D\DEVparent title\endcsname{#1}%
}
\providecommand{\DNsubtitle}[1]{%
  % Dispatch to \D<parent>subtitle.
  \csname D\DEVparent subtitle\endcsname{#1}%
}

\providecommand{\DNliteralblock}[1]{%
  \Dmakelistenvironment{}{%
    \ifthenelse{\equal{\Dinsidetabular}{true}}{%
      \setlength{\leftmargin}{0pt}%
    }{}%
    \setlength{\rightmargin}{0pt}%
  }{%
    \raggedright\item\noindent\nohyphens{\textnhtt{#1\Dfinalstrut}}%
  }%
}
\providecommand{\DNdoctestblock}[1]{\DNliteralblock{#1}}
\providecommand{\DNliteral}[1]{\textnhtt{#1}}
\providecommand{\DNemphasis}[1]{\emph{#1}}
\providecommand{\DNstrong}[1]{\textbf{#1}}
\providecommand{\DECvisitdocument}{\begin{document}\noindent}
\providecommand{\DECdepartdocument}{\end{document}}
\providecommand{\DNtopic}[1]{%
  \ifthenelse{\equal{\DEVcurrentNtopicAcontents}{1}}{%
    \addtocounter{Dtoclevel}{1}%
    \par\noindent%
    #1%
    \addtocounter{Dtoclevel}{-1}%
  }{%
    \par\noindent%
    \Dmakebox{#1}%
  }%
}
\providecommand{\DNadmonition}[1]{%
  \DNtopic{#1}%
}
\providecommand{\Dformatrubric}[1]{\textbf{#1}}
\Dprovidelength{\Dprerubricspace}{0.3em}
\providecommand{\DNrubric}[1]{%
  \vspace{\Dprerubricspace}\par\noindent\Dformatrubric{#1}\par%
}

\providecommand{\Dbullet}{}
\providecommand{\DECsetbullet}[1]{\renewcommand{\Dbullet}{#1}}
\providecommand{\DNbulletlist}[1]{%
  \Difinsidetoc{%
    \Dtocbulletlist{#1}%
  }{%
    \Dmakelistenvironment{\Dbullet}{}{#1}%
  }%
}
% Todo: So what on earth is @pnumwidth?
\renewcommand{\@pnumwidth}{2.2em}
\providecommand{\DNlistitem}[1]{%
  \Difinsidetoc{%
    \ifthenelse{\equal{\theDtoclevel}{1}\and\equal{\Dlocaltoc}{false}}{%
      {%
        \par\addvspace{1em}\noindent%
        \sectfont%
        #1\hfill\pageref{\DEVcurrentNlistitemAtocrefid}%
      }%
    }{%
      \@dottedtocline{0}{\Dtocindent}{0em}{#1}{%
        \pageref{\DEVcurrentNlistitemAtocrefid}%
      }%
    }%
  }{%
    \item{#1}%
  }%
}
\providecommand{\DNenumeratedlist}[1]{#1}
\Dprovidecounter{Dsectionlevel}{0}
\providecommand{\Dvisitsectionhook}{}
\providecommand{\Ddepartsectionhook}{}
\providecommand{\DECvisitsection}{%
  \addtocounter{Dsectionlevel}{1}%
  \Dvisitsectionhook%
}
\providecommand{\DECdepartsection}{%
  \Ddepartsectionhook%
  \addtocounter{Dsectionlevel}{-1}%
}

% Using \_ will cause hyphenation after _ even in \textnhtt-typewriter
% because the hyphenat package redefines \_.  So we use
% \textunderscore here.
\providecommand{\DECtextunderscore}{\textunderscore}

\providecommand{\Dtextinlineliteralfirstspace}{{ }}
\providecommand{\Dtextinlineliteralsecondspace}{{~}}

\Dprovidelength{\Dlistspacing}{0.8\baselineskip}

\providecommand{\Dsetlistrightmargin}{%
  \ifthenelse{\lengthtest{\linewidth>12em}}{%
    % Equal margins.
    \setlength{\rightmargin}{\leftmargin}%
  }{%
    % If the line is narrower than 10em, we don't remove any further
    % space from the right.
    \setlength{\rightmargin}{0pt}%
  }%
}
\providecommand{\Dresetlistdepth}{false}
\Dprovidelength{\Doriginallabelsep}{\labelsep}
\providecommand{\Dmakelistenvironment}[3]{%
  % Make list environment with support for unlimited nesting and with
  % reasonable default lengths.  Parameters:
  % 1. Label (same as in list environment).
  % 2. Spacing (same as in list environment).
  % 3. List contents (contents of list environment).
  \ifthenelse{\equal{\Dinsidetabular}{true}}{%
    % Unfortunately, vertical spacing doesn't work correctly when
    % using lists inside tabular environments, so we use a minipage.
    \begin{minipage}[t]{\linewidth}%
  }{}%
    {%
      \renewcommand{\Dneedvspace}{false}%
      % \parsep0.5\baselineskip
      \renewcommand{\Dresetlistdepth}{false}%
      \ifnum \@listdepth>5%
      \protect\renewcommand{\Dresetlistdepth}{true}%
      \@listdepth=5%
      \fi%
      \begin{list}{%
          #1%
        }{%
          \setlength{\itemsep}{0pt}%
          \setlength{\partopsep}{0pt}%
          \setlength{\topsep}{0pt}%
                                  % List should take 90% of total width.
          \setlength{\leftmargin}{0.05\linewidth}%
          \ifthenelse{\lengthtest{\leftmargin<1.8em}}{%
            \setlength{\leftmargin}{1.8em}%
          }{}%
          \setlength{\labelsep}{\Doriginallabelsep}%
          \Dsetlistrightmargin%
          #2%
        }{%
          #3%
        }%
      \end{list}%
      \ifthenelse{\equal{\Dresetlistdepth}{true}}{\@listdepth=5}{}%
    }%
  \ifthenelse{\equal{\Dinsidetabular}{true}}{\end{minipage}}{}%
}
\providecommand{\Dfinalstrut}{\@finalstrut\@arstrutbox}
\providecommand{\DAlastitem}[5]{#5\Dfinalstrut}

\Dprovidelength{\Ditemsep}{0pt}
\providecommand{\DECmakeenumeratedlist}[6]{%
  % Make enumerated list.
  % Parameters:
  % - prefix
  % - type (\arabic, \roman, ...)
  % - suffix
  % - suggested counter name
  % - start number - 1
  % - list contents
  \newcounter{#4}%
  \Dmakelistenvironment{#1#2{#4}#3}{%
    % Use as much space as needed for the label.
    \setlength{\labelwidth}{10em}%
    % Reserve enough space so that the label doesn't go beyond the
    % left margin of preceding paragraphs.  Like that:
    %
    %    A paragraph.
    %
    %   1. First item.
    \setlength{\leftmargin}{2.5em}%
    \Dsetlistrightmargin%
    \setlength{\itemsep}{\Ditemsep}%
    % Use counter recommended by Python module.
    \usecounter{#4}%
    % Set start value.
    \addtocounter{#4}{#5}%
  }{%
    % The list contents.
    #6%
  }%
}


% Single quote in literal mode.  \textquotesingle from package
% textcomp has wrong width when using package ae, so we use a normal
% single curly quote here.
\providecommand{\DECtextliteralsinglequote}{'}


% "Tabular lists" are field lists and options lists (not definition
% lists because there the term always appears on its own line).  We'll
% use the terminology of field lists now ("field", "field name",
% "field body"), but the same is also analogously applicable to option
% lists.
%
% We want these lists to be breakable across pages.  We cannot
% automatically get the narrowest possible size for the left column
% (i.e. the field names or option groups) because tabularx does not
% support multi-page tables, ltxtable needs to have the table in an
% external file and we don't want to clutter the user's directories
% with auxiliary files created by the filecontents environment, and
% ltablex is not included in teTeX.
%
% Thus we set a fixed length for the left column and use list
% environments.  This also has the nice side effect that breaking is
% now possible anywhere, not just between fields.
%
% Note that we are creating a distinct list environment for each
% field.  There is no macro for a whole tabular list!
\Dprovidelength{\Dtabularlistfieldnamewidth}{6em}
\Dprovidelength{\Dtabularlistfieldnamesep}{0.5em}
\providecommand{\Dinsidetabular}{false}
\providecommand{\Dsavefieldname}{}
\providecommand{\Dsavefieldbody}{}
\Dprovidelength{\Dusedfieldnamewidth}{0pt}
\Dprovidelength{\Drealfieldnamewidth}{0pt}
\providecommand{\Dtabularlistfieldname}[1]{\renewcommand{\Dsavefieldname}{#1}}
\providecommand{\Dtabularlistfieldbody}[1]{\renewcommand{\Dsavefieldbody}{#1}}
\Dprovidelength{\Dparskiptemp}{0pt}
\providecommand{\Dtabularlistfield}[1]{%
  {%
    % This only saves field name and field body in \Dsavefieldname and
    % \Dsavefieldbody, resp.  It does not insert any text into the
    % document.
    #1%
    % Recalculate the real field name width everytime we encounter a
    % tabular list field because it may have been changed using a
    % "raw" node.
    \setlength{\Drealfieldnamewidth}{\Dtabularlistfieldnamewidth}%
    \addtolength{\Drealfieldnamewidth}{\Dtabularlistfieldnamesep}%
    \Dmakelistenvironment{%
      \makebox[\Drealfieldnamewidth][l]{\Dsavefieldname}%
    }{%
      \setlength{\labelwidth}{\Drealfieldnamewidth}%
      \setlength{\leftmargin}{\Drealfieldnamewidth}%
      \setlength{\rightmargin}{0pt}%
      \setlength{\labelsep}{0pt}%
    }{%
      \item%
      \settowidth{\Dusedfieldnamewidth}{\Dsavefieldname}%
      \setlength{\Dparskiptemp}{\parskip}%
      \ifthenelse{%
        \lengthtest{\Dusedfieldnamewidth>\Dtabularlistfieldnamewidth}%
      }{%
        \mbox{}\par%
        \setlength{\parskip}{0pt}%
      }{}%
      \Dsavefieldbody%
      \setlength{\parskip}{\Dparskiptemp}%
      %XXX Why did we need this?
      %\@finalstrut\@arstrutbox%
    }%
    \par%
  }%
}

\providecommand{\Dformatfieldname}[1]{\textbf{#1:}}
\providecommand{\DNfieldlist}[1]{#1}
\providecommand{\DNfield}[1]{\Dtabularlistfield{#1}}
\providecommand{\DNfieldname}[1]{%
  \Dtabularlistfieldname{%
    \Dformatfieldname{#1}%
  }%
}
\providecommand{\DNfieldbody}[1]{\Dtabularlistfieldbody{#1}}

\providecommand{\Dformatoptiongroup}[1]{%
  % Format option group, e.g. "-f file, --input file".
  \texttt{#1}%
}
\providecommand{\Dformatoption}[1]{%
  % Format option, e.g. "-f file".
  % Put into mbox to avoid line-breaking at spaces.
  \mbox{#1}%
}
\providecommand{\Dformatoptionstring}[1]{%
  % Format option string, e.g. "-f".
  #1%
}
\providecommand{\Dformatoptionargument}[1]{%
  % Format option argument, e.g. "file".
  \textsl{#1}%
}
\providecommand{\Dformatoptiondescription}[1]{%
  % Format option description, e.g.
  % "\DNparagraph{Read input data from file.}"
  #1%
}
\providecommand{\DNoptionlist}[1]{#1}
\providecommand{\Doptiongroupjoiner}{,{ }}
\providecommand{\Disfirstoption}{%
  % Auxiliary macro indicating if a given option is the first child
  % of its option group (if it's not, it has to preceded by
  % \Doptiongroupjoiner).
  false%
}
\providecommand{\DNoptionlistitem}[1]{%
  \Dtabularlistfield{#1}%
}
\providecommand{\DNoptiongroup}[1]{%
  \renewcommand{\Disfirstoption}{true}%
  \Dtabularlistfieldname{\Dformatoptiongroup{#1}}%
}
\providecommand{\DNoption}[1]{%
  % If this is not the first option in this option group, add a
  % joiner.
  \ifthenelse{\equal{\Disfirstoption}{true}}{%
    \renewcommand{\Disfirstoption}{false}%
  }{%
    \Doptiongroupjoiner%
  }%
  \Dformatoption{#1}%
}
\providecommand{\DNoptionstring}[1]{\Dformatoptionstring{#1}}
\providecommand{\DNoptionargument}[1]{{ }\Dformatoptionargument{#1}}
\providecommand{\DNdescription}[1]{%
  \Dtabularlistfieldbody{\Dformatoptiondescription{#1}}%
}

\providecommand{\DNdefinitionlist}[1]{%
  \begin{description}%
    \parskip0pt%
    #1%
  \end{description}%
}
\providecommand{\DNdefinitionlistitem}[1]{%
  % LaTeX expects the label in square brackets; we provide an empty
  % label.
  \item[]#1%
}
\providecommand{\Dformatterm}[1]{#1}
\providecommand{\DNterm}[1]{\hspace{-5pt}\Dformatterm{#1}}
% I'm still not sure what's the best rendering for classifiers.  The
% colon syntax is used by reStructuredText, so it's at least WYSIWYG.
% Use slanted text because italic would cause too much emphasis.
\providecommand{\Dformatclassifier}[1]{\textsl{#1}}
\providecommand{\DNclassifier}[1]{~:~\Dformatclassifier{#1}}
\providecommand{\Dformatdefinition}[1]{#1}
\providecommand{\DNdefinition}[1]{\par\Dformatdefinition{#1}}

\providecommand{\Dlineblockindentation}{2.5em}
\providecommand{\DNlineblock}[1]{%
  \Dmakelistenvironment{}{%
    \ifthenelse{\equal{\DEVparent}{lineblock}}{%
      % Parent is a line block, so indent.
      \setlength{\leftmargin}{\Dlineblockindentation}%
    }{%
      % At top level; don't indent.
      \setlength{\leftmargin}{0pt}%
    }%
    \setlength{\rightmargin}{0pt}%
    \setlength{\parsep}{0pt}%
  }{%
    #1%
  }%
}
\providecommand{\DNline}[1]{\item#1}

\providecommand{\DNtransition}{%
  \raisebox{0.25em}{\parbox{\linewidth}{\hspace*{\fill}\hrulefill\hrulefill\hspace*{\fill}}}%
}

\providecommand{\Dformatblockquote}[1]{%
  % Format contents of block quote.
  % This occurs in block-level context, so we cannot use \textsl.
  {\slshape#1}%
}
\providecommand{\Dformatattribution}[1]{---\textup{#1}}
\providecommand{\DNblockquote}[1]{%
  \Dmakebox{%
    \Dformatblockquote{#1}
  }%
}
\providecommand{\DNattribution}[1]{%
  \par%
  \begin{flushright}\Dformatattribution{#1}\end{flushright}%
}


% Sidebars:
% Vertical and horizontal margins.
\Dprovidelength{\Dsidebarvmargin}{0.5em}
\Dprovidelength{\Dsidebarhmargin}{1em}
% Padding (space between contents and frame).
\Dprovidelength{\Dsidebarpadding}{1em}
% Frame width.
\Dprovidelength{\Dsidebarframewidth}{2\fboxrule}
% Position ("l" or "r").
\providecommand{\Dsidebarposition}{r}
% Width.
\Dprovidelength{\Dsidebarwidth}{0.45\linewidth}
\providecommand{\DNsidebar}[1]{
  \parpic[\Dsidebarposition]{%
    \begin{minipage}[t]{\Dsidebarwidth}%
      % Doing this with nested minipages is ugly, but I haven't found
      % another way to place vertical space before and after the fbox.
      \vspace{\Dsidebarvmargin}%
      {%
        \setlength{\fboxrule}{\Dsidebarframewidth}%
        \setlength{\fboxsep}{\Dsidebarpadding}%
        \fbox{%
          \begin{minipage}[t]{\linewidth}%
            \setlength{\parindent}{\Dboxparindent}%
            #1%
          \end{minipage}%
        }%
      }%
      \vspace{\Dsidebarvmargin}%
    \end{minipage}%
  }%
}


% Citations and footnotes.
\providecommand{\Dformatfootnote}[1]{%
  % Format footnote.
  {%
    \footnotesize#1%
    % \par is necessary for LaTeX to adjust baselineskip to the
    % changed font size.
    \par%
  }%
}
\providecommand{\Dformatcitation}[1]{\Dformatfootnote{#1}}
\Dprovidelength{\Doriginalbaselineskip}{0pt}
\providecommand{\DNfootnotereference}[1]{%
  {%
    % \baselineskip is 0pt in \textsuperscript, so we save it here.
    \setlength{\Doriginalbaselineskip}{\baselineskip}%
    \textsuperscript{#1}%
  }%
}
\providecommand{\DNcitationreference}[1]{{[}#1{]}}
\Dprovidelength{\Dfootnotesep}{3.5pt}
\providecommand{\Dsetfootnotespacing}{%
  % Spacing commands executed at the beginning of footnotes.
  \setlength{\parindent}{0pt}%
  \hspace{1em}%
}
\providecommand{\DNfootnote}[1]{%
  % See ltfloat.dtx for details.
  {%
    \insert\footins{%
      % BUG: This is too small if the user adds
      % \onehalfspacing or \doublespace.
      \vspace{\Dfootnotesep}%
      \Dsetfootnotespacing%
      \Dformatfootnote{#1}%
    }%
  }%
}
\providecommand{\DNcitation}[1]{\DNfootnote{#1}}
\providecommand{\Dformatfootnotelabel}[1]{%
  % Keep \footnotesize in footnote labels (\textsuperscript would
  % reduce the font size even more).
  \textsuperscript{\footnotesize#1{ }}%
}
\providecommand{\Dformatcitationlabel}[1]{{[}#1{]}{ }}
\providecommand{\Dformatmultiplebackrefs}[1]{%
  % If in printing mode, do not write out multiple backrefs.
  \ifthenelse{\equal{\Dprinting}{true}}{}{\textsl{#1}}%
}
\providecommand{\Dthislabel}{}
\providecommand{\DNlabel}[1]{%
  % Footnote or citatation label.
  \renewcommand{\Dthislabel}{#1}%
  \ifthenelse{\not\equal{\DEVsinglebackref}{}}{%
    \let\Doriginallabel=\Dthislabel%
    \def\Dthislabel{%
      \Dsinglefootnotebacklink{\DEVsinglebackref}{\Doriginallabel}%
    }%
  }{}%
  \ifthenelse{\equal{\DEVparent}{footnote}}{%
    % Footnote label.
    \Dformatfootnotelabel{\Dthislabel}%
  }{%
    \ifthenelse{\equal{\DEVparent}{citation}}{%
      % Citation label.
      \Dformatcitationlabel{\Dthislabel}%
    }{}%
  }%
  % If there are multiple backrefs, add them now.
  \Dformatmultiplebackrefs{\DEVmultiplebackrefs}%
}
\providecommand{\Dsinglefootnotebacklink}[2]{%
  % Create normal backlink of a footnote label.  Parameters:
  % 1. ID.
  % 2. Link text.
  % Treat like a footnote reference.
  \Dimplicitfootnotereference{\##1}{#2}%
}
\providecommand{\DECmultifootnotebacklink}[2]{%
  % Create generated backlink, as in (1, 2).  Parameters:
  % 1. ID.
  % 2. Link text.
  % Treat like a footnote reference.
  \Dimplicitfootnotereference{\##1}{#2}%
}
\providecommand{\Dsinglecitationbacklink}[2]{\Dsinglefootnotebacklink{#1}{#2}}
\providecommand{\DECmulticitationbacklink}[2]{\DECmultifootnotebacklink{#1}{#2}}


\providecommand{\DECmaketable}[2]{%
  % Make table.  Parameters:
  % 1. Table spec (like "|p|p|").
  % 2. Table contents.
  {%
    \ifthenelse{\equal{\Dinsidetabular}{true}}{%
      % Inside longtable; we cannot have nested longtables.
      \begin{tabular}{#1}%
        \hline%
        #2%
      \end{tabular}%
    }{%
      \renewcommand{\Dinsidetabular}{true}%
      \begin{longtable}{#1}%
        \hline%
        #2%
      \end{longtable}%
    }%
  }%
}
\providecommand{\DNthead}[1]{%
  #1%
  \endhead%
}
\providecommand{\DNrow}[1]{%
  #1\tabularnewline%
  \hline%
}
\providecommand{\Dinsidemulticolumn}{false}
\providecommand{\Dcompensatingmulticol}[3]{%
  \multicolumn{#1}{#2}{%
    {%
      \renewcommand{\Dinsidemulticolumn}{true}%
      % Compensate for weird missing vertical space at top of paragraph.
      \raisebox{-2.5pt}{#3}%
    }%
  }%
}
\providecommand{\DECcolspan}[2]{%
  % Take care of the morecols attribute (but incremented by 1).
  &%
  \Dcompensatingmulticol{#1}{l|}{#2}%
}
\providecommand{\DECcolspanleft}[2]{%
  % Like \Dmorecols, but called for the leftmost entries in a table
  % row.
  \Dcompensatingmulticol{#1}{|l|}{#2}%
}
\providecommand{\DECsubsequententry}[1]{%
  &#1%
}
\providecommand{\DNentry}[1]{%
  % The following sequence adds minimal vertical space above the top
  % lines of the first cell paragraph, so that vertical space is
  % balanced at the top and bottom of table cells.
  \ifthenelse{\equal{\Dinsidemulticolumn}{false}}{%
    \vspace{-1em}\vspace{-\parskip}\par%
  }{}%
  #1%
  % No need to add an ampersand ("&"); that's done by \DECsubsequententry.
}
\providecommand{\DAtableheaderentry}[5]{\Dformattableheaderentry{#5}}
\providecommand{\Dformattableheaderentry}[1]{{\bfseries#1}}


\providecommand{\DNsystemmessage}[1]{%
  {%
    \ifthenelse{\equal{\Dprinting}{false}}{\color{red}}{}%
    \bfseries%
    #1%
  }%
}


\providecommand{\Dinsidehalign}{false}
\newsavebox{\Dalignedimagebox}
\Dprovidelength{\Dalignedimagewidth}{0pt}
\providecommand{\Dhalign}[2]{%
  % Horizontally align the contents to the left or right so that the
  % text flows around it.
  % Parameters:
  % 1. l or r
  % 2. Contents.
  \renewcommand{\Dinsidehalign}{true}%
  % For some obscure reason \parpic consumes some vertical space.
  \vspace{-3pt}%
  % Now we do something *really* ugly, but this enables us to wrap the
  % image in a minipage while still allowing tight frames when
  % class=border (see \DNimageCborder).
  \sbox{\Dalignedimagebox}{#2}%
  \settowidth{\Dalignedimagewidth}{\usebox{\Dalignedimagebox}}%
  \parpic[#1]{%
    \begin{minipage}[b]{\Dalignedimagewidth}%
      % Compensate for previously added space, but not entirely.
      \vspace*{2.0pt}%
      \vspace*{\Dfloatimagetopmargin}%
      \usebox{\Dalignedimagebox}%
      \vspace*{1.5pt}%
      \vspace*{\Dfloatimagebottommargin}%
    \end{minipage}%
  }%
  \renewcommand{\Dinsidehalign}{false}%
}


% Maximum width of an image.
\providecommand{\Dimagemaxwidth}{\linewidth}
\providecommand{\Dfloatimagemaxwidth}{0.5\linewidth}
% Auxiliary variable.
\Dprovidelength{\Dcurrentimagewidth}{0pt}
\providecommand{\DNimageAalign}[5]{%
  \ifthenelse{\equal{#3}{left}}{%
    \Dhalign{l}{#5}%
  }{%
    \ifthenelse{\equal{#3}{right}}{%
      \Dhalign{r}{#5}%
    }{%
      \ifthenelse{\equal{#3}{center}}{%
        % Text floating around centered figures is a bad idea.  Thus
        % we use a center environment.  Note that no extra space is
        % added by the writer, so the space added by the center
        % environment is fine.
        \begin{center}#5\end{center}%
      }{%
        #5%
      }%
    }%
  }%
}
% Base path for images.
\providecommand{\Dimagebase}{}
% Auxiliary command.  Current image path.
\providecommand{\Dimagepath}{}
\providecommand{\DNimageAuri}[5]{%
  % Insert image.  We treat the URI like a path here.
  \renewcommand{\Dimagepath}{\Dimagebase#3}%
  \Difdefined{DcurrentNimageAwidth}{%
    \Dwidthimage{\DEVcurrentNimageAwidth}{\Dimagepath}%
  }{%
    \Dsimpleimage{\Dimagepath}%
  }%
}
\Dprovidelength{\Dfloatimagevmargin}{0pt}
\providecommand{\Dfloatimagetopmargin}{\Dfloatimagevmargin}
\providecommand{\Dfloatimagebottommargin}{\Dfloatimagevmargin}
\providecommand{\Dwidthimage}[2]{%
  % Image with specified width.
  % Parameters:
  % 1. Image width.
  % 2. Image path.
  % Need to make bottom-alignment dependent on align attribute (add
  % functional test first).  Need to observe height attribute.
  %\begin{minipage}[b]{#1}%
    \includegraphics[width=#1,height=\textheight,keepaspectratio]{#2}%
  %\end{minipage}%
}  
\providecommand{\Dcurrentimagemaxwidth}{}
\providecommand{\Dsimpleimage}[1]{%
  % Insert image, without much parametrization.
  \settowidth{\Dcurrentimagewidth}{\includegraphics{#1}}%
  \ifthenelse{\equal{\Dinsidehalign}{true}}{%
    \renewcommand{\Dcurrentimagemaxwidth}{\Dfloatimagemaxwidth}%
  }{%
    \renewcommand{\Dcurrentimagemaxwidth}{\Dimagemaxwidth}%
  }%
  \ifthenelse{\lengthtest{\Dcurrentimagewidth>\Dcurrentimagemaxwidth}}{%
    \Dwidthimage{\Dcurrentimagemaxwidth}{#1}%
  }{%
    \Dwidthimage{\Dcurrentimagewidth}{#1}%
  }%
}
\providecommand{\Dwidthimage}[2]{%
  % Image with specified width.
  % Parameters:
  % 1. Image width.
  % 2. Image path.
  \Dwidthimage{#1}{#2}%
}

% Figures.
\providecommand{\DNfigureAalign}[5]{%
  % Hack to make it work Right Now.
  %\def\DEVcurrentNimageAwidth{\DEVcurrentNfigureAwidth}%
  %
    %\def\DEVcurrentNimageAwidth{\linewidth}%
    \DNimageAalign{#1}{#2}{#3}{#4}{%
      \begin{minipage}[b]{0.4\linewidth}#5\end{minipage}}%
    %\let\DEVcurrentNimageAwidth=\relax%
  %
  %\let\DEVcurrentNimageAwidth=\relax%
}
\providecommand{\DNcaption}[1]{\par\noindent{\slshape#1}}
\providecommand{\DNlegend}[1]{\DECauxiliaryspace#1}

\providecommand{\DCborder}[1]{\fbox{#1}}
% No padding between image and border.
\providecommand{\DNimageCborder}[1]{\frame{#1}}


% Need to replace with language-specific stuff.  Maybe look at
% csquotes.sty and ask the author for permission to use parts of it.
\providecommand{\DECtextleftdblquote}{``}
\providecommand{\DECtextrightdblquote}{''}

% Table of contents:
\Dprovidelength{\Dtocininitialsectnumwidth}{2.4em}
\Dprovidelength{\Dtocadditionalsectnumwidth}{0.7em}
% Level inside a table of contents.  While this is at -1, we are not
% inside a TOC.
\Dprovidecounter{Dtoclevel}{-1}%
\providecommand{\Dlocaltoc}{false}%
\providecommand{\DNtopicClocal}[1]{%
  \renewcommand{\Dlocaltoc}{true}%
  \addtolength{\Dtocsectnumwidth}{2\Dtocadditionalsectnumwidth}%
  \addtolength{\Dtocindent}{-2\Dtocadditionalsectnumwidth}%
  #1%
  \addtolength{\Dtocindent}{2\Dtocadditionalsectnumwidth}%
  \addtolength{\Dtocsectnumwidth}{-2\Dtocadditionalsectnumwidth}%
  \renewcommand{\Dlocaltoc}{false}%
}
\Dprovidelength{\Dtocindent}{0pt}%
\Dprovidelength{\Dtocsectnumwidth}{\Dtocininitialsectnumwidth}
% Compensate for one additional TOC indentation space so that the
% top-level is unindented.
\addtolength{\Dtocsectnumwidth}{-\Dtocadditionalsectnumwidth}
\addtolength{\Dtocindent}{-\Dtocsectnumwidth}
\providecommand{\Difinsidetoc}[2]{%
  \ifthenelse{\not\equal{\theDtoclevel}{-1}}{#1}{#2}%
}
\providecommand{\DNgeneratedCsectnum}[1]{%
  \Difinsidetoc{%
    % Section number inside TOC.
    \makebox[\Dtocsectnumwidth][l]{#1}%
  }{%
    % Section number inside section title.
    #1\quad%
  }%
}
\providecommand{\Dtocbulletlist}[1]{%
  \addtocounter{Dtoclevel}{1}%
  \addtolength{\Dtocindent}{\Dtocsectnumwidth}%
  \addtolength{\Dtocsectnumwidth}{\Dtocadditionalsectnumwidth}%
  #1%
  \addtolength{\Dtocsectnumwidth}{-\Dtocadditionalsectnumwidth}%
  \addtolength{\Dtocindent}{-\Dtocsectnumwidth}%
  \addtocounter{Dtoclevel}{-1}%
}


% For \DECpixelunit, the length value is pre-multiplied with 0.75, so by
% specifying "pt" we get the same notion of "pixel" as graphicx.
\providecommand{\DECpixelunit}{pt}
% Normally lengths are relative to the current linewidth.
\providecommand{\DECrelativeunit}{\linewidth}


% ACTION: These commands actually *do* something.
% Ultimately, everything should be done here, and no active content should be
% above (not even \usepackage).

\DSearly
\DSpackages
\DSfrenchspacing
\DSsymbols
\DSlate

\makeatother
