# Why my code for format change doesn't work? (Format Rules)

I want to write the following in mathematica

$$(\sum_{k=1}^n a^2_k)$$

Now I've written this as an indexed variable

Sum[(a^2)[k], {k, 1, n}]


which in mathematica shows up as $$\sum_{k=1}^n (a[k])^2$$ But I want this displayed as the Latex form, with the subscripts (but I don't want to use the subscript command) and I tried

(# /: Format[(Power[#[i_], exp_])] :=
Subscript[(Power[#, exp]), i]) & /@ {a};


But I get an error saying

"Rule for Format of (a^exp_)[i_] can only be attached to Power."

Please guide me on what I am doing wrong with the Format command.

• "but I don't want to use the subscript command" Why not? (a^2)[k] doesn't make much sense for anything else than displaying an expression in a certain way, anyway. Oct 8, 2017 at 11:28
• @Szabolcs , because I don't the fullform of the expression to have subscripts. But I just want it to be displayed that way. Oct 8, 2017 at 11:57
• I don't understand your comment because it is not a full sentence. Oct 8, 2017 at 12:39
• @Szabolcs , there is nothing ambiguous about that sentence. When I use FullForm[expr] on the expression containing things like the one mentioned in the question, I don't want it show subscripts in the fullform. I just want to use the Format command to display it that way. This question relates to the use of the format command. Oct 8, 2017 at 13:24
• "I don't the fullform" is not a full sentence. I don't think a reasonable answer can be given unless you explain what you want in very clear terms. (a^2)[k] is plainly incorrect in Mathematica. Redefining the way Power formats is a bad idea. I am not sure what you are trying to achieve with #. For these reasons, I am going to stop here. Oct 8, 2017 at 13:32

Format[a[k_]] := Subscript[a,k]

$$\sum _{k=1}^{\infty } a_k{}^2$$