5

In my thesis i need to use quite often the partial differential fraction like

\frac{\partial^\alpha x}{\partial y^\alpha }}

I therefore defined the command

\newcommand{\partialfrac}[3][]{\frac{\partial^ #1 #2}{\partial #3 ^ #1}}

That should be equal to the first one using

\partialfrac[\alpha]{x}{y}

That works if I define the optional #1 input. I would like to be able to skip the superscript if i want to (usually ^1 is not written...) but if I omit the optional input i receive an error. Is there a way to tell latex to "skip" the superscript somehow?

Thankyou

6

An expandable test would be:

\ifx\\#1\\%
  % #1 is empty
\else
  % #1 is not empty
\fi

Also ^#1 fails if #1 consists of several tokens. This is fixed by ^{#1}.

\documentclass{article}

\newcommand*{\partialfrac}[3][]{%
  \frac{%
    \partial \ifx\\#1\\\else^{#1}\fi #2%
  }{%
    \partial #3 \ifx\\#1\\\else^{#1}\fi
  }%
}

\begin{document}
\[
  \partialfrac{a}{b} = \partialfrac[\alpha]{x}{y}
\]
\end{document}

Result

4

I think there is still room for an xparse-based version. Thanks to \IfNoValueTF test, it is possible to check whether the optional first argument is present:

\documentclass{article}
\usepackage{xparse}

\NewDocumentCommand{\partialfrac}{o m m}{%
\IfNoValueTF{#1}{%
 \frac{\partial #2}{\partial #3}%
}{%
 \frac{\partial^{#1}#2}{\partial #3^{#1}}%
}%
}

\begin{document}
\[
  \partialfrac{a}{b} = \partialfrac[\alpha]{x}{y}
\]
\end{document}

The result:

enter image description here

3

use etoolbox to test for optional argument and act accordingly.

\newcommand{\partialfrac}[3][]{\frac{\partial\ifblank{#1}{}{^ #1} #2}
      {\partial #3 \ifblank{#1}{}{^ #1}}}

(untested)

1

Without a package, you can do it thus:

\newcommand{\partialfrac}[3][]{%
  \ifx\relax#1\relax\let\mysscript\relax\else\def\mysscript{^{#1}}\fi%
  \frac{\partial\mysscript #2}{\partial #3\mysscript}}
1

I'm sorry I opened this question: i tried for 1 hour before opening it and then found the solution 2 minutes after....

This

\newcommand{\partialfrac}[3][]{\frac{\partial^{#1}#2}{\partial#3^{#1}}}

works as intended.

Thank you very much

  • 1
    You will note that a slight horizontal space is introduces with the syntax ^{}, which may not be what you actually wish, when the argument is blank. – Steven B. Segletes Apr 28 '14 at 13:42
  • You are right... I will look for one of yours solution. – Luca Amerio Apr 28 '14 at 14: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.