1

Could anyone explain why the sub-indexes were placed not at their correct positions when color is used? enter image description here

With some further experiments I found that the problem can be fixed by surrounding the sub-indexes with more {}, but why without the extra {} the code does not produce correct output?

\documentclass[11pt]{article}
\usepackage{xcolor}
\newcommand{\dint}{\displaystyle \int}
\begin{document}
\[
\dint_1^e \frac{\ln x}{x} dx =
\dint^{\color{red} \ln e}_{\color{red} \ln 1} u ~du       %buggy  
=\dint_{\color{cyan}{\ln 1}}^{\color{cyan}{\ln e}} u ~du  %buggy
= \dint^{\ln e}_{\ln 1} u ~du           %without using color, it works fine 
\]
\end{document}

1 Answers1

2

\color works by setting a node which inserts the color using a backend-specific code. It then inserts via \aftergroup a node which restores the color. If you have

X^{\color{red} abc} _x

Then this ends up being

X^{\special{red} abc}\special{restore color} _x

and the subscript ends up subscripting the color restore node rather than the intended base.

Conversely with an extra group

X^{{\color{red} abc}} _x

The color restore stays in the superscript

    X^{{\special{red} abc}\special{restore color}} _x

so the subscript acts on the intended base.

Bernard
  • 271,350
David Carlisle
  • 757,742