1

I have used \theta hundreds of times in my document. Now I decided that I would like all thetas to be in red color.

So I would like to redefine the command \theta to create a red theta.

MWE:

\documentclass{article}
\usepackage{xcolor}

\let\oldtheta\theta \renewcommand{\theta}{\mathcolor{red}{\oldtheta}} \newcommand{\thetaWrongSpacing}{{\mathcolor{red}{\oldtheta}}}

\begin{document}

$M^\theta$ %gives error message $\theta_1\oldtheta_1$ %works perfectly $M^\thetaWrongSpacing$ %works perfectly $\thetaWrongSpacing_1$ %wrong spacing $M^\alpha$ % seems to work perfectly, but David Carlisle says this should not be used according to the LaTeX manual

\end{document}

but then $M^\theta$ results in an error. I can fix this by enclosing the definition of the command with {}, but then the spacing is not correct.

How can I redefine \theta in the preamble without making any change within the document?

PS: \renewcommand{\theta}{\begingroup\mathcolor{red}{\oldtheta}\endgroup} does also produce an error for $M^\theta$ and flaws the spacing of \theta_1.

Jakob
  • 993

2 Answers2

2

The reason of bad spacing with colorized theta in you example is due to old concept of colors implementation. The colors in pdftex or XeTeX is based on inserted \pdcolorstacks (roughly speaking). They must be inserted when a color change starts and when the color is returned back, then it is inserted too. Roughly speaking:

{color-change text}_subscript

expands to

{pdfcolorstack text}pdfcolorstack_subscript

and the secnod pdfcolorstack is the reason of the problem.

On the other hand, if we are using a modern concept of colors (using attributes, provided by LuaTeX), then there isn't problem with spacing. For example, in OpTeX:

\let\oritheta=\theta

\def\theta{{\Red\oritheta}}

Test $a^\theta$ and $\theta_1$. \bye

Spacing is correct. Or you can use LuaLaTeX with appropriate coloring package based on color attributes.

Note1: your macro with \begingroup...\endgroup doesn't solve the spacing problem for subscripts. The \pdfcolorstack is here too and the unwanted kern 0.2779pt (from italic correction of theta) is here too. Your comment "works perfectly" in your source file isn't true.

Note2 (due to the discussion here): TeX was designed for humans, they create and read the source files of TeX. It is much more comfortable to write $M^\theta$ than $M^{\theta}$. So, we cannot prohibit the first syntax which is more suitable for humans.

wipet
  • 74,238
  • Consistency also helps humans make less syntax mistakes. Is it possible to add some minimal code with Lua attributes to demonstrate the method used by OpTeX? – yannisl Nov 19 '23 at 12:11
  • 1
    @YiannisLazarides the code snnipet that ends with \bye is the method to be used with OpTeX. The format use lua attributes for colors, and has color support by default. – Udi Fogiel Nov 19 '23 at 12:25
  • @wipet I have updated my question. Now everything is correct again I think. (I didn't had the \begingroup and \endgroup inside in the original question, so I removed it again and updated the PS) – Jakob Nov 19 '23 at 14:41
1

As wipet mentioned, you can use Lua attributes to color the glyphs without changing the spacing. In LaTeX you can do that using the package luacolor.

Result with xcolor

\documentclass{article}
\usepackage{xcolor}

\let\oldtheta\theta \renewcommand*{\theta}{{\mathcolor{red}\oldtheta}}

\begin{document}
$M^\theta M^\oldtheta$

$\theta_1 \oldtheta_1$

% check that the spacing is the same 
$M^\theta$\llap{$M^\oldtheta$}

$\theta_1$\llap{$\oldtheta_1$}

\end{document}

enter image description here

Result with luacolor

\documentclass{article}
\usepackage{luacolor}

\let\oldtheta\theta \renewcommand*{\theta}{{\color{red}\oldtheta}}

\begin{document}
$M^\theta M^\oldtheta$

$\theta_1 \oldtheta_1$

% check that the spacing is the same 
$M^\theta$\llap{$M^\oldtheta$}

$\theta_1$\llap{$\oldtheta_1$}

\end{document}

enter image description here

Udi Fogiel
  • 3,824