# How to add a vertical line to a matrix?

Today, I want to write a program to show the solving process of a the inverse of a matrix, and I use the Elementary Transformation, so I need to add a vertical dashed line to the matrix.

I hope the result looks like this: ### Question

Is it possible to achieve this effect?

### Update1:

For example, I have the following augmented matrix,

augmentedMatrix[n_Integer] :=
Join[
RandomInteger[{1, 10}, {n, n}], IdentityMatrix@n, 2]

augmentedMatrix // MatrixForm Then I need the result shown as below. Namely, myStyle[augmentedMatrix]$$\Rightarrow$$ ### Update2:

Öskå's answer, the final effect that apply augmentedMatrixForm to my function:

  augmentedMatrixForm[mat_?MatrixQ] :=
Module[{middlemat, len},
len = Length@First@mat;
middlemat = {Take[#, len/2] & /@ mat, Take[#, -len/2] & /@ mat};
MatrixForm@
List@Grid[
List@(TableForm[#, TableSpacing -> {1, 1}] & /@ middlemat),
Dividers -> {# ->
Directive[Red, Dashed] & /@ (Range@Length@middlemat)[[2 ;;]]}]
] I found that the matrix cannot align beautifully when the matrix contained fraction.

• – kglr Sep 25 '14 at 12:36

Grid directly supports such lines, called Dividers:

m = augmentedMatrix;

g = Grid[m, Dividers -> {7 -> {Red, Dashed}}] All that remains is to incorporate the large ( ) brackets used by MatrixForm:

MatrixForm[{{g}}] Another approach is to realize that both MatrixForm and Grid produce a GridBox expression:

Shallow[ToBoxes @ MatrixForm[m], 5]
Shallow[ToBoxes @ Grid[m], 3]

TagBox[RowBox[{(,,GridBox[<<5>>],,)}],Function[BoxForme$,BoxForme$]]

TagBox[GridBox[<<3>>],Grid]


We can manipulate that Box data to get the desired result:

ToBoxes @ MatrixForm[m] /.
GridBox[x__] :>
GridBox[x, GridBoxDividers -> {"ColumnsIndexed" -> {7 -> {Red, Dashed}}}] //
DisplayForm The more verbose syntax required (replacement, and GridBoxDividers etc.) is certainly not ideal, but I think it is useful to understand that at a fundamental level all these functions are working with the same FrontEnd Box "primitives."

### Proposal

Finally, here is what I propose for actual use, packaging the first method above into a hopefully convenient form.

Format[matWithDiv[n_, opts : OptionsPattern[Grid]][m_?MatrixQ]] :=
MatrixForm[{{Grid[m, opts, Dividers -> {n -> {Red, Dashed}}]}}]


Now:

augmentedMatrix // matWithDiv Additional Grid options can be used:

augmentedMatrix // matWithDiv[7, Background -> LightOrange] You can of course combine the formatting with the matrix generation, e.g.:

augMat2[n_, opts: OptionsPattern[Grid]] := augmentedMatrix[n] // matWithDiv[n + 1, opts]

augMat2 augMat2[5, Frame -> All] Be aware that, like MatrixForm, you will need to extract the first part of the output if you wish to manipulate the raw matrix data:

augMat2[5, Frame -> All] // First

{{8, 9, 9, 5, 5, 1, 0, 0, 0, 0},
{7, 7, 4, 6, 6, 0, 1, 0, 0, 0},
{4, 5, 4, 10, 6, 0, 0, 1, 0, 0},
{7, 4, 2, 7, 9, 0, 0, 0, 1, 0},
{7, 2, 9, 9, 1, 0, 0, 0, 0, 1}}

• Dear Mr.Wizard, +1,Firstly, thanks sincerely, In addition, your solution always detaied and rigorous, as well as surprised me. :-) – xyz Sep 25 '14 at 14:06
• @Tangshutao You're welcome, and thanks for the Accept. – Mr.Wizard Sep 26 '14 at 11:54
SeedRandom@0;
mat = RandomInteger[{0, 10}, {2, 3, 3}];
MatrixForm@List@Grid[List@(TableForm /@ mat), Dividers -> {2 -> Directive[Red, Dashed]}] Of course you can play with spacings: TableForm[#, TableSpacing -> {1, 1}] & /@ mat To go a bit further with Dividers:

SeedRandom@0;
mat = RandomInteger[{0, 10}, {10, 3, 3}];
MatrixForm@
List@Grid[List@(TableForm[#, TableSpacing -> {1, 1}] & /@ mat),
Dividers -> {# -> Directive[Red, Dashed] & /@ (Range@Length@mat)[[2 ;;]]}] With the new matrix (I removed the Join) it works the same:

augmentedMatrix[n_Integer] := {RandomInteger[{1, 10}, {n, n}], IdentityMatrix@n}
mat = augmentedMatrix;
MatrixForm@
List@Grid[List@(TableForm[#, TableSpacing -> {1, 1}] & /@ mat),
Dividers -> {# -> Directive[Red, Dashed] & /@ (Range@Length@mat)[[2 ;;]]}] • @Tangshutao You should not Accept an answer so quickly. I was working on an answer and it is disheartening to see that you feel that no additional answers are useful. – Mr.Wizard Sep 25 '14 at 13:22
• @Mr.Wizard Knowing that you will pwnd me :) – Öskå Sep 25 '14 at 13:26
• @Öskå I wouldn't say that; I just have a different approach. – Mr.Wizard Sep 25 '14 at 13:50
• @Tangshutao That took longer than expected, but I rethought what I was writing half way through and started over. I hope that my answer will now both give a useful method and some understanding of the low-level Front End functions in play here. – Mr.Wizard Sep 25 '14 at 13:51