Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
    
My ignorance leads me to suspect this is the sort of thing LuaTeX may be great for... –  ShreevatsaR Jul 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. –  Andrew Stacey Jul 27 '10 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 '10 at 16:46
    
It's probably wiser to just define a macro and use that? –  SamB Dec 19 '10 at 5:59
add comment

3 Answers

up vote 15 down vote accepted

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

share|improve this answer
    
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? –  Andrew Stacey Jul 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 Robertson Jul 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. –  Andrew Stacey Jul 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 Robertson Jul 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. –  Andrew Stacey Sep 6 '10 at 10:07
show 3 more comments

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...

share|improve this answer
    
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). –  Andrew Stacey Jul 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 Robertson Jul 28 '10 at 14:24
add comment

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?

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.