I'm used to pressing Ctrl+Shift+I (or N if I want a shorter form) to reformat my input cells in a nicer way after editing them, e.g., to remove unnecessary parentheses, add or remove spaces as needed, etc.

The issue comes when I define a custom output format for some expression. For example, let's say that I'm working with indexed variables of the form a[i, j]. It's easier to use, for example because I can just match patterns with things like _a. However, it is visually more pleasing to view such a variable as Subscript[a, i, j] in the output of a computation, because (among other things) that's what I actually write on my piece of paper when doing the math myself.

I know how to define an output format that takes care of the visual aspect:

Format[a[i_, j_]] := Subscript[a, i, j]
a[1, 2] + a[2, 3]
(* will display as expected *)

However, if I then use Ctrl+Shift+I on a cell that contains a[1,2], the expression will turn into Subscript[a, 1, 2], with no turning back.

Some options I'm aware of:

  • Use the Notation package. But it seems heavy weight, and if I want to avoid breaking Ctrl+Shift+N, I need to define two-way notation, which could be undesirable.
  • Define a format function like format[expr_] := expr /. a[i_, j_] :> Subscript[a, i, j] and then set $Post = format. But then, this runs every single time I get an output, and I'm afraid the ReplaceAll will get slow on large expressions, or if I start having more custom notation.

Any ideas?


1 Answer 1


You need MakeBoxes+TemplateBox:

MakeBoxes[a[i_, j_], StandardForm] ^:= 
 TemplateBox[MakeBoxes /@ {i, j}, "a", 
  DisplayFunction -> (SubscriptBox["a", RowBox[{#, ",", #2}]] &)]

enter image description here

In Comparison with Notation Method

It's worth pointing out the effect of the method above is different from the two way notation of Notation package:

<< Notation`

   SubscriptBox["a", RowBox[{"i_", ",", "j_"}]]] \[DoubleLongLeftRightArrow] 
     ParsedBoxWrapper[RowBox[{"a", "[", RowBox[{"i_", ",", "j_"}], "]"}]]]

enter image description here

As we can see, the Subscript has been hajacked by Notation package. Once a 2D $a_{i,j}$ is created in the notebook, it'll be interpreted as a[i, j]. (Things like expr defined by expr = Subscript[a, 1, 2] won't be influenced, though. )

On the contrary, the MakeBoxes+TemplateBox has just created something looks exactly the same as Subscript[…]. Subscript won't be influenced:

enter image description here

  • $\begingroup$ Thanks! That works nicely. How does this compare to the Notation package? $\endgroup$ Commented May 11 at 13:49
  • 1
    $\begingroup$ @NajibIdrissi Just as you've noticed :) . 1. We don't need to load the package so it's light-weighted. 2. It's actually different from the two way notation of Notation package. Once we define a two way notation with Notation package, all the $a_{i,j}$ will be treated as a[i,j] even if we manually input a, Ctrl+-,i,j or Subscript[a, i, j]. But using my method, a, Ctrl+-,i,j or Subscript[a, i, j] will still be interpreted as Subscript[a, i, j]. In other words, my method doesn't hijack Subscript, it only creates something looks exactly the same as Subscript. $\endgroup$
    – xzczd
    Commented May 11 at 15:44
  • $\begingroup$ @NajibIdrissi I've elaborated a bit with 2 GIFs, see my update. $\endgroup$
    – xzczd
    Commented May 11 at 16:07
  • 1
    $\begingroup$ BTW, though not quite related to your question, you may want to read this to learn what Notation package actually does: mathematica.stackexchange.com/a/185475/1871 @NajibIdrissi $\endgroup$
    – xzczd
    Commented May 11 at 16:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.