%\iffalse
%
%esdiff.dtx version 1.2 April 2008
%Eddie Saudrais (eddie.saudrais@wanadoo.fr)
%Code slightly revised by Christophe Jorssen
%
%<*driver>
\documentclass[a4paper]{ltxdoc}
\usepackage{esdiff}
\parindent 0cm
\topmargin -1cm
\begin{document}
\DocInput{esdiff.dtx}
\end{document}
%</driver>
%\fi
%\CheckSum{486}
%\title{Writing derivatives}
%\author{Eddie Saudrais}
%\date{\today}
%\maketitle
%\MakeShortVerb{\|}
%\begin{abstract}
%This document describes the package \textsf{esdiff} that makes
%writing derivatives very easy.
%It allows to write derivatives, partial derivatives, multiple
%derivatives.
%Some typographic options are available.
%The macro about crossed derivatives was given on
%\texttt{comp.text.tex} by Peter Schmitt.
%\end{abstract}
%\section{Derivatives}
%
%\DescribeMacro{\diff}
%
%Derivatives are obtained with the command \textsf{diff}.
%
%To get $\diff{f}{x}$, just write |$\diff{f}{x}$|.
%
%The ordre of the derivative can be set in square brackets.
%So |$\diff[n]{f}{x}$| gives $\diff[n]{f}{x}$.
%
%\DescribeMacro{\diff*}
%
%It's easy to precise the point where the derivative is calculated:
%
% |$\diff*{f}{x}{x_0}$|
%gives $\diff*{f}{x}{x_0}$.
%
%Of course, those two options can be used together:
%|$\diff*[2]{g}{y}{0}$| gives as expected $\diff*[2]{g}{y}{0}$.
%
%\section{Partial derivatives}
%
%\DescribeMacro{\diffp}
%
%Partial derivatives are obtained with the macro
%\textsf{diffp}, using the same syntaxe as with \textsf{diff}:
%a star version to put a subscript, and square brackets to set
%the order of the derivative according to only one variable.
%
%To get $\diffp{f}{x}$, just write
%|$\diffp{f}{x}$|.
%
%We obtain $\diffp[2]{f}{x}$ writing |$\diffp[2]{f}{x}$|.
%
%\DescribeMacro{\diffp*}
%
%The notation $\diffp*{p}{V}{T}$ is the result of
%|$\diffp*{p}{V}{T}$|.
%
%The notation of crossed derivatives is automatic.
%To get $\diffp{f}{{x}{y^2}}$, write
%
%|$\diffp{f}{{x}{y^2}}$|.
%This method can be used to write a partial derivative, without
%square beackets. So |$\diffp{f}{{x^2}}$| gives $\diffp{f}{{x^2}}$.
%
%We can mix options:
% |$\diffp*{f}{{x^2}{y^3}}{z}$|
%gives $\diffp*{f}{{x^2}{y^3}}{z}$.
%
%\section{Options}
%
%Several options are available:
%\begin{itemize}
%\item
%by default, derivatives are written in displaystyle on text mode.
%The \textsf{display} option set this default. With the \textsf{text}
%option, derivatives are written in textstyle on text mode;
%\item
%by default, the \texttt{d} of the derivative is written in roman.
%Available options are \textsf{roman} and \textsf{italic};
%\item
%we can set the space between the \texttt{d} and the symbol of the
%function or of the variable. By default, this space is null.
%Available options are \textsf{thin}, \textsf{med} and
%\textsf{big}. It may be fine to add a space when the \texttt{d}
%is in italic;
%\item
%We can set the space between the $\partial$ and the symbol of the
%function or of the variable. By default, this space is null.
%Available options are \textsf{thinp}, \textsf{medp} and
%\textsf{bigp};
%\item
%the space between $\partial x$ and $\partial y$ in crossed derivatives may be too
%narrow. It is null by default. Available options are
%\textsf{thinc}, \textsf{medc} and \textsf{bigc};
%\end{itemize}
%
%\section{The code}
%\StopEventually{}
%The package presents itself.
%    \begin{macrocode}
%<*package>
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{esdiff}
%    \end{macrocode}
%We set default values for the options.
%   \begin{macrocode}
\newcommand*{\ES@taille}[1]{\displaystyle{#1}}
\newcommand*{\ES@difint}{\mkern 0mu}
\newcommand*{\ES@derpint}{\mkern 0mu}
\newcommand*{\ES@croisint}{\mkern 0mu}
%    \end{macrocode}
%The \textsf{d} is roman by default..
%    \begin{macrocode}
\newcommand*{\ES@dop}{\mathrm{d}}
%    \end{macrocode}
%Options:
%    \begin{macrocode}
\DeclareOption{display}{\renewcommand*{\ES@taille}[1]{\displaystyle{#1}}}
\DeclareOption{text}{\renewcommand*{\ES@taille}[1]{\textstyle{#1}}}
\DeclareOption{roman}{\renewcommand*{\ES@dop}{\ensuremath{\mathrm{d}}}}
\DeclareOption{italic}{\renewcommand*{\ES@dop}{\ensuremath{d}}}
\DeclareOption{thin}{\renewcommand*{\ES@difint}{\ensuremath{\,}}}
\DeclareOption{med}{\renewcommand*{\ES@difint}{\ensuremath{\;}}}
\DeclareOption{big}{\renewcommand*{\ES@difint}{\ensuremath{\:}}}
\DeclareOption{thinp}{\renewcommand*{\ES@derpint}{\ensuremath{\,}}}
\DeclareOption{medp}{\renewcommand*{\ES@derpint}{\ensuremath{\;}}}
\DeclareOption{bigp}{\renewcommand*{\ES@derpint}{\ensuremath{\:}}}
\DeclareOption{thinc}{\renewcommand*{\ES@croisint}{\ensuremath{\,}}}
\DeclareOption{medc}{\renewcommand*{\ES@croisint}{\ensuremath{\;}}}
\DeclareOption{bigc}{\renewcommand*{\ES@croisint}{\ensuremath{\:}}}
\ProcessOptions\relax
%    \end{macrocode}
%
%\subsection{Derivative}
%Test of star version.
%    \begin{macrocode}
\newcommand*{\diff}{}
\def\diff{\@ifstar{\ES@diffstar}{\ES@diffnostar}}
%    \end{macrocode}
%Star version. The subscript is given.
%    \begin{macrocode}
\def\ES@diffstar{\@ifnextchar[{\ES@diffstar@i}{\ES@diffstar@ii}}
%    \end{macrocode}
%The ordre is given in square brakets, and there is a subscript.
%    \begin{macrocode}
\def\ES@diffstar@i[#1]#2#3#4{%
	\mathchoice{%
	\left(\frac{\ES@dop^{#1}\ES@difint#2}%
	{\ES@dop\ES@difint#3^{#1}}\right)_{\mkern-7mu#4}}%
	{\ES@taille{\left(\frac{\ES@dop^{#1}\ES@difint#2}%
	{\ES@dop\ES@difint#3^{#1}}\right)_{\mkern-7mu#4}}}%
	{\scriptstyle{\left(\frac{\ES@dop^{#1}\ES@difint#2}%
	{\ES@dop\ES@difint#3^{#1}}\right)_{\mkern-7mu#4}}}%
	{\scriptstyle{\left(\frac{\ES@dop^{#1}\ES@difint#2}%
	{\ES@dop\ES@difint#3^{#1}}\right)_{\mkern-7mu#4}}}}
%    \end{macrocode}
%There is only a subscript.
%    \begin{macrocode}
\def\ES@diffstar@ii#1#2#3{%
	\mathchoice{\left(\frac{\ES@dop\ES@difint#1}%
	{\ES@dop\ES@difint#2}\right)_{\mkern-7mu#3}}%
	{\ES@taille{\left(\frac{\ES@dop\ES@difint#1}{\ES@dop\ES@difint#2}%
	\right)_{\mkern-7mu#3}}}{\scriptstyle{\left(%
	\frac{\ES@dop\ES@difint#1}{\ES@dop\ES@difint#2}\right)%
	_{\mkern-7mu#3}}}%
	{\scriptstyle{\left(\frac{\ES@dop\ES@difint#1}{\ES@dop\ES@difint#2}\right)%
	_{\mkern-7mu#3}}}}
%    \end{macrocode}
%
%No star version (no subscript).
%    \begin{macrocode}
\def\ES@diffnostar{\@ifnextchar[{\ES@diffnostar@i}{\ES@diffnostar@ii}}
%    \end{macrocode}
%The ordre is given in square brackets.
%    \begin{macrocode}
\def\ES@diffnostar@i[#1]#2#3{\mathchoice{\frac{\ES@dop^{#1}\ES@difint#2}%
{\ES@dop\ES@difint#3^{#1}}}%
{\ES@taille{\frac{\ES@dop^{#1}\ES@difint#2}{\ES@dop\ES@difint#3^{#1}}}}%
{\scriptstyle{\frac{\ES@dop^{#1}\ES@difint#2}{\ES@dop\ES@difint#3^{#1}}}}%
{\scriptstyle{\frac{\ES@dop^{#1}\ES@difint#2}{\ES@dop\ES@difint#3^{#1}}}}}
%    \end{macrocode}
%Basic notation.
%    \begin{macrocode}
\def\ES@diffnostar@ii#1#2{\mathchoice%
{\frac{\ES@dop\ES@difint#1}{\ES@dop\ES@difint#2}}%
{\ES@taille{\frac{\ES@dop\ES@difint#1}{\ES@dop\ES@difint#2}}}%
{\scriptstyle{\frac{\ES@dop\ES@difint#1}{\ES@dop\ES@difint#2}}}%
{\scriptstyle{\frac{\ES@dop\ES@difint#1}{\ES@dop\ES@difint#2}}}}
%    \end{macrocode}
%
%\subsection{Partial derivatives}
%Test of the star version.
%    \begin{macrocode}
\newcommand*{\diffp}{}
\def\diffp{\@ifstar{\ES@diffpstar}{\ES@diffpnostar}}
%    \end{macrocode}
%Star version: a subscript is given.
%    \begin{macrocode}
\def\ES@diffpstar{\@ifnextchar[{\ES@diffpstar@i}{\ES@diffpstar@ii}}
%    \end{macrocode}
%Ordre of derivative is given, and there is a subscript.
%    \begin{macrocode}
\def\ES@diffpstar@i[#1]#2#3#4{%
	\mathchoice{\left(\frac{\partial^{#1}%
	\ES@derpint#2}{\partial\ER@derpint#3^{#1}}\right)_{\mkern-7mu#4}}%
	{\ES@taille{\left(\frac{\partial^{#1}\ES@derpint#2}{\partial\ES@derpint#3^{#1}}%
	\right)_{\mkern-7mu#4}}}%
	{\scriptstyle{\left(\frac{\partial^{#1}\ES@derpint#2}%
	{\partial\ES@derpint#3^{#1}}%
	\right)_{\mkern-7mu#4}}}%
	{\scriptstyle{\left(\frac{\partial^{#1}\ES@derpint#2}%
	{\partial\ES@derpint#3^{#1}}%
	\right)_{\mkern-7mu#4}}}}
%    \end{macrocode}
%There is only a subscript.
%    \begin{macrocode}
\def\ES@diffpstar@ii#1#2#3{\mathchoice{\left({\begingroup
            \toks0={}\count0=0
            \ES@degree #2\ES@degree
            \frac{\partial\ifnum\count0>1^{\the\count0 }\fi\ES@derpint#1}%
            {\the\toks0}%
            \endgroup}\right)_{\mkern-7mu#3}}%
            {\ES@taille{\left({\begingroup
            \toks0={}\count0=0
            \ES@degree #2\ES@degree
            \frac{\partial\ifnum\count0>1^{\the\count0 }\fi\ES@derpint#1}%
            {\the\toks0}%
            \endgroup}\right)_{\mkern-7mu#3}}}%
            {\scriptstyle{\left({\begingroup
            \toks0={}\count0=0
            \ES@degree #2\ES@degree
            \frac{\partial\ifnum\count0>1^{\the\count0 }\fi\ES@derpint#1}%
            {\the\toks0}%
            \endgroup}\right)_{\mkern-7mu#3}}}%
            {\scriptstyle{\left({\begingroup
            \toks0={}\count0=0
            \ES@degree #2\ES@degree
            \frac{\partial\ifnum\count0>1^{\the\count0 }\fi\ES@derpint#1}%
            {\the\toks0}%
            \endgroup}\right)_{\mkern-7mu#3}}}}
%    \end{macrocode}
%
%No star version (no subscript is given).
%    \begin{macrocode}
\def\ES@diffpnostar{\@ifnextchar[{\ES@diffpnostar@i}{\ES@diffpnostar@ii}}
%    \end{macrocode}
%The ordre of derivative is given.
%    \begin{macrocode}
\def\ES@diffpnostar@i[#1]#2#3{%
	\mathchoice{\frac{\partial^{#1}\ES@derpint#2}%
	{\partial\ES@derpint#3^{#1}}}%
	{\ES@taille{\frac{\partial^{#1}\ES@derpint#2}{\partial\ES@derpint#3^{#1}}}}%
	{\scriptstyle{\frac{\partial^{#1}\ES@derpint#2}{\partial\ES@derpint#3^{#1}}}}%
	{\scriptstyle{\frac{\partial^{#1}\ES@derpint#2}{\partial\ES@derpint#3^{#1}}}}}
%    \end{macrocode}
%Basic notation.
%    \begin{macrocode}
\def\ES@diffpnostar@ii#1#2{\mathchoice{\begingroup
            \toks0={}\count0=0
            \ES@degree #2\ES@degree
            \frac{\partial\ifnum\count0>1^{\the\count0 }\fi\ES@derpint#1}%
            {\the\toks0}%
            \endgroup}%
            {\ES@taille{\begingroup
            \toks0={}\count0=0
            \ES@degree #2\ES@degree
            \frac{\partial\ifnum\count0>1^{\the\count0 }\fi\ES@derpint#1}%
            {\the\toks0}%
            \endgroup}}%
            {\scriptstyle{\begingroup
            \toks0={}\count0=0
            \ES@degree #2\ES@degree
            \frac{\partial\ifnum\count0>1^{\the\count0 }\fi\ES@derpint#1}%
            {\the\toks0}%
            \endgroup}}%
            {\scriptstyle{\begingroup
            \toks0={}\count0=0
            \ES@degree #2\ES@degree
            \frac{\partial\ifnum\count0>1^{\the\count0 }\fi\ES@derpint#1}%
            {\the\toks0}%
            \endgroup}}}
%    \end{macrocode}
%
%Macros used for crossed derivatives.
%    \begin{macrocode}
\def\ES@degree #1{\ifx #1\ES@degree \expandafter\ES@stopd
                      \else \expandafter\ES@addd \fi #1^1$#1\ES@addd}
\def\ES@stopd #1\ES@addd{}
\def\ES@addd #1^#2#3$#4\ES@addd{\advance\count0 #2
                          \toks0=\expandafter{\the\toks0%
                           {\partial\ES@derpint #4}%
                          \ES@croisint}\ES@degree}
%</package>
%    \end{macrocode}
%\Finale