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.

## Best Answer

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:

The default

`<order>`

is empty, implying the first order partial derivative. If you want the default to be`2`

, modify the definition to readTechnically 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}`

:The optional argument beats this hands down.