Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

Like this.

Is it possible?

share|improve this question
    
Alex1123, just out of interest, did you see the "Answer" to your question? –  bibadia Nov 26 '13 at 22:09
    
Yes. I am kind of surprised that there is no easier way to do that :) Also I don't need that for now. –  Alex11223 Nov 26 '13 at 22:19
    
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. –  Alex11223 Nov 26 '13 at 22:30
    
There probably is a slightly easier way. I'll try to get back to you on that. –  bibadia Nov 27 '13 at 9:09
    
I've added a couple of other approaches, but I don't currently see any way of avoiding the VBA altogether. –  bibadia Nov 27 '13 at 18:58

2 Answers 2

up vote 2 down vote accepted

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.

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.