% \iffalse meta-comment
%
% Copyright 2010 by Stefan Majewsky <majewsky.stefan@ages-skripte.org>
%
% This file may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.2
% 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.2 or later is part of all distributions of LaTeX
% version 1999/12/01 or later.
%
% \fi
%
% \iffalse
%<package>\NeedsTeXFormat{LaTeX2e}[1999/12/01]
%<package>\ProvidesPackage{delim}
%<package>   [2011/09/13 1.0 Typesetting of mathematical delimiters]
%
%<*driver>
\documentclass{ltxdoc}
 \usepackage[english]{babel}
 \usepackage[utf8]{inputenc}
 \usepackage[T1]{fontenc}
 \usepackage{delim,calc}

 \newenvironment{pexample}
  {\vspace{1em}\begin{tabular}{@{\hspace{1em}}l}}
  {\end{tabular}\par}
 \newenvironment{example}[1]
  {\vspace{1em}\begin{tabular}{@{\hspace{1em}}p{#1\linewidth}@{\hspace{3em}}l}}
  {\end{tabular}\vspace{1em}}

 \setlength\parindent{0pt}
 \EnableCrossrefs
 \CodelineIndex
 \RecordChanges
 \begin{document}
  \DocInput{delim.dtx}
 \end{document}
%</driver>
%
% \fi
%
% \CheckSum{66}
%
% \CharacterTable
%  {Upper-case    \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
%   Lower-case    \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
%   Digits        \0\1\2\3\4\5\6\7\8\9
%   Exclamation   \!     Double quote  \"     Hash (number) \#
%   Dollar        \$     Percent       \%     Ampersand     \&
%   Acute accent  \'     Left paren    \(     Right paren   \)
%   Asterisk      \*     Plus          \+     Comma         \,
%   Minus         \-     Point         \.     Solidus       \/
%   Colon         \:     Semicolon     \;     Less than     \<
%   Equals        \=     Greater than  \>     Question mark \?
%   Commercial at \@     Left bracket  \[     Backslash     \\
%   Right bracket \]     Circumflex    \^     Underscore    \_
%   Grave accent  \`     Left brace    \{     Vertical bar  \|
%   Right brace   \}     Tilde         \~}
%
% \changes{v1.0}{2011/09/13}{Simplified user interface}
% \changes{v0.2}{2010/05/13}{Much simpler implementation without ifthen package}
% \changes{v0.1}{2010/05/06}{Initial Version}
%
% \GetFileInfo{delim.sty}
%
% \DoNotIndex{}
%
% \title{The \textsf{delim} package\thanks{This document
%   corresponds to \textsf{delim}~\fileversion,
%   dated~\filedate.}}
% \author{Stefan Majewsky \\ \texttt{majewsky@gmx.net}}
%
% \maketitle
% \begin{abstract}
%  This package simplifies typesetting of variable-size delimiters (like
%  parentheses) in mathematical expressions.
% \end{abstract}
%
% {\LaTeX} provides some commands for the correct typesetting of delimiters in
% mathematical expressions. Consider the following equation:
%
% \begin{example}{0.15}
%  $\displaystyle(a + \frac bc) \cdot d$ &
%               |(a + \frac bc) \cdot d|
% \end{example}
%
% By default, the parentheses will not scale up correctly. The |\left| and |\right| commands fix this problem:
%
% \begin{example}{0.15}
%  $\displaystyle\left(a + \frac bc\right) \cdot d$ &
%               |\left(a + \frac bc\right) \cdot d|
% \end{example}
%
% But because it's cumbersome to write |\left| and |\right| every time, we
% define and use a macro:
%
% \delimdef\p#1{\dleft(#1\dright)}
% \begin{pexample}
%  |\delimdef\p#1{\dleft(#1\dright)}|
% \end{pexample}
% \begin{example}{0.15}
%  $\displaystyle\p{a + \frac bc} \cdot d$ &
%               |\p{a + \frac bc} \cdot d|
% \end{example}
%
% \DescribeMacro{\delimdef}
% \DescribeMacro{\dleft} \DescribeMacro{\dmiddle} \DescribeMacro{\dright}
% We did not use |\def|, but |\delimdef| which is defined by this package.
% Also, we substituted |\dleft| and |\dright| for |\left| and |\right|. There's
% also |\dmiddle|, as in this example:
%
% \delimdef\braket#1#2{\dleft\langle#1\dmiddle\vert#2\dright\rangle}
% \begin{pexample}
%  |\delimdef\braket#1#2{\dleft\langle#1\dmiddle\vert#2\dright\rangle}|
% \end{pexample}
% \begin{example}{0.15}
%  $\displaystyle\braket{\psi_n(t)}\psi$ &
%               |\braket{\psi_n(t)}\psi|
% \end{example}
%
% But why wouldn't we want to use |\def|? Because |\left|, |\middle| and
% |\right| are not always what you want. For example, if you want the delimiters
% to be a bit bigger in the last example, you would substitute |\bigl|, |\bigm|
% and |\bigr| for |\left|, |\middle| and |\right|. This is not possible with
% a simple |\def| macro. Macros defined by |\delimdef| can substitute |\dleft|
% etc. for any common set of delimiter commands, by using a \emph{size prefix}:
%
% \begin{example}{0.15}
%  $\displaystyle\mbig\braket{\psi_n(t)}\psi$ &
%               |\mbig\braket{\psi_n(t)}\psi|
% \end{example}
%
% Let's look at another example:
%
% \begin{example}{0.15}
%  $\displaystyle\p{\underbrace{a_1 + a_2}_{=b}}$ &
%               |\p{\underbrace{a_1 + a_2}_{=b}}|
% \end{example}
%
% We want to use our |\p| macro, but the parentheses should be reset to their
% normal size. The size prefix |\mnorm| does just that:
%
% \begin{example}{0.15}
%  $\displaystyle\mnorm\p{\underbrace{a_1 + a_2}_{=b}}$ &
%               |\mnorm\p{\underbrace{a_1 + a_2}_{=b}}|
% \end{example}
%
% \DescribeMacro{\mnorm} \DescribeMacro{\mbig} \DescribeMacro{\mBig}
% \DescribeMacro{\mbigg} \DescribeMacro{\mBigg} \DescribeMacro{\mauto}
% The following size prefixes are defined by this package:
%
% \begin{example}{0.15}
%  $\displaystyle\mnorm\p{\frac ab}$ & |\mnorm| (normal character size) \\[1em]
%  $\displaystyle\mbig\p{\frac ab}$ & |\mbig = \bigl| etc. \\[1em]
%  $\displaystyle\mBig\p{\frac ab}$ & |\mBig = \Bigl| etc. \\[1em]
%  $\displaystyle\mbigg\p{\frac ab}$ & |\mbigg = \biggl| etc. \\[1em]
%  $\displaystyle\mBigg\p{\frac ab}$ & |\mBigg = \Biggl| etc. \\[1.5em]
%  $\displaystyle\mauto\p{\frac ab}$ & |\mauto = \left| etc. (default behavior)
% \end{example}
%
% \DescribeMacro{\delim@load}
% If no prefix is given, |\mauto| is used. New size prefixes can be defined
% using the |\delim@load| macro; refer to the implementation of the existing
% prefixes for details.
%
% \clearpage\section*{Implementation}
%
% \begin{macro}{\delim@load}
% Size prefixes use this macro to enter a new delimiter level and define the
% delimiter commands for that level. |\delim@loaded| signals that delimiter
% commands have been provided for this level. Its exact content is irrelevant,
% only the fact that it is defined is needed (see below).
%    \begin{macrocode}
\def\delim@load#1#2#3{%
 \begingroup%
 \def\dleft{#1}%
 \def\dmiddle{#2}%
 \def\dright{#3}%
 \def\delim@loaded{}%
}%
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\mauto}\begin{macro}{\mnorm}\begin{macro}{\mbig}\begin{macro}{\mBig}\begin{macro}{\mbigg}\begin{macro}{\mBigg}
% The size prefixes are defined using |\delim@load|.
%    \begin{macrocode}
\newcommand\mauto{\delim@load\left\middle\right}%
\newcommand\mnorm{\delim@load\relax\relax\relax}%
\newcommand\mbig{\delim@load\bigl\bigm\bigr}%
\newcommand\mBig{\delim@load\Bigl\Bigm\Bigr}%
\newcommand\mbigg{\delim@load\biggl\biggm\biggr}%
\newcommand\mBigg{\delim@load\Biggl\Biggm\Biggr}%
%    \end{macrocode}
% \end{macro}\end{macro}\end{macro}\end{macro}\end{macro}\end{macro}
% \begin{macro}{\delimdef}
% This defines a new delimiter macro. The macro substitution text is
% extended by a grouping level, with additional logic being collected in
% |\delim@begingroup|.
%    \begin{macrocode}
\def\delimdef#1#{\delim@def{#1}}%
\def\delim@def#1#2{\def#1{\delim@begingroup#2\endgroup}}%
%    \end{macrocode}
% Special thanks go to Martin Scharrer for pointing out to me the capabilities
% of |\def| used in this implementation (see
% |http://tex.stackexchange.com/questions/28207/|).
% \end{macro}
% \begin{macro}{\delim@begingroup}
% |\delim@begingroup| ensures that delimiters are loaded (default is |\mauto|)
% and the |\delim@loaded| flag is cleared (for cascaded delimiter macros to work
% properly). The |\begingroup| is contained in the delimiter macro, see the
% definition of |\delim@load|.
%    \begin{macrocode}
\def\delim@begingroup{%
 \ifx\delim@loaded\undefined\mauto\fi%
 \let\delim@loaded\undefined%
}%
%    \end{macrocode}
% \end{macro}
%
% \Finale
