summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/doc/latex/diffcoeff/diffcoeff.tex
blob: c8df6968c63b1f217641062905fa665c0859929d (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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
%% LyX 2.3.1-1 created this file.  For more info, see http://www.lyx.org/.
%% Do not edit unless you really know what you are doing.
\documentclass[english]{article}
\usepackage{lmodern}
\renewcommand{\sfdefault}{lmss}
\renewcommand{\ttdefault}{lmtt}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}
\setcounter{secnumdepth}{2}
\setcounter{tocdepth}{2}
\usepackage{booktabs}
\usepackage{amstext}

\makeatletter

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% LyX specific LaTeX commands.
%% Because html converters don't know tabularnewline
\providecommand{\tabularnewline}{\\}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Textclass specific LaTeX commands.
\newenvironment{example}%
{\begin{center}\ttfamily}{\end{center}}
\newenvironment{lyxcode}
	{\par\begin{list}{}{
		\setlength{\rightmargin}{\leftmargin}
		\setlength{\listparindent}{0pt}% needed for AMS classes
		\raggedright
		\setlength{\itemsep}{0pt}
		\setlength{\parsep}{0pt}
		\normalfont\ttfamily}%
	 \item[]}
	{\end{list}}
\providecommand*{\strong}[1]{\textbf{#1}}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% User specified LaTeX commands.
\usepackage{diffcoeff}

\@ifundefined{showcaptionsetup}{}{%
 \PassOptionsToPackage{caption=false}{subfig}}
\usepackage{subfig}
\makeatother

\usepackage{babel}
\begin{document}
\title{\texttt{diffcoeff}~\\
a \LaTeX{} package for writing\texttt{}~\\
differential coefficients easily}
\author{Andrew Parsloe\\
{\small{}(ajparsloe@gmail.com)}}
\maketitle
\begin{abstract}
\noindent \texttt{diffcoeff.sty} allows the easy writing of ordinary and
partial differential coefficients of arbitrary (algebraic or numeric) order.
For mixed partial derivatives, the overall order (the superscript on $\partial$
in the numerator) is calculated by the package. Optional arguments allow
the easy specification of a point of evaluation for ordinary derivatives,
or variables held constant for partial derivatives, and the placement of
the differentiand (in the numerator or appended). Some tweaking of the
display is possible through key = value settings. Secondary commands provide
analogous coefficients constructed from $D,\thinspace\Delta,$ and $\delta$,
and a command for writing Jacobians. The package uses \texttt{expl3} and
\texttt{xparse} from the \LaTeX 3 bundles, \texttt{l3kernel} and \texttt{l3packages}.
\end{abstract}

\section{Version}

Version 1.1 of the \texttt{diffcoeff} package changes the handling of error
messages which was just plain wrong in version 1 and caused a conflict
with the \texttt{mhchem} package. 

\texttt{diffcoeff.sty} had a `big brother' \texttt{diffcoeffx.sty} that
was `\texttt{diffcoeff} on steroids'. However a bug in \texttt{diffcoeffx}
means it didn't do all it claimed and it will be withdrawn. 

\section{Requirements}

The \LaTeX{} package \texttt{diffcoeff.sty} is written in the expl3 language
of \LaTeX 3\texttt{ }and requires the bundles \texttt{l3kernel} and \texttt{l3packages}
(the latter for the \texttt{xparse} module). However, granted the presence
of these bundles in your \TeX{} distribution, the \LaTeX 3 element should
be invisible to the user. 

The package is invoked in the usual way by entering
\begin{lyxcode}
\textbackslash usepackage\{diffcoeff\}
\end{lyxcode}
in the preamble of your document.

\paragraph{Note on terminology}

I refer throughout to the quantity or function being differentiated as
the \emph{differentiand} (in line with \emph{integrand}, \emph{operand},
etc.).

\section{Ordinary differential coefficients \label{sec:Ordinary-differential-coefficien}}

Writing\textbf{ }\texttt{\textbackslash diff\{y\}\{x\}} will produce $\diff{y}{x}$
in text style (i.e. placed between \texttt{\$ \$}) or 
\[
\diff{y}{x}
\]
in display style (i.e. placed between \texttt{\textbackslash{[} \textbackslash{]}}
). In fact \texttt{\textbackslash diff yx} (omitting the braces) will
produce these results, with a saving on keystrokes. The braces are needed
only when differentiand or variable of differentiation is more than a single
token.

There is one other form: we can insert a slash, `/', between numerator
and denominator: \texttt{\textbackslash diff f/x} produces $\diff f/x$
which may be preferred for textstyle differential coefficients on occasion.
Nothing is gained in this particular instance. It is quicker to type the
five keystrokes d, f, /, d, x than it is to type the nine of \textbackslash ,
d, i, f, f, , f, /, x but there are occasions when this is not always the
case. 

An optional first argument allows the order of differentiation to be specified.
The order need not be a number; an algebraic order of differentiation is
perfectly acceptable or, indeed, a mix:
\begin{example}
\textbackslash diff{[}2{]}\{y\}\{x\} $\Longrightarrow\quad{\displaystyle \diff[2]{y}{x},}$\medskip{}

\textbackslash diff{[}n+1{]}\{y\}\{x\} $\Longrightarrow\quad{\displaystyle \diff[n+1]{y}{x}}.$
\end{example}

(And again the braces can be omitted for single letters like \textbf{x}
and \textbf{y}.)

In slash style, \texttt{\textbackslash diff{[}2{]}f/x} (11 keystrokes)
produces $\diff[2]f/x$, not significantly more typing than \texttt{d\textasciicircum 2f/dx\textasciicircum 2}
(9 keystrokes).

If you want to specify a point at which the derivative is evaluated, append
a final optional argument, but note that it is given in \emph{braces} rather
than square brackets:
\begin{example}
\textbackslash diff{[}2{]}\{y\}\{x\}\{0\} $\Longrightarrow\quad{\displaystyle \diff[2]{y}{x}{0}}$
\end{example}

\noindent (In this example it seems neater \emph{not} to finish with a
full stop or other punctuation.) The use of braces means that the differential
coefficient can be followed immediately by a mathematical expression wrapped
in \texttt{\textbackslash\{ \textbackslash\}}, or \texttt{{[} {]}},
without the expression being confused with the (final) optional argument.
Note also that there must be \emph{no space} before the argument: it follows
\emph{immediately} on the second mandatory argument (if it follows at all). 

We could save a few keystrokes by writing this last example as \texttt{\textbackslash diff{[}2{]}yx\{0\}}.
The braces around the final optional argument can \emph{not} be omitted
\textendash{} otherwise there is no way of knowing that it \emph{is }the
final optional argument and not part of a following expression.

In slash style, the trailing optional argument can be used, but perhaps
should not be. It looks ugly: 
\begin{example}
\textbackslash diff{[}2{]}y/x\{0\} $\Longrightarrow\quad{\displaystyle \diff[2]{y}/{x}{0}}$
\end{example}

Slash style is a more casual rendering of the derivative, intended for
inline use within text and it would be better to use a phrase like `evaluated
at zero'.

\subsection{\textbackslash diffset: formatting tweaks}

There are a number of tweaks one can make to the display of a derivative.
Many people now use upright (roman) forms for the `d's of a differential
coefficient, rather than math italic. To do this, put the command 
\begin{example}
{\footnotesize{}\textbackslash}diffset{[}roman = true{]}
\end{example}

\noindent in the preamble of your document (following the \texttt{\textbackslash usepackage\{diffcoeff\}}
of course). The default is math italic.

It is possible that you may want more space between the `d' in the numerator
of a differential coefficient and the superscripted order of the derivative.
Using an upright `d' alleviates this problem, but if using the default
math italic for the `d's, the separation can be altered by using the 
\begin{example}
\textbackslash diffset{[}d-sep = $n${]}
\end{example}

\noindent command which adds an extra $n$~mu to \TeX 's spacing. The
default value for $n$ is 1 (i.e. 1~mu). The new separation will affect
all derivatives following the new setting. Put in the preamble, the new
separation will be document-wide.

A third tweak changes the delimiters used to indicate the point of evaluation.
By default there is nothing on the left side and a vertical rule with the
point of evaluation subscripted to it on the right. You may prefer subscripted
parentheses. In that case write
\begin{example}
\textbackslash diffset{[}d-delims~=~(){]}\textmd{.}
\end{example}

Whatever delimiters you choose need to work with \LaTeX 's \texttt{\textbackslash left}
and \texttt{\textbackslash right} commands and consist of exactly two
tokens. \texttt{{[}} and \texttt{{]}}\textbf{ }are acceptable as also are
pairs like \texttt{\textbackslash lceil \textbackslash rceil}, \texttt{\textbackslash lfloor
\textbackslash rfloor} but if you want to use \texttt{\textbackslash\{}\textbf{
}and\textbf{ }\texttt{\textbackslash\}} you need to place the \texttt{\textbackslash diffset}
command between maths delimiters. The default pair, as indicated, is \texttt{. |},
t or full stop being \LaTeX 's way of suppressing (in this case) the left
delimiter. 

If you change the delimiters, say to \textbf{( )}, then the position of
the subscript may need adjusting. To do this, use the command
\begin{example}
\textbackslash diffset{[}d-nudge = $n${]}
\end{example}

A suggested setting for parentheses \textbf{( )} is $-6$ (in fact $-6$~mu
but the `mu' is supplied by \texttt{diffcoeff}). Thus the total change
would be
\begin{example}
\textbackslash diffset{[}d-delims = ( ), d-nudge = -6{]}
\end{example}

producing, for example,
\begin{example}
\textbackslash diff{[}n{]}\{y\}\{x\}\{0\} $\diffset[d-delims=(),d-nudge=-6]\Longrightarrow\quad{\displaystyle \diff[n]{y}{x}{0}}.$
\end{example}

The default setting for \textbf{. |} is 0. Simply writing
\begin{example}
\textbackslash diffset $\diffset$
\end{example}

will return all settings to their defaults.

\subsection{Variations}

\subsubsection{Appending the differentiand: \textbackslash diff{*}}

If you want the differentiand to follow the differential coefficient rather
than sit in the numerator, perhaps because it is a fraction itself or because
it is long, like a polynomial ($ax^{2}+bx+c$), then one way to achieve
that is to leave the first mandatory argument in the \texttt{\textbackslash diff}
command empty and immediately follow the differential operator with the
differentiand:
\begin{example}
\textbackslash diff\{\}\{x\}(ax\textasciicircum 2+bx+c) $\Longrightarrow\quad{\displaystyle \diff{}{x}(ax^{2}+bx+c)}.$
\end{example}

Another is to use the star form of the \texttt{\textbackslash diff }command,
\begin{example}
\textbackslash diff{*}{[}2{]}\{\textbackslash frac\{F(x)\}\{G(x)\}\}\{x\}
$\Longrightarrow\quad{\displaystyle \diff*[2]{\frac{F(x)}{G(x)}}{x}.}$ 
\end{example}

\noindent The LaTeX expression can be harder to read if, as here, one is
using a command like \texttt{\textbackslash frac} with its own pairs of
braces, but it is much easier, if one isn't sure whether the differentiand
should be appended or in the numerator, simply to insert or delete an asterisk
than move the differentiand from one place to the other. The star form
becomes especially useful if you want to both append the differentiand
\emph{and }indicate the point of evaluation, since it saves having to set
up the \texttt{\textbackslash left.} and \texttt{\textbackslash right|}\textbf{
}delimiters and the subscript:
\begin{example}
\textbackslash diff{*}\{\textbackslash frac\{F(x)\}\{G(x)\}\}\{x\}\{0\}
$\Longrightarrow\quad{\displaystyle \diffset[d-delims=.|,d-nudge=0]\diff*{\frac{F(x)}{G(x)}}{x}{0}}$ 
\end{example}

In slash style with the star option, an example above becomes
\begin{example}
\textbackslash diff{*}\{(ax\textasciicircum 2+bx+c)\}/\{x\} $\Longrightarrow\quad\text{\ensuremath{{\displaystyle \diff*{(ax^{2}+bx+c)}/{x}}}}$, 
\end{example}

where the derivative is automatically enclosed in parentheses by \texttt{diffcoeff}.

\subsubsection{Multi-character variables of differentiation}

Derivatives of a function-of-a-function may require forming a differential
coefficient in which the variable of differentiation is more complicated
than a single symbol like \texttt{x} or \texttt{\textbackslash alpha}.
For instance, to differentiate $\ln x^{2}$ (the logarithm of $x^{2}$)
one first differentiates in $x^{2}$ then in $x$. The initial differentiation
can be rendered
\begin{example}
\textbackslash diff\{\textbackslash ln x\textasciicircum 2\}\{x\textasciicircum 2\}
$\Longrightarrow\quad{\displaystyle \diff{\ln x^{2}}{x^{2}}}$; \medskip{}

diff\{\textbackslash ln x\textasciicircum 2\}/\{x\textasciicircum 2\}
$\Longrightarrow\quad{\displaystyle \diff{\ln x^{2}}/{x^{2}}}.$ 
\end{example}

\noindent Because of the superscript in the variable of differentiation
$x^{2}$, parentheses have been automatically inserted in the denominator.
This does not happen in a first-order derivative unless there is a superscript
present. For instance,
\begin{example}
\textbackslash diff\{\textbackslash ln\textbackslash sin x\}\{\textbackslash sin
x\} $\Longrightarrow\quad{\displaystyle \diff{\ln\sin x}{\sin x}.}$ 
\end{example}

\noindent displays without parentheses. However, for higher order derivatives
parentheses are \emph{always} inserted to avoid confusion:
\begin{example}
\textbackslash diff{[}2{]}\{\textbackslash ln\textbackslash sin x\}\{\textbackslash sin
x\} $\Longrightarrow\quad{\displaystyle \diff[2]{\ln\sin x}{\sin x}.}$ 
\end{example}


\paragraph{Positioning the d in the numerator}

When appending a differentiand, you may want to change the position of
the `d' in the numerator, particularly if the variable of differentiation
is a multi-character symbol or the order of differentiation is a multi-character
value like $n+1$. 

If you `manually' append the differentiand, then there are various ways
of altering the placement of the `d' from the default midpoint: use \texttt{\textbackslash hfill}
to push it hard to the left; use \texttt{\textbackslash hfil} to\textbf{
}push it to the left an intermediate amount; use \texttt{\textbackslash hphantom}
or \texttt{\textbackslash hspace}, both with a braced argument, to push
it to the left some custom amount; use \texttt{\textbackslash hspace}\textbf{
}with a \emph{negative} braced argument to push it to the right.\emph{
}These same means can be used to shift the `d' when using the starred
form of \texttt{\textbackslash diff}.\textbf{ }The effect is exactly the
same, too:
\begin{example}
\textbackslash diff{[}n+1{]}\{\textbackslash hphantom\{\textbackslash sin
x\}\}\{\textbackslash sin x\}\textbackslash ln\textbackslash sin x $\Longrightarrow\quad{\displaystyle \diff[n+1]{\hphantom{\sin x}}{\sin x}\ln\sin x},$\medskip{}

\textbackslash diff{*}{[}n+1{]}\{\textbackslash hphantom\{\textbackslash sin
x\}\textbackslash ln\textbackslash sin x\}\{\textbackslash sin x\} $\Longrightarrow\quad{\displaystyle \diff*[n+1]{\hphantom{\sin x}\ln\sin x}{\sin x}}.$
\end{example}

\noindent In the starred form \texttt{diffcoeff} understands that the formatting
is not appended with the differentiand but stays in the numerator. (But
a \emph{second} \texttt{\textbackslash hphantom} or \texttt{\textbackslash hfil}
etc. would be appended.) These are to be compared with
\begin{example}
\textbackslash diff{*}{[}n+1{]}\{\textbackslash ln\textbackslash sin
x\}\{\textbackslash sin x\} $\Longrightarrow\quad{\displaystyle \diff*[n+1]{\ln\sin x}{\sin x},}$
\end{example}

where no phantom has been used. Which is better? Deleting the asterisk
gives
\begin{example}
\textbackslash diff{[}n+1{]}\{\textbackslash ln\textbackslash sin x\}\{\textbackslash sin
x\} $\Longrightarrow\quad{\displaystyle \diff[n+1]{\ln\sin x}{\sin x},}$
\end{example}

In slash style, the phantom (or \texttt{\textbackslash hfil} etc.) is
ignored:
\begin{example}
\textbackslash diff{*}{[}n+1{]}\{\textbackslash hphantom\{\textbackslash sin
x\textbackslash sin x\textbackslash sin x\}\textbackslash ln\textbackslash sin
x\}/\{\textbackslash sin x\} $\Longrightarrow\quad{\displaystyle \diff*[n+1]{\hphantom{\sin x\sin x\sin x}\ln\sin x}/{\sin x}}.$
\end{example}


\subsubsection{Iterated derivatives}

A second derivative is an iterated derivative, i.e., one in which a differential
coefficient forms the differentiand of another differential coefficient:
\begin{example}
\textbackslash diff{[}2{]}yx = \textbackslash diff{*}\{\textbackslash diff
yx\}x $\Longrightarrow{\displaystyle \diff[2]yx=\diff*{\diff yx}x},$
\end{example}

or even
\begin{example}
\textbackslash diff{[}2{]}yx = \textbackslash diff\{\textbackslash diff
yx\}x $\Longrightarrow{\displaystyle \diff[2]yx=\diff{\diff yx}x},$
\end{example}

where omission of unnecessary braces has aided readability. Note how easy
it is to switch between the different forms on the right, simply by inserting
or removing an asterisk. 

\subsection{Forming `derivatives' with D, \textbackslash Delta, \textbackslash delta}

Often one wants to construct analogues of a differential coefficient but
with symbols other than $d$ or $\partial$. The \texttt{diffcoeff} package
offers three alternatives, all with the same pattern of optional and mandatory
arguments as for \texttt{\textbackslash diff}, except for the slash form.
There is \emph{no} slash option.

An uppercase $D$ is used in place of $d$ for the \emph{material} or \emph{substantive}
derivative of a quantity in (for example) fluid dynamics. Write \texttt{\textbackslash Diff}
to invoke this command:\footnote{The \texttt{\textbackslash diffp} command, the partial derivative, in
the example is discussed in the next section.}
\begin{example}
\textbackslash Diff\{\textbackslash rho\}\{t\}=\textbackslash diffp\textbackslash rho
t + \textbackslash mathbf\{u\textbackslash cdot\}\textbackslash nabla\textbackslash rho
$\Longrightarrow{\displaystyle \Diff{\rho}{t}=\diffp\rho t+\mathbf{u\cdot}\nabla\rho.}$ 
\end{example}

(The braces could also be removed from the arguments of \texttt{\textbackslash Diff}
as they have been from the arguments of \texttt{\textbackslash diffp}.)\texttt{ }

The `D's are romanised (along with the `d's of ordinary derivatives)
with the 
\begin{example}
\textbackslash diffset{[}roman = true{]}
\end{example}

command. The default is math italic.

The command \texttt{\textbackslash diffd} will form a fraction often used
in introductory calculus texts (and other places):\footnote{I considered using \texttt{\textbackslash diffg} for this command as in
`diff greek' but decided that the more likely mind-phrase is `diff delta',
leading to the use of `d' rather than `g'.}
\begin{example}
\textbackslash diffd\{y\}\{x\} $\Longrightarrow{\displaystyle \diffd yx.}$ 
\end{example}

Similarly, \texttt{\textbackslash Diffd} forms a fraction with $\Delta$:
\begin{example}
\textbackslash Diffd\{y\}\{x\} $\Longrightarrow{\displaystyle \Diffd{y}{x}.}$ 
\end{example}

Higher order forms of these derivatives are produced in the same way as
with \texttt{\textbackslash diff}, using an optional argument to specify
the order:
\begin{example}
\textbackslash diffd{[}2{]}\{y\}\{x\} $\Longrightarrow{\displaystyle \diffd[2]yx.}$ 
\end{example}

A final optional argument, enclosed in braces, specifies a point of evaluation,
care being taken, as ever, to ensure that there is no space between it
and the second mandatory argument:
\begin{example}
\textbackslash Diffd\{y\}\{x\}\{x=0\} $\Longrightarrow{\displaystyle \Diffd yx{x=0}.}$ 
\end{example}


\section{Partial differential coefficients\label{sec:Partial-differential-coefficient}}
\noindent \begin{flushleft}
Partial differential coefficients follow the same pattern as for ordinary
derivatives, with some generalisations arising from the greater possibilities.
The command this time is \texttt{\textbackslash diffp}. Thus \textbf{\textbackslash diffp\{F\}\{x\}}
produces $\diffp{F}{x}$ in text style and 
\[
\diffp{F}{x}
\]
 in display style. Braces can be omitted for single token differentiands
and variables: \texttt{\textbackslash diffp Fx} does the job.\textbf{
}As for \texttt{\textbackslash diff}, there is a slash form for more casual
use: \texttt{\textbackslash diffp F/x} displaying as $\diffp F/x$. Given
that \texttt{\textbackslash partial} takes 8 keystrokes to type, the slash
form \emph{does }economise on keystrokes for a partial derivative.
\par\end{flushleft}

\begin{flushleft}
Again an optional argument allows the specification of the order of differentiation
and it may be numeric or algebraic or a mix of the two. For a second or,
indeed, an $n+4$th-order partial derivative, 
\par\end{flushleft}
\begin{example}
\textbackslash diffp{[}n+4{]}\{F\}\{x\} $\Longrightarrow\quad{\displaystyle {\displaystyle \diffp[n+4]{F}{x},}}$\medskip{}

\textbackslash diffp{[}n+4{]}\{F\}/\{x\} $\Longrightarrow\quad{\displaystyle {\displaystyle \diffp[n+4]{F}/{x},}}$
\end{example}

In a subject like thermodynamics, there is a need to indicate which variable
or variables are held constant when the differentiation occurs. To show
this, append a final optional argument. Thus to differentiate the entropy
$S$ in the temperature $T$ while holding the volume $V$ constant, write
\begin{example}
\textbackslash diffp\{S\}\{T\}\{V\} $\Longrightarrow\quad{\displaystyle \diffp{S}{T}{V}}$
\end{example}

As with \texttt{\textbackslash diff}\textbf{ }note how the final optional
argument is given in braces rather than square brackets, and that there
must be \emph{no space} before the argument: if used, it follows \emph{immediately}
on the second mandatory argument. This means that the differential coefficient
can be followed immediately by a mathematical expression wrapped in \textbackslash\{
\textbackslash\}, or {[} {]}, without the expression being confused with
the (final) optional argument.

We could save a few keystrokes by writing this last example as \texttt{\textbackslash diffp
ST\{V\}}. The braces around the optional argument can \emph{not} be dispensed
with (otherwise there is no way of knowing that it \emph{is} the final
optional argument and not part of a following expression). 

Note that for the slash form of the derivative it is anticipated that there
will be no trailing optional argument. If you \emph{do} use one, you will
need to change the nudge value either with the \texttt{\textbackslash diffset}
command or, better, by including a spacing command in the third argument:
\begin{example}
\textbackslash diffp\{S\}/\{T\}\{\textbackslash ;V\} $\Longrightarrow\quad{\displaystyle \diffp{S}/{T}{\;V}}$
\end{example}

Without the spacing command, the subscript encroaches on the right parenthesis.

\subsubsection{Appending the differentiand}

If you want to remove the differentiand from the numerator to instead follow
the derivative, one way, as for ordinary derivatives, is to leave the first
mandatory argument empty and manually append the differentiand:
\begin{example}
\textbackslash diffp{[}n{]}\{\}xf(x) $\Longrightarrow\quad{\displaystyle \diffp[n]{}xf(x).}$
\end{example}

However, you may wonder how that would look with the differentiand in the
numerator, which is a good reason for preferring the starred form of the
\texttt{\textbackslash diffp} command to achieve an appended derivative:
\begin{example}
\textbackslash diffp{*}{[}n{]}\{f(x)\}x $\Longrightarrow\quad{\displaystyle \diffp*[n]{f(x)}x.}$
\end{example}

Now it is easy to switch between an appended differentiand and one in the
numerator simply by inserting or deleting the asterisk. In the slash form,
parentheses are automatically inserted around the differential operator:
\begin{example}
\textbackslash diffp{*}{[}n{]}\{f(x)\}/x $\Longrightarrow\quad{\displaystyle \diffp*[n]{f(x)}/x.}$
\end{example}

It also happens, for example in thermodynamics, that you may wish to both
append the differentiand \emph{and} indicate variables held constant. In
that case, the starred \texttt{\textbackslash diffp} command is much easier
to use. Thus, to express a relation in thermodynamics,
\begin{example}
\textbackslash diffp{*}\{\textbackslash frac \{P\}\{T\}\}\{U\}\{V\} =
\textbackslash diffp{*}\{\textbackslash frac\{1\}\{T\}\}\{V\}\{U\} $\Longrightarrow\quad{\displaystyle \diffp*{\frac{P}{T}}{U}{V}=\diffp*{\frac{1}{T}}{V}{U}}$
\end{example}

\noindent where the starred form automatically takes care of the parentheses
and subscripts. Again, not all the braces are necessary, with some help
to readability:
\begin{example}
\textbackslash diffp{*}\{\textbackslash frac PT\}U\{V\} = \textbackslash diffp{*}\{\textbackslash frac
1T\}V\{U\} $\Longrightarrow\quad{\displaystyle \diffp*{\frac{P}{T}}U{V}=\diffp*{\frac{1}{T}}V{U}}$
\end{example}


\subsection{Mixed partial derivatives}

The new thing with partial derivatives, not present with ordinary derivatives,
is \emph{mixed} partial derivatives, where there is more than one variable
of differentiation. If each variable is differentiated only to the first
order, then it is easy to specify the derivative. Say $f(x,y,z)$ is a
function of three variables, as indicated. Then
\begin{example}
\textbackslash diffp\{f\}\{x\c{,}y,z\} $\Longrightarrow\quad{\displaystyle \diffp{f}{x,y,z}}.$
\end{example}

The variables of differentiation are listed in order in a comma list forming
the second mandatory argument. The total order of differentiation (3 in
this example) is inserted automatically \textendash{} \texttt{diffcoeff}
does the calculation itself. There is also a slash form:
\begin{example}
\textbackslash diffp\{f\}/\{x\c{,}y,z\} $\Longrightarrow\quad{\displaystyle \diffp{f}/{x,y,z}}.$
\end{example}

If we want to differentiate variables to higher order, then their orders
need to be specified explicitly. To do so use a comma list also in the
\emph{optional} argument:
\begin{example}
\textbackslash diffp{[}2,3{]}\{f\}\{x,y,z\} $\Longrightarrow\quad{\displaystyle \diffp[2,3]{f}{x,y,z}.}$
\end{example}

\noindent Notice that the overall order of the derivative \textendash{}
6 \textendash{} is again automatically calculated and inserted as a superscript
on the $\partial$ symbol in the numerator. In this example, the comma
list of orders has only two members, even though there are three variables.
It is assumed that the orders given in the comma list of orders apply in
sequence to the variables, the first order to the first variable, the second
to the second variable, and so on, and that any subsequent orders not listed
in the optional argument are, by default, 1. Thus we need to specify only
2 and 3 in the example; the order of $z$ is 1 by default.

But you \emph{cannot} use an order specification like \texttt{{[},,2{]}}.
This will be treated as if it were \texttt{{[}2{]}}. (This is a feature
of comma lists in the expl3 language used by \texttt{diffcoeff.sty}.) Instead
write \texttt{{[}1,1,2{]}}.\textbf{ }It is only the \emph{tail} of an order
specification which can be omitted.

The automatic calculation of the overall order of differentiation remains
true even when some or all of the orders for the individual variables are
algebraic. For example, differentiating in three variables with orders
\texttt{2k}, \texttt{m-k-2}, \texttt{m+k+3}, we have 
\begin{example}
\textbackslash diffp{[}2k-1,m-k-2,m+k+3{]}\{F(x,y,z)\}\{x,y,z\} $\Longrightarrow\quad{\displaystyle \diffp[2k-1,m-k-2,m+k+3]{F(x,y,z)}{x,y,z}}$,
\end{example}


\subsection{The order-override option}

In this example the overall order is presented as \texttt{2k+2m}. You might
prefer this to be presented as \texttt{2(k+m)} instead. Although \texttt{diffcoeff}
takes some steps to present the overall order appropriately, it does not
factorise expressions. If you want to present the order in a manner distinct
from that of \texttt{diffcoeff}, use the\emph{ order-override option},
which is a second optional argument immediately following the first:
\begin{example}
\textbackslash diffp{[}2k-1,m-k-2,m+k+3{]}{[}2(k+m){]}\{F(x,y,z)\}\{x,y,z\}
$\Longrightarrow\quad{\displaystyle \diffp[2k-1,m-k-2,m+k+3][2(m+k)]{F(x,y,z)}{x,y,z}}$.
\end{example}

The order-override option does exactly that: overrides the presentation
of the calculated order with the manually given one. (In fact the algorithm
does not get called at all.)

\subsubsection{Order specifications beyond the scope of \texttt{diffcoeff.sty}}

The order specification can include signed integers, variables like $k$
and $\alpha$ with signed integer coeffients, and products of any number
of variables like $mn$ or $kmn$ with signed integer coefficients. The
algorithm that calculates the overall order in \texttt{diffcoeff.sty} \emph{cannot}
handle\texttt{ }exponents, subscripts or parentheses. For such constructs,
or more exotic ones, the order-override option is always available. If
it is present (even if empty), the algorithm is bypassed completely and
one can include `anything' there without causing error.

I doubt that these limitations matter in any practical sense. We are in
`overkill' territory here. Mixed partial derivatives are used far more
rarely than the `pure' ones, and mixed partial derivatives to `exotic'
orders of differentiation are used \emph{vanishingly} rarely, and in any
case the order-override option is always available. But should you, in
some freak circumstance, find yourself needing to write such things, then
I suggest you use \texttt{diffcoeffx.sty}, which is \texttt{diffcoeff.sty}
`on steroids'. It can handle the situations described above that are
beyond the scope of \texttt{diffcoeff.sty}, and it uses exactly the same
commands so there is nothing new to remember. It also provides additonal
functionality for the trailing optional argument.

\subsubsection{Presentation of the overall order}

To take a grotesque example, that will never arise in practice, consider
the following: 
\begin{example}
\textbackslash diffp{[}kmn-mn+n-1,2kmn-mn+2n-1,n+1{]}\{f\}\{x,y,z,w\}
$\Longrightarrow{\displaystyle \diffp[kmn-mn+n-1,2kmn-mn+2n-1,n+1]{f}{x,y,z,w}}.$
\end{example}

As noted earlier, since the final variable $w$ is differentiated only
to order 1, there is no need to specify it in the comma list of orders.
The implicit 1 contributes to the vanishing of the numerical part in the
overall order of differentiation. In this example, the overall order contains
multivariable terms, $kmn$ and $mn$. \texttt{diffcoeff} initially organises
these in the sequence: \ldots{} 3-variable terms before 2-variable terms
before single-variable terms, generally before the numerical term. However
if a minus sign precedes the first many-variable term, and the numerical
term is positive, it will be presented first:
\begin{example}
\textbackslash diffp{[}12-2km,k-1,km+1{]}\{f\}\{x,y,z,w\} $\Longrightarrow{\displaystyle \diffp[12-2km,km-1,k+1]{f}{x,y,z,w}}.$
\end{example}

Should the numerical term either vanish or be negative and the leading
algebraic term is preceded by a minus sign, \texttt{diffcoeff} will look
for an algebraic term with a preceding $+$ sign and put that first:
\begin{example}
\textbackslash diffp{[}2km-3k-1,2k-1,-3km+4k+1{]}\{f\}\{x,y,z,w\} $\Longrightarrow{\displaystyle \diffp[2km-3k-1,2k-1,-3km+4k+1]{f}{x,y,z,w}}.$
\end{example}


\subsection{\textbackslash diffset: formatting tweaks}

As with ordinary derivatives, there are a number of tweaks one can make
to the display of a partial derivative. 

You may want more space between the $\partial$ symbol in the numerator
of a partial derivative and the superscripted order of the derivative.
The separation can be altered by using the 
\begin{example}
\textbackslash diffset{[}p-sep = $n${]}
\end{example}

\noindent command which adds an extra $n$~mu to \TeX 's spacing. The
default value is 1 (i.e. 1~mu). The new separation will affect all derivatives
following the new setting. Put in the preamble, the new separation will
be document-wide.

You may also want to adjust the spacing between the terms in the denominator.
This can be done with the command
\begin{example}
\textbackslash diffset{[}sep=$n${]}
\end{example}

which adds an extra $n$~mu to \TeX 's spacing. The default value is 2~mu.

If you wish to indicate the point at which a partial derivative is evaluated,
you may not want to use parentheses, since these when subscripted are widely
held to indicate variables held constant. To change the delimiter on the
right to a vertical line, use
\begin{example}
\textbackslash diffset{[}p-delims = . | {]}\textmd{,}
\end{example}

the dot suppressing the delimiter on the left. (Note that to use \texttt{\textbackslash\{}
and \texttt{\textbackslash\}} as delimiters, \texttt{\textbackslash diffset
}must be placed between maths delimiters.) 

Changing the delimiters will usually require a repositioning of the subscript.
The command is
\begin{example}
\textbackslash diffset{[}p-nudge = $n${]}\textmd{.}
\end{example}

For parentheses the default value of $n$ is $-6$, but for the vertical
rule a zero value is appropriate. Thus the overall command for . | would
be
\begin{example}
\textbackslash diffset{[}p-delims = . |, p-nudge = 0{]} \textmd{.}
\end{example}

Writing 
\begin{example}
\textbackslash diffset
\end{example}

will return all settings to their default values.

\subsection{Variations}

\subsubsection{Multi-character variables of differentiation}

In thermodynamics one may want to differentiate in the reciprocal of the
temperature, $1/T$. In tensor calculus the differentiations are almost
always in terms of super- or subscripted coordinates, and in many other
contexts this is the case too. This is why a comma list is used in \texttt{diffcoeff}
for specifying the variables of differentiation for partial derivatives.
Although it would be nice to write the minimal \texttt{\{xy\}} for this
rather than \texttt{\{x,y}\}, the extra writing is trivial and the comma
list allows the simplest handling of multi-character variables:
\begin{example}
\textbackslash diffp\{A\_i\}\{ x\textasciicircum j,x\textasciicircum k
\} $\Longrightarrow{\displaystyle \diffp{A_{i}}{x^{j},x^{k}},}$
\end{example}

taken from tensor calculus, or this strange object taken from statistical
mechanics:
\begin{example}
\textbackslash diffp{[}2{]}q\{\textbackslash frac 1\textbackslash Theta\}
$\Longrightarrow{\displaystyle \diffp[2]q{\frac{1}{\Theta}}}$.
\end{example}

The parentheses have been inserted automatically by \texttt{diffcoeff}
to clarify exactly what the variable of differentiation is.

\subsubsection{Use of phantoms when appending differentiands}

As for ordinary derivatives, when appending a differentiand you may want
to include a phantom (\texttt{\textbackslash hphantom} etc.) in the numerator
of the differential coefficient to alter the placement of the $\partial$
symbol. This may be particularly relevant if the order of differentiation
is a multi-character symbol or if there are a number of variables of differentiation. 

Either means of achieving the appended differentiand achieve the same result:
\begin{example}
\textbackslash diffp{[}m,2{]}\{\textbackslash hphantom\{\textbackslash partial
y \textbackslash partial \}\}\{x,y,z\} (\textbackslash ln \textbackslash cos
x + \textbackslash ln \textbackslash sin y)z $\Longrightarrow\quad{\displaystyle \diffp[m,2]{\hphantom{\partial y\partial}}{x,y,z}}(\ln\cos x+\ln\sin y)z,$\medskip{}

\textbackslash diffp{*}{[}m,2{]}\{\textbackslash hphantom\{\textbackslash partial
y \textbackslash partial \}(\textbackslash ln \textbackslash cos x +
\textbackslash ln \textbackslash sin y)z\}\{x,y,z\} $\Longrightarrow\quad{\displaystyle \diffp*[m,2]{\hphantom{\partial y\partial}(\ln\cos x+\ln\sin y)z}{x,y,z}},$
\end{example}

which is to be compared with the derivative without the phantom,
\begin{example}
\textbackslash diffp{*}{[}m,2{]}\{(\textbackslash ln \textbackslash cos
x + \textbackslash ln \textbackslash sin y)z\}\{x,y,z\} $\Longrightarrow\quad{\displaystyle \diffp*[m,2]{(\ln\cos x+\ln\sin y)z}{x,y,z}}.$
\end{example}

\noindent In the starred form, \texttt{diffcoeff} understands that the
phantom is not appended with the differentiand but stays in the numerator.
(But a \emph{second} phantom would be appended.) 

\subsubsection{Iterated derivatives}

Partial derivatives can be iterated. For example,
\begin{example}
\textbackslash diffp f\{x,y\} = \textbackslash diffp{*}\{\textbackslash diffp
fy\}x $\Longrightarrow{\displaystyle \diffp f{x,y}=\diffp*{\diffp fy}x,}$\medskip{}

\textbackslash diffp f\{x,y\} = \textbackslash diffp\{\textbackslash diffp
fy\}x $\Longrightarrow{\displaystyle \diffp f{x,y}=\diffp{\diffp fy}x.}$
\end{example}

It is easy to switch between these forms by inserting or deleting the asterisk.

\subsection{Jacobians}

\texttt{diffcoeff} provides a command \texttt{\textbackslash jacob} for
constructing Jacobians. For example
\begin{example}
\textbackslash jacob\{u,v,w\}\{x,y,z\} $\Longrightarrow{\displaystyle \jacob{u,v,w}{x,y,z}.}$
\end{example}

The comma lists can contain any number of variables. \texttt{\textbackslash jacob}
does \emph{not} check that the two arguments contain the same number of
variables, so it is perfectly possible to form an object like
\begin{example}
\textbackslash jacob\{u,v,w\}\{x,y\} ,
\end{example}

which as far as I know has no meaning.

\section{Discussion of the code}

I set about creating this package when faced with trying to parse \LaTeX{}
expressions involving derivatives for another program I was working on.
Trying to parse \texttt{\textbackslash frac\{d<something>\}\{d<something
else>\}}, perhaps with \texttt{\textbackslash mathrm\{d\}}'s, and a superscript
on the first \texttt{d}, perhaps with a \texttt{\textbackslash tfrac}
or \texttt{\textbackslash dfrac} for the \texttt{\textbackslash frac},\textbf{
}wasn't quite hopeless, but it was certainly \emph{messy}. (I used regular
expressions to transform the fraction into something more systematic.) 

\subsection{Other packages}

Looking through the MiK\TeX{} distribution and, less assiduously, through
CTAN, produced the following packages which provide macros for derivatives.
(Strangely, AMS packages do not touch this subject, as far as I can see.)
\begin{itemize}
\item \texttt{bropd}
\begin{itemize}
\item \texttt{\textbackslash od{[}n{]}\{y\}\{x\}} and \texttt{\textbackslash pd{[}n{]}\{y\}\{x\}}
for ordinary and partial derivatives of order \texttt{n} in one variable
\item \texttt{\textbackslash pd\{u\}\{x,x,t\}} for a mixed partial derivative,
order 2 in \texttt{x}, 1 in \texttt{t}
\item \texttt{\textbackslash pd\{\}\{z\}\{x+y\}} for appending \texttt{(x+y)}
\item \texttt{\textbackslash pd\{!\}\{z\}\{x+y\}} for appending \texttt{x+y}
\end{itemize}
\item \texttt{commath}
\begin{itemize}
\item \texttt{\textbackslash od{[}n{]}\{y\}\{x\}} and \texttt{\textbackslash pd{[}n{]}\{y\}\{x\}}
for ordinary and partial derivatives of order \texttt{n} in one variable
\item \texttt{\textbackslash md\{f\}\{5\}\{x\}\{2\}\{y\}\{3\}} for a 5th order
mixed partial derivative
\item \texttt{\textbackslash tmd}, \texttt{\textbackslash dmd} and similar
commands for forcing text and display styles
\end{itemize}
\item \texttt{esdiff}
\begin{itemize}
\item \texttt{\textbackslash diff{[}n{]}\{y\}\{x\}}\textbf{ }and \texttt{\textbackslash diffp{[}n{]}\{y\}\{x\}}\textbf{
}for ordinary and partial derivatives of order \texttt{n} in one variable
\item \texttt{\textbackslash diffp\{f\}\{\{x\textasciicircum 2\}\{y\}\{z\textasciicircum 3\}\}}
for a mixed partial derivative of order 6 in three variables
\item \texttt{\textbackslash diff{*}{[}n{]}\{y\}\{x\}\{0\}} for indicating the
point of evaluation of the derivative (using a subscript on parentheses)
\item \texttt{\textbackslash diffp{*}\{P\}\{T\}\{V\}} to indicate a variable
held constant
\end{itemize}
\item \texttt{physymb}
\begin{itemize}
\item \texttt{\textbackslash ud\{y\}\{x\}} and \texttt{\textbackslash pd\{y\}\{x\}}
for ordinary and partial derivatives of first order
\item \texttt{\textbackslash udd\{y\}\{x\}}, \texttt{\textbackslash uddd\{y\}\{x\}}
and \texttt{\textbackslash pdd\{y\}\{x\}}, \texttt{\textbackslash pddd\{y\}\{x\}}
for second and third order ordinary and partial derivatives
\item higher order derivatives not catered for
\end{itemize}
\end{itemize}
None of the packages quite gave what I wanted (but for all that, I suspect
cope with well over 90\% of use cases). \texttt{esdiff} comes closest but
failed when it came to combining algebraic and numeric orders of differentation
in a mixed partial derivative. Also the need to em-brace variables in a
mixed partial derivative in \texttt{esdiff}\textbf{ }was another (small)
count against it. 

\subsection{diffcoeff.sty}
\begin{itemize}
\item The distinctive feature of \texttt{diffcoeff.sty} is that it will automatically
form the overall order of a mixed partial derivative, including those containing
both algebraic and numeric contributions to the order:
\end{itemize}
\begin{example}
\textbackslash diffp{[}m-k-1,m+k{]}\{F(x,y,z)\}\{x,y,z\} $\Longrightarrow{\displaystyle \diffp[m-k-1,m+k]{F(x,y,z)}{x,y,z}}.$
\end{example}

\begin{itemize}
\item Ease of use was another major consideration, trying to avoid the unnecessary
writing of superscripts and subscripts and brace pairs. In this example,
no superscripts are written and only the two inescapable brace pairs are
required. 
\begin{itemize}
\item The use of a comma list for the second mandatory argument in a partial
derivative is another example. That makes differentiations in super- or
subscripted symbols easier to both write and read by avoiding `entanglements'
of braces.
\end{itemize}
\item I've also tried to make the options `natural' and consistent across both
ordinary and partial derivatives. Looking at the other packages listed
above, writing something like \texttt{\textbackslash diff{[}n{]}\{f\}\{x\}}
(which can be trimmed to \texttt{\textbackslash diff{[}n{]}fx} in this
instance) seems `natural' \textendash{} only \texttt{physymb} deviates
from the pattern. It seems consistent with this pattern to use a comma
list as an optional argument for mixed partial derivatives. 
\item I debated whether to include provision for points of evaluation and variables
held constant into the \texttt{\textbackslash diff} and \texttt{\textbackslash diffp}
commands. \texttt{esdiff} certainly allows this. I think a case can be
made, in subjects like thermodynamics, to consider the parentheses and
subscript as part of the overall symbol. The partial derivative itself
doesn't give the full story; it is ambiguous. Hence provision for these
extra elements was included in \texttt{\textbackslash diff} and \texttt{\textbackslash diffp}.
It's positioning as a final optional argument also felt natural given the
position of the resulting symbol in the displayed derivative:
\end{itemize}
\begin{example}
\textbackslash diffp ST\{V\} $\Longrightarrow\quad{\displaystyle \diffp ST{V}}$
\end{example}

\begin{itemize}
\item Although initially I used standard square brackets for this trailing optional
argument, the possibility of an immediately following mathematical expression
being enclosed in square brackets convinced me to use braces for the argument.
An immediately following expression can now be enclosed in \texttt{{[}
{]}}, or \texttt{\textbackslash\{ \textbackslash\}}, without ambiguity.
\item The star option also prompted the reflection: is it needed? One can always
leave the first mandatory argument empty and append the differentiand `by
hand'. But once the provision for points of evaluation or variables held
constant was incorporated into the \texttt{\textbackslash diff} and \texttt{\textbackslash diffp}
commands, the star option became the simplest way of handling appended
differentiands using the extra provision. (Note that it conflicts with
the star option in \texttt{esdiff}, but I can't see the packages ever being
used together.) And once the option is available, it provides a simple
way to switch between differentiand in the numerator/differentiand appended.
\item The final option added to the package was the slash option. This was prompted
after seeing the expression $\diff*{[\log f(z)]}/z$ in a text on statistical
mechanics. Alerted to the form, I then skimmed through various texts and
found this form of the derivative was used sufficiently often to justify
inclusion. The placement of the slash, between the two mandatory arguments,
seemed more-or-less self-evident.
\end{itemize}

\subsection{The mixed partial derivatives algorithm}

It occurred to me, after I had created an algorithm for splitting a linear
expression composed of signs, integers and variables into its numerical
and algebraic parts, that the same algorithm could be used in a recursive
way to simplify the algebraic part of the expression.

Given an order specification like, say, \textbf{\strong{\textbf{{[}2m+k\textendash 1,2m\textendash k+1,2k,1{]}}}},
the idea is to concatenate the terms with intervening \textbf{+} signs,
thus \textbf{\strong{\textbf{2m+k\textendash 1+2m\textendash k+1+ 2k+1}}},
then split this expression into numeric and algebraic parts, giving \textbf{\strong{\textbf{\textendash 1+ 1+1}}}
for the numeric part and \textbf{\strong{\textbf{2m+k+2m\textendash k+2k}}}
for the algebraic part. The numeric part, assumed to be a combination of
integers, is evaluated and the result stored. For the algebraic part, remove
throughout all instances of one of the variables, say \textbf{\strong{\textbf{m}}}.
The result is \textbf{\strong{\textbf{2+k+2\textendash k+2k}}}. Split
this into numeric and algebraic parts: \textbf{\strong{\textbf{2+2}}}
for the numeric part and \textbf{\strong{\textbf{k\textendash k+2k}}}
for the algebraic part. Evaluate the numeric part, \textbf{\strong{\textbf{+4}}},
and you have the overall coefficient of the variable \textbf{\strong{\textbf{m}}}.
Repeat the process for the next variable, and so on until all variables
have been accounted for. 

In fact repeating the process for the next variable, \strong{k} in this
example, immediately reveals a problem. Removing \strong{k} from \textbf{\strong{\textbf{k\textendash k+2k}}}
leaves \strong{\textendash +2} which evaluates to \strong{\textendash 2}
whereas the correct coefficient for \strong{k} should be \strong{+2}.
The solution is to insert \strong{1} before any `bare' variable \textendash{}
a variable preceded only by a sign rather than a number. In that case the
expression we remove \strong{k} from is \strong{1k\textendash 1k+2k}
giving the correct overall coefficient \strong{+2}.

A second problem may arise if there are terms involving products of variables
as in the order specification \strong{{[}mk\textendash 2,2m+1,2k+1{]}}.
This splits into a numeric part \strong{\textendash 2+1+1} evaluating
to \strong{0}, and an algebraic part \strong{mk+2m+2k}. If we choose
\strong{m} as the first variable to remove from this expression, we get
\strong{+2} for the numeric part (and hence the overall coefficient of
\strong{m}) and \strong{k+2k} for the algebraic part, which is wrong,
since that will lead to the wrong overall coefficient \strong{+3} for
\strong{k}, and the 2-variable term \strong{mk} will not get treated
at all. The cure is to treat \strong{mk} as a variable itself, count the
number of tokens in each such product and start the removal process with
the largest.

\subsubsection{The splitting algorithm}

Write $s$ for a sign, one of \strong{+}, \strong{\textendash{}}, and
\strong{s} for the state of assembling a signed term; a signed term is
a string of one or more signs. Write $d$ for a digit, one of 0123456789,
and \strong{n} for the state of assembling a numeric term; a numeric term
is a signed term followed by a string of one or more digits. Write $v$
for a variable, usually a letter from the roman alphabet but in principle
any single token that is not a sign or a digit, and \strong{a} for the
state of assembling an algebraic term; an algebraic term is a numeric term
followed by a string of one or more variables. Rather than referring to
a signed-term-assembling state, we shall (obviously) simply refer to a
\emph{signed state}, and similarly to a \emph{numeric state} and an \emph{algebraic
state}.

\begin{table}
\noindent \centering{}\caption{\label{tab:Input-output-states}State transitions}
\medskip{}
\begin{tabular}{ccccc}
\cmidrule{2-5} 
 & Curr. state & Curr. token & Action & Next state\tabularnewline
\cmidrule{2-5} 
1 & \strong{s} & $s$ & $Ts\to s'$; $T=s'$ & \strong{s}\tabularnewline
\cmidrule{2-5} 
2 & \strong{s} & $d$ & $Td$ & \strong{n}\tabularnewline
\cmidrule{2-5} 
3 & \strong{s} & $v$ & $Vv$; $T1v$ & \strong{a}\tabularnewline
\cmidrule{2-5} 
4 & \strong{n} & $s$ & $\mathbf{N}T$; $T=s$ & \strong{s}\tabularnewline
\cmidrule{2-5} 
5 & \strong{n} & $d$ & $Td$ & \strong{n}\tabularnewline
\cmidrule{2-5} 
6 & \strong{n} & $v$ & $Vv$; $Tv$ & \strong{a}\tabularnewline
\cmidrule{2-5} 
7 & \strong{a} & $s$ & $\mathbf{V}V,$; $V=\textrm{Ø}$; $\mathbf{A}T$; $T=s$ & \strong{s}\tabularnewline
\cmidrule{2-5} 
8 & \strong{a} & $d$ & error & \strong{!!}\tabularnewline
\cmidrule{2-5} 
9 & \strong{a} & $v$ & $Vv$; $Tv$ & \strong{a}\tabularnewline
\cmidrule{2-5} 
\end{tabular}
\end{table}
We also want to record the variables in the extended sense of products
of same. Call a one-token variable a prime variable. Then in this desired
sense, a variable is a string of one or more prime variables. 

Let $\mathbf{E}$ be the initial expression. Let $\mathbf{A}$ be a container
for the algebraic part of $\mathbf{E}$; let $\mathbf{N}$ be a container
for the numeric part of $\mathbf{E}$; and let $\mathbf{V}$ be a container
for the extended variables in $\mathbf{E}$. Let $T$ be a container in
which to accumulate the current term, and $V$ a container in which to
accumulate the current extended variable (if any). Initially all these
containers are empty ($\textrm{Ø}$).

We work through $\mathbf{E}$ token by token from the left. The table shows
the alternatives. 
\begin{itemize}
\item Row 1. The current token is a sign $s$ and the system is in a signed state
\strong{s}. We append $s$ to the current term, $Ts$, then resolve the
juxtaposition of signs according to the familiar rules: $++\to+$, $--\to+$,
$+-\to-$, $-+\to-$, so that $T$ contains only the resolved sign $s'$.
The system remains in a signed state.
\item Row 2. The current token is a digit $d$ and the system is in a signed-
state \strong{s}. We append $d$ to the current term, $Td$ (which will
now consist of a sign and a digit), and the system shifts to a numeric
state \strong{n}.
\item Row 3. The current token is a prime variable $v$ and the system is in
a signed state \strong{s}. We start assembling an extended  variable,
$Vv$, and append $1v$ to the current term, $T1v$, where the $1$ is
necessary as discussed earlier (and in any case `sign variable' is not
a recognised \emph{term} \textendash{} neither signed, numeric or algebraic).
The system shifts to an algebraic state \strong{a}.
\item Row 4. The current token is a sign $s$ and the system is in a numeric
state \strong{n}. The current term is a numeric term, a sign followed
by at least one digit, and is complete. We append it to the numeric part
$\mathbf{N}$ of $\mathbf{E}$, $\mathbf{N}T$, then initialise $T$ to
$s$. The system shifts to a signed state.
\item Row 5. The current token is a digit $d$ and the system is in a numeric
state \strong{n}. We append $d$ to the current term, $Td$, and remain
in a numeric state.
\item Row 6. The current token is a prime variable $v$ and the system is in
a numeric state \strong{n}. We start assembling a  variable, $Vv$, and
also append $v$ to the current term, $Tv$. The system shifts to an algebraic
state \strong{a}.
\item Row 7. The current token is a sign $s$ and the system is in an algebraic
state \strong{a}. The current term is an algebraic term, a sign followed
by at least one digit followed by at least one prime variable, and is complete.
We append it to the algebraic part $\mathbf{A}$ of $\mathbf{E}$, $\mathbf{A}T$,
then initialise $T$ to $s$. We also append $V$, in which we have been
accumulating the (extended) variable, to $\mathbf{V}$, $\mathbf{V}V,$,
then empty $V$ in preparation for the next (extended) variable. Attention
is drawn to the comma following $V$ also appended to $\mathbf{V}$, so
that we can distinguish where one variable ends and the next begins. The
system shifts to a signed state.
\item Row 8. The current token is a digit $d$ and the system is in an algebraic
state \strong{a}. This situation should not arise.\emph{ }We don't write
$k2$; we write $2k$ \textendash{} number precedes variable. An error
is generated. 
\item Row 9. The current token is a variable $v$ and the system is in an algebraic
state \strong{a}. We append $v$ to the current extended  variable, $Vv$,
and also append $v$ to the current term, $Tv$. The system remains in
an algebraic state \strong{a}.
\end{itemize}
To get things under way, an initial plus sign is put in $T$, $T=+$, and
the system is set to the signed state \strong{s}. In order that \emph{all}
terms of $\mathbf{E}$ are recorded in either $\mathbf{N}$ or $\mathbf{A}$,
and all extended variables in $\mathbf{V}$, we append a plus sign to $\mathbf{E}$:
$\mathbf{E}+$. Since an expression doesn't end with a trailing sign (we
don't write, e.g., \textbf{\strong{\textbf{2m+k\textendash{}}}}), the
process necessarily terminates either in row 4 or row 7 with the final
term appended either to $\mathbf{N}$ or $\mathbf{A}$ and with $T=+$;
if it terminates in row 7, the final extended variable is appended to $\mathbf{V}$,
$\mathbf{V}V$ (and $V$ is emptied, although that hardly matters at this
point).

\subsubsection{An enlarged scheme?}

Row 8 of our table generates an error: a digit following a variable. But
having allowed products of variables like \texttt{mn} ($mn$), it is very
tempting to allow \texttt{mm}, i.e. \texttt{m\textasciicircum 2} ($m^{2}$)
and, indeed, \texttt{m\textasciicircum n} ($m^{n}$). And if we allow
\texttt{m\textasciicircum 2} and \texttt{m\textasciicircum n}, how can
we say no to subscripted forms like \texttt{k\_2} ($k_{2}$) and \texttt{k\_n}
($k_{n}$)? Or, for that matter, \texttt{k\_+} ($k_{+}$) and \texttt{k\_-}
($k_{-}$), and therefore \texttt{m\textasciicircum +} ($m^{+}$) and
\texttt{m\textasciicircum -} ($m^{-}$)? And having extended the scheme
in this way to exponents of \emph{variables}, surely it should also encompass
exponents of \emph{numbers}, not only an obvious case like \texttt{2\textasciicircum 2}
($2^{2}$) but less obviously, yet still compellingly, \texttt{2\textasciicircum n}
($2^{n}$)? 

Each of these extensions produces its own problems, but they can all be
accommodated within an enlarged scheme, as can the use of parentheses (with
numerical coefficients). Table~\ref{tab:Input-output-states} translates
neatly into code. Rather than add these complications to \texttt{diffcoeff.sty},
I have transferred the enlarged scheme to \texttt{diffcoeff.sty}'s `big
brother', \texttt{diffcoeffx.sty}. The comparable table and routine resulting
from it in \texttt{diffcoeffx.sty} is much bigger and less obvious than
in \texttt{diffcoeff.sty}.

\subsubsection{Some code details}

In the code, the states are distinguished by integers as indicated in Table~\ref{tab:State-integers}.
Tokens are assigned similar integer indexes, as indicated in the table.
The relevant routine is \texttt{\textbackslash\_\_diffco\_get\_curr\_index:NN}.
The actions embodied in Table~\ref{tab:Input-output-states} are encoded
in \texttt{\textbackslash\_\_diffco\_compare\_states:NNNNN} which is
a direct translation of the table into expl3 code.

\begin{table}[h]
\caption{Some code details}

\noindent \centering{}\subfloat[\label{tab:State-integers}State integers]{\centering{}%
\begin{tabular}{|c|c|c|}
\hline 
State & Index & Tokens\tabularnewline
\hline 
\hline 
signed & 0 & $+$ $-$\tabularnewline
\hline 
numeric & 1 & 0123456789\tabularnewline
\hline 
algebraic & 2 & variables\tabularnewline
\hline 
\end{tabular}}~~~\subfloat[Translations]{
\centering{}%
\begin{tabular}{|c|c|}
\hline 
Symbol & Code variable\tabularnewline
\hline 
\hline 
$s$,$d$,$v$ & \texttt{\textbackslash l\_\_diffco\_curr\_tok\_tl}\tabularnewline
\hline 
$T$ & \texttt{\textbackslash l\_\_diffco\_curr\_term\_tl}\tabularnewline
\hline 
$V$ & \texttt{\textbackslash l\_\_diffco\_curr\_var\_tl}\tabularnewline
\hline 
$\mathbf{N}$ & \texttt{\textbackslash l\_\_diffco\_nos\_tl}\tabularnewline
\hline 
$\mathbf{A}$ & \texttt{\textbackslash l\_\_diffco\_alg\_tl}\tabularnewline
\hline 
$\mathbf{V}$ & \texttt{\textbackslash l\_\_diffco\_vars\_prop}\tabularnewline
\hline 
\end{tabular}}
\end{table}
A property list is used to store the variables, organised by size \textendash{}
the number of tokens composing an extended variable. This enables the sorting
by size needed for the determination of the overall coefficients of variables
by removing them in turn from the algebraic part of the expression. That
process is conducted in the routine \texttt{\textbackslash\_\_diffco\_eval\_vars:NN}.
The variables are recorded only on the first scan through the order specification
expression. This is the function of the boolean \texttt{\textbackslash l\_\_diffco\_vars\_noted\_bool}
which is set in \texttt{\textbackslash\_\_diffco\_eval\_vars:NN}. Evaluation
of the numeric parts of expressions is provided by \texttt{\textbackslash\_\_diffco\_eval\_nos:N}.

\section{Summary of main commands}

\subsubsection{Ordinary derivatives}

The syntax is
\begin{example}
{\small{}\textbackslash diff{[}order{]}\{differentiand\}\{variable\}\{point
of evaluation\}}{\small\par}
\end{example}

for the differentiand in the numerator, and where the final argument, although
using braces, is an \emph{optional} argument. A starred form appends the
differentiand:
\begin{example}
{\small{}\textbackslash diff{*}{[}order{]}\{differentiand\}\{variable\}\{point
of evaluation\}}{\small\par}
\end{example}

No space must occur between the final optional argument, if it is used,
and the second mandatory argument. 

There are also slash forms of both these commands:
\begin{example}
{\small{}\textbackslash diff{[}order{]}\{differentiand\}/\{variable\}\{point
of evaluation\}}{\small\par}

{\small{}\textbackslash diff{*}{[}order{]}\{differentiand\}/\{variable\}\{point
of evaluation\}}{\small\par}
\end{example}

For the starred form, the differential coefficient is enclosed in parentheses.

Precisely similar definitions, but without the slash forms, apply to \texttt{\textbackslash Diff},
forming a differential coefficient with $D$, \texttt{\textbackslash diffd},
forming a differential coefficient with $\delta$, and \texttt{\textbackslash Diffd},
forming a differential coefficient with $\Delta$.

\subsubsection{Partial derivatives}

The syntax is
\begin{example}
{\small{}\textbackslash diffp{[}order spec.{]}{[}order override{]}\{differentiand\}\{variables\}\{constant
variables\}}{\small\par}
\end{example}

for the differentiand in the numerator and where the final argument, although
in braces, is an \emph{optional} argument. No space must occur between
the final optional argument, if it is used, and the second mandatory argument.
The \textbf{\strong{\textbf{order spec.}}} is a comma-separated list;
the \strong{variables} is also a comma-separated list. A starred form
appends the differentiand:
\begin{example}
{\small{}\textbackslash diffp{*}{[}order{]}{[}order~override{]}\{differentiand\}\{variables\}\{constant
variables\}}{\small\par}
\end{example}

Slash forms exist also for these commands:
\begin{example}
{\small{}\textbackslash diffp{[}order spec.{]}{[}order override{]}\{differentiand\}/\{variables\}\{constant
variables\}}{\small\par}

{\small{}\textbackslash diffp{*}{[}order{]}{[}order~override{]}\{differentiand\}/\{variables\}\{constant
variables\}}{\small\par}
\end{example}

For the starred version of the slash form, the differential coefficient
is enclosed in parentheses.

\subsubsection{Settings}
\begin{example}
\textbackslash diffset{[}option1=<value1>,option2=<value2>,...{]}
\end{example}

All numerical values should be integers (\texttt{diffcoeff} interprets
this in units of mu, 1/18 of an em). To return all options to default values,
write
\begin{example}
\textbackslash diffset
\end{example}

The options and defaults are
\begin{description}
\item [{\strong{roman = false}}] \textbf{\strong{\textbf{true}}} gives upright
(roman) \textbf{\strong{\textbf{d}}} and \textbf{\strong{\textbf{D}}} 
\item [{\strong{d-delims = . |}}] delimiters which, when subscripted, indicate
the point of evaluation of an ordinary derivative
\item [{\strong{p-delims = ( )}}] delimiters which, when subscripted, indicate
variables held constant for partial derivatives
\item [{\strong{d-nudge = 0}}] adjustment for positioning the subscript to
the preceding delimiters
\item [{\strong{p-nudge = $-$6}}] adjustment for positioning the subscript
to the preceding delimiters
\item [{\strong{d-sep = 1}}] additional separation between the \textbf{$d$
}and its superscript in the numerator of a second or higher order ordinary
derivative
\item [{\strong{p-sep = 1}}] additional separation between the \textbf{$\partial$}
and its superscript in the numerator of a second or higher order partial
derivative
\item [{\strong{sep = 2}}] additional separation between the terms in the denominator
of a mixed partial derivative
\end{description}

\subsection{Change history}

Uploaded to CTAN 2016-06-27

2016-06-30 Removed the unused \texttt{extend} package option (a relic from
earlier experiments).

2018-10-25 v.1.1 Changed the handling of error messages.
\end{document}