I have macros of the form
\newcommand{\typo}[1]{{\color{red}#1}}% for pointing out that there is a typo
When I use this command in mathmode though, it creates incorrect spacing with binary operators ($A\otimes B$ versus $A\typo{\otimes}B$).
I suspect this is a consequence of writing \otimes within {...} (see Make sure the spacing of a macro for a binary operator), but {...} is necessary to keep the color contained. I could fix this issue by replacing $A\typo{\otimes}B$ with $A\mathbin{\typo{\otimes}}B$.
But is there a way to fix the definition of the macro \typo, so that $A\typo{\otimes}B$ will produce the same spacing as $A\otimes B$?
Here is a MWE:
\documentclass{article}
\usepackage{xcolor}
\newcommand{\typo}[1]{{\color{red}#1}}% for pointing out that there is a typo
\begin{document}
Compare $A\otimes B$ with $A\typo{\otimes}B$ with $A\mathbin{\typo{\otimes}}B$.
The first and third have correct spacing; the middle does not.
\end{document}

\begingroup ... \endgroupseems to be exactly what I was looking for, thank you! (The second solution does not work for me, because I do want to use\typoin non-math mode as well. -- or would I still be able to do so??) – Anna Jan 26 '22 at 00:32\newcommand{\typo}[1]{\csname\ifmmode math\else text\fi color\endcsname{red}{#1}}? – Ulrich Diez Jan 26 '22 at 02:23\csname? What is the advantage of your solution over the chosen solution? – Anna Jan 27 '22 at 07:27" so it works in text and math and picks up the finer spacing details for subscripts that \mathcolor has (try\typo{\sum}_0^n` ) – David Carlisle Jan 27 '22 at 09:11\csnamedo?" ;-) The search results, e.g., reveal the following question, to which, so it happens, I also wrote an answer as recently as December 16: What exactly do \csname and \endcsname do? . ;-) I don't know if my answer is understandable, but there are other answers as well, and there is also the possibility to ask for clarification via comments/via follow-up questions/via chat. :-) – Ulrich Diez Jan 27 '22 at 11:39\csname; my bad. The more important part of my comment was: What is the advantage of your suggestion over the solution given by David Carlisle? :-) – Anna Jan 28 '22 at 11:10\csname..\endcsname-thing selects the command depending on the current typesetting-mode, avoiding typesetting non-math-material via\mathcolor. To be honest, I didn't know whether this is an advantage with the variant of\mathcolorfrom the latex-dev branch, the usage whereof is suggested by David Carlisle: I just recently learned that there is a new implementation of\mathcolorin the development pre-release of the LaTeX kernel and I did not yet find the time to study all the code carefully. ... – Ulrich Diez Jan 28 '22 at 21:49\mathcolorfor typesetting non-math-material as well. David Carlisle told me ["it (\mathcolor) doesn't work well in text and we will probably make it error just as \mathrm` gives an error if used outside math."](https://chat.stackexchange.com/transcript/message/60284513#60284513) So selecting the appropriate coloring-command depending on the current typesetting-mode seems an advantage to me. ... – Ulrich Diez Jan 28 '22 at 21:50\mathcolor. Definitely not all of those are suitable for typesetting material outside math-mode and with those a mechanism for selecting\textcolorif not in math-mode definitely is an advantage. ;-) – Ulrich Diez Jan 28 '22 at 21:50\typoshould be robust and probably the argument can be omitted, i.e.,\csname @ifdefinable\endcsname{\typo}{\DeclareRobustCommand{\typo}{\csname\ifmmode math\else text\fi color\endcsname{red}}}. Unnecessarily wasting memory for an unneeded macro argument - I don't know what I was thinking! :-) – Ulrich Diez Jan 28 '22 at 22:13