14

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?

1
  • 1
    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
    Commented Dec 3, 2010 at 17:24

4 Answers 4

8

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.

6

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.

5
  • Watch out for the false positives by cases like \left\{ x \right. :)
    – percusse
    Commented Jan 5, 2012 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. Commented Jan 6, 2012 at 9:13
  • I will give it a try as soon as possible.
    – percusse
    Commented Jan 6, 2012 at 11:27
  • Some years along, and I'm still thankful for this package - while the original link seems dead, CTAN still has it, and it's making life a lot easier. Excellent job at weeding out the false positives, and while not always spot on, the messages usually are very close to the source of the error. I haven't found better yet :) Commented Sep 14, 2019 at 20:21
  • @TatjanaHeuser that's nice to hear! The original link is dead indeed, and I've left academia in the mean time, so I'm not using LaTeX much anymore. I was working on some improvements to filter out more false positives, but that's been dormant for a while now. If you spot a bug though, I'm happy to try and fix it. Commented Sep 16, 2019 at 20:40
4

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.

7
  • 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
    Commented Dec 3, 2010 at 14:45
  • This sounds very hard to implement in TeX. Use perl. :-) Commented Dec 3, 2010 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.
    Commented Jan 19, 2011 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. Commented Jan 19, 2011 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.
    Commented Jan 20, 2011 at 3:34
3

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. :-)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .