4

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}

Actual vs. Intended outputs

2
  • 2
    Use \sb instead of _ for starting subscripts. I'd not use \ifthenelse: there is \str_if_eq:eeTF instead.
    – egreg
    Commented Aug 31, 2021 at 16:19
  • Awesome! Done, on both counts! I had been trying str_if_eq but with the wrong types. Need to get my head around those. Will accept if you post this as an answer.
    – Py_Dream
    Commented Aug 31, 2021 at 16:33

1 Answer 1

5

You need \sb or \c_math_subscript_token instead, because _ is interpreted as a letter inside the scope of \ExplSyntaxOn.

\documentclass[12pt]{article}
\usepackage{amsmath}

\ExplSyntaxOn

\NewDocumentCommand \deriv { O{} }
  {
    \int_case:nn { \str_count:N {#1} }
      {
       {0} {\mathrm{D}}
       {1} {\mathrm{D}^{#1}}
       {2} {
             \str_if_eq:eeTF { \str_item:Nn { #1 }{1} } {,}
               {
                 \mathrm{D}\sb{\str_item:Nn {#1}{2}}
               }
               {
                 \mathrm{D}^{\str_item:Nn {#1}{1}}\sb{\str_item:Nn {#1}{2}}
               }
           }
     }
  }
\ExplSyntaxOff

\begin{document}

\begin{enumerate}
    \item Out: $\deriv f$; Intended: $\mathrm{D} f$
    \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}

Using \ifthenelse is unnecessary. You need \usepackage{xparse} only if your LaTeX is not older than 2020-10-01.

I changed the argument specifier from o to O{}, so if missing it's the same as using [].

enter image description here

1
  • Thank you! Is it also better to use \sp instead of ^ for superscripts?
    – Py_Dream
    Commented Aug 31, 2021 at 16:43

You must log in to answer this question.

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