March 22nd, 2009

beartato phd

(no subject)

Some of you might possibly be interested in this disgusting but effective TeX trick that I figured out: You can say \colorify{character}{color name} to make every instance of character a certain color in math mode. Color names work as usual for the color package. I like to do this to metavariables for different syntactic classes so that they stand out nicely.

It's moderately gross just to accomplish it for one particular choice of character, but the spectacularly gross bits are involved in packaging it up in a nice little function that takes a character as an argument and computes its math character code and sets up the right macros. I used pretty much every dirty TeX trick I know of: token lists, a counter, \expandafter, \afterassignment, \edef, and the truly wretched \lccode/\lowercase hack to get an arbitrary "active" character.

Ah, TeX. It really succeeds in combining the simple-to-understand control flow of aspect-oriented programming with the high-level expressivity of assembly language.

It has the unfortunate side-effect that you can no longer use that character in control sequences in math mode, since it relies on making it a so-called "active-character" in math mode. I think if you wanted all your Gs colored (for instance) and still wanted \Gamma you could maybe \let it to another named before you \colorifyed G? No apparently that works fine! Much to my surprise. I misjudged how active characters work. Maybe this is magic related to \mathcode "8000 that doesn't work for \catcode 13.


\documentclass{article}
\usepackage{color}

Collapse )

\colorify{A}{red}

\begin{document}

A$ABC$

\end{document}


This yields a black A, then a red A, then a black B and C, the latter three characters in italics.