1

Let's say in my preamble I define \DeclareMathOperator\rad{rad} and have the text

Let $f$ be a smooth function and $f^{\rad}$ its radial symmetrization.

It is inconvenient to have to enclose the \rad in curly braces. I could work around it by defining it as \newcommand\rad{{\operatorname{rad}}} instead. But at another place in my document I write

Let $n$ be a positive integer and $2 \rad(n)$ be twice its radical.

If I use \newcommand\rad{{\operatorname{rad}}}, there is no space between 2 and rad. (See newcommand vs. DeclareMathOperator)

So my question is: How can I allow a command to be used directly in superscript or subscript, while preserving the correct spacing when it is appears in the middle of a formula?


Related: Double subscript error with \newcommand?

3
  • 2
    enclosing \rad in braces shouldn't be seen as inconvenient, it is the (only) documented syntax for ^ in latex, which takes a brace delimited argument. The latex book always shows x^{2} for example even though x^2 works due to implementation details and lack of error checking. Jun 21, 2018 at 18:57
  • there we go...\^([^{\\]|\\[a-zA-Z]*) Jun 21, 2018 at 19:41
  • Remark: There's always the option of globally search-and-replace the document in LaTeX itself instead of externally, or maybe making ^ (and _) math-active, but they have other disadvantages.
    – user202729
    Jun 13, 2022 at 16:06

3 Answers 3

5

If you just use \rad in exponents and not really as an operator, then defining

\newcommand{\rad}{{\mathrm{rad}}}

will let you type

f^\rad

Note that \DeclareMathOperator is not a shorthand for getting \mathrm.

If you need \rad as an operator in other contexts, then no, you can't, for the same reason that ^\notin will produce an error.

On the other hand, is there a real reason for sparing a couple of braces? Any good TeX editor will supply the braces as soon as you enter ^. Adding them always will save you from head scratching when something goes wrong.

1

Why in the world would you use the same macro, \rad, for two completely different and unrelated operations, radical and radial symmetrization, just because both happen to start with the letters “rad”? Use different macros for semantically different operations. Apart from avoiding situations such as the one you describe, it also makes it easier to search trough the document for e.g. all occurrences of radial symmetrizations. You could even program these commands to automatically add entries to the index whenever they are used.

With regard to the question of f^\radial vs. f^{\radial}, do yourself a favour and always brace upper and lower indices, unless perhaps if you are dealing with extremely simple situations such as a_0, x^2, p_i, etc. It will be less error-prone, and frankly, f^\radial just looks weird, like something that only accidentally works (which is actually the case, as demonstrated by you).

\documentclass{article}

\usepackage{amsmath}

\DeclareMathOperator\rad{rad}
    % I would have used "\radical",
    % but this is already defined.

\newcommand\radial{{\mathrm{rad}}}

\begin{document}

\( \rad(n) = f^{\radial} \)

\end{document}
1
  • 1
    +1 for advising to use different semantic syntaxes when the same output means two different things.
    – daleif
    Mar 30, 2023 at 7:27
0

This is an analysis on why it's impossible if \rad is "just" a macro.

(of course you can redefine the dollar or redefine ^ and _ to be an active character etc., in which case it isn't too difficult, but it isn't covered here)


First, if you work through the source code and fully expand out the normal definition of \operatorname{rad}, it's equivalent to

\mathop{
    \newmcodes@
    \kern\z@
    \operator@font
    rad
} \nolimits@

Then, the code used to scan for a math subformula after a ^ is the following:

scan_math procedure in TeX

As you can see the allowed options are (only cur_cmd is considered thus implicit and explicit character token are both accepted)

  • something of catcode letter or other, or something \chardefed: either

    • its \mathcode is used (not useful to typeset multiple characters), or
    • if the mathcode is "8000 then it's recursively expanded as an active character. (reduces to other cases. The bracing does not help, try yourself:
    \mathcode `c="8000
    {\catcode`c\active\gdef c{ab}}
    $2^c$
    

    this code results in 2ab being typesetted)

  • \char ⟨number⟩: not useful, as above.

  • \mathchar ⟨number⟩: not useful, as above.

  • something \mathchardefed: not useful, as above.

  • \delimiter ⟨number⟩: not useful.

  • { ... }.

In the last case, the token must be {, and we recall that in normal context this will make it a \mathord (that is {,} is the same as \mathord,).

By the table of math spacing rules:

TeX by topic, table of math spacing rules

normally 2\rad will make it the case of row 0:ord and column 1:op, which always inserts "1:thin space" (regardless of math style) and 2{...} will make it the case of row 0:ord and column 0:ord = no space.

We can manually insert a \thinspace but this is obviously wrong when used in other cases e.g. a exponent. Even detecting the current mathstyle (with \mathchoice) can't help.


For academical purpose, it is probably also possible (without redefining either $ or ^ as I mentioned above) if you create a font such that some single glyph has the 3 characters rad, then proceed to define \rad to be a single \mathchardef of it.

You must log in to answer this question.

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