I am exploring macros and expl3
by implementing simple functionalities. Here I am trying to implement the derivative operator, with order in the superscript and element (in case of a vector-valued function) in the subscript. I have included the output I obtain and the intended outputs in the 4 use-cases. Cases 1 and 2 work as intended, but for 3 and 4, the _
is output directly rather than the intended subscript. Would appreciate some guidance as to what I am doing wrong.
\documentclass[12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{xifthen}
\usepackage{xparse}
\usepackage{xstring}
\usepackage{amsmath}
\ExplSyntaxOn
\NewDocumentCommand \deriv { o }{
\int_case:nn { \str_count:N {#1} }
{
{0} {\mathrm{D}}
{1} {\mathrm{D}^{#1}}
{2} {
\ifthenelse{\equal{\str_item:Nn { #1 }{1}}{,}}{
\mathrm{D}_{\str_item:Nn {#1}{-1}}
}{
\mathrm{D}^{\str_item:Nn {#1}{1}}_{\str_item:Nn {#1}{-1}}
}
}
}
}
\ExplSyntaxOff
\begin{document}
\begin{enumerate}
\item Out: $\deriv[] f$; Intended: $\mathrm{D} f$
\item Out: $\deriv[n] f$; Intended: $\mathrm{D}^n f$
\item Out: $\deriv[,j] f$; Intended: $\mathrm{D}_j f$
\item Out: $\deriv[nj] f$; Intended: $\mathrm{D}^n_j f$
\end{enumerate}
\end{document}
\sb
instead of_
for starting subscripts. I'd not use\ifthenelse
: there is\str_if_eq:eeTF
instead.str_if_eq
but with the wrong types. Need to get my head around those. Will accept if you post this as an answer.