# Add to subscript in macro definition

I have the following command

\def\pderv#1#2{{#1}_{#2}}

which is supposed to denote partial derivatives. In some documents, I sometimes redefine it as the tradional

\def\pderv#1#2{\frac{\partial #1}{\partial #2}}

This works well in most cases. So that \pderv{f}{x} is either f_x or \frac{\partial f}{\partial x} depending on my definition.

However, sometimes I have a function f_i. In this case, I would like to denote the derivative with either f_{i,x} or \frac{\partial f_i}{\partial x}. The later is doable with the second definition. However, the first definition obviously makes it {f_i}_x.

Is there anyway to to modify the first definition so that if a subscripted expression is passed, like f_i, the output is f_{i,x}? I don't mind modifying the way I call the command (in both definitions) if that's the only way.

Obligatory MWE:

\documentclass{article}

\begin{document}
\def\pderv#1#2{\frac{\partial #1}{\partial #2}}
$\pderv{f}{x},\quad\pderv{f_{i}}{x}$

\def\pderv#1#2{{#1}_{#2}}
$\pderv{f}{x},\quad\pderv{f_{i}}{x}$
\end{document}

You can kill two birds with a stone using xparse and expl3.

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\pderv}{smm}
{
\IfBooleanTF{#1}
{% \pderv* uses the fraction mode
\frac{\partial #2}{\partial #3}
}
{% inline mode
\tohiko_pderv:nn { #2 } { #3 }
}
}

\tl_new:N \tohiko_pderv_function_tl
\tl_new:N \tohiko_pderv_variable_tl

\cs_new_protected:Nn \tohiko_pderv:nn
{
\regex_match:nnTF { _ } { #1 }
{% there is a subscript in the function name
\tl_set:Nn \tohiko_pderv_function_tl { #1 }
\tl_set:Nn \tohiko_pderv_variable_tl { #2 }
\regex_match:nnTF { _ .* \^ } { #1 }
{% there is a superscript after the subscript
\regex_replace_once:nnN
% match everything from _ to ^ and from ^ to the end
{ _ (.*) \^ (.*) \Z }
% replace with \sb{<subscript>,#2}^{<superscript>}
{ \c{sb}\cB\{\1,\u{tohiko_pderv_variable_tl}\cE\}\c{sp}\cB\{\2\cE\} }
% in
\tohiko_pderv_function_tl
% use the modified token list
\tl_use:N \tohiko_pderv_function_tl
}
{% there is no superscript after the subscript
\regex_replace_once:nnN
% match everything from _ to the end
{ _ (.*) \Z }
% replace with \sb{<subscript>,#2}
{ \c{sb}\cB\{\1,\u{tohiko_pderv_variable_tl}\cE\} }
% in
\tohiko_pderv_function_tl
% use the modified token list
\tl_use:N \tohiko_pderv_function_tl
}
}
{% no subscript
#1\sb{#2}
}
}

\ExplSyntaxOff

\begin{document}
$\pderv*{f}{x}\quad\pderv*{f_{i}}{x}\quad\pderv*{f_{i}^{2}}{x}\quad\pderv*{f^{2}_{i}}{x}$

$\pderv{f}{x}\quad\pderv{f_{i}}{x}\quad\pderv{f_{i}^{2}}{x}\quad\pderv{f^{2}_{i}}{x}$

\end{document}

If we use \pderv*{f}{x} we get the fraction form. Without the *, the first argument is scanned for a _. In this case everything from _ to the end of the argument is replaced by _{<whatever>,#2}. However, attention is kept on whether _{<subscript>} is followed by ^{<superscript>} and the appropriate actions are taken.

• Brilliant! Should I understand from this that this is not possible to do in LaTeX2? Also I just realized that this does not work for \pderv{f_i^2}{x} (but does work for \pderv{f^2_i}{x}). I don't know LaTeX3, so I am not sure how to fix the code or if it is indeed possible. – Tohiko Mar 31 '20 at 12:19
• @Tohiko Oh, superscripts! I'll try that. It is also possible with “legacy programming”, but much more complicated. – egreg Mar 31 '20 at 13:49
• @Tohiko The new version also deals with superscripts. – egreg Mar 31 '20 at 14:12