[Tex/LaTex] change the font and colour of a letter permanently

colorfontsmacrossymbols

For my lectures, I like to colour the symbols in the equations to make it easier to follow what's going on – a bit like syntax highlighting. So all vector spaces are one colour, all sets another, e is in roman font, and so forth. At the moment, I do this by defining new macros for each symbol, so a typical line reads something like this:

Define \(\tyz^\tyw \coloneqq \tye^{\tyw \ty\ln(\tyz)}\)

I'd really like to be able to just type

Define \(z^w \coloneqq e^{w \ln(z)}\)

To do this, I'd need to be able to tell TeX that the maths symbol for, say, 'e' was not 'e' but actually 'e' in roman font and with a particular colour.

Does anyone have any ideas on how to do this?

Best Answer

In regular latex you can choose different fonts for different symbols, but not different colours. If you don't mind using xelatex or lualatex, however, you could try my new package "Unicode-math" which does allow you to do this sort of thing. The interface isn't that great, yet, but the general idea is

\setmathfont{XITS Math}
\setmathfont[range= <Unicode slot for char you want>, Color=red]
            {XITS Math}

Limited by the number of fonts you can use at the moment, I'm afraid.

Update: Here's an actual example:

\documentclass{article}
\usepackage{unicode-math}
\begin{document}
\setmathfont{XITS Math}
\setmathfont[range="65,math-style=upright,Colour=FF0000]{XITS Math}
$def$
\end{document}

It doesn't work if you put Colour before the math-style, but I can't explain why that is right now. I also can't justify why you need to use "65 rather than "1D452. It seems my code has taken on a life of its own :)