7

I would like to mark revised parts of a large document in color, and up to now always used

\usepackage{xcolor}
\newcommand\rev[1]{\textcolor{red}{#1}}

to then do

Lorem ipsum dolor \rev{git} amet, consetetur sadipscing elitr,

This works well, but unfortunately influences the placement of glyphs sometimes, e.g.,

\documentclass{article}

\setlength{\parindent}{0pt}

\usepackage{amsmath}                                                        
\usepackage{xcolor}
\newcommand\rev[1]{\textcolor{red}{#1}}

\begin{document}

AV\\
\rev{A}V

\begin{align}
&U \oplus V\\
&U \rev{\oplus} V
\end{align}

\end{document}

gives

AV spacing oplus spacing

What is a better way of coloring parts of a document?

3
  • 1
    Please make your code compilable (if possible), or at least complete it with \documentclass{...}, the required \usepackage's, \begin{document}, and \end{document}. That may seem tedious to you, but think of the extra work it represents for TeX.SX users willing to give you a hand. Help them help you: remove that one hurdle between you and a solution to your problem.
    – jub0bs
    Jun 6, 2014 at 9:58
  • I think it has to do with the fact that \oplus is math, whereas your defined color is using \textcolor, which is used for text... but that's just my guess Jun 6, 2014 at 9:58
  • 3
    Related: tex.stackexchange.com/a/85035/31034
    – user31034
    Jun 6, 2014 at 10:15

2 Answers 2

4
  1. Color is implemented via whatsits (\special, \pdfliteral). These will destroy the implicit kerning as shown in the example with AV.

    LuaTeX provides a way for a complete different implementation of color via attributes and not via whatsits. Package luacolor goes this new way.

  2. The problem in math is different. LaTeX implements colors via grouping. The color is set at the start of the group and reset after the group via \aftergroup. However \textcolor uses curly braces instead of \begingroup and \endgroup. Curly braces have a side effect in math, they form a subformula with \mathord spacing like ordinary symbols. See the answer, which is cited already in the comment from ferahfeze.

The example of the question will work, if both package luacolor and using \begingroup/\endgroup instead of curly braces are used:

% lualatex
\documentclass[fleqn]{article}

\setlength{\parindent}{0pt}

\usepackage{amsmath}
\usepackage{xcolor}
\usepackage{luacolor}
\newcommand\rev[1]{%
  \protect\leavevmode
  \begingroup
    \color{red}%
    #1%
  \endgroup
}
 
\begin{document}

AV\\
\rev{A}V
%
\begin{align*}
&U \oplus V\\
&U \rev{\oplus} V
\end{align*}

\end{document}

Result

Refinement for package xspace

\xspace detects } (via \egroup) and suppresses the space. But \endgroup is not detected. The following refinement uses \begingroup and \endgroup only in math mode, where spaces does not matter:

\documentclass[fleqn]{article}

\setlength{\parindent}{0pt}

\usepackage{amsmath}
\usepackage{xspace}
\usepackage{xcolor}
\usepackage{luacolor}
\newcommand*{\rev}{}
\DeclareRobustCommand{\rev}[1]{%
  \ifmmode
    \begingroup
      \color{red}%
      #1%
    \endgroup
  \else
    \textcolor{red}{#1}%
  \fi
}

\begin{document}

AV\\
\rev{A}V
%
\begin{align*}
&U \oplus V\\
&U \rev{\oplus} V
\end{align*}

\newcommand{\X}{X\xspace}
\X Y\\
\rev{\X} Y\\
$\X Y$\\
$\rev{\X} Y$

\end{document}

Result xspace

1
  • That't much better. Great post. And good to read that it does not work in pdfLaTeX... got a bit frustrated when searching for an answer.
    – LaRiFaRi
    Jun 6, 2014 at 11:34
1

I fear, the "AV" case is too complicated. LaTeX does not kern over the brackets. It wouldn't do a kern between different fonts (emphasized, bold...), too. You can do some reading here. Quite difficult and I didn't manage to solve your case. I hope, the marking of just one single letter is not that often in your work. If it really disturbs you, you will have to add a manual kerning to it.

For the math case, you have to ensure that the colouring does not change your highlight from math- to textmode. I added this solution below. I hope, there will be a better solution around, but I got the feeling, that you should ask a second question here on how to kern \textcolor{red}{V}A and \textcolor{red}{AV}A correctly.

% arara: pdflatex

\documentclass{article}
\usepackage{mathtools}                                                        
\usepackage{xcolor}

\newcommand*{\rev}[1]{%
  \protect\leavevmode
  \begingroup
    \color{red}#1%
  \endgroup
    }

\begin{document}
\centering
AV\\
\rev{A}\kern-0.26ex V
\begin{align}
&U \oplus V\\
&U \rev{\oplus} V
\end{align}
\end{document}

enter image description here

You must log in to answer this question.

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