I didn’t think we had any serious math build-up bugs thanks to very extensive testing, but there was at least one bug anyhow. (If you find such bugs, please let me know by email or as a comment on a post). This one was quite simple and it existed in both Office 2010 and 2013. It was easy for me to fix and for end users to work around, as this post shows. And the post has a happy ending.

The bug was that build-up of a linear format expression like (n+1)!^2 effectively hung Word! The hang is not Word’s fault; my build-up code inserted an invalid math tree into Word’s backing store and the LineServices math handler gave up on it. You could ctrl+z (undo) out of the hang if that occurred to you. The problem was that the code to identify the factorial as the base of the superscript object identified only the exclamation point and the single character preceding the exclamation point. In the bug scenario, the preceding character is the end delimiter for a math object, specifically the parenthesized expression (n+1). Correct code includes the whole expression since that and the exclamation point comprise the factorial that’s being squared.

To gain a little insight into the importance of the bug, I skimmed through some of my math books and found some factorial expressions raised to a power. Two observations about the factorial expressions

  1. They are always enclosed in parentheses or square brackets, i.e., not bare as in the hang bug. It’s certainly better typography and more readable to enclose them this way
  2. They are in advanced math, not likely to appear in math up through the first year of calculus

Here are three of the equations I found with factorial expressions raised to a power
 As you see, they aren’t very simple and they are enclosed in parentheses or brackets, which don’t hang. Neither does k!2, although I never saw that combination. So I don’t think the bug occurs in the vast majority of mathematical text. This is probably why our extensive math test suite didn’t include this configuration. If you have cases that are more common, please let me know or write a comment describing them. And in any event, someone might type such expressions by mistake and be dismayed by the hang.

To get a feeling for why the bug could cause a hang, you can see that having the end delimiter of the parenthesized expression inside the base of the superscript object breaks the nesting of math objects in the math tree. Specifically, with {} to mark the start and end of math objects and | to separate arguments, the squared factorial should be nested as {^{(n+1}!|2}, but instead the math objects overlap as {(n+1{­^}!|2}. No bueno and Word hangs!

Two workarounds are possible (other than using the typographically preferred brackets): \begin(n+1)!\end^2 and \open(n+1)!\close^2. The second workaround inserts an invisible bracket around the (n+1)!, while the first does not. If you use the math structures in the math ribbon, you can also get the built-up expression given by the first workaround. However, that workaround builds down to the original (n+1)!^2, which won’t rebuild up correctly. So it was better to use the \open…\close workaround, which does round-trip through the linear format. Or better yet, use [(n+1)!]^2.

The bug doesn’t exist in Word 2007, for which only the exclamation point ends up as the base of the superscript object. That choice of base isn’t correct, but at least it doesn’t hang and it looks reasonable.

And now the good news: the bug is fixed and is downloaded automatically in updates to Office 2013. In fact, one of the cool things about Office now is that such fixes are deployed easily right over the Internet.