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?

  • My ignorance leads me to suspect this is the sort of thing LuaTeX may be great for... Jul 27, 2010 at 12:59
  • It just feels as though there ought to be some way to say to TeX, "when you encounter an 'a', actually print 'a'" (and in a different colour) since that's - in effect - what happens when you change font. Jul 27, 2010 at 13:12
  • One can use tex.stackexchange.com/questions/363/… to parse all formulas in any way (for example \applytolist{calc} from Scott's answer there does somewhat similar (although more simple) task).
    – Grigory M
    Jul 27, 2010 at 16:46
  • It's probably wiser to just define a macro and use that?
    – SamB
    Dec 19, 2010 at 5:59

3 Answers 3


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:

\setmathfont{XITS Math}
\setmathfont[range="65,math-style=upright,Colour=FF0000]{XITS Math}

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 :)

  • That looks very interesting and might be just enough to switch me to lualatex (nothing against switching, just haven't on the "if it ain't broke ..." principle). Can you explain your last remark a little more? Do you mean that it only works with certain fonts? Jul 28, 2010 at 14:47
  • Right; it only works with OpenType maths fonts, of which there are few. XITS is the STIX fonts (which match times) with the necessary math parameters added; Asana Math is based on Palatino; and Cambria Math is proprietary and distributed with MS Office. Jul 29, 2010 at 5:10
  • I'm not getting this to work; I'm guessing that I need to upgrade my full texlive tree to 2010 (currently on 2009). I got the latest fontspec, expl3, and xpackages (as suggested on CTAN for unicode-math) but it's complaining about other stuff ... hang on, looking at the errors there's something deeper going on. kpsewhich fontspec.sty points to my local install of the newer version but the old version is being loaded. Jul 30, 2010 at 10:26
  • BTW, I'll try and get the colour stuff working next week before I go away... after that I can't promise anything, though -- have to finish my thesis! Jul 30, 2010 at 14:23
  • I've finally managed to get TeXLive 2010 installed and tested this. The second example is definitely promising and if you can get colour working I think that this would be fantastic. By the way, it didn't work with xelatex - it complained about the space in the name 'XITS Math' - but did work with lualatex. Sep 6, 2010 at 10:07

Obvious idea is to use something like

\catcode\`\x=13 \def x{something}`

but one need to do it only in formulas. Probably one can try to redefine \( to include \bgroup\catcode....

But it will break any commands having x in their name...

  • The breaking would be a problem. The s's in "\sin" and "s" should come out in different colours (note that they are already in different fonts so TeX can distinguish between them). Jul 27, 2010 at 11:24
  • 2
    Using math active characters would make this more robust. Look up \mathcode "8000 in tex by topic, for example. Jul 28, 2010 at 14:24

This is wild speculation, but you could define a new math environment which first scans the tokens in the input replacing some predefined set of symbols with their corresponding "colored" versions and leaving everything else the same. Finally, you do let LaTeX evaluate the resulting list of tokens.

Still, I'm not sure how many things would be broken by this. And diving inside of {} will also probably be tricky.

Really have no clue about this, but maybe this is the kind of things you could do with LuaTeX?

You must log in to answer this question.

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