TensorScript - Writing tensors component in Mathematica

Subsuperscript only accepts three arguments, hence it is not possible to create tensors components as is done in General Relativity.

For this, I created TensorScript. Simple usage is as follows:

2017-08-06_211811.png

Where negative indices are on top.

Code (not the best or smarttest implementation, but it works):

TensorScript /: MakeBoxes[TensorScript[head_Symbol, vars___], format_] := Module[{parse, Up, Down},
	parse[n_Integer /; n < 0] := Up[-n];
	parse[-v_] := Up[v];
	parse[v_] := Down[v];
	
	TemplateBox[{
		ToBoxes@head,
		RowBox@Riffle[parse /@ {vars} /. {Down[v_] :> ToBoxes@Invisible@v, Up[v_] :> ToBoxes@v}, "\[InvisibleSpace]"],
		RowBox@Riffle[parse /@ {vars} /. {Up[v_] :> ToBoxes@Invisible@v, Down[v_] :> ToBoxes@v}, "\[InvisibleSpace]"],
		RowBox@Riffle[ToBoxes /@ {head, vars}, ","]
		}, "TensorScript",
		DisplayFunction :> (SubsuperscriptBox[#1, #2, #3] &),
		InterpretationFunction :> (RowBox@{"TensorScript", "[", RowBox@{#4},"]"} &),
		Tooltip -> Automatic]
]
1 Like