How to create an augmented matrix in Word 2007+ Equations editor?

17,021

Solution 1

I think you can do it this way...

  1. Insert an equation.
  2. From the Bracket dropdown, insert the first item from the "Bracket with separators" group
  3. Select the first of the two boxes
  4. From the Matrix dropdown, insert a 3x3 Empty matrix
  5. Right-click the matrix and use the Insert option to insert columns and rows as necessary
  6. Select the second of the original two boxes
  7. From the Matrix dropdown, insert a 3x1 Empty matrix, then add rows as necessary.

This results in a separator that is quite close to the two matrices. To add extra space, after step (4) you can type a space, and before step 6, you can insert a space. I do not know if that is a good way to do things.

You can do it in VBA using something like this. This stuff is all new to me so can doubtless be improved. If you are only working regularly with a small number of array sizes, you could consider using the VBA to generate each array, then save it as a building block/autotext.

Sub testInsertAugmentedMatrix1()
' Insert a test equation at the selection point
Call insertAugmentedMatrix1(Selection.Range, 2, 5)
End Sub

Sub insertAugmentedMatrix1(rng As Word.Range, RowCount As Integer, ColumnCount As Integer)
' Insert a "basic" augmented matrix at the specified range,
' with RowCount rows, ColumnCount columns, and a single column after the separator

Dim mainFunction As Word.OMathFunction
Dim subFunction As Word.OMathFunction
' Insert the framework
rng.OMaths.Add rng
With rng.OMaths(1)
  Set mainFunction = .Functions.Add(.Range, wdOMathFunctionDelim, 2)
  With mainFunction
    .Delim.BegChar = 40
    .Delim.SepChar = 124
    .Delim.EndChar = 41
    .Delim.Grow = True
    .Delim.Shape = wdOMathShapeCentered
  End With
  With mainFunction.Args(1)
    Set subFunction = .Functions.Add(.Range, wdOMathFunctionMat, ColumnCount * RowCount, ColumnCount)
    subFunction.Range.InsertAfter " "
  End With
  With mainFunction.Args(2)
    Set subFunction = .Functions.Add(.Range, wdOMathFunctionMat, RowCount, 1)
    subFunction.Range.InsertBefore " "
  End With
  Set subFunction = Nothing
  Set mathFunction = Nothing
End With
End Sub

Another way in VBA is to build a "Math string" like this:

Sub testInsertAugmentedMatrix2()
' Insert a test equation at the selection point
Call insertAugmentedMatrix2(Selection.Range, 4, 6)
End Sub

Sub insertAugmentedMatrix2(rng As Word.Range, RowCount As Integer, ColumnCount As Integer)
Const mthMatrix As Long = &H25A0 '"Black Square"
Const chrMatrixColumnDelimiter As String = "&"
Const chrMatrixRowDelimiter As String = "@"
Const mthVbar As Long = &H2502

Dim i As Integer
Dim strArray As String
strArray = ""
For i = 1 To RowCount
  If i > 1 Then
    strArray = strArray & chrMatrixRowDelimiter
  End If
  strArray = strArray & String(ColumnCount - 1, chrMatrixColumnDelimiter)
Next
rng.Text = "(" & _
ChrW(mthMatrix) & "(" & strArray & ")" & _
" " & ChrW(mthVbar) & " " & _
ChrW(mthMatrix) & "(" & String(RowCount - 1, chrMatrixRowDelimiter) & ")" & _
")"
rng.OMaths.Add rng
rng.OMaths.BuildUp

End Sub

Or, you can use the "math autocorrect" tokens \matrix etc. instead of the special Unicode characters, like this. The mathSubstitute function is copied from my post here and hasn't been extensively tested. I think this is potentially the most readable approach.

Sub testInsertAugmentedMatrix3()
' Insert a test equation at the selection point
Call insertAugmentedMatrix3(Selection.Range, 4, 6)
End Sub

Sub insertAugmentedMatrix3(rng As Word.Range, RowCount As Integer, ColumnCount As Integer)
Const mthMatrix As String = "\matrix"
Const chrMatrixColumnDelimiter As String = "&"
Const chrMatrixRowDelimiter As String = "@"
Const mthVbar As String = "\vbar"

Dim i As Integer
Dim strArray As String
strArray = ""
For i = 1 To RowCount
  If i > 1 Then
    strArray = strArray & chrMatrixRowDelimiter
  End If
  strArray = strArray & String(ColumnCount - 1, chrMatrixColumnDelimiter)
Next
rng.Text = mathSubstitute("(" & _
mthMatrix & "(" & strArray & ")" & _
" " & mthVbar & " " & _
mthMatrix & "(" & String(RowCount - 1, chrMatrixRowDelimiter) & ")" & _
")")
rng.OMaths.Add rng
rng.OMaths.BuildUp

End Sub

Function mathSubstitute(s As String) As String
Const bslash As String = "\"
Dim a() As String
Dim sout As String
Dim i As Integer
Dim j As Integer
Dim sac As String
sout = ""
If s <> "" Then
  a = Split(s, bslash)
  sout = a(LBound(a))
  For i = LBound(a) + 1 To UBound(a)
    Debug.Print a(i)
    For j = 1 To Len(a(i))
      On Error Resume Next
      sac = Application.OMathAutoCorrect.Entries(bslash & Left(a(i), j)).Value
      If Err.Number = 0 Then
        sout = sout & sac & Mid(a(i), j + 1)
        Exit For
      Else
        sac = ""
        Err.Clear
      End If
    Next
    If sac = "" Then sout = sout & bslash & a(i)
    'Debug.Print sout
  Next
End If
On Error GoTo 0
mathSubstitute = sout
End Function

There is a paper by Murray Sargent here that describes how all this stuff is supposed to work. I don't think the equation numbering thing in there works in Word but it may do so elsewhere.

Solution 2

Create an equation object. Right click, change to inline. Paste the following into it, then right click and change to display.

[■(&@&)│■(&@&)]

That will give you an augmented matrix with two 2x2 parts separated by a divider. In general, switching to inline gives you the chance to mess around with the code that generates the equation object.

Solution 3

This worked for me in both Word and OneNote.

Type [|] and a space in the equation editor. You will now have two placeholders adjacent to the | where you can insert two bracket-less matrices (from the toolbar). After you inserted the two matrices, you can insert spaces before and after the | to make it more readable.

Share:
17,021
AlexP11223
Author by

AlexP11223

Updated on September 18, 2022

Comments

  • AlexP11223
    AlexP11223 over 1 year

    How to create an augmented matrix in Word 2007+ (2010) Equations editor?

    Like this.

    Is it possible?

    • Admin
      Admin over 10 years
      Alex1123, just out of interest, did you see the "Answer" to your question?
    • AlexP11223
      AlexP11223 over 10 years
      Yes. I am kind of surprised that there is no easier way to do that :) Also I don't need that for now.
    • AlexP11223
      AlexP11223 over 10 years
      Although maybe it is not so bad... When I tried it month ago I found some issue but do not remember what it was. Looks fine now.
    • Admin
      Admin over 10 years
      There probably is a slightly easier way. I'll try to get back to you on that.
    • Admin
      Admin over 10 years
      I've added a couple of other approaches, but I don't currently see any way of avoiding the VBA altogether.