TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Say I have an expressions like $(\sin(x-3(z^{(x-2)^2}))+1)^2$ with many nested expressions. To better visualize this in the output (and to help finding missing delimiters when proof-reading) I'd like to have nested expressions and their delimiter show up in a different color than its surrounding. Is there a package for that purpose?

share|improve this question
I think while typing this is a feature your editor of choice has to provide. Packages would only influence the actual output and not your editor input. Most editors provide such syntax highlighting. Have a look through the options or let us know which one you are using – Martin H Oct 13 '11 at 11:08
@MartinH true, but I am actually looking for colored output, so I can TeX a longer calculation and have a nicer overview of nesting on the printout – Tobias Kienzler Oct 13 '11 at 11:14
You want Rainbow delimiters in the output? – Seamus Oct 13 '11 at 11:41
@Seamus: Well, maybe not too rainbowy, but yes. – Tobias Kienzler Oct 13 '11 at 11:45
@Whomever-tries-his-hand-at-that: I see three approaches: try to hack into the breqn package (which is aware of nesting since it makes that affect the badness of line-breaks), or into the nath package (which IIRC changes every ( to \left(: then hook to add a test on the \currentgrouplevel to each left and right delimiter), or a more down-to-earth approach by setting the \mathcode of ( etc. to be "8000, and defining an active ( to produce the right color and increment a nesting counter. – Bruno Le Floch Oct 13 '11 at 12:03
up vote 6 down vote accepted

I am not aware of any packages that do this. A relatively easy way is to redefine ( and ) as command sequences that keep count of the level and change the colour accordingly. That would look like this:



    \message{nesting error}%
  (test(multi(levels)colors)like this)

The amount of colors specified this way is limited, I couldn't think of something more elegant right now. Perhaps somebody has a suggestion how colors can be picked in a better way.

The result:

nesting colouring

And your example:

example nested colours

Note the exponents have the colour of the parentheses group they are in, this might not be what you actually want. It would require a bit of additional code to fix this. I think it looks horrible, but that is mainly due to the poor choice of colours on my end.

share|improve this answer
this looks good, thank you! I guess with a second counter one could even extend this to make sure neighbouring nested expressions like (this (one (is)) (a neighbour)) have different colors. Another extension would be including brackets ([]) and braces ({}), but then one could go even further and add a delimiter matching algorithm and... erm, I stop here and just accept your answer :-) – Tobias Kienzler Oct 13 '11 at 13:43
If you really want to you can take this quite far. This is just a basic working example to show (one of) the mechanism(s) you can use to achieve this kind of thing. If you are going to extend it into something more powerful, please let me know, I'd be interested to see how far it can be extended. Although I am unsure of how much demand there is for something like this :) – Roelof Spijker Oct 13 '11 at 13:48
true. But you're probably right concerning the lack of demand, it appears to work fine the way you wrote it, so unless someone else feels the desire for improvements I'd call YAGNI :-7 But I guess the exponential's color can be adapted the easiest by making sub- and superscripts raise the color nesting level as well – Tobias Kienzler Oct 13 '11 at 13:54
When you want characters to behave as if they were active in math mode, set their \mathcode to "8000, and define them for instance using \begingroup\lccode`~=`(\lowercase{\endgroup\def~} instead of \def(. Then they will still behave as expected outside math mode, an still be usable in arguments such as for \begin{picture}. – Bruno Le Floch Oct 13 '11 at 16:10
@Tobias: \lowercase is just there to make it easier to define an active character: in this precise case, it converts the active ~ into an active (. See for instance egreg's answers tex.stackexchange.com/questions/15744/… and tex.stackexchange.com/questions/28581/… – Bruno Le Floch Oct 14 '11 at 22:01

Your Answer


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.