3

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}

David Carlisle
  • 757,742
Anna
  • 89

1 Answers1

7

enter image description here

a \begingroup group does not affect spacing in the same way.

\documentclass{article} 
\usepackage{xcolor}
\newcommand{\typo}[1]{\begingroup\color{red}#1\endgroup}% 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}

Of if you use the next version of (x)color already available if you use pdflatex-dev rather than pdflatex in texlive you can use \mathcolor which uses a definition based on this idea (but handling superscripts in a better way)

\documentclass{article} 
\usepackage{color}
\usepackage{xcolor}
\newcommand{\typo}[1]{\mathcolor{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}

David Carlisle
  • 757,742
  • \begingroup ... \endgroup seems to be exactly what I was looking for, thank you! (The second solution does not work for me, because I do want to use \typo in non-math mode as well. -- or would I still be able to do so??) – Anna Jan 26 '22 at 00:32
  • 4
    @Anna How about \newcommand{\typo}[1]{\csname\ifmmode math\else text\fi color\endcsname{red}{#1}} ? – Ulrich Diez Jan 26 '22 at 02:23
  • @UlrichDiez, I don't know how to parse your solution, sorry! I see that your snippet is checking whether I am or am not in mathmode. What's \csname? What is the advantage of your solution over the chosen solution? – Anna Jan 27 '22 at 07:27
  • 1
    @Anna csname "constructs" a command so Ulrich's code is essentially: " if in math \mathcolor{...} else \textcolor{...}" 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
  • @Anna I mean no disrespect, but let me point out that you are in the right place at TeX-LaTeX-StackExchange to do a "Search on TeX - LaTeX..." for "What does \csname do?" ;-) 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
  • 1
    @Anna Regarding the fuzzy phrase "... whether I am or am not in mathmode...": When you work with TeX/LaTeX, it is very important to distance yourself from the machinery, so that if something doesn't work out, you can take it with humor. ;-)) – Ulrich Diez Jan 27 '22 at 11:43
  • @UlrichDiez I could indeed have looked up \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
  • @Anna The \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 \mathcolor from the latex-dev branch, the usage whereof is suggested by David Carlisle: I just recently learned that there is a new implementation of \mathcolor in 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
  • @Anna ... So I asked in chat whether there are drawbacks when using the command \mathcolor for 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
  • @Anna ... Besides this, in the past I saw various "home-brewed" definitions of \mathcolor. Definitely not all of those are suitable for typesetting material outside math-mode and with those a mechanism for selecting \textcolor if not in math-mode definitely is an advantage. ;-) – Ulrich Diez Jan 28 '22 at 21:50
  • @Anna Probably \typo should 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
  • @UlrichDiez Neither do I know what you were thinking :-D Thanks a lot for the in-depth explanation that goes far beyond my knowledge of it all. Hopefully, I'll appreciate your solution as much as it deserves eventually ;-) – Anna Jan 29 '22 at 23:22
  • @UlrichDiez Now, I suddenly get where your first comment came from: It was in reply to my own question, "or would I still be able to do so??". Took me only (!) a week to put 2 and 2 together. So: Please disregard my "What is the advantage of your solution over the chosen solution?". – Anna Feb 04 '22 at 06:08
  • @Anna "Took me only (!) a week to put 2 and 2 together." So you are a lot faster than I usually am. :-) – Ulrich Diez Feb 04 '22 at 13:53