# Is there a package to differently color nested expressions in the output?

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?

-
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

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:

\documentclass{article}

\usepackage{xcolor}

\makeatletter
\let\dotlessi\i
\catcode(=\active
\catcode)=\active
\newcounter{nestlvl}
\setcounter{nestlvl}{1}
\newcounter{temp}
\newcommand{\colorlst}{black,blue,red,green,yellow,brown}
\def({%
\setcounter{temp}{0}%
\@for\i:=\colorlst\do{%
\stepcounter{temp}%
\ifnum\arabic{temp}<\arabic{nestlvl}%
\color{\i}%
\fi%
}%
\char(%
}
\def){%
\setcounter{temp}{0}%
\ifnum\arabic{nestlvl}<0%
\message{nesting error}%
\fi%
\char)%
\@for\i:=\colorlst\do{%
\stepcounter{temp}%
\ifnum\arabic{temp}<\arabic{nestlvl}%
\color{\i}%
\fi%
}%
}
\let\i\dotlessi
\makeatother
\begin{document}
(test(multi(levels)colors)like this)
\end{document}


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:

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