# How to programatically generate a textual cell displaying the matrix with decorated raised and lowered indices?

This is a basic example of what I want to generate.

Cell[BoxData[
FormBox[
RowBox[{"[", GridBox[{
{
RowBox[{
SuperscriptBox["e",
OverscriptBox["1", "_"]],
SubscriptBox["\[InvisibleComma]", "1"]}],
RowBox[{
SuperscriptBox["e",
OverscriptBox["1", "_"]],
SubscriptBox["\[InvisibleComma]", "2"]}]},
{
RowBox[{
SuperscriptBox["e",
OverscriptBox["2", "_"]],
SubscriptBox["\[InvisibleComma]", "1"]}],
RowBox[{
SuperscriptBox["e",
OverscriptBox["2", "_"]],
SubscriptBox["\[InvisibleComma]", "2"]}]}
CellChangeTimes->{3.7415291626120205*^9, {3.7415292581546125*^9, 3.7415292673520265*^9}}]


I tried the following, with the hope I could simply copy the output and past it into a "DisplayFormual" cell.

Module[{element}
, element[name_][i_, j_] :=
TemplateApply[
"\!$$\*SuperscriptBox[\(n$$, OverscriptBox[$$a$$, \
$$_$$]]\)\!$$\*SubscriptBox[\(\[InvisibleComma]$$, $$b$$]\)",  name, "a" -> i, "b" -> j|>];
Grid[Array[element["e"], {3, 3}]
]
]


It generated the grid the way I want it to look, but it didn't work when I tried to paste it.

What is the best way to accomplish this?

• Is Array[Subsuperscript[e, ##] &, {10, 10}] sufficient? Then you'd convert it to boxes with ToBoxes. – b3m2a1 Jul 25 '18 at 18:09

This is a way that looks applicable to may of the situations I encounter. If someone has a better idea. I would be happy to try it.

templateMatrix[template_, n_] := Module[{ebox, i1, i2}
, ebox[t_][ix_, jx_] :=
TemplateApply[StringTemplate[t],  ix, "j" -> jx|>];
CellPrint[Cell[BoxData[FormBox[
RowBox[{"[", GridBox[Array[ebox[template], {n, n}]], "]"}]
templateMatrix["\!$$\*SuperscriptBox[\(e$$, OverscriptBox[$$i$$, \
$$_$$]]\)\!$$\*SubscriptBox[\(\[InvisibleComma]$$, $$j$$]\)", 3]
templateMatrix["\!$$\*FractionBox[\(\[PartialD]\*SuperscriptBox[\(x$$,\
OverscriptBox[$$i$$, $$_$$]]\), \
$$\[PartialD]\*SuperscriptBox[\(x$$, $$j$$]\)]\)", 3]
`