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

I'm trying to define a custom command for use in math mode, defined as

\newcommand{\deriv2}[2]{\ensuremath{\frac{\partial^2 {#1}}{\partial {#2}^2}}}

Other non-math mode \newcommands work in my document, but as soon as I put one into math mode I get the error messages:

! LaTeX Error: Missing \begin{document}

! You can't use `macro parameter character #' in math mode.

I have also tried defining my \newcommand without \ensuremath, and in all combinations of calling it within the equation environment, $$, and \[ \]. I've also tested it without the arguments #1 and #2, but any \newcommand I make seems to fail in math mode.

Now, I know one can roll their own commands to save tedium in math mode, and even pass arguments to them.

share|improve this question
You can't define a command with a digit in its name – egreg Feb 14 '12 at 15:32
You can't define commands with numbers in their name. – Ulrike Fischer Feb 14 '12 at 15:34
Ok, so it really was a simple matter of syntax: dropping the '2' from '\deriv2' did fix it. Thanks for the swift responses to egreg and Ulrike Fischer! – aejsk Feb 14 '12 at 15:47
What do we do with this question now? Close as too localised? – Seamus Feb 14 '12 at 16:14
You could also use \pderiv[2]{...}{...} from cool. – rdhs Feb 14 '12 at 16:33
up vote 5 down vote accepted

Here is an alternative to your current situation - using an optional argument to specify the derivative order. This way you don't have to define a macro for "each" derivative:


Here's a minimal example:

enter image description here

\newcommand{\deriv}[3][]{% \deriv[<order>]{<func>}{<var>}
  \ensuremath{\frac{\partial^{#1} {#2}}{\partial {#3}^{#1}}}}
In text mode there is~\deriv{y}{x} and~\deriv[2]{y}{x}. In display mode there is
  \deriv{y}{x}\ \textrm{and}\ \deriv[2]{y}{x}\rlap{.}

The default <order> is empty, implying the first order partial derivative. If you want the default to be 2, modify the definition to read


Technically it is possible to use a macro with numbers in them, but the usage is much less intuitive than adding something like an optional argument (as given above). Here's an implementation that now allows you to use \nameuse{deriv2}{y}{x}:

\expandafter\def\csname deriv2\endcsname#1#2{%
  \ensuremath{\frac{\partial^2 {#1}}{\partial {#2}^2}}}

The optional argument beats this hands down.

share|improve this answer

you can fool TeX and use the 2 as a parameter:

\newcommand\deriv[3]{\ensuremath{\frac{\partial^2 {#2}}{\partial {#3}^2}}}

now you can use \deriv2{x}{y}. But that works only when there is no \deriv command

share|improve this answer
This also only works for single-letter first-arguments. But who's interested in (say) the 11th-order partial derivative, right? ;) – Werner Feb 14 '12 at 19:39
did I said something different? – Herbert Feb 14 '12 at 19:40

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.