2

I've create a command to quickly insert derivative operator for my ODE class, but I want to two of the four fields to default to certain values because they will be the most used, but potentially will need to be changed.

\newcommand{\dydx}[4]{\frac{d^{#1}{#2}}{d{#3}^{#4}}}

I'd like #2 and #3 to be y and x respectively by default but capable of being changed when I need to like \dxdy{2}{u}{t}{2}

  • I suppose your example is \dydx, not \dxdy. B.t.w., how can #1 and #4 be different? – Bernard Jan 11 '18 at 0:23
  • Yes, you're correct on both accounts. Became so used to typing it I didn't even consider they were the same. – Scott Bean Jan 12 '18 at 1:08
4

If you don't mind a change in the syntax,

\documentclass{article}
\usepackage{amsmath}
\usepackage{xparse}

\NewDocumentCommand{\dydx}{O{y}mO{x}}{%
  \frac
    {d\ifnum#2=1\else^{#2}\fi#1}
    {d#3\ifnum#2=1\else^{#2}\fi}%
}

\begin{document}

\[
\dydx{2}+\dydx[u]{2}[t]+\dydx[v]{1}+\dydx{3}[t]
\]

\end{document}

enter image description here

Maybe you'll like the following way, with the same output.

\documentclass{article}
\usepackage{amsmath}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\dydx}{O{}}
 {
  \group_begin:
  \keys_set:nn { scott/dydx } { #1 }
  \scott_dydx:
  \group_end:
 }

\keys_define:nn { scott/dydx }
 {
  x .tl_set:N = \l__scott_dydx_x_tl,
  y .tl_set:N = \l__scott_dydx_y_tl,
  n .tl_set:N = \l__scott_dydx_n_tl,
  x .initial:n = x,
  y .initial:n = y,
 }
\cs_new_protected:Nn \scott_dydx:
 {
  \frac
   {
    d
    \tl_if_empty:NF \l__scott_dydx_n_tl { ^{\l__scott_dydx_n_tl } }
    \l__scott_dydx_y_tl
   }
   {
    d
    \l__scott_dydx_x_tl
    \tl_if_empty:NF \l__scott_dydx_n_tl { ^{\l__scott_dydx_n_tl } }
   }
 }
\ExplSyntaxOff

\begin{document}

\[
\dydx[n=2]+\dydx[y=u,x=t,n=2]+\dydx[y=v]+\dydx[n=3,x=t]
\]

\end{document}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.