summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/delim/delim.dtx
blob: b4ce90aebfe034f45c88047dc28205b3b85586fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
% \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