Debian Codegraph @

texmf-dist/source/latex/skmath/skmath.tex

raw
1 %% skmath improved math commands
2 %%
3 %% Copyright (C) 2012-2019 by Simon Sigurdhsson <[email protected]>
4 %%
5 %% This work may be distributed and/or modified under the
6 %% conditions of the LaTeX Project Public License, either version 1.3
7 %% of this license or (at your option) any later version.
8 %% The latest version of this license is in
9 %% http://www.latex-project.org/lppl.txt
10 %% and version 1.3 or later is part of all distributions of LaTeX
11 %% version 2005/12/01 or later.
12 %%
13 %% This work has the LPPL maintenance status `maintained'.
14 %%
15 %% The Current Maintainer of this work is Simon Sigurdhsson.
16 %%
17 %% This work consists of the file skmath.tex
18 %% and the derived file skmath.sty.
19 \PassOptionsToPackage{intlimits,sumlimits,namelimits}{amsmath}
20 \documentclass[commonsets,load]{skdoc}
21 \usepackage{amsmath}
22 \usepackage{csquotes}
23 \ProvideDocumentCommand\d{m}{#1}
24 \ProvideDocumentCommand\pd{smm}{#2#3}
25 \ProvideDocumentCommand\td{mm}{#1#2}
26 \ProvideDocumentCommand\abs{m}{#1}
27 \ProvideDocumentCommand\norm{m}{#1}
28 \ProvideDocumentCommand\abs{m}{#1}
29 \ProvideDocumentCommand\P{m}{#1}
30 \ProvideDocumentCommand\given{}{}
31 \ProvideDocumentCommand\E{m}{#1}
32 \ProvideDocumentCommand\var{m}{#1}
33 \ProvideDocumentCommand\cov{mm}{#1#2}
34 \ProvideDocumentCommand\N{}{}
35 \ProvideDocumentCommand\Z{}{}
36 \ProvideDocumentCommand\Q{}{}
37 \ProvideDocumentCommand\R{}{}
38 \ProvideDocumentCommand\C{}{}
39 \ProvideDocumentCommand\ii{}{i}
40 \ProvideDocumentCommand\jj{}{j}
41 \ProvideDocumentCommand\ee{}{e}
42 \ProvideDocumentCommand\sfrac{mm}{#1#2}
43 \ProvideDocumentCommand\argmin{om}{#1}
44 \ProvideDocumentCommand\argmax{om}{#1}
45
46 % Declare the target files
47 \SelfPreambleTo{\mypreamble}
48 \DeclareFile[key=package,preamble=\mypreamble]{skmath.sty}
49 \OnlyDescription
50
51 % Hack for LaTeX3 code
52 \ExplSyntaxOn
53 \cs_set_protected_nopar:Npn\ExplHack{
54 \char_set_catcode_letter:n{ 58 }
55 \char_set_catcode_letter:n{ 95 } }
56 \ExplSyntaxOff
57
58 % This is where the documentation begins
59 \begin{document}
60 % Change & version info
61 \version{0.5a}
62 \changes{0.1}{Initial version}
63 \changes{0.1c}{Moved package from \pkg{docstrip} to \pkg{skdoc}}
64 \changes{0.1d}{Fixed fatal documentation and package errors}
65 \changes{0.1e}{Added statistics commands}
66 \changes{0.1g}{Documentation fixes}
67 \changes{0.2}{Use \pkg{expl3} functionality throughout the package}
68 \changes{0.3}{Added \cs{min}/\cs{max} and friends. Added \cs{pd}}
69 \changes{0.3a}{Added \cs{sinh}, \cs{cosh} and \cs{tanh}}
70 \changes{0.3b}{Detect empty arguments in trigonometric and logarithmic functions, fix \cs{ln}}
71 \changes{0.4}{Added \opt{notation} option, macros for complex numbers}
72 \changes{0.4a}{Replaced deprecated/removed \pkg{expl3} constructs}
73 \changes{0.4b}{Track \pkg{expl3} changes (thanks to Joseph Wright)}
74 \changes{0.5}{Added \cs{td} and \cs{ee}}
75 \changes{0.5a}{Track \pkg{expl3} changes (thanks to Phelype Oleinik)}
76 % Don't forget to update the version number and release date of
77 % the package declaration in the implementation!
78
79 % Metadata
80 \package[ctan=skmath,vcs=https://github.com/urdh/skmath]{skmath}
81 \author{Simon Sigurdhsson}
82 \email{[email protected]}
83
84 % First page
85 \maketitle
86 \begin{abstract}
87 The \thepackage\ package provides improved and new math commands
88 for superior typesetting with less effort.
89 \end{abstract}
90
91 \section{Introduction}
92 This package intends to provide helpful (re-)definitions of commands
93 related to typesetting mathematics, and specifically typesetting
94 them in a more intuitive, less verbose and more beautiful way.
95 It was originally not intended for use by the public, and as such
96 there may be incompatibilities with other packages of which I am
97 not aware, but I figured it could be useful to other people as well.
98
99 \section{Usage}
100 \subsection{Options}
101 As of version \theversion, the package provides two key-value options.
102
103 \Option{commonsets}\WithValues{true,false}\AndDefault{false}
104 Optionally define \cs{N}, \cs{Z}, \cs{Q}, \cs{R} and \cs{C} as blackboard
105 variants of the respective letters, to represent the common sets
106 of numbers.
107
108 \Option{notation}\WithValues{iso,english,german,legacy}\AndDefault{legacy}
109 This option controls the style of a few typographic elements that differ
110 between countries and standards (such as the style of integrals, derivatives
111 and greek letters).
112
113 \subsection{New commands}
114 The package defines a number of new commands that aid in typesetting
115 certain mathematical formulae.
116
117 \DescribeMacro\N
118 \DescribeMacro\Z
119 \DescribeMacro\Q
120 \DescribeMacro\R
121 \DescribeMacro\C
122 These commands are only available if the \opt{commonsets}
123 option is given. They typeset the set of natural, integer, rational,
124 real and complex numbers respectively.
125 \begin{example}
126 \begin{equation*}
127 \N, \Z, \Q, \R, \C.
128 \end{equation*}
129 \end{example}
130
131 \DescribeMacro\ii
132 \DescribeMacro\jj
133 These commands typeset the imaginary unit (either \(\ii\) as used in
134 mathematics or \(\jj\) as used in electrotechnology). While normal use
135 of the package simply results in italic characters, setting the \opt{notation}
136 option to \texttt{iso} will set these upright.
137
138 \DescribeMacro\ee
139 This command typesets Euler's number \(\ee = \sum_{n=0}^\infty\frac{1}{n!}\).
140 The style is affected by the \opt{notation} option in the same way as \Macro\exp.
141
142 \DescribeMacro\norm{<expression>}
143 \DescribeMacro\abs{<expression>}
144 The commands \Macro\norm and \Macro\abs, quite expectedly, typeset
145 the norm ans absolute value of an expression, respectively. They
146 have one mandatory argument (the expression), and different norms
147 can be achieved by appending a subscript after the argument of
148 \Macro\norm.
149 \begin{example}
150 \begin{equation*}
151 \norm{\vec{x}}_p =
152 \left(\sum_{i=1}^n \abs{x_i}^p\right)%
153 ^{\sfrac{1}{p}}
154 \end{equation*}
155 \end{example}
156
157 \DescribeMacro\d{<variable>}
158 There is also a command \Macro\d, with one mandatory argument, that
159 typesets the differential part of an integral.
160 \begin{example}
161 \begin{equation*}
162 \int_{\R}\! \frac{\sin{x}}{x} \d{x}
163 \end{equation*}
164 \end{example}
165
166 \DescribeMacro\pd*{<function>}{<var>,\meta{var},...}
167 This macro typesets a partial derivative. The starred variant typesets
168 derivatives as subscripts, i.e. \(\pd*{f}{x^2,y}\), while the unstarred
169 variant typesets full fractions:
170 \begin{example}
171 \begin{equation*}
172 \pd{f}{x^m,y^n}
173 \end{equation*}
174 \end{example}
175
176 As the example shows, the comma-separated list of variables also
177 supports superscripts to denote the number of derivatives, and the
178 sum of the variables is automatically calculated.
179
180 \DescribeMacro\td{<function>}{<var>}
181 This macro typesets a total derivative. Unlike \Macro\pd, this macro does
182 not have a starred variant, and only typesets full fractions:
183 \begin{example}
184 \begin{equation*}
185 \td{f}{x^m}
186 \end{equation*}
187 \end{example}
188
189 \DescribeMacro\E{<expression>}
190 The command \Macro\E typesets the expectation of a random variable.
191 \begin{example}
192 \begin{equation*}
193 \E{\hat{\mu}} = \mu
194 \end{equation*}
195 \end{example}
196
197 \DescribeMacro\P{<expression>\AlsoMacro\given <expression>}
198 The \Macro\P command typesets a probability. The \Macro\given command
199 can be used to typeset conditional probabilities, within \Macro\P.
200 \begin{example}
201 \begin{equation*}
202 \P{A\given B} =
203 \frac{\P{B\given A}\P{A}}{\P{B}}
204 \end{equation*}
205 \end{example}
206
207 \DescribeMacro\var{<expression>}
208 \DescribeMacro\cov{<expression>}{<expression>}
209 The commands \Macro\var and \Macro\cov typeset the variance and
210 covariance of an expression.
211 \begin{example}
212 \begin{gather*}
213 \var{X} = \E{(X-\mu)^2}\\
214 \cov{X}{Y} = \E{XY}-\E{X}\E{Y}
215 \end{gather*}
216 \end{example}
217
218 \subsection{Improved commands}
219 In addition to adding new commands, this package also redefines
220 already existing commands in a mostly backwards-compatible way
221 to improve their usefulness.
222
223 \DescribeMacro\sin[<power>]{<expression>}
224 \DescribeMacro\arcsin{<expression>}
225 \DescribeMacro\cos[<power>]{<expression>}
226 \DescribeMacro\arccos{<expression>}
227 \DescribeMacro\tan[<power>]{<expression>}
228 \DescribeMacro\arctan{<expression>}
229 \DescribeMacro\cot[<power>]{<expression>}
230 \DescribeMacro\sinh[<power>]{<expression>}
231 \DescribeMacro\cosh[<power>]{<expression>}
232 \DescribeMacro\tanh[<power>]{<expression>}
233 The trigonometric functions have been redefined
234 to typeset more easily. They typeset \meta{expression} as an
235 argument of the expression, and (if applicable) \meta{power} as
236 a superscript between the function and its argument,
237 \emph{e.g.} \(\sin[2]{\phi}\).
238 When the argument is empty, no parentheses are emitted: \(\cos{}\).
239
240 \DescribeMacro\ln{<expression>}
241 The natural logarithm macro \Macro\ln has also been redefined to
242 require an argument which is typeset as the argument of the logarithm.
243 \DescribeMacro\log[<base>]{<expression>}
244 The related macro \Macro\log is redefined in a similar way, but also
245 accepts an optional argument denoting the base of the logarithm:
246 \(\log[2]{x}\).
247 As with the trigonometric functions, no parentheses are emitted if
248 the mandatory argument is empty: \(\log{}\).
249
250 \DescribeMacro\exp*{<expression>}
251 The exponential, \Macro\exp, is redefined to typeset its argument as a
252 superscript of \(\ee\) in some display styles, and as an argument of
253 \(\mathrm{exp}\) otherwise:
254 \begin{equation*}
255 \exp{\sqrt{2}\exp{x}}
256 \end{equation*}
257 Additionally, it is possible to force the \(\mathrm{exp}\) mode by
258 using the starred variant.
259
260 \DescribeMacro\min*[<domain>]{<expression>}
261 \DescribeMacro\argmin*[<domain>]{<expression>}
262 \DescribeMacro\max*[<domain>]{<expression>}
263 \DescribeMacro\argmax*[<domain>]{<expression>}
264 \DescribeMacro\sup*[<domain>]{<expression>}
265 \DescribeMacro\inf*[<domain>]{<expression>}
266 The maximum/minimum macros have been redefined in a manner similar to
267 the trigonometric functions. They typeset \meta{expression} inside
268 curly brackets (the starred version omits the brackets), with the
269 optional \meta{domain} typeset in a subscript after the operator
270 (\emph{e.g.} \(\min*[x\in\R_{+}]{f(x)}\)). Argument variants are also
271 provided, and the \meta{expression} is centered underneath the operator
272 if possible:
273 \begin{equation*}
274 \argmin*[x\in\R_{+}]{f(x)}
275 \end{equation*}
276
277 \subsection{Stylistic changes}
278 Some commands have been redefined in a completely backwards-compatible
279 way to improve the end result of their typesetting.
280
281 \DescribeMacro\frac{<numerator>}{<denominator>}
282 The \Macro\frac command has been changed to improve typesetting,
283 allowing displaystyle math in some settings.
284
285 \DescribeMacro\bar{<expression>}
286 \DescribeMacro\vec{<expression>}
287 The \Macro\bar command has been changed to cover the entire
288 \meta{expression} (\emph{i.e.} \(\bar{uv}\)), and \Macro\vec has
289 been changed to match the \cs{vectorsym} command provided by
290 \pkg{isomath}.
291
292 \DescribeMacro\Re{<expression>}
293 \DescribeMacro\Im{<expression>}
294 These commands typeset the real and imaginary part of a number. Standard use
295 of the package takes definitions roughly from \pkg{amsmath}, while setting
296 the \opt{notation} option to \texttt{iso} changes the definitions to match
297 ISO~80000-2.
298
299 \section{Known issues}
300 A list of current issues is available in the Github repository of this
301 package\footnote{\url{https://github.com/urdh/skmath/issues}}, but as
302 of the release of \theversion, there is one known issue.
303 \begin{description}
304 \item[\#15] The package is incompatible with (at least)
305 \pkg{blindtext}, when including math in the blind text. This
306 is due to the redifinition of \cs{sin} (and friends), which
307 is incompatible with the original \pkg{amsmath} definition.
308 This is a feature, not a bug.
309 \item[\#29] The spacing between operator names and parentheses
310 is typographically incorrect. A work-around until this is
311 fixed in \pkg{skmath} is to use the \cs{mleftright} macro from
312 \pkg{mleftright} to redefine \cs{left} and \cs{right}.
313 \end{description}
314
315 If you discover any bugs in this package, please report them to the issue
316 tracker in the \thepackage\ Github repository.
317
318 \Implementation\ExplHack
319 \section{Implementation}
320 The package implementation is very simple. First, we do the standard
321 \LaTeXe\ preamble thing, then we require some dependencies.
322 \changes{0.1b}{Load \pkg{amsmath} with \texttt{intlimits} option}
323 \changes{0.2a}{Load \pkg{amsmath} with more \texttt{limits} options}
324 \begin{MacroCode}{package}
325 \RequirePackage{expl3,l3keys2e,xparse}
326 \ProvidesExplPackage{skmath}
327 {2019/10/15}{0.5a}{improved math commands}
328 \PassOptionsToPackage{intlimits,sumlimits,namelimits}{amsmath}
329 \RequirePackage{amssymb,mathtools,xfrac}
330 \end{MacroCode}
331
332 We start by declaring internal options related to the notation styles.
333 First, some placeholders to detect failures.
334 \begin{MacroCode}{package}
335 \msg_new:nnnn{skmath}{undefined-macro}{The~macro~`\token_to_str:N#1'~was~undefined!}
336 {This~is~an~error~in~the~notation~option~handling.~Please~file~an~issue\\
337 on~Github~(https://github.com/urdh/skmath/issues)~with~a~copy~of~this\\
338 message~attached~as~well~as~a~list~of~the~options~passed~to~`skmath'.}
339 \cs_gset_nopar:Npn\__skmath_integral_d:{
340 \msg_critical:nnn{skmath}{undefined-macro}{\__skmath_integral_d:}
341 }
342 \cs_gset_nopar:Npn\__skmath_natural_log_e:{
343 \msg_critical:nnn{skmath}{undefined-macro}{\__skmath_natural_log_e:}
344 }
345 \cs_gset_nopar:Npn\__skmath_imaginary_unit:n#1{
346 \msg_critical:nnn{skmath}{undefined-macro}{\__skmath_imaginary_unit:n}
347 }
348 \cs_gset_nopar:Npn\__skmath_total_derivative_d:{
349 \msg_critical:nnn{skmath}{undefined-macro}{\__skmath_total_derivative_d:n}
350 }
351 \end{MacroCode}
352 Then, the key-value options.
353 \begin{MacroCode}{package}
354 \keys_define:nn{skmath / internal}{
355 % More on this:
356 % * http://en.wikipedia.org/wiki/Typographical_conventions_in_mathematical_formulae
357 % * ISO 80000-2
358 % * http://www.tug.org/TUGboat/Articles/tb18-1/tb54becc.pdf
359 % * http://tex.stackexchange.com/q/14821/66
360 integral-d .choice:,
361 integral-d .value_required:n = true,
362 integral-d / upright .code:n = { \cs_gset_nopar:Npn\__skmath_integral_d:{{\operator@font d}} },
363 integral-d / slanted .code:n = { \cs_gset_nopar:Npn\__skmath_integral_d:{{d}} },
364 isomath .bool_set:N = \g__skmath_load_isomath_bool,
365 isomath .value_required:n = true,
366 natural-log .choice:,
367 natural-log .value_required:n = true,
368 natural-log / upright .code:n = { \cs_gset_nopar:Npn\__skmath_natural_log_e:{{\operator@font e}} },
369 natural-log / slanted .code:n = { \cs_gset_nopar:Npn\__skmath_natural_log_e:{{e}} },
370 imaginary-unit .choice:,
371 imaginary-unit .value_required:n = true,
372 imaginary-unit / upright .code:n = { \cs_gset_nopar:Npn\__skmath_imaginary_unit:n##1{{\operator@font ##1}} },
373 imaginary-unit / slanted .code:n = { \cs_gset_nopar:Npn\__skmath_imaginary_unit:n##1{{##1}} },
374 complex-part-symbols .bool_set_inverse:N = \g__skmath_iso_complex_parts_bool,
375 complex-part-symbols .value_required:n = true,
376 total-derivative-d .choice:,
377 total-derivative-d .value_required:n = true,
378 total-derivative-d / upright .code:n = { \cs_gset_nopar:Npn\__skmath_total_derivative_d:{{\operator@font d}} },
379 total-derivative-d / slanted .code:n = { \cs_gset_nopar:Npn\__skmath_total_derivative_d:{{d}} },
380 }
381 \end{MacroCode}
382
383 We also declare the \enquote{public} options.
384 \begin{MacroCode}{package}
385 \keys_define:nn{skmath}{
386 commonsets .bool_set:N = \g__skmath_define_common_sets_bool,
387 commonsets .default:n = true,
388 commonsets .initial:n = false,
389 % TODO: write tests for the notation option
390 notation .choice:,
391 notation / iso .meta:nn = {skmath / internal}{
392 integral-d = upright,
393 isomath = true,
394 natural-log = upright,
395 imaginary-unit = upright,
396 complex-part-symbols = false,
397 total-derivative-d = upright,
398 },
399 notation / german .meta:nn = {skmath / internal}{
400 integral-d = upright,
401 isomath = true,
402 natural-log = slanted, %???
403 imaginary-unit = slanted, %???
404 complex-part-symbols = true, %???
405 total-derivative-d = upright, %???
406 },
407 notation / english .meta:nn = {skmath / internal}{
408 integral-d = slanted,
409 isomath = true,
410 natural-log = slanted, %???
411 imaginary-unit = slanted, %???
412 complex-part-symbols = true, %???
413 total-derivative-d = slanted, %???
414 },
415 notation / legacy .meta:nn = {skmath / internal}{
416 integral-d = upright,
417 isomath = true,
418 natural-log = slanted,
419 imaginary-unit = slanted,
420 complex-part-symbols = true,
421 total-derivative-d = slanted,
422 },
423 % Possibly more style sets... ?
424 notation .initial:n = legacy, % Or is it? Perhaps a legacy option is required.
425 notation .value_required:n = true,
426 % Note also that the styles should probably affect isomath and/or other packages.
427 }
428 \ProcessKeysOptions{skmath}
429 \end{MacroCode}
430
431 We optionally load \pkg{isomath}, depending on notation style.
432 \begin{MacroCode}{package}
433 \bool_if:NTF\g__skmath_load_isomath_bool{
434 \RequirePackage{isomath}
435 }{
436 % TODO: check if there is an alternative package we should load here.
437 }
438 \end{MacroCode}
439
440 Then, a utility conditional\ldots
441 \begin{macro*}{\__skmath_if_novalue_or_empty:nF}[1]
442 {Token or string}
443 \begin{MacroCode}{package}
444 \prg_new_conditional:Npnn\__skmath_if_novalue_or_empty:n#1{F}{
445 \IfNoValueTF{#1}{
446 \prg_return_true:
447 }{
448 \tl_if_empty:nTF{#1}{
449 \prg_return_true:
450 }{
451 \prg_return_false:
452 }
453 }
454 }
455 \end{MacroCode}
456 \end{macro*}
457
458 \ldots and a helper macro.
459 \begin{macro*}{\__skmath_parens:n}[1]
460 {Argument to wrap in parentheses, unless empty}
461 \begin{MacroCode}{package}
462 \cs_new_nopar:Npn\__skmath_parens:n#1{
463 \__skmath_if_novalue_or_empty:nF{#1}{\left(#1\right)}
464 }
465 \end{MacroCode}
466 \end{macro*}
467
468 We optionally provide commands to typeset common sets.
469 \begin{MacroCode}{package}
470 \bool_if:NT\g__skmath_define_common_sets_bool{
471 \end{MacroCode}
472 \begin{macro}{\N}
473 \changes{0.1b}{Moved to \textsf{xparse} command definition}
474 \begin{MacroCode}{package}
475 \NewDocumentCommand\N{}{\ensuremath{\mathbb{N}}}
476 \end{MacroCode}
477 \end{macro}
478 \begin{macro}{\Z}
479 \changes{0.1b}{Moved to \textsf{xparse} command definition}
480 \begin{MacroCode}{package}
481 \NewDocumentCommand\Z{}{\ensuremath{\mathbb{Z}}}
482 \end{MacroCode}
483 \end{macro}
484 \begin{macro}{\Q}
485 \changes{0.1b}{Moved to \textsf{xparse} command definition}
486 \begin{MacroCode}{package}
487 \NewDocumentCommand\Q{}{\ensuremath{\mathbb{Q}}}
488 \end{MacroCode}
489 \end{macro}
490 \begin{macro}{\R}
491 \changes{0.1b}{Moved to \textsf{xparse} command definition}
492 \begin{MacroCode}{package}
493 \NewDocumentCommand\R{}{\ensuremath{\mathbb{R}}}
494 \end{MacroCode}
495 \end{macro}
496 \begin{macro}{\C}
497 \changes{0.1b}{Moved to \textsf{xparse} command definition}
498 \begin{MacroCode}{package}
499 \NewDocumentCommand\C{}{\ensuremath{\mathbb{C}}}
500 \end{MacroCode}
501 \end{macro}
502 \begin{MacroCode}{package}
503 }
504 \end{MacroCode}
505
506 Then, macros related to complex numbers are defined.
507 \begin{macro}{\ii}
508 \begin{MacroCode}{package}
509 \DeclareDocumentCommand\ii{}{\ensuremath{\__skmath_imaginary_unit:n{i}}}
510 \end{MacroCode}
511 \end{macro}
512 \begin{macro}{\jj}
513 \begin{MacroCode}{package}
514 \DeclareDocumentCommand\jj{}{\ensuremath{\__skmath_imaginary_unit:n{j}}}
515 \end{MacroCode}
516 \end{macro}
517 \begin{macro}{\Re}
518 \begin{MacroCode}{package}
519 \bool_if:NTF\g__skmath_iso_complex_parts_bool{
520 \cs_new_eq:NN\__skmath_Re:\Re
521 \DeclareDocumentCommand\Re{m}{\ensuremath{\__skmath_Re:\__skmath_parens:n{#1}}}
522 }{
523 \DeclareDocumentCommand\Re{m}{\ensuremath{{\operator@font Re}{#1}}}
524 }
525 \end{MacroCode}
526 \end{macro}
527 \begin{macro}{\Im}
528 \begin{MacroCode}{package}
529 \bool_if:NTF\g__skmath_iso_complex_parts_bool{
530 \cs_new_eq:NN\__skmath_Im:\Im
531 \DeclareDocumentCommand\Im{m}{\ensuremath{\__skmath_Im:\__skmath_parens:n{#1}}}
532 }{
533 \DeclareDocumentCommand\Im{m}{\ensuremath{{\operator@font Im}{#1}}}
534 }
535 \end{MacroCode}
536 \end{macro}
537
538 This is followed by commands to typeset the norm and absolute value.
539 \begin{macro}{\abs}
540 \begin{MacroCode}{package}
541 \DeclarePairedDelimiter\abs{\lvert}{\rvert}
542 \end{MacroCode}
543 \end{macro}
544 \begin{macro}{\norm}
545 \begin{MacroCode}{package}
546 \DeclarePairedDelimiter\norm{\lVert}{\rVert}
547 \end{MacroCode}
548 \end{macro}
549
550 Next come the statistical commands.
551 \begin{macro}{\E}
552 \changes{0.1e}{Added \cs{E} command}
553 \changes{0.1f}{Fixed \enquote{Command \cs{E} already defined!} error}
554 \changes{0.2}{Use \cs{operatorname}}
555 Here, we define \cs{E} after the preamble since it may break otherwise.
556 \begin{MacroCode}{package}
557 \AtBeginDocument{
558 \DeclareDocumentCommand\E{m}{%
559 \ensuremath{\operatorname{E}\left[#1\right]}%
560 }
561 }
562 \end{MacroCode}
563 \end{macro}
564 The \Macro\P command saves any old \Macro\given command, replacing
565 it locally with the new \Macro\given command provided by the package.
566 \begin{macro}{\P}
567 \changes{0.1e}{Added \cs{P} command}
568 \changes{0.2}{Use \cs{operatorname}, use \cs{cs_new_eq:NN} instead of
569 \cs{let}}
570 \begin{MacroCode}{package}
571 \DeclareDocumentCommand\P{m}{%
572 \ensuremath{\operatorname{P}%
573 \mkern-1.5mu\left(%
574 \cs_set_eq:NN\__skmath_saved_given:\given%
575 \end{MacroCode}
576 \begin{macro}{\given}
577 \changes{0.1e}{Added \cs{given} command}
578 \begin{MacroCode}{package}
579 \DeclareDocumentCommand\given{}{\mid}%
580 \end{MacroCode}
581 \end{macro}
582 \begin{MacroCode}{package}
583 #1%
584 \cs_set_eq:NN\given\__skmath_saved_given:%
585 \right)%
586 }%
587 }
588 \end{MacroCode}
589 \end{macro}
590 \begin{macro}{\var}
591 \changes{0.1e}{Added \cs{var} command}
592 \changes{0.2}{Use \cs{operatorname}}
593 \begin{MacroCode}{package}
594 \DeclareDocumentCommand\var{m}{%
595 \ensuremath{\operatorname{Var}\left(#1\right)}%
596 }
597 \end{MacroCode}
598 \end{macro}
599 \begin{macro}{\cov}
600 \changes{0.1e}{Added \cs{cov} command}
601 \changes{0.2}{Use \cs{operatorname}}
602 \begin{MacroCode}{package}
603 \DeclareDocumentCommand\cov{mm}{%
604 \ensuremath{\operatorname{Cov}\left(#1,#2\right)}%
605 }
606 \end{MacroCode}
607 \end{macro}
608
609 We replace all trigonometric functions and some other
610 common functions with alternatives that take an argument
611 (or optionally, several arguments).
612 \begin{MacroCode}{package}
613 \cs_new_eq:NN\__skmath_sin:\sin
614 \cs_new_eq:NN\__skmath_cos:\cos
615 \cs_new_eq:NN\__skmath_tan:\tan
616 \cs_new_eq:NN\__skmath_cot:\cot
617 \cs_new_eq:NN\__skmath_arcsin:\arcsin
618 \cs_new_eq:NN\__skmath_arccos:\arccos
619 \cs_new_eq:NN\__skmath_arctan:\arctan
620 \cs_new_eq:NN\__skmath_sinh:\sinh
621 \cs_new_eq:NN\__skmath_cosh:\cosh
622 \cs_new_eq:NN\__skmath_tanh:\tanh
623 \cs_new_eq:NN\__skmath_ln:\ln
624 \cs_new_eq:NN\__skmath_log:\log
625 \cs_new_eq:NN\__skmath_exp:\exp
626 \cs_new_eq:NN\__skmath_min:\min
627 \cs_new_eq:NN\__skmath_max:\max
628 \cs_new_eq:NN\__skmath_sup:\sup
629 \cs_new_eq:NN\__skmath_inf:\inf
630 \end{MacroCode}
631 \begin{macro}{\sin}
632 \begin{MacroCode}{package}
633 \RenewDocumentCommand\sin{om}{%
634 \IfNoValueTF{#1}
635 {\ensuremath{\__skmath_sin:\__skmath_parens:n{#2}}}
636 {\ensuremath{\__skmath_sin:\c_math_superscript_token{#1}\__skmath_parens:n{#2}}}%
637 }
638 \end{MacroCode}
639 \end{macro}
640 \begin{macro}{\cos}
641 \begin{MacroCode}{package}
642 \RenewDocumentCommand\cos{om}{%
643 \IfNoValueTF{#1}
644 {\ensuremath{\__skmath_cos:\__skmath_parens:n{#2}}}
645 {\ensuremath{\__skmath_cos:\c_math_superscript_token{#1}\__skmath_parens:n{#2}}}%
646 }
647 \end{MacroCode}
648 \end{macro}
649 \begin{macro}{\tan}
650 \begin{MacroCode}{package}
651 \RenewDocumentCommand\tan{om}{%
652 \IfNoValueTF{#1}
653 {\ensuremath{\__skmath_tan:\__skmath_parens:n{#2}}}
654 {\ensuremath{\__skmath_tan:\c_math_superscript_token{#1}\__skmath_parens:n{#2}}}%
655 }
656 \end{MacroCode}
657 \end{macro}
658 \begin{macro}{\cot}
659 \begin{MacroCode}{package}
660 \RenewDocumentCommand\cot{om}{%
661 \IfNoValueTF{#1}
662 {\ensuremath{\__skmath_cot:\__skmath_parens:n{#2}}}
663 {\ensuremath{\__skmath_cot:\c_math_superscript_token{#1}\__skmath_parens:n{#2}}}%
664 }
665 \end{MacroCode}
666 \end{macro}
667 \begin{macro}{\arcsin}
668 \begin{MacroCode}{package}
669 \RenewDocumentCommand\arcsin{m}{%
670 \ensuremath{\__skmath_arcsin:\__skmath_parens:n{#1}}%
671 }
672 \end{MacroCode}
673 \end{macro}
674 \begin{macro}{\arccos}
675 \begin{MacroCode}{package}
676 \RenewDocumentCommand\arccos{m}{%
677 \ensuremath{\__skmath_arccos:\__skmath_parens:n{#1}}%
678 }
679 \end{MacroCode}
680 \end{macro}
681 \begin{macro}{\arctan}
682 \begin{MacroCode}{package}
683 \RenewDocumentCommand\arctan{m}{%
684 \ensuremath{\__skmath_arctan:\__skmath_parens:n{#1}}%
685 }
686 \end{MacroCode}
687 \end{macro}
688 \begin{macro}{\sinh}
689 \begin{MacroCode}{package}
690 \RenewDocumentCommand\sinh{om}{%
691 \IfNoValueTF{#1}
692 {\ensuremath{\__skmath_sinh:\__skmath_parens:n{#2}}}
693 {\ensuremath{\__skmath_sinh:\c_math_superscript_token{#1}\__skmath_parens:n{#2}}}%
694 }
695 \end{MacroCode}
696 \end{macro}
697 \begin{macro}{\cosh}
698 \begin{MacroCode}{package}
699 \RenewDocumentCommand\cosh{om}{%
700 \IfNoValueTF{#1}
701 {\ensuremath{\__skmath_cosh:\__skmath_parens:n{#2}}}
702 {\ensuremath{\__skmath_cosh:\c_math_superscript_token{#1}\__skmath_parens:n{#2}}}%
703 }
704 \end{MacroCode}
705 \end{macro}
706 \begin{macro}{\tanh}
707 \begin{MacroCode}{package}
708 \RenewDocumentCommand\tanh{om}{%
709 \IfNoValueTF{#1}
710 {\ensuremath{\__skmath_tanh:\__skmath_parens:n{#2}}}
711 {\ensuremath{\__skmath_tanh:\c_math_superscript_token{#1}\__skmath_parens:n{#2}}}%
712 }
713 \end{MacroCode}
714 \end{macro}
715 \begin{macro}{\ln}
716 \begin{MacroCode}{package}
717 \RenewDocumentCommand\ln{m}{%
718 \ensuremath{\__skmath_ln:\__skmath_parens:n{#1}}%
719 }
720 \end{MacroCode}
721 \end{macro}
722 \begin{macro}{\log}
723 \begin{MacroCode}{package}
724 \RenewDocumentCommand\log{om}{%
725 \IfNoValueTF{#1}
726 {\ensuremath{\__skmath_log:\__skmath_parens:n{#2}}}
727 {\ensuremath{\__skmath_log:\c_math_subscript_token{#1}\__skmath_parens:n{#2}}}%
728 }
729 \end{MacroCode}
730 \end{macro}
731 \begin{macro}{\ee}
732 \begin{MacroCode}{package}
733 \DeclareDocumentCommand\ee{}{\ensuremath{\__skmath_natural_log_e:}}
734 \end{MacroCode}
735 \end{macro}
736 \begin{macro}{\exp}
737 \changes{0.1b}{Moved to \textsf{xparse} command definition}
738 \changes{0.3a}{Added starred variant}
739 \begin{MacroCode}{package}
740 \RenewDocumentCommand\exp{sm}{\ensuremath{
741 \IfBooleanTF{#1}{
742 \__skmath_exp:\__skmath_parens:n{#2}
743 }{
744 \mathchoice
745 {\__skmath_natural_log_e:\c_math_superscript_token{#2}}
746 {\__skmath_exp:\left(#2\right)}
747 {\__skmath_exp:\left(#2\right)}
748 {\__skmath_exp:\left(#2\right)}
749 }
750 }}
751 \end{MacroCode}
752 \end{macro}
753 \begin{macro*}{\__skmath_minmax_backend:nnn}
754 \begin{MacroCode}{package}
755 \cs_new_nopar:Npn\__skmath_minmax_backend:nnnn#1#2#3#4{%
756 \use:c{__skmath_#1:}
757 \IfNoValueF{#3}{
758 \c_math_subscript_token{
759 \mathchoice{\mathclap{#3}}{#3}{#3}{#3}
760 }
761 }
762 \IfBooleanTF{#2}{#4}{\left\{#4\right\}}
763 }
764 \end{MacroCode}
765 \end{macro*}
766 \begin{macro}{\min}
767 \begin{MacroCode}{package}
768 \RenewDocumentCommand\min{som}{%
769 \ensuremath{\__skmath_minmax_backend:nnnn{min}{#1}{#2}{#3}}
770 }
771 \end{MacroCode}
772 \end{macro}
773 \begin{macro}{\argmin}
774 \begin{MacroCode}{package}
775 \NewDocumentCommand\argmin{som}{%
776 \ensuremath{\arg\__skmath_minmax_backend:nnnn{min}{#1}{#2}{#3}}
777 }
778 \end{MacroCode}
779 \end{macro}
780 \begin{macro}{\max}
781 \begin{MacroCode}{package}
782 \RenewDocumentCommand\max{som}{%
783 \ensuremath{\__skmath_minmax_backend:nnnn{max}{#1}{#2}{#3}}
784 }
785 \end{MacroCode}
786 \end{macro}
787 \begin{macro}{\argmax}
788 \begin{MacroCode}{package}
789 \NewDocumentCommand\argmax{som}{%
790 \ensuremath{\arg\__skmath_minmax_backend:nnnn{max}{#1}{#2}{#3}}
791 }
792 \end{MacroCode}
793 \end{macro}
794 \begin{macro}{\sup}
795 \begin{MacroCode}{package}
796 \RenewDocumentCommand\sup{som}{%
797 \ensuremath{\__skmath_minmax_backend:nnnn{sup}{#1}{#2}{#3}}
798 }
799 \end{MacroCode}
800 \end{macro}
801 \begin{macro}{\inf}
802 \begin{MacroCode}{package}
803 \RenewDocumentCommand\inf{som}{%
804 \ensuremath{\__skmath_minmax_backend:nnnn{inf}{#1}{#2}{#3}}
805 }
806 \end{MacroCode}
807 \end{macro}
808
809 The fraction command is modified to improve typesetting.
810 \begin{macro}{\frac}
811 \changes{0.1b}{Moved to \textsf{xparse} command definition}
812 \begin{MacroCode}{package}
813 \RenewDocumentCommand\frac{mm}{\genfrac{}{}{}{}%
814 {\displaystyle #1}{\displaystyle #2}}
815 \end{MacroCode}
816 \end{macro}
817
818 We introduce a couple of helper macros for \cs{pd} and \cs{td}.
819 \begin{macro*}{\__skmath_if_numerical_p:n}
820 \begin{macro*}{\__skmath_if_numerical:nT}
821 \begin{macro*}{\__skmath_if_numerical:nF}
822 \begin{macro*}{\__skmath_if_numerical:nTF}[1]
823 {Token or string}
824 \begin{MacroCode}{package}
825 \prg_new_conditional:Npnn\__skmath_if_numerical:n#1{p,T,F,TF}{
826 \str_case_e:nnTF{#1}{
827 {0}{}
828 {1}{}
829 {2}{}
830 {3}{}
831 {4}{}
832 {5}{}
833 {6}{}
834 {7}{}
835 {8}{}
836 {9}{}
837 }
838 {\prg_return_false:}
839 {\prg_return_false:}
840 }
841 \end{MacroCode}
842 \end{macro*}
843 \end{macro*}
844 \end{macro*}
845 \end{macro*}
846 \begin{macro*}{\__skmath_pd_vars_sum:n}[1]
847 {Comma-separated list of variables, possibly with superscripts}
848 \begin{MacroCode}{package}
849 \cs_new_nopar:Npn\__skmath_pd_vars_sum:n#1{
850 \tl_clear:N\l_tmpa_tl
851 \int_zero:N\l_tmpa_int
852 \bool_set_true:N\l_tmpa_bool
853 \clist_set:Nn\l_tmpa_clist{#1}
854 \clist_map_inline:Nn\l_tmpa_clist{
855 \seq_set_split:Nnn\l_tmpa_seq{^}{##1}
856 \int_compare:nT{\seq_count:N\l_tmpa_seq<2}{
857 \seq_put_right:Nn\l_tmpa_seq{1}
858 }
859 \seq_get_right:NN\l_tmpa_seq\l_tmpb_tl
860 \__skmath_if_numerical:nTF{\tl_use:N\l_tmpb_tl}{
861 \int_add:Nn\l_tmpa_int{\tl_use:N\l_tmpb_tl}
862 }{
863 \bool_set_false:N\l_tmpa_bool
864 \tl_if_empty:NF\l_tmpa_tl{\tl_put_right:Nn\l_tmpa_tl{+}}
865 \tl_put_right:Nx\l_tmpa_tl{\tl_use:N\l_tmpb_tl}
866 }
867 }
868 \int_compare:nT{\l_tmpa_int>\c_zero_int}{\int_use:N\l_tmpa_int}
869 \bool_if:NF\l_tmpa_bool{
870 \int_compare:nT{\l_tmpa_int>\c_zero_int}{+}
871 \tl_use:N\l_tmpa_tl
872 }
873 }
874 \end{MacroCode}
875 \end{macro*}
876 \begin{macro*}{\__skmath_pd_if_vars_sum_above_one_p:n}
877 \begin{macro*}{\__skmath_pd_if_vars_sum_above_one:nT}
878 \begin{macro*}{\__skmath_pd_if_vars_sum_above_one:nF}
879 \begin{macro*}{\__skmath_pd_if_vars_sum_above_one:nTF}[1]
880 {Comma-separated list of variables, possibly with superscripts}
881 \begin{MacroCode}{package}
882 \prg_new_conditional:Npnn\__skmath_pd_if_vars_sum_above_one:n#1{p,T,F,TF}{
883 \clist_set:Nn\l_tmpa_clist{#1}
884 \int_compare:nTF{\clist_count:N\l_tmpa_clist>\c_one_int}{
885 \prg_return_true:
886 }{
887 \clist_pop:NN\l_tmpa_clist\l_tmpa_tl
888 \seq_set_split:NnV\l_tmpa_seq{^}{\l_tmpa_tl}
889 \int_compare:nTF{\seq_count:N\l_tmpa_seq<2}{
890 \prg_return_false:
891 }{
892 \prg_return_true:
893 }
894 }
895 }
896 \end{MacroCode}
897 \end{macro*}
898 \end{macro*}
899 \end{macro*}
900 \end{macro*}
901 \begin{macro*}{\__skmath_pd_superscript_vars:n}[1]
902 {Comma-separated list of variables, possibly with superscripts}
903 \begin{MacroCode}{package}
904 \cs_new_nopar:Npn\__skmath_pd_superscript_vars:n#1{
905 \clist_set:Nn\l_tmpa_clist{#1}
906 \clist_map_inline:Nn\l_tmpa_clist{
907 \partial
908 \seq_set_split:Nnn\l_tmpa_seq{^}{##1}
909 \seq_pop:NN\l_tmpa_seq\l_tmpb_tl
910 \tl_use:N\l_tmpb_tl
911 \int_compare:nT{\seq_count:N\l_tmpa_seq>0}{
912 \seq_pop:NN\l_tmpa_seq\l_tmpb_tl
913 \c_math_superscript_token
914 \tl_use:N\l_tmpb_tl
915 }
916 }
917 }
918 \end{MacroCode}
919 \end{macro*}
920 \begin{macro*}{\__skmath_pd_subscript_vars:n}[1]
921 {Comma-separated list of variables, possibly with superscripts}
922 \begin{MacroCode}{package}
923 \cs_new_nopar:Npn\__skmath_pd_subscript_vars:n#1{
924 \clist_set:Nn\l_tmpa_clist{#1}
925 \clist_map_inline:Nn\l_tmpa_clist{
926 \seq_set_split:Nnn\l_tmpa_seq{^}{##1}
927 \seq_pop:NN\l_tmpa_seq\l_tmpa_tl
928 \int_set:Nn\l_tmpa_int{\c_one_int}
929 \int_compare:nT{\seq_count:N\l_tmpa_seq>\c_zero_int}{
930 \seq_pop:NN\l_tmpa_seq\l_tmpb_tl
931 \int_set:Nn\l_tmpa_int{\tl_use:N\l_tmpb_tl}
932 }
933 \prg_replicate:nn{\l_tmpa_int}{\tl_use:N\l_tmpa_tl}
934 }
935 }
936 \end{MacroCode}
937 \end{macro*}
938 \begin{macro*}{\__skmath_pd_fraction:nn}[2]
939 {Tokens representing a mathematical function}
940 {Comma-separated list of variables, possibly with superscripts}
941 \begin{MacroCode}{package}
942 \cs_new_nopar:Npn\__skmath_pd_fraction:nn#1#2{
943 \frac{
944 \partial
945 \__skmath_pd_if_vars_sum_above_one:nT{#2}{
946 \c_math_superscript_token{\__skmath_pd_vars_sum:n{#2}}
947 }
948 {#1}
949 }{
950 \__skmath_pd_superscript_vars:n{#2}
951 }
952 }
953 \end{MacroCode}
954 \end{macro*}
955 \begin{macro*}{\__skmath_pd_subscript:nn}[2]
956 {Tokens representing a mathematical function}
957 {Comma-separated list of variables, possibly with superscripts}
958 \begin{MacroCode}{package}
959 \cs_new_nopar:Npn\__skmath_pd_subscript:nn#1#2{
960 {#1}\c_math_subscript_token{
961 \__skmath_pd_subscript_vars:n{#2}
962 }
963 }
964 \end{MacroCode}
965 \end{macro*}
966 \begin{macro*}{\__skmath_td_fraction:nn}[2]
967 {Tokens representing a mathematical function}
968 {A single variable, possibly with a superscript}
969 \begin{MacroCode}{package}
970 \cs_new_nopar:Npn\__skmath_td_fraction:nn#1#2{
971 \frac{
972 \__skmath_total_derivative_d:
973 \__skmath_pd_if_vars_sum_above_one:nT{#2}{
974 \c_math_superscript_token{\__skmath_pd_vars_sum:n{#2}}
975 }
976 {#1}
977 }{
978 \__skmath_total_derivative_d:
979 #2
980 }
981 }
982 \end{MacroCode}
983 \end{macro*}
984
985 Definition of \cs{bar}, \cs{pd}, \cs{td} and \cs{d} is deferred until after all
986 packages are loaded to avoid collisions with other packages.
987 \begin{MacroCode}{package}
988 \AtBeginDocument{%
989 \end{MacroCode}
990 The \cs{bar} command is modified to impove typesetting.
991 \begin{macro}{\bar}
992 \changes{0.1b}{Added \cs{bar} replacement}
993 \changes{0.1h}{Wrap in \cs{AtBeginDocument}}
994 \begin{MacroCode}{package}
995 \DeclareDocumentCommand\bar{m}{%
996 \ensuremath{\mkern 1.5mu\overline{\mkern-1.5mu{#1}\mkern-1.5mu}\mkern 1.5mu}}
997 \end{MacroCode}
998 \end{macro}
999
1000 This is the partial derivative macro, but most of the functionality
1001 was defined as private macros earlier.
1002 \begin{macro}{\pd}[3]
1003 {Boolean distinguishing between starred and unstarred variant}
1004 {Tokens representing a mathematical function}
1005 {Comma-separated list of variables, possibly with superscripts}
1006 \begin{MacroCode}{package}
1007 \DeclareDocumentCommand\pd{smm}{
1008 \ensuremath{
1009 \IfBooleanTF{#1}
1010 {\__skmath_pd_subscript:nn{#2}{#3}}
1011 {\__skmath_pd_fraction:nn{#2}{#3}}
1012 }
1013 }
1014 \end{MacroCode}
1015 \end{macro}
1016
1017 We also have the total derivative macro,
1018 \begin{macro}{\td}[2]
1019 {Tokens representing a mathematical function}
1020 {A single variable, possibly with a superscript}
1021 \begin{MacroCode}{package}
1022 \DeclareDocumentCommand\td{mm}{
1023 \ensuremath{
1024 \__skmath_td_fraction:nn{#1}{#2}
1025 }
1026 }
1027 \end{MacroCode}
1028 \end{macro}
1029
1030 We introduce a command to typeset the differential part
1031 of integrals, shamefully stolen from an answer on \TeX.SE.
1032 \begin{macro}{\d}
1033 \changes{0.1a}{Fixed obtuse errors}
1034 \changes{0.1b}{Moved to \textsf{xparse} command definition}
1035 \changes{0.2}{Use \cs{peek_meaning_ignore_spaces:NT} instead of \cs{@ifnextchar}}
1036 \changes{0.3b}{Use \cs{operator@font} instead of \cs{mathrm}}
1037 \begin{MacroCode}{package}
1038 \DeclareDocumentCommand\d{m}{\ensuremath{\,\__skmath_integral_d: #1%
1039 \peek_meaning_ignore_spaces:NT\d{\!}}}
1040 \end{MacroCode}
1041 \end{macro}
1042 \begin{MacroCode}{package}
1043 }
1044 \end{MacroCode}
1045
1046 Finally, we define a nicer way to denote vectors.
1047 \begin{macro}{\vec}
1048 \changes{0.2}{Use \cs{cs_new_eq:NN} instead of \cs{let}}
1049 \begin{MacroCode}{package}
1050 \cs_set_eq:NN\vec\vectorsym
1051 \end{MacroCode}
1052 \end{macro}
1053
1054 \begin{MacroCode}{package}
1055 \endinput
1056 \end{MacroCode}
1057
1058 \Finale
1059 \section{Installation}
1060 The easiest way to install this package is using the package
1061 manager provided by your \LaTeX\ installation if such a program
1062 is available. Failing that, provided you have obtained the package
1063 source (\file{skmath.tex} and \file{Makefile}) from either CTAN
1064 or Github, running \texttt{make install} inside the source directory
1065 works well. This will extract the documentation and code from
1066 \file{skmath.tex}, install all files into the TDS tree at
1067 \texttt{TEXMFHOME} and run \texttt{mktexlsr}.
1068
1069 If you want to extract code and documentation without installing
1070 the package, run \texttt{make all} instead. If you insist on not
1071 using \texttt{make}, remember that packages distributed using
1072 \pkg{skdoc} must be extracted using \texttt{pdflatex}, \emph{not}
1073 \texttt{tex} or \texttt{latex}.
1074
1075 \PrintChanges
1076 \PrintIndex
1077 \end{document}