23

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.

6
  • 16
    You can't define a command with a digit in its name
    – egreg
    Commented Feb 14, 2012 at 15:32
  • 6
    You can't define commands with numbers in their name. Commented Feb 14, 2012 at 15:34
  • 1
    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
    Commented Feb 14, 2012 at 15:47
  • What do we do with this question now? Close as too localised?
    – Seamus
    Commented Feb 14, 2012 at 16:14
  • 1
    You could also use \pderiv[2]{...}{...} from cool.
    – Chel
    Commented Feb 14, 2012 at 16:33

3 Answers 3

20

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:

\deriv[<order>]{<func>}{<var>}

Here's a minimal example:

enter image description here

\documentclass{article}
\newcommand{\deriv}[3][]{% \deriv[<order>]{<func>}{<var>}
  \ensuremath{\frac{\partial^{#1} {#2}}{\partial {#3}^{#1}}}}
\begin{document}
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{.}
\]
\end{document}

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

\newcommand{\deriv}[3][2]{...}

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}}}
\makeatletter
\newcommand{\nameuse}[1]{\@nameuse{#1}}%
\makeatother

The optional argument beats this hands down.

7

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

4
  • 6
    This also only works for single-letter first-arguments. But who's interested in (say) the 11th-order partial derivative, right? ;)
    – Werner
    Commented Feb 14, 2012 at 19:39
  • 1
    did I said something different?
    – user2478
    Commented Feb 14, 2012 at 19:40
  • 3
    @Herbert: While you didn't make a false statement, you omitted an important and somewhat surprising piece of information. Your response to Werner implies that this omission is unimportant -- but that's untrue, and a bit peevish to boot. Commented Nov 22, 2018 at 10:59
  • no, I gave a solution to what the aejsk asked and of course, in math we do not have derivations more than four ...
    – user2478
    Commented Nov 22, 2018 at 11:12
5

Since from 2019-07-21 there is a new package called derivative. It is very simple to use. Some very simple macros have been defined into this package.

enter image description here

\documentclass[a4paper,12pt]{article}
\usepackage{derivative}
\begin{document}
In text mode there is $\pdv{y}{x}, \quad \pdv[2]{y}{x}$. 
In display mode there is
\[\pdv{y}{x}, \quad \pdv[2]{y}{x}.\]
\end{document}

I have added a screenshot of page 7 to obtain the code of the your question.

enter image description here

You must log in to answer this question.

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