I often have to write things like

$left(a \left(b \left( c ... \right) \right) \right)$

And in the final output, its quite hard to distinguish which brackets go together.

Now, in some editors, there are features that brackets that match are same-colored, like "red( blue( green( green) blue) red)".

Is that possible, somehow, to be done by LaTeX automatically? Matching left- and right-brackets shouldn't be a thing with the \left-\right-markers, and doing something like building up a color-list that's just "tracked" somehow is easy in other programming languages as well, e.g. in perl with push and pop from an @array.

But I'm not a very good LaTeX-programmer. So, does this feature exist already? Or is it anyhow easy to be implemented?


  • 2
    NAA but the usually followed order is ...{[({[(...)]})]}... That's usually one way to tell what goes with what – Au101 Dec 1 '16 at 19:50
  • 2
    Welcome to TeX.SE. Are you habitually prefixing \left and \right to math "fences" -- round parentheses, square brackets, and curly braces? If so, do read the following postings: Is it ever bad to use \left and \right and “(” or “\left(” parentheses. – Mico Dec 1 '16 at 20:12

How about this:

    \expandafter\def\csname bracketcolor\the\bracketnum\endcsname{\color{#1}}%
    \csname bracketcolor\the\bracketnum\endcsname
    \csname bracketcolor\the\bracketnum\endcsname


\[\left( 1 +\left(2+\left(3+4\left(\right)\right)a\right)\right)\]

\[\color{orange}\left( 1 +\left(2+\left(3+4\left(\right)\right)a\right)\right)\]

You can set the colors you want by saying \makecolorlist{color1,color2,...,colorn}. If you use more than n nested braces, after that point it stop applying (just use the background color). The output is:

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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