# Math macro for typesetting partial derivative with variable placeholder for both numerator and denominator

I'd like to type time-derivatives such as $\frac{\partial x}{\partial t} = f(x,t)$ as well as spatial gradients such as $\frac{\partial y}{\partial x} = k y$

To make the code readable and to avoid typing \partial repeatedly, I found a macro \pder (which I have in my preamble) that uses the xparse package

\DeclareDocumentCommand{\pder}{ O{} O{} m }{\frac{\partial^{#2}#1}{\partial#3^{#2}}}


However, \pder{t}{x} produces outputs like

rather than the desired output produced by \frac{\partial x}{\partial t} which is

How can I declare a math macro that accepts variables for numerator and denominator but does not leave a space in between?

PS: I have no clue why Tex.SX does not render the typed content within $..$. If anyone knows what I am doing wrong, please let me know and I shall fix it.

• using \$ within the web page is mathjax syntax, which is not enabled on this site – David Carlisle Apr 27 '18 at 20:29
• That sounds a bit silly to me on the part of site deployment. This is a math-heavy site. In the absence of this feature, how can one effectively convey math-typesetting issues – Krishna Apr 27 '18 at 20:46
• @Krishna We're interested in TeX code, not in the approximate rendering made by MathJax. – egreg Apr 27 '18 at 21:00
• It is because it is a TeX site that mathjax is disabled, we need to see tex output not javascript otherwise it just leads to confusion. – David Carlisle Apr 27 '18 at 21:01
• Khan Academies KaTeX supports true latex (well, a subset for now), but with a growing number of ligatures as the project matures and is starting to rival the approximate rendering by mathjax. Maybe we can look into it for this site? – Krishna Apr 27 '18 at 21:03

Don't reinvent the wheel: there already the esdiff package which eases typesetting derivatives in Leibniz' notation (partial or nonpartial). In addition, it can automatically determine the order of derivation for crossed derivatives, and takes care of the evaluation point:

\documentclass{article}%

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{newtxtext, newtxmath}
\usepackage{esdiff}

\begin{document}

$\diffp*{u}{t}{\mkern2mu t = 0}\qquad \diffp{u}{t x}\qquad \diffp{u}{{t^2}x}$%

\end{document}


• Didn't know about its existence!! – Krishna Apr 27 '18 at 20:43
• Sorry for asking, but are there more such nifty packages that relieve the burden of typing a math-heavy PhD thesis? – Krishna Apr 27 '18 at 20:44
• To my knowledge, there are physics and commath (they're rather physics-oriented) and a number of specialised packages, not specifically made to simplify typing, but offering all-in-one solutions for specific constructions (e.g. tikz-cd for commutative diagrams). To be noted: the author of esdiff offers very nice (extensible) arrows for vectors in esvect. – Bernard Apr 27 '18 at 21:02
• @Bernard eek please don't recommend commath, – David Carlisle Apr 27 '18 at 21:04
• @David Carlisle: Actually, I only mentioned it, as I've never used it. Does it have bugs? – Bernard Apr 27 '18 at 21:10

your \pder only takes one mandatory argument so you have \pder{t} and then {x} later with unused brace group

define it with two arguments

\DeclareDocumentCommand{\pder}{ O{} O{} m m}%
{\frac{\partial^{#2}#1#4}{\partial#3^{#2}}}


Here is my solution. It can count the differentiations.

\documentclass[17pt]{extarticle}

\usepackage{xparse}

\makeatletter
\newcounter{pdiff@total}

\DeclareDocumentCommand\linefrac{mm}
{\left.#1\middle\slash{#2}\right.}

\NewDocumentCommand\pdiff{s o >{\SplitList{,}} m}%
{\IfBooleanTF{#1}{\let\@tempa\linefrac}{\let\@tempa\frac}%
\setcounter{pdiff@total}{0}%
\ProcessList{#3}{\pdiff@c}%
\@tempa%
{\partial \ifnum\thepdiff@total >1\relax ^{\arabic{pdiff@total}} \fi%
\IfValueT{#2}{#2}}%
{\ProcessList{#3}{\pdiff@vars}}}

\NewDocumentCommand\pdiff@c{> { \SplitArgument { 1 } { * } } m}{\@pdiff@c#1}

$\pdiff{x} \quad \pdiff[f(x)]{x*2,y*95,z*4} \quad \pdiff*[f(\vec r)]{\vec r*5}$