TemplateBox

TemplateBox[{(Subscript[box, 1]),(Subscript[box, 2]),...},tag] 
is a low level box structure which displays as the DsiplayFunction of the template box filled in with the (Subscript[box, i]).

TemplateBox is low-level notebook structure used to represent a parameterized box structure. The DisplayFunction option of the TemplateBox is applied to the box arguments to yield a new box structure which is displayed.

The DisplayFunction option is normally specified on the style sheet in order that when the style sheet changes all displayed instances of the template box change unfiormly.

If the tag of the TemplateBox is a string then that style will be used for the TemplateBox.

You can write MakeBoxes and MakeExpression rules which use TemplateBoxes in order to define new notations.

({{Examples, More Examples ⊳}})

The Binomial style is present in the core style sheet so its DisplayFunction is used when assembling the box to display:

TemplateBox[{"a","b"},"Binomial"]//RawBoxes

The actual DisplayFunction of the Binomial style specifed in the core style sheet is DisplayFunction ->(RowBox[{"(", GridBox[{{#}, {#2}}], ")"}]& ).

Many special functions are represented using template boxes. For example, the function LegendreP is typeset in TraditionalForm using a TemplateBox.

TemplateBox[{"a","b"},"LegendreP"]//RawBoxes

We can specify the display function of the TemplateBox explicilty but it is usually specified on the style sheet.

c+RawBoxes @ TemplateBox[{"a","b"},"MyCommutator",DisplayFunction:>(RowBox[{"[",RowBox[{#1,",",#2}],"]"}]&)]

TemplateBoxes can be nested to any level

TemplateBox[{TemplateBox[{"n","x"},"LegendreP"],"b"},"Binomial"]//RawBoxes

More Examples

Scope

Generalizations & Extensions

Options

DisplayFunction

If we specify the DisplayFunction option explicilty in the TemplateBox than this overrides any DisplayFunction inherited from the StyleSheet.

c+RawBoxes @ TemplateBox[{"a","b"},"Binomial"]

c+RawBoxes @ TemplateBox[{"a","b"},"Binomial",DisplayFunction:>(RowBox[{"[",RowBox[{#1,",",#2}],"]"}]&)]

The result of applying the DisplayFunction to the box arguments must yield a valid box structure or the box will not be displayed correctly

TemplateBox[{"a","b"},"MyCommutator",DisplayFunction:>(RowBox[{"[",foo[{#1,",",#2}],"]"}]&)]//RawBoxes

InterpretationFunction

The InterpretationFunction must yield a parsable box structure when applied to the TemplateBox arguments. The assmebled box structure is used when sending the boxes to the kernel for evaluation and for copying and pasting into external applications.

RawBoxes @ TemplateBox[{"a","b"},"MyCommutator",
InterpretationFunction:>(RowBox[{"LieBracket","[",RowBox[{#1,",",#2}],"]"}]&),DisplayFunction:>(RowBox[{"[",RowBox[{#1,",",#2}],"]"}]&),
Tooltip:>Automatic]

This same structure used as input evaluates to LieBracket.

(MyCommutator[a, b])

If the InterpretationFunction option is Automatic then the name of the tag is used as the function head of the box structure sent to the kernel.

RawBoxes @ TemplateBox[{"a","b"},"MyCommutator",
InterpretationFunction:>Automatic,DisplayFunction:>(RowBox[{"[",RowBox[{#1,",",#2}],"]"}]&),
Tooltip:>Automatic]

This same structure used as input evaluates to MyCommutator.

(MyCommutator[a, b])

Tooltip

You can specify the tooltip that will be displayed when the mouse hovers over a template box.

TemplateBox[{"a","b"},"Binomial",Tooltip:>"a vacuous tooltip"]//RawBoxes

If you specify the option Tooltip -> Automatic then a tooltip will be generated from the name of the tag of the TemplateBox.

TemplateBox[{"X","b"},"LieDerivative",DisplayFunction:>(RowBox[{SubscriptBox["ℒ",#1],#2}]&),
Tooltip:>Automatic]//RawBoxes

SyntaxForm

SyntaxForm is specified as a string containing an operator whose precedence level is the same as that of the contents of the InterpretationBox and TagBox. For example, SyntaxForm -> "*". You can also distinguish the infix and prefix form of an operator, for example, by setting SyntaxForm to "\!\(\*
StyleBox["a", "TI"]\\  + \\ \*
StyleBox["b", "TI"]\)" and "+ a", respectively.

TemplateBox[{"x"},"DerivativeOperator",DisplayFunction:>(FractionBox["∂",RowBox[{"∂",#1}]]&),
SyntaxForm->"∂"]//RawBoxes

possible values for the SyntaxForm option include: