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.

I defined a macro for writing a partial derivative:

\newcommand{\pder}[2]{\frac{\partial#1}{\partial#2}}

this works when inserting two parameters. But I want to modify it so that when inserting only a single parameter, it will leave the numerator blank and fill only the denominator with that parameter. i.e.,

\pder{x}

will produce

\frac{\partial}{\partial x}

How can I achieve that?

share|improve this question
1  
Also see this related question on a flexible-partial-derivative-macro. –  Peter Grill Sep 23 '11 at 16:30
add comment

4 Answers

up vote 12 down vote accepted

The best approach is to make the numerator variable optional:

\newcommand{\pder}[2][]{\frac{\partial#1}{\partial#2}}

Now \pder[f]{x} and \pder{x} will work as you wish.

A solution that uses the syntaxes \pder{f}{x} and \pder{x} is

\makeatletter
\DeclareRobustCommand{\pder}[1]{%
  \@ifnextchar\bgroup{\@pder{#1}}{\@pder{}{#1}}}
\newcommand{\@pder}[2]{\frac{\partial#1}{\partial#2}}
\makeatother

which however is quite risky, because an open brace after \pder{x} will be mistaken for the start of the second argument (spaces are ininfluent). All in all, the optional argument path seems better.

If you don't plan to often use \pder inside moving arguments, \DeclareRobustCommand can be changed into \newcommand, but the command would be fragile and so needing \protect in front of it when in moving arguments.

share|improve this answer
    
It works alright, but is it possible to define it so that the commmand \pder{y}{x} is still a valid one, instead of the square brackets? as I find this form (curly braces) nicer. –  PineApple Sep 22 '11 at 11:37
    
@PineApple See edited answer –  egreg Sep 22 '11 at 12:55
add comment

In addition to following the excellent answers of egreg and Ant, who both suggest to use a definition of the \pder macro where the first (numerator) argument is explicitly optional, you could also just keep your existing macro but insert a pair of empty braces, like {}, whenever you want the numerator to consist of just the partial symbol:

$\pder{f(x,y,z)}{x}$ and $\pder{}{x} f(x,y,z)$

Not as elegant as the other method, for sure, but this way your code will immediately make clear if you accidentally forgot to specify one or the other argument of the \pder command. Happy TeXing!

share|improve this answer
add comment

You can make #1 an optional parameter by providing a default value for it (in this case nothing at all):

(edited to fix an accidental paste of the original definition - thanks @Gonzalo for spotting this!)

\newcommand{\pder}[2][]{\frac{\partial#1}{\partial#2}}

This allows

$\pder{x}$

and

$\pder[y]{x}$

(note the square brackets for the now optional argument).

share|improve this answer
1  
Your definition, as it is, requires two mandatory arguments, so $\pder{x}$ won't work and $\pder[y]{x}$ will produce undesired results. If you want the first argument to be optional with no default value, your definition should be \newcommand{\pder}[2][]{\frac{\partial#1}{\partial#2}}. –  Gonzalo Medina Sep 22 '11 at 11:57
    
Edited to fix. Sorry -- I managed to paste the original version rather than my changed one from my text editor! –  Ant Sep 22 '11 at 13:29
add comment

use it as an optional argument:

\documentclass{article}
\newcommand\pder[2][]{\ensuremath{\frac{\partial#1}{\partial#2}}} 
\begin{document}
\pder{x} will produce $\frac{\partial}{\partial x}$

\pder[x]{y} will produce $\frac{\partial x}{\partial y}$

\end{document}

enter image description here

share|improve this answer
1  
Why do you use two extra macros and \@ifnextchar instead of the regular optional argument syntax like ant and egreg? –  Tobi Sep 22 '11 at 11:21
add comment

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.