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.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

While writing lecture notes from statistical physics I found out, that I am often writing very similar long LaTeX sequences like

\frac{\partial }{\partial <foo>}

\frac{\partial <bar>}{\partial <foo>} and

\left( \frac{\partial <bar>}{\partial <foo>} \right)_{<baz>}

I wanted ease my life by using forms


\dddfrac{<foo>}{<bar>}, and

\dddfrac{<foo>}{<bar>}{<baz>} all with the same name, so they are easy to remember.

One friend of mine (who's help is unavailable now) helped me with the following TeX hackery:

%requires \usepackage{gmverb}
      \@ifundefined{dddfrac@ii}{% 1 argument only
        \frac{\partial }{\partial \dddfrac@i}
      }{% 2 arguments
        \frac{\partial \dddfrac@i}{\partial \dddfrac@ii}%
        }}{% 3 arguments
        \left( \frac{\partial \dddfrac@i}{\partial \dddfrac@ii} \right)_{\dddfrac@iii}%

I have experience in LaTeX, but cannot write in pure TeX (but I understand what the code does more-or-less).

Now although this works as it is, the code is ridiculously long to write if I want to have, say, 20 such commands. Is there any standard way of slipstreaming implementation of such behaviour, for a person who don't know how to program in pure TeX? Maybe someone already wrote package for this case, similar to e.g. the suffix package?

Second question: LaTeX has a tradition of introducing starred versions for commands with some extra functionality instead of sufixing name with e.g. "Ex" (like Microsoft did). Is it possible to go along this tradition with the \dddfrac command and define \dddfrac* with another set of definitions for different number of arguments?

Matbe the suffix will help? After all, this package allows for definitions of starred versions of commands or for a version of command with 1 argument and without. But the documenation is rather short and requires knowledge of TeX, which I humbly hope to be able to live without.

share|improve this question
Take look at the cool or esdiff package. Perhaps this question can be helpful, too. – Thorsten Donig Mar 17 '12 at 12:22
up vote 12 down vote accepted

The xparse package has been mentioned in other answers. This offers a generic way to define (LaTeX2e-like) document commands without programming. There are a few ways you could implement what you want. One way is


        {\partial #2}
        {\partial #1}%
      \left (
      \frac{\partial #2}{\partial #1}
      \right )




This sets up \dddfrac to require one mandatory argument (#1) and two arguments which are 'optional groups'. The first is set up with G{}, which means the default value is nothing at all. The second is set up as g, which will return a special marker \NoValue if it is not given. I use that to test for the last argument, which can then be used to tell is we need the \left ( ... \right ) pair. I've done the test once: you could do it twice and not need to repeat the \frac.

share|improve this answer
I don't have the xparse package, so I have to ask: What would $\dddfrac{x}+1$ do? – yo' Mar 17 '12 at 14:08
@tohecz The g argument looks for a brace group ({ ... }), so $\dddfrac{x}+1$ will not treat + as the second argument, and you'll get \frac{\partial}{\partial x} + 1. – Joseph Wright Mar 17 '12 at 14:25
Ok, thanks. These inconsistencies will kill me one day. – yo' Mar 17 '12 at 14:28
@tohecz I did not say I would recommend this input syntax. In general, I favour sticking to LaTeX standards, so either separate commands or optional arguments in [ ... ]. – Joseph Wright Mar 17 '12 at 16:44
I have to agree on this. Having a macro with optional number of obligatory-looking parameters is strange and I would not expect it in a standard class/package. The only exception IMHO is in preamble and inside special environments like picture. – yo' Mar 17 '12 at 16:49

With xparse it is easy to define a command with more than one optional argument.


\NewDocumentCommand{\dddfrac}{m O{} D(){}}{%
         \partial #2
         \partial #1






  • \NewDocumentCommand works similar to \newcommand but takes the argument specification as second argument.

  • In the argument specification we use m to declare a mandatory argument, O{<default>} to define an optional argument and D<delim 1><delim 2>{<default>} to define an optional argument delimited by <delim 1> and <delim 2>.

    You may replace D(){} by O{} and use \dddfrac{<foo>}[<bar>][<baz>]

  • We store the third argument #3 in \@tempa and compare it via \ifx with \@empty if it is empty we do nothing and otherwise (\else) we print the parens.

  • \makatletter enables @ to be part of control sequences

share|improve this answer

Well in LaTeX you have a command called \newcommand. You can write small macro's with it.

the syntax of the \newcommand command is:


so you can write your macro's the following way:

\newcommand{\dddafrac}[1]{\ensuremath{\frac{\partial }{\partial #1}}}
\newcommand{\dddbfrac}[2]{\ensuremath{\frac{\partial #1}{\partial #2}}}
\newcommand{\dddcfrac}[3]{\ensuremath{\left( \frac{\partial #1}{\partial #2} \right)_{#3}}}

I think it's quite clear what this code does. Using #1 you retrieve the first argument.

share|improve this answer
I know that method. But it is imposing remembering three names instead of one. – Adam Ryczkowski Mar 17 '12 at 13:31

Your Answer


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.