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 – Dr Krishnakumar Gopalakrishnan Apr 27 '18 at 20:46
  • 1
    @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? – Dr Krishnakumar Gopalakrishnan 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:


\usepackage{newtxtext, newtxmath}


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


enter image description here

| improve this answer | |
  • Didn't know about its existence!! – Dr Krishnakumar Gopalakrishnan 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? – Dr Krishnakumar Gopalakrishnan 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}%
| improve this answer | |

Here is my solution. It can count the differentiations.





\NewDocumentCommand\pdiff{s o >{\SplitList{,}} m}%
   {\partial \ifnum\thepdiff@total >1\relax ^{\arabic{pdiff@total}} \fi%

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

\NewDocumentCommand\@pdiff@c{m G{1}}{\addtocounter{pdiff@total}{#2}}

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

\NewDocumentCommand\@pdiff@vars{m g}{\mathinner{\partial #1 \IfValueT{#2}{^ {#2}}}}


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


Partial differentiation

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.