7

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>}

\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}
\fooatletter{%
  \def\dddfrac#1{%
    \relaxen\dddfrac@i
    \relaxen\dddfrac@ii
    \relaxen\dddfrac@iii
    \def\dddfrac@i{#1}%
    \@ifnextchar\bgroup\dddfrac@getii\dddfrac@typeset}%
  % 
  \def\dddfrac@getii#1{%
    \def\dddfrac@ii{#1}%
    \@ifnextchar\bgroup\dddfrac@getiii\dddfrac@typeset}%
  %
  \def\dddfrac@getiii#1{%
    \def\dddfrac@iii{#1}%
    \dddfrac@typeset}%
  %
  \def\dddfrac@typeset{%
    \@ifundefined{dddfrac@iii}{%
      \@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.

1

3 Answers 3

12

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

\documentclass{article}

\usepackage{xparse}
\NewDocumentCommand{\dddfrac}{mG{}g}{%
  \IfNoValueTF{#3}
    {%
      \frac
        {\partial #2}
        {\partial #1}%
    }    
    {%
      \left (
      \frac{\partial #2}{\partial #1}
      \right )
      _{#3}
    }%
}

\begin{document}
$\dddfrac{x}$

$\dddfrac{y}{f_y}$

$\dddfrac{y}{f_y}{n}$
\end{document}

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.

5
  • I don't have the xparse package, so I have to ask: What would $\dddfrac{x}+1$ do?
    – yo'
    Commented Mar 17, 2012 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
    Commented Mar 17, 2012 at 14:25
  • Ok, thanks. These inconsistencies will kill me one day.
    – yo'
    Commented Mar 17, 2012 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
    Commented Mar 17, 2012 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'
    Commented Mar 17, 2012 at 16:49
6

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

\documentclass{scrartcl}

\usepackage{xparse}
\NewDocumentCommand{\dddfrac}{m O{} D(){}}{%
   \def\@tempa{#3}%
   \ifx\@tempa\@empty\else\left(\fi%
      \frac{%
         \partial #2
      }{%
         \partial #1
      }
   \ifx\@tempa\@empty\else\right)_{#3}\fi%
}

\begin{document}
$\dddfrac{x}$

$\dddfrac{y}[f_y]$

$\dddfrac{f(x)}(n)$

$\dddfrac{y}[f_y](n)$
\end{document}

Explanations

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

4

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

the syntax of the \newcommand command is:

\newcommand{\commandname}[numberOfArguments][defaultArgument]{implementation}

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.

1
  • I know that method. But it is imposing remembering three names instead of one. Commented Mar 17, 2012 at 13:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .