Sunday 27 January 2019

GrindEQ, MS-Word macros for equations and more

I have spent a couple of weeks enhancing my equation handling so that I fully mastered equation numbers, and, using GrindEQ, I could fairly easily get from an MS-Word document containing equations to publishing the equations on this blogger blog or on physics forums. This is an update of older posts in Tools, here and here. I will recap and add in new stuff.

This post has three main sections
1) Equations in MS-Word
2) Converting GrindEQ To Web
3) Getting the macros

1) Equations in MS-Word

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

After the AB icon, they have the following effects
  1. Select table
  2. Add bars on table
  3. Remove bars from table
  4. Make table a box. 
  5. (##\pi##) Insert left justified equation (font size 12). Macro: InsertEquationInLine
  6. Insert unbarred 2 x 1 table with centred justified equation in column 1, (nn) in column 2 (Fig 1). Macro: EquationTable2
  7. 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
The first four are very useful for quickly sorting problems with tables and tidying them. They are all linked directly to MS-Word commands. 5-7 are linked to the macros. 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.


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 Developer / Macros button which explains the aa in the name. Which brings us to the final macro.

2) aConvertGrindEQToWeb

So I paid 49€ for GrindEQ and got to work on converting their peculiar .tex file output into something that was digestible by my blogger blog and Physics Forums (PF). It took about eight days which was somewhat longer than I expected.

I modified the MathJax script on my website to use $$ for free and ## for inline Latex equation delimiters. That is the same as PF. The script is at MathJaxSnippet.txt.

My Macro aConvertGrindEQToWeb takes GrindEQ output, usually in a .tex file, and Converts it to Latex which is can be copied and pasted into the PF editor or Blogger when it has the MathJax script installed. So:
  1. You create a .tex file from a Word document with GrindEQ. This is still  displayed in Word.
  2. Convert that with aConvertGrindEQToWeb (my Word macro.)
  3. Copy and paste the resulting text (text only) into PF or Blogger.

There are uncountable options when generating the .tex file. My macro would certainly not work if some were selected. So all the options that I use are shown in Word Macros for GrindEQ.pdf.

Converting GrindEQ files 

Here are a few notable points, which also may only be valid with options shown below.

GrindEQ automatic equation numbers are almost unusable. Therefore I have disregarded them and assume that all equation numbers are manually inserted.

Free equation delimiters ## {\backslash}[ … {\backslash}]## are converted to ##\$\$ … \$\$##

Inline delimiters  $ … $ are converted to ## \#\# …\#\# ##

Multiple $ and # in text are difficult or impossible to display, therefore we replace them all by $£ and #£. (The multiple $ and # above are in inline 'equations').

We replace all ' by {'} in equations, because a single ' in a superscript crashes MathJax and PF.

My  macro assumes that everything in a table is an equation. This makes sense because tables need special treatment anyway when putting them into a web editor.
GrindEQ converts Word tables into a ##{\backslash}##begin{tabular} …  ##{\backslash}##end{tabular} structure. This fairly conveniently converts to ##{\backslash}##begin{align} … ##{\backslash}##end{align} structures for Latex, no further equation delimiters are need in such a region. Alpha numeric equation numbers have the alpha part in italics because they are treated as an equation.  (I have not seen {tabular} in any Latex documentation and MathJax does not like it.)

There are many other expressions like \noindent in .tex files. They are removed.

Latex equations contain many expressions like \mathrm{cosh}. The characters \{ } are given special treatment as are $ # and others. In particular my macro uses Cyrillic characters л and м as intermediaries. Do not use them in your document! As a result the order in which everything is translated is delicately balanced. A warning if you want to modify the macro.

GrindEQ produces \mathrm{\alphaup } etc for non-italic (upright) Greek letters. Mathjax does not like it. They are all converted to \alpha etc. They become italic.

The macro is quite good, but not perfect!

3) Getting the macros

The macros are in VBA which is a bit amateurish, but that is what the Word macro recorder writes.

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 on the Developer tab. (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.


Screenshots of GrindEQ options Word Macros for GrindEQ.pdf.


  1. This looks brilliant. Next week I’m getting into my next assignment (reactor accidents!) and it will definitely come in handy! Harry

  2. The blog post looks brilliant. In the essays that I have biweekly, I often have to write equations but normally only 1 or 2 per doc so in that case it's probably not worth using the macros. In summer, on the other hand, I'll be doing a research project that’s 10 pages and may well be quite mathsy (depending on the exact topic), in which case I'll definitely be using your macro and will undoubtedly have to make some enquiries since im totally illiterate in Microsoft macros. I’ll make sure to share it with my staircase mates that all do maths, physics or engineering. Josh