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.

3 Answers
3

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

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.

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

2

@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

@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

\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.