## Sunday, 21 July 2019

### MS-Word macros for equations and more

Since I have got a new computer and Office 365 (upgrading from Office 2007), I have been taking advantage of the new features. In particular the ability to switch to Latex equations is great and I have used it and been able to stop using GrindEQ. I have written a completely new macro to generate web pages containing Latex. Sadly, there seems to be no way to determine in Word VBA whether equations are in Latex or Unicode mode. This must be done by hand. Also sadly, if Latex mode is always turned on, variables such as $x,\phi$ come out as non italic. This is disliked by mathematicians.

This is an update of older posts like this one in Tools. I will recap and add in the new stuff.

This post has three main sections
1) Equations in MS-Word
2) Converting a Word document to something suitable for a web page.
3) Getting the macros

## 1) Equations in MS-Word

I had been copying and pasting tables and equations in MS-Word for far too long, so I wrote some word macros to speed things up. I can now easily produce correctly aligned equations like
 Fig 1: Aligning equation numbers neatly using 2 x 1 tables
and
 Fig 2: LHS of equations unchanged and aligning equation numbers
I now have a "Quick access toolbar", part of it is like this

From the left, they have the following effects
1. Select table (MS-Word command)
2. Outside border on selected table Macro: BordersOutside
3. Remove all borders from table Macro: BordersNone
4. Add all borders to table Macro: BordersAll
5. 0.8 cm row height on selected rows Macro: Point8cmTableRows
6. ($\pi$) Insert inline equation (font size 12). Macro: InsertEquationInLine
7. Insert unbarred 2 x 1 table with centred justified equation in column 1, (nn) in column 2 (Fig 1). Macro: EquationTable2
8. Insert unbarred 3 x 1 with right justified equation in column 1, left justified equation in column 2 and (nn) in column 3 (Fig 2) Macro: EquationTable3
9. Renumber all equations Macro: aaRenumberEquations
The first five are useful for quickly sorting problems with tables and tidying them. With 7 & 8, the equation number (nn) is one bigger than the last one up the document. The one line tables are inserted according to customary MS-Word rules. There are three constants in the macros which may be adjusted to suit your taste:
• EquationFontSize - default 12 points
• TableHeight - height of new equation rows in cm, default 1.29
• EquationColumnWidth - column width in cm of equation number column, default 1.38
It often happens that you need to insert a few extra equations in the middle of a document. Here's a tip: Insert the first equation which will probably have the same number as the equation below. Change its number to 100 (or another large number). Following equations will be numbered 101, 102, ... When you have finished adding the group, renumber all equations. Which brings us to the next macro.

### aaRenumberEquations

Obviously after a bit of copying and pasting or writing things out of order, equation numbers can become a mess. For this we have the aaRenumberEquations macro which just renumbers all the equations from 1-n. It also adjusts all the references to said equations. Equation numbers such as a, b, b123,  2.5 will be renumbered.

I have tested it on the .docx which produced Commentary 1.1 Tensors matrices and indexes.pdf . It contains 213 equations numbers and 154 references to them. It took 112 seconds. The macro estimates the time that will be taken at the beginning (92 seconds in this case) and gives suitable warnings if it is more than 40 seconds. Clearly the overall time will depend on the machine.

#### aaRenumberEquations detects two kinds of error:

1) There is a reference to an equation that does not exist and the reference number would become a new equation number. In this case you get an error like
Equation (4) is referenced. It does not exist.
2) Equations are numbered with the same number twice or more and that number is used as an equation reference. (It does not matter if duplicate equation numbers are used but they are not referenced). In this case you get an error like
Equation (33) is defined 3 times and referenced one or more times.
Progress, and all errors found are displayed in another window.

I now activate this macro from the Quick access tool bar because it is robust and I use it often.
I used to only activate it from the Macros button, which is in the View and Developer ribbons, which explains the aa in the name.

It is now possible to create Word documents containing beautifully numbered equation and created .pdf files from them for example. What about a web page or a long question with equation numbers on Physics Forums? That brings us to the final macro which I use rarely and therefore only activate from the Macros button.

## 2) aPrepareForWeb

aPrepareForWeb creates a new window and copies unformatted text and Latex equations from the current document into the new window. Latex equation mode must be switched on in Word. As stated above, there seems to be no way to determine in Word VBA whether equations are in Latex or Unicode mode.  aPrepareForWeb uses  for display and ## for inline Latex equation delimiters. These can easily be changed. They are the same as Physics Forums. Obviously the host website must deal with these delimiters and Latex. An example MathJax script, used on this website, is at MathJaxSnippet.txt.

aPrepareForWeb also treats tables as if they contained equations with equation numbers in the last column. It uses the Latex environment begin{align} .. end{align} (backslashes omitted here!) and other gizmos to produce aligned equations for a website. This has the unfortunate side effect that genuine word tables can get destroyed! The remedy is obvious.

There is a file for demonstrating and testing the converter here: aPrepareForWeb conversion tester.docx.

## 3) Getting the macros

The macros are in Word VBA. If you would like to use these macros click here to see every version. Open the most recent then copy and paste into the Visual Basic editor, Normal / Modules, on the Developer tab. That will save them in Normal.dotm. (If you can't see the developer tab in the 'Ribbon', search for developer in your Word help. It's version dependent.) To run the macros you may also need to 'Trust access to the VBA project object model' in the Word Trust Center Settings.

If you have any questions, just leave a comment or email me.