Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

share|improve this question

5 Answers 5

up vote 6 down vote accepted

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

share|improve this answer

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

share|improve this answer

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)

share|improve this answer

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}}
share|improve this answer

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

share|improve this answer
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 at 13:42
    
You are right... I will look for one of yours solution. –  Luca Amerio Apr 28 at 14:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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