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}

1 Answer 1

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}
14
  • \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 at 0:32
  • 4
    @Anna How about \newcommand{\typo}[1]{\csname\ifmmode math\else text\fi color\endcsname{red}{#1}} ? Jan 26 at 2: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 at 7: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 ) Jan 27 at 9:11
  • 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. ;-)) Jan 27 at 11:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.