2

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}

giobrach
  • 369

1 Answers1

4

You can use \mathpalette to distinguish between the different math styles:

enter image description here

\documentclass{article}

\usepackage{mathtools}

\makeatletter \newcommand{@distr}[2]{\underbracket[0.3pt][1pt]{#1#2}} \newcommand{\distr}[1]{\mathpalette{@distr}{#1}} \makeatother

\begin{document}

[ a_f\ a_{\distr{f}} \qquad \mathcal{L}\psi\ \mathcal{L}\distr{\psi} ]

\end{document}

See The mysteries of \mathpalette for a discussion on how \mathpalette works.

Werner
  • 603,163
  • Thank you for your input. Any suggestions for the spacing issue? – giobrach Nov 23 '21 at 17:39
  • @giobrach: Perhaps use \mathcal{L}\!\distr{\psi}. – Werner Nov 23 '21 at 18:59
  • I had thought of that, but I wonder whether there is a way to include \! in the definition of \distr in 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
  • @giobrach: Try this code. It redefines \mathcal to check if it's followed by \distr (rather than defining \distr to look back; it's easy peeking ahead). If so, it inserts \!. – Werner Nov 23 '21 at 20:09
  • Thank you! Actually, I've determined that the issue appears for all characters appearing before \distr, not just those of the calligraphic variety. Is there a way to redefine \distr in 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
  • Ok, solved! I just needed to surround the definition of \distr with { }. Thanks for everything! – giobrach Nov 23 '21 at 20:21
  • @giobrach: You can also try this option, which removes the horizontal spacing that could be introduced by \underbracket... – Werner Nov 23 '21 at 20:25
  • Even better! Thanks! – giobrach Nov 23 '21 at 20:27