I am looking to define the custom command \distr as follows:
\newcommand{\distr}[1]{\underbracket[0.3pt][1pt]{#1}}
The command \underbracket is the one appearing in mathtools. However, I see (at least) two issues upon compiling: (1) when e.g. distr{f} appears in a subscript/superscript, the character f retains its usual size instead of being shrinked to \small; (2) sometimes there is unwanted spacing around \distr{ }, for example when script letters appear before it (like \mathscr{L}\distr{\psi}). How do I implement \distr in such a way to avoid these issues? Apart from mathtools, I am using standard mathematics symbol packages such as amsmath, physics, and amssymb.
MWE:
\documentclass[preview]{standalone}
\usepackage{mathtools}
\usepackage[cal=euler]{mathalfa}
\newcommand{\distr}[1]{\underbracket[0.3pt][1pt]{#1}}
\begin{document}
\begin{equation}
a_f\ a_{\distr{f}} \qquad \mathcal{L}\psi\ \mathcal{L}\distr{\psi}
\end{equation}
\end{document}

\mathcal{L}\!\distr{\psi}. – Werner Nov 23 '21 at 18:59\!in the definition of\distrin such a way that it shows up only when needed (i.e. when a character such as\mathcal{L}precedes it). – giobrach Nov 23 '21 at 20:03\mathcalto check if it's followed by\distr(rather than defining\distrto look back; it's easy peeking ahead). If so, it inserts\!. – Werner Nov 23 '21 at 20:09\distr, not just those of the calligraphic variety. Is there a way to redefine\distrin the same fashion (maybe with something like\@ifprevchar, if it exists)? If you care to include it in the answer, I would be glad to accept it. – giobrach Nov 23 '21 at 20:16\distrwith{ }. Thanks for everything! – giobrach Nov 23 '21 at 20:21\underbracket... – Werner Nov 23 '21 at 20:25