2

I want to make a command to simplify my equations typesetting. I have many equations including high order derivative

\frac{\partial^2y}{\partial x^2}
\frac{\partial^3z}{\partial t^3}    
....

Now I make a command

\newcommand\mfrac[3]{\frac{\partial^{\if #1=1 \relax \else #1}#2}{\partial #3^{\if #1=1 \relax \else #1}}}

The command has three arguments: the first is the order of the derivative, the second is the argument in denominator and the third is in numerator. If the order is 1, it should be omitted.

But it didn't work well, How can I make such a command?

5

In my opinion, you should not reinvent the wheel, and load the esdiff package, which already does it (and more) with its \diff and \diffp commands. It can take subscripts into account. In partial derivatives, it has the capability to calculate the order of derivation from the exponents of the variables. Demo:

\documentclass{article}%

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{mathtools}
\usepackage[thinc]{esdiff}

 \begin{document}

\begin{alignat*}{3}
     \diff{f}{x} &\qquad  \diff[4]{g}{t} \\[2ex]
     \diffp{f}{x} &\qquad\diffp{g}{tu}& & \qquad &  \diffp{g}{{t^2}{u^3}}
 \end{alignat*}

 \end{document}

enter image description here

  • Thanks for your answer @Bernhard. I haven't heard of esdiff package, and I plan to watch the esdiff document – Ice0cean Jul 9 '16 at 13:53
1

I use a predefined command for derivatives as well but I took a different approach. I defined the order of the derivative as an optional argument. So I need to specify it only if it is not 1. I hope this solves your problem although it is a different approach.

\DeclareMathOperator{\diff}{d\,}
\newcommand{\abl}[3][]{\ensuremath{\frac{\diff^{#1}#2}{\diff #3^{#1}}}}
  • Thanks for your answer @Enno. It is an alternative approach. – Ice0cean Jul 9 '16 at 13:28
  • May I suggest (I use something like that for the differential symbol in integrals) to use a value like, more or less, \mkern1.5mu. In my opinion, \,is a little too much (it is 3mu, i.e. 1/6em). – Bernard Jul 9 '16 at 14:25
  • I am open to suggestions about the spacing. i just took \, because it looked good to me but I do not know what for example AMS or IUPAC or the regarding ISO say about this. – Enno Jul 9 '16 at 14:32
1

Your mistake is: \if 2=1 does not evaluate the test 2=1, it compares two next tokens 2 and = and if these tokens are equal, then 1 is printed. But they are not equal, of course. You need to use \ifnum 2=1. Second mistake: your test is not finalized by \fi.

This is not reinventing the wheel. This is only a simple exercise of basic TeX programming. Your macro can be defined for example by:

\def\mfrac#1#2#3{{\partial\ifnum#1>1^{#1}\fi#2\over\partial#3\ifnum#1>1^{#1}\fi}}

IMHO, it is much more helpful for you to understand, where was your mistake and how conditionals at TeX primitive level work than to know the usage of a special LaTeX package.

  • Thanks for your reply @wipet. Your answer is detailed and the explanation helps me a lot. The usage of TeX primitive command is more meaningful :) – Ice0cean Jul 10 '16 at 1:09

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.