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.

My ignorance leads me to suspect this is the sort of thing LuaTeX may be great for...
–
ShreevatsaRJul 27 '10 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.
–
Loop SpaceJul 27 '10 at 13:12

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

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?
–
Loop SpaceJul 28 '10 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.
–
Will RobertsonJul 29 '10 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.
–
Loop SpaceJul 30 '10 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!
–
Will RobertsonJul 30 '10 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.
–
Loop SpaceSep 6 '10 at 10:07

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).
–
Loop SpaceJul 27 '10 at 11:24

2

Using math active characters would make this more robust. Look up \mathcode "8000 in tex by topic, for example.
–
Will RobertsonJul 28 '10 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?