%% LyX 2.2.0 created this file.  For more info, see http://www.lyx.org/.
%% Do not edit unless you really know what you are doing.
\documentclass[english,extend]{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}}
\newcommand{\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{}(aparsloe@clear.net.nz)}}
\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{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}

\end{document}
