5

I'm trying to create a differentiation command. At the moment I have the following:

\newcommand{\diff}[1][]{%
  \def\ArgI{{#1}}%
  \diffRelay%
}
\newcommand{\diffRelay}[2][]{%
  \frac{\textnormal{d}^{#1}\ArgI}{\textnormal{d}#2^{#1}}%
}

This allows me to use it like \diff{x}, diff[y]{x}, diff[y][2]{x} to create diff-by-x, diff-y-by-x, second-derivative-of-y-by-x. To simply write the n-th derivative of anything I have to use \diff[][n]{x}.

What I'm looking for is a way to check if the first optional argument is numerical, so I can omit the empty argument to create n-th derivative:

\newcommand{\diff}[1][]{%
  \def\ArgI{{#1}}%
  \diffRelay%
}
\newcommand{\diffRelay}[2][]{%
  \if \ArgI is numerical % better: \and #1 is empty
    \frac{\textnormal{d}^{\ArgI}}{\textnormal{d}#2^{\ArgI}}%
  \else
    \frac{\textnormal{d}^{#1}\ArgI}{\textnormal{d}#2^{#1}}%
}
1
  • how do you define "numerical" you can test for digits, but the example you actually give is n which is perhaps harder to distinguish from x in general, unless that was a meta-example... Commented May 18, 2015 at 9:20

2 Answers 2

4

I think you want something like this (which actually tests for a sequence of digits resulting in >0 so don't use [000] ;-)

\documentclass{article}

\newcommand{\diff}[1][]{%
  \def\ArgI{#1}%
  \diffRelay%
}
\def\eatrelax#1\relax{}
\newcommand{\diffRelay}[2][]{%
  \expandafter\eatrelax\ifnum0=0\ArgI\relax% is numerical % better: \and #1 is empty
    \frac{\textnormal{d}^{#1}\ArgI}{\textnormal{d}#2^{#1}}%
  \else\relax
    \frac{\textnormal{d}^{\ArgI}}{\textnormal{d}#2^{\ArgI}}%
 \fi
}

\begin{document}

$\diff[2]{x}$

$\diff[y]{x}$

$\diff[y][2]{x}$

\end{document}
6
  • While I decided to answer the question as asked, I'd agree with egreg that using a different input syntax would be better. Commented May 18, 2015 at 11:04
  • I did not understand \ifnum0=0. Can you explain about that?
    – Say OL
    Commented Jul 9, 2015 at 7:00
  • @SayOL if \argI is say 2 then \ifnum0=0\argI is \ifnum0=02.. which is false. If \argI is X then \ifnum0=0\argI is \ifnum0=0X.. which will test true and expand to X... In both cases you don't really want whatever you get but the \eatrelax cleans up afterwards. Commented Jul 9, 2015 at 8:26
  • I understand that part now. But in the definition of \eatrelax, why is it required to add open and close curly braces?
    – Say OL
    Commented Jul 9, 2015 at 8:58
  • @SayOL that is the syntax of \def you have to put the replacement text (nothing here) between {..} same as \newcommand\foo[1]{} but the site doesn't really support multiple questions in comments, feel free to construct a new question if you have a question:-) Commented Jul 9, 2015 at 13:40
4

You don't need the test for numeric, in my opinion. I suggest a different syntax:

\der[<function>]{<variable>}[<order>]

with two optional arguments; if you omit the first, you get the operator, otherwise the derivative; the second argument is the order of derivation.

With xparse it's easy to do it; the first optional argument has empty default value, whereas we use the \IfNoValueTF test for the second one, in order to avoid an empty superscript which would add an unwanted \scriptspace.

\documentclass{article}
\usepackage{xparse}

\NewDocumentCommand{\der}{O{}mo}{%
  \IfNoValueTF{#3}
    {\frac{\mathrm{d}#1}{\mathrm{d}#2}}
    {\frac{\mathrm{d}^{#3}#1}{\mathrm{d}#2^{#3}}}%
}

\begin{document}
\[
\der[y]{x}\quad
\der[y]{x}[2]\quad
\der[y]{x}[n]\quad
\der{x}\quad
\der{x}[2]\quad
\der{x}[n]
\]
\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .