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.

12
  • 4
    note that ^\theta is not supported latex syntax (you are supposed to use braces) Nov 16, 2023 at 12:30
  • 3
    in some simple cases it might seem to work, but, as you see here, it does not work in general and the latex manual never mentions that syntax. Nov 16, 2023 at 12:36
  • 1
    @Jakob for a simpler example try \newcommand\hmm{10} $M^{\hmm} = M^\hmm$ Nov 16, 2023 at 12:37
  • 1
    Only because some syntax is commonly used doesn't necessarily mean it is correct ... There are lots of examples where syntax is still abundantly used despite being deprecated. Nov 16, 2023 at 12:46
  • 1
    I see that this commonly used syntax is officially wrong and we shoudl not use it in teh future, but I would still be interested if there is a fix for my problem (other than search and replace all ^\theta by ^{\theta} and to convince my coauthors not to use ^\theta anymore.
    – Jakob
    Nov 16, 2023 at 12:49

2 Answers 2

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.

3
  • 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, 2023 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, 2023 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, 2023 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

You must log in to answer this question.

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