Is there a package for warning about unbalanced delimiters?

I'm copy-editing a rather lengthy set theory book. One thing I'd like to check systematically is whether all left-right unambiguous delimiters (parentheses brackets curly, square and angle) are balanced.

I could write a Perl script to check for this, but was thinking that there might already be a LaTeX package which warns you about unbalanced delimiters. Is there such a thing?

-
A first pass would be to count the number of \{ and see if it matches the number of \} and so on. That would at least guarantee you have an even number of errors... –  Seamus Dec 3 '10 at 17:24

You might have a look at match_parens. This Ruby script helps in in balancing parentheses, braces, brackets - the characters {}[]()<> are supported.

It's written for source file checking, but perhaps it may help you also in checking output (perhaps after conversion to text) or in writing your own tool.

-

Most editors will provide the functionality of matching delimiters. And yes, a simple script could tell if your delimiters were balanced, and find delimiters which were not matched. But it won't be able to correct unmatched delimiters—only the user can say what he means.

When I'm writing I'm compiling every few minutes to make sure that the copy is what I want. If you're talking about matching delimiters related to TeX input, TeX will usually complain because your unintended mismatch will cause an error.

-
I'm talking about delimiters in the output not delimiters from the POV of TeX code. I know there are editors which match delimiters (e.g., Vim, which I use) but that's less useful when you're given a source file rather than typing it yourself. I'm also not looking for something which will do automatic correction, which I suspect is impossible to do reliably. –  uckelman Dec 3 '10 at 14:45
This sounds very hard to implement in TeX. Use perl. :-) –  Matthew Leingang Dec 3 '10 at 14:51
@Matthew: Really? Read the source using the e-TeX extension \readline, then iterate over every character using a delimited macro. Every time you see a (, increment a counter. Every time you see a ), decrement the counter. If the counter ever becomes negative or is positive at the end, there's an error. Not hard to implement in TeX, just a bit silly. –  TH. Jan 19 '11 at 0:13
@TH.: I think we are in agreement. What I meant by "hard" was "complicated enough that I would consider other methods", which is what I think you meant by "silly." But out of curiosity, how do you find an unmatched (? An unmatched ) would be easy—concurrent with the counter going negative. –  Matthew Leingang Jan 19 '11 at 14:06
@Matthew: If your counter is positive at the end, you have an unmatched left parenthesis. Remembering which ones are unmatched takes a bit more work. It's easiest with a stack (not terribly hard to implement in TeX, even if you don't use groups). Of course if you have a stack, then you have your counter too. Every element left on the stack (which would be a line number or (line, column) pair if you wanted to be fancy) is an unmatched parenthesis. –  TH. Jan 20 '11 at 3:34

I'm a bit late to the party, but I wrote a LaTeX parenthesis checker myself, because I got fed up with the sometimes very poor error reporting by TeX. The source file check-parens.cc (CTAN package) contains instructions on how to compile (requires C++x0 support) and run it. It uses a stack, just as the match_parens script, but recognizes some basic LaTeX syntax and more precise reporting of which parentheses fail to match.

-
Watch out for the false positives by cases like \left\{ x \right. :) –  percusse Jan 5 '12 at 23:48
@percursse: yes, I had given that some thought. For that reason, I make a distinction between "hard" and "soft" parentheses; the hard ones (like "{", "\(" and "\[") are required by (La)TeX syntax and must match, while the "soft" ones are document content and need not match: a non-match warning is issued, though. –  eldering Jan 6 '12 at 9:13
I will give it a try as soon as possible. –  percusse Jan 6 '12 at 11:27

I'm sure this is too late to help you, but ChkTeX will warn you if you mismatch ()[]{}. It also emits many other warnings, but you can configure it to only print information about mismatched parentheses. Or you could read the other warnings and become enlightened. :-)

-