\ProvidesPackage{dlfltxbcodetips}[2007/07/11 v0.26 by Lars 'daleif' Madsen]

% this pacakge collects some macros presented as code tips in my LaTeX
% book: "Introduktion til LaTeX". As the macros might be useful for
% other than the normal readers of my book, the longer macros are
% collected in this package.

% This package be distributed and/or modified under the
%% conditions of the LaTeX Project Public License, either version 1.3
%% of this license or (at your option) any later version.
%% The latest version of this license is in
%%   http://www.latex-project.org/lppl.txt
%% and version 1.3 or later is part of all distributions of LaTeX
%% version 2003/12/01 or later.
%% 
%% This work has the LPPL maintenance status "maintained".
%% 
%% The Current Maintainer of this work is Lars Madsen (daleif@imf.au.dk).
%%


% HISTORY
%
% 20070601 Added better implementation of \nuparrow, \ndownarrow and
%          \bigtimes, all due to Enrico Gregorio
%          also made the environment uded inside \NewShadedTheorem
%          configurable. 
% 20070604 Karl Ove Hufthammer mentioned that mathdesign does not like
%          amssymb, and _does_ implement \nleftarrow \nrightnarrow. The
%          package option 'noamssymb' now disables loading amssymb
% 20070606 alternative konstruction of \NewShadedTheorem, now using
%          ntheorem konstructions internally
% 20070710 bug fix regarding shadedtheorems, the counter aliasing did
%          not work correctly
% 20070711 Bug fixed in \ArrowBetweenLines, the \ifin@ should go
%          inside the \noalign, not outside.

\newif\ifdlf@do@load@amssymb
\dlf@do@load@amssymbtrue

\DeclareOption{noamssymb}{\dlf@do@load@amssymbfalse}

\ProcessOptions\relax

\RequirePackage{amsmath}



% \InsertTheoremBreak removes the spacing above the first item,
% \InsertTheoremBreak* does not
\newcommand\InsertTheoremBreak{%
    \@ifstar{\item[\vbox{\null}]}{%
      \begingroup % keep changes local
      \setlength\itemsep{0pt}%
      \setlength\parsep{0pt}%
       \item[\vbox{\null}]%
      \endgroup%
     }}

% macro made on request by Morten H\o gholm
% \ArrowBetweenLines adds arrow on the left
% \ArrowBetweenLines* on the right
% more information is needed
% requires the amsmath package
% Now including a fix explained by Jean-Côme Charpentier
\def\ArrowBetweenLines{\relax
  \iffalse{\fi\ifnum0=`}\fi
  \@ifstar{\ArrowBetweenLines@auxI{00}}{\ArrowBetweenLines@auxI{01}}}
\def\ArrowBetweenLines@auxI#1{%
  \@ifnextchar[%
  {\ArrowBetweenLines@auxII{#1}}%
  {\ArrowBetweenLines@auxII{#1}[\Updownarrow]}}
\def\ArrowBetweenLines@auxII#1[#2]{%
  \ifnum0=`{\fi \iffalse}\fi
  \crcr
  \noalign{\nobreak\vskip-\baselineskip\vskip-\lineskip}%
  \noalign{\expandafter\in@\expandafter{\@currenvir}%
      {alignedat,aligned,gathered}%
  \ifin@ \else\notag\fi}%
  \if#1 &&\quad #2\else #2\quad\fi
  \\\noalign{\nobreak\vskip-\lineskip}}


% for making theorems with shaded background
% requires the use of ntheorem, (x)color and framed
% currently only ntheorem is supported (perhaps support for amsthm or
% theorem will be added later)
% has the same syntax as \newtheorem
%
% redesigned 2007/06/06
\def\theoremframecommand{}
\newcommand\NewShadedTheorem[1]{%
%   \@ifpackageloaded{ntheorem}{
%     \@ifpackagewith{ntheorem}{framed}{}{
%       \PackageError{dlfltxbcodetips}{
%         \string\NewShadedTheorem\space requires ntheorem to be ^^J
%         loaded with the framed option}{}
%       }
%   }{
%     \PackageError{dlfltxbcodetips}{\string\NewShadedTheorem\space can
%       only be used^^J with the ntheorem package}{}
%     }
  \@ifnextchar[{\NST@levelii{#1}}{\NST@levelii{#1}[]}}
\def\NST@levelii#1[#2]#3{%
  \@ifnextchar[{\NST@leveliii{#1}[#2]{#3}}{\NST@leveliii{#1}[#2]{#3}[]}}
\newcommand\NST@helper[1]{%
  \edef\@tempa{c@#1}\edef\@tempb{c@#1@inner}%
  \expandafter\let\csname\@tempa\expandafter\endcsname\csname\@tempb\endcsname%
  \edef\@tempa{p@#1}\edef\@tempb{p@#1@inner}%
  \expandafter\let\csname\@tempa\expandafter\endcsname\csname\@tempb\endcsname%
  \edef\@tempa{cl@#1}\edef\@tempb{cl@#1@inner}%
  \expandafter\let\csname\@tempa\expandafter\endcsname\csname\@tempb\endcsname%
  \edef\@tempa{the#1}\edef\@tempb{the#1@inner}%
  \expandafter\let\csname\@tempa\expandafter\endcsname\csname\@tempb\endcsname%
}
\def\NST@leveliii#1[#2]#3[#4]{%
  \ifx\\#2\\% i.e. no common counter
    \ifx\\#4\\% i.e. no dominant counter
      \newshadedtheorem{#1@inner}{#3}%
      \NST@helper{#1}%
    \else% i.e. dominant counter
      \newshadedtheorem{#1@inner}{#3}[#4]%
      \NST@helper{#1}%
  \fi%
  \else% i.e. common counter
    \newshadedtheorem{#1@inner}[#2]{#3}%
  \fi%
  \newenvironment{#1}{%
    % code stolen from ntheorem.sty
    \thm@topsepadd \theorempostskipamount
    \ifvmode \advance\thm@topsepadd\partopsep\fi
    \trivlist
    \@topsep \theorempreskipamount
    \@topsepadd \thm@topsepadd
    \item
      \setlength\theorempreskipamount{0pt}%
      \setlength\theorempostskipamount{0pt}%
      \begin{#1@inner}
      }{\end{#1@inner}\endtrivlist\@endparenv}%
  }

% Mathematics into Type by Ellen Swanson recommends that line-broken
% displayed alignments should be indented by 2em (and other rules)
% instead of aligning ti the left and indenting all subsequent lines,
% it is easier to just pull back the first line. Just align all lines
% to the left and replace the & on the fist line with \PullBack
% The indentation amount can be changed by \PullBack[3], i.e. no unit,
% 'em will be used automatically
\newlength\PullBackLength
\newcommand\PullBack[1][2]{%
  \setlength{\global\PullBackLength}{#1em}%
  \kern\PullBackLength%
  & 
  \kern-\PullBackLength}


% this implements a stack that holds a user build mathindent
% not easy to explain in words see the documentation
\newtoks\dlf@STACK
\def\dlf@Prepend#1(to:)#2{\toks0={#1}%
  \global\edef\act{\global\noexpand#2={\the\toks0 \the#2}}%
  \act}
\def\dlf@PopOff#1{\global\edef\act{\global\noexpand\dlf@SplitOff\the#1%
    (tail:)\noexpand#1}\act}
\def\dlf@SplitOff#1#2(tail:)#3{#3={#2}} 
\newlength\MathIndentLength
\newcommand\MathIndent{\kern\MathIndentLength}
\newcommand\PopMathIndent{%
  \@ifstar{\@PopMathIndent\relax}{\@PopMathIndent\MathIndent}}
\newcommand\@PopMathIndent[1]{\dlf@PopOff\dlf@STACK
  \settowidth\@tempdimc{\ensuremath{\displaystyle\phantom{\the\dlf@STACK}}}
  \setlength{\global\MathIndentLength}{\@tempdimc}#1}
\newcommand\SetMathIndent[1]{\dlf@STACK={}\dlf@Prepend{{#1}}(to:)\dlf@STACK
  \settowidth\@tempdimc{\ensuremath{\displaystyle\phantom{\the\dlf@STACK}}}
  \setlength{\global\MathIndentLength}{\@tempdimc}#1}
\newcommand\AddtoMathIndent[1]{\dlf@Prepend{{#1}}(to:)\dlf@STACK
  \settowidth\@tempdimc{\ensuremath{\displaystyle\phantom{\the\dlf@STACK}}}
   \setlength{\global\MathIndentLength}{\@tempdimc}#1}


% this constructs a version of \bigtimes. The version here is due to
% Enrico Gregorio, presented in http://groups.google.com/group/comp.text.tex/msg/9685c9405df2ff94

\newcommand\dlf@b@gtimes[1]{%
  \vcenter{\hbox{#1$\m@th\mkern-2mu\times\mkern-2mu$}}}
\newcommand\dlf@bigtimes{%
  \mathchoice{\dlf@b@gtimes\huge}         % display style
             {\dlf@b@gtimes\LARGE}        % text style
             {\dlf@b@gtimes{}}            % script style
             {\dlf@b@gtimes\footnotesize} % script script style
}
\newcommand\bigtimes{\mathop{\dlf@bigtimes}\displaylimits}



\ifdlf@do@load@amssymb
  \RequirePackage{amssymb}
\fi

\RequirePackage{graphicx}

% better construction for \nuparrow and \ndownarrow, se also
% http://groups.google.com/group/comp.text.tex/msg/689cc8bd604fdb51 
% again due to Enrico Gregorio
% construction requires graphicx
\def\dlf@nrotarrow#1#2{%
  \setbox0=\hbox{$\m@th#1\uparrow$}\dimen0=\dp0
  \setbox0=\hbox{%
    \reflectbox{\rotatebox[origin=c]{90}{$\m@th#1\mkern2.22mu #2$}}}%
  \dp0=\dimen0 \box0 \mkern2.3965mu
}

\@ifundefined{nrightarrow}{%
  \PackageWarning{dlfltxbcodetips}{\string\nrightarrow\space not found,^^J 
    \string\nuparrow\space will not be defined}
}{
  \def\nuparrow{\mathrel{\mathpalette\dlf@nrotarrow\nrightarrow}}
}

\@ifundefined{nleftarrow}{%
  \PackageWarning{dlfltxbcodetips}{\string\nleftarrow\space not found,^^J 
    \string\ndownarrow\space will not be defined}
}{
  \def\ndownarrow{\mathrel{\mathpalette\dlf@nrotarrow\nleftarrow}} 
}


% this can be used as the first thing in an amsmath alignment
% environments for displayed math. It switches \abovedisplayskip and
% \abovedisplayshortskip before the display, usefull in some cases to
% save some space
\newcommand\SwapDeadSpace{%
 \noalign{\vskip-\abovedisplayskip\vskip\abovedisplayshortskip}
}


