# Having trouble defining a function with head Superscript

I wish to define a function in the following form,

Superscript[Subscript[Γ, i_, j_], k_][g_, var_List] :=
Block[{ginv = Inverse[g], n = Length[var]},
Sum[
ginv[[k, l]] (D[g[[j, l]], var[[i]]] + D[g[[i, l]], var[[j]]] - D[g[[i, j]],
var[[l]]]), {l, 1, n}]];


However, whenever I input something of the form

Power[Subscript[Γ, 1, 1], 1][g, var]


Power simplifies and the expression doesn't evaluate. Is there any way to redefine the meaning of a Superscript in this context? I've tried unprotecting both Superscript and Power, but neither of those options worked.

• Might you provide the motivation for this wish? There may well be a better approach. (E.g., is your core concern the display of the expression in a notebook?) – Alan Jun 24 '17 at 20:23
• In Riemannian geometry, we denote this term by $\Gamma_{ij}^k$, and that is the form that I'm used to seeing. More or less, I'm trying to keep my notation as close to standard mathematical notation as possible. – Monstrous Moonshine Jun 24 '17 at 20:30
• My question is whether you care about how you type it, or how it displays. E.g., suppose your define Format[G][i_, j_, k_]] := DisplayForm@ RowBox[{SubsuperscriptBox[G, RowBox@{i, ",", j}, k]}] (but replace G with a capital gamma). Then you would type G[i,j,k] but it would display as you desire. – Alan Jun 24 '17 at 21:56

Instead of directly using Superscript and Subscript in the function definition, you could create an InputAlias that is entered by typing escgamesc to create a template of the desired form. With that template, you can define an InterpretationFunction that tells Mathematica what to do with the input once the template has been filled in.

For your function, I would recommend restricting it to act only on numeric values of the arguments i, j, k, because they appear as array indices in the evaluation. Your definition is contained in a differently named function myGamma to separate it from the formatting-related definitions. This is implemented below:

ClearAll[myGamma]

GammaAppearance[i_, j_, k_] :=
TemplateBox[{i, j, k}, "myGamma",
DisplayFunction :> (SubsuperscriptBox["Γ",
RowBox[{#1, ",", #2}], #3] &),
InterpretationFunction :> (RowBox[{"myGamma", "[",
RowBox[{#1, ",", #2, ",", #3}], "]"}] &)]

myGamma[i_?NumericQ, j_?NumericQ, k_?NumericQ] :=
Function[{g, var},
Module[{ginv = Inverse[g], n = Length[var]},
Sum[ginv[[k, l]] (D[g[[j, l]], var[[i]]] + D[g[[i, l]], var[[j]]] -
D[g[[i, j]], var[[l]]]), {l, 1, n}]]]

myGamma /: MakeBoxes[myGamma[i_, j_, k_], StandardForm] :=
GammaAppearance[ToBoxes[i], ToBoxes[j], ToBoxes[k]]

SetOptions[EvaluationNotebook[],
InputAliases ->
DeleteDuplicates@
Join[{"gam" ->
GammaAppearance["\[SelectionPlaceholder]", "\[Placeholder]",
"\[Placeholder]"]},
InputAliases /.
Quiet[Options[EvaluationNotebook[], InputAliases]] /.
InputAliases -> {}]]


The SetOptions adds the input alias to the list of abbreviations known to the notebook in which you're currently evaluating commands. It's written with a Quiet wrapper to suppress warnings when there aren't any existing aliases yet.

If you now press escgamesc and enter $\Gamma_{i,j}^{k}$ with symbolic indices, the template remains unevaluated and displays just as you want. But if the indices have numerical values, the function definition is invoked:

gTest = DiagonalMatrix[{x y, y z, z x}];
varTest = {x, y, z};