# A Math Build-up Bug

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.

1. Christian P. says:

Now I'm asking myself why I posted this on Answers and didn't email you directly.

Anyways, speaking about the equation editors stability: I noticed that at times, if you edit equations in Word 2010 documents opened in Word 2013 in compatibility mode, the application crashes. I couldn't find something reproducible though, this seemed to happen at random (but I ran into crashes this on different computers and with different files).

Be that as it may, I've been using the equation editor very intensively over the past months and wanted to say that I'm impressed by its quality. Keep up the good work ðŸ™‚

2. davidacoder says:

I thought anything that crashes an application is treated as a security vulnerability these days in MS and therefore gets fixed right away, not in the next version?

3. Mikael S. says:

Word will some times crash using backspace. Try the following

– Open a new document.

– Press enter once

– Insert new equation

– type: sin(x)

– Press backspace until sin(x) is gone

– Word crashes completely

4. MurrayS3 says:

I can't repro this bug in Word 2013. I tried various combinations of inserting the equation (alt+= vs menus) and resolving the sin(x) with a space or not. What version of Word do you have?

5. Mikael S. says:

I'm using Word 2013, 32-bit (not virtualized). Danish language.

I tried disabling add-ons as well. I can reproduce it on several computers.

It is not a problem in Word 2010.

The error occurs when sin(x) is completely deleted and you press delete one more time to delete the first enter.

6. MurrayS3 says:

Could you download the Office Configuration Analyzer Tool (OffCAT) from the URL support.microsoft.com/…/2939112 That would collect information about the crash that would help us identify the cause. We're very sorry about the problem and would like to fix it.

7. Mikael S. says:

I ran the tool and exported the following xml.file:

onedrive.live.com/redir