## Preventing Superscript from being interpreted as Power when using Ctrl+^ shortcut?

17

9

I have very strong desire to use superscript as the index of the variable.

However, it looks like that the Mathematica automatically recognize the superscript as the power and I got message that my variable with superscript is 'protected'.

Could you help me to make the superscript used as the index of the variable instead of power?

### UPDATE (16-June-2015):

This question is being reopened and a bounty is being awarded on this. Previous answers are very good, however the bounty is to be awarded on an answer which solve this specific problem:

How to change the behaviour of Ctrl+^ keybinding so that it produces Superscript instead of Power.

I'm not sure if it's possible to do, although somebody else might have an answer. I've had this problem before, and just use subscripts instead. For example: $$v_{k_,j_}:=\mbox{Sin}[k^2+j^3];$$ Is there a reason why subscripts are not as good for your application? – DumpsterDoofus – 2013-10-08T18:38:11.837

1

@shivams, these seem related to your update of this question: http://mathematica.stackexchange.com/q/68864, http://mathematica.stackexchange.com/q/85823

– Michael E2 – 2015-06-16T19:15:51.603

@MichaelE2 Incidentally I believe one will need to edit the Menu items rather than the KeyEvent items if one is to override this particular combination. If you can show me otherwise please do so. – Mr.Wizard – 2015-06-17T08:23:36.387

I was wondering about that, because I noticed the difference in the other answers and yours. It makes sense that Menu key combinations are intercepted before they become a KeyEvent in a particular notebook. But I assume one could get a superscript with a different combination using KeyEvent, which would be my preferred solution, since I use Ctrl-6 so often -- I want Power! ;-) – Michael E2 – 2015-06-17T12:39:12.863

@Michael I added that to my answer too. I still don't like the fact that the typesetting menu says Superscript when it means Power however. – Mr.Wizard – 2015-06-17T16:24:19.677

@Mr.Wizard It's not Power here: [esc]int[esc] [ctrl-6] 1 [ctrl-shift-5] 0 x [esc]dd[esc] x. (No <kbd> in comments, I guess.) – Michael E2 – 2015-06-17T20:11:37.820

@MichaelE2 Good point. However I still find the interpretation of Superscript in such uses misleading from a menu section titled Typesetting. If it were "2D entry forms" or something I would not be surprised. – Mr.Wizard – 2015-06-18T00:56:52.087

@Mr.Wizard I didn't mean to disagree with that. Did something change at some point? Consider Superscript[6, 4] vs ToExpression@SuperscriptBox[6, 4] -- the naming inconsistency is not limited to the menus, as if in V1, say, Superscript was interpreted as Power. – Michael E2 – 2015-06-18T01:28:38.927

@shivams I see that the bounty is about to expire. Is there anything more I can do to improve my answer? – Mr.Wizard – 2015-06-22T21:39:29.250

@Mr.Wizard Thanks. Your solution works!! Albeit with a bit of modification regarding the location of the mentioned files on my Linux system :) – shivams – 2015-06-23T05:01:56.050

@shivams You're welcome! Could you inform me of the changes needed? I would like to update my answer to be applicable to more systems. – Mr.Wizard – 2015-06-23T06:56:07.667

@Mr.Wizard My relevant file was located as : $InstallationDirectory/SystemFiles/FrontEnd/TextResources/X/MenuSetup.tr. This is for Mathematica 9 on Linux Mint. – shivams – 2015-06-23T07:17:27.827 @shivams Does $OperatingSystem return "X" on your system? – Mr.Wizard – 2015-06-23T07:34:37.503

@Mr.Wizard No. It returns Unix. – shivams – 2015-06-23T07:36:04.510

27

Superscript is not interpreted as Power:

Presumably you are referring to what happens when you enter a power in superscript notation using the key combination Ctrl+6. Mathematica is capable of representing both this power notation and a formatted plain Superscript. In my opinion it is a failing that the power notation appears in the Typesetting menu while the latter is missing; if anything it should be the other way around I think. Since there is no key combination for raw Superscript I propose using a palette or input alias:

## Palette

You may enter a formatting template using a palette button which may be created with:

CreatePalette @ PasteButton @ Superscript[\[SelectionPlaceholder], \[Placeholder]]


Click that palette button to insert a template for plain superscript in the current Notebook. Use Tab to move between fields.

## Input alias

Either open the Option Inspector, select Global Preferences, type "InputAliases" to find the appropriate entry, and add this to the list of rules:

"sps" -> TemplateBox[{"\[SelectionPlaceholder]", "\[Placeholder]"}, "Superscript"]


Or add it programmatically (run this only once):

AppendTo[
CurrentValue[$FrontEnd, "InputAliases"], "sps" -> TemplateBox[{"\[SelectionPlaceholder]", "\[Placeholder]"}, "Superscript"] ]  Now type: EscspsEsc to enter a template for plain Superscript. (In version 10.0.0 the first field will not be automatically selected due to a bug; see Input Aliases in Mathematica 10.) ## Bounty A bounty was added for: [A] solution to map the Ctrl+^ keybinding to produce superscript instead of power. To accomplish this first copy MenuSetup.tr from the installation directory to the matching path in your user directory and open the user copy for editing: os =$OperatingSystem /. "Unix" -> "X";

CopyFile @@ (
FileNameJoin[{#, "SystemFiles", "FrontEnd", "TextResources", os, "MenuSetup.tr"}] & /@
{$InstallationDirectory,$UserBaseDirectory})

% // SystemOpen


Then within the user copy edit the appropriate MenuItem to read:

MenuItem["&Superscript",
FrontEndExecute[{
FrontEndSelectionMove[FrontEndInputNotebook[], All, Word],
FrontEndNotebookApply[FrontEndInputNotebook[],
TemplateBox[{"\[SelectionPlaceholder]", "\[Placeholder]"}, "Superscript"],
Placeholder]
}],
]


Restart Mathematica.

You may now enter raw Superscript by using Ctrl+6 where 6 is the number-line six above the alphabetic keyboard.

You can still enter Power notation by using Ctrl+Shift+6 or Ctrl+Keypad-6, the latter assuming that Num Lock is on.

For those you prefer the reverse behavior you can instead copy and edit KeyEventTranslations.tr and change the Item:

Item[KeyEvent["^", Modifiers -> {Control}], "Superscript"]


to:

Item[KeyEvent["^", Modifiers -> {Control}],
FrontEndExecute[{
FrontEndSelectionMove[FrontEndInputNotebook[], All, Word],
FrontEndNotebookApply[FrontEndInputNotebook[],
TemplateBox[{"\[SelectionPlaceholder]", "\[Placeholder]"}, "Superscript"],
Placeholder]
}]
]


Now Ctrl+6 is Power and Ctrl+Shift+6 is raw Superscript. However the Typesetting menu item remains misleadingly named Superscript so I would personally change that to Power if adopting this binding.

Also see:

Glad that you updated your bounty answer to match Linux systems as well. This should now work for Windows and Linux. However, perhaps if you get your hands around a Mac, then check if this works on that as well. – shivams – 2015-06-23T07:54:26.047

How do you name \Sigma^2 i.e. variance? I try both \Sigma CTRL-^ + 2 and superscript[\Sigma,2] unsuccessfully. – Léo Léopold Hertz 준영 – 2016-09-23T10:42:33.127

Note that this superscript gives problems when used in a string. The FullForm still contains TemplateBox and TeXForm gives an error when applied to it. (While it functions normally when applied to the old "Power" superscript in a string.)

If I try to evaluate the box first by doing something like TeXForm@ToString[ a (Ctrl + ^) b] I also get a very weird result \text{ b$\backslash$na}.

(Sorry for the formatting. I meant if I use your key combination above to create a superscript a^b.) – Kvothe – 2019-04-09T08:21:28.163

@Kvothe Thank you, I was not aware of that TeXForm bug. I shall open a new Question on this as I don't know a good solution. (One could convert strings to held expressions, then replace Hold with Defer, but this seems like something that should be patched at the level of TeXForm.) – Mr.Wizard – 2019-04-10T06:52:54.660

@Kvothe It turns out that is a known TeXForm bug, and there is a patch: https://mathematica.stackexchange.com/q/76801/121

– Mr.Wizard – 2019-04-10T10:37:05.490

10

For internal representation, I prefer avoiding subscripts and superscripts, so I'll give a way for using superscripts for input and output formatting, while the internal expression is of the form x[i].

For output formatting, something like this:

Format[x[i_]] := Superscript[x, i]


Example

Table[x[i], {i, 3}]


For input, this works, but I would wonder whether it is safe:

Power[x, i_] ^:= x[i];
Unprotect[Power];
Power /: Set[Power[x, i_], val_] := x[i] = val;
Protect[Power];

x^i // InputForm
(* x[i] *)


It also works with keyboard input of superscripts.

(x^1 + x^2)^3 // Expand


7

If you want to use superscripts so as to follow some textbook symbols then use Symbolize

Needs["Notation"]


I'm going to paste an image because pasting Symbolize gives you this:

Symbolize[ParsedBoxWrapper[SuperscriptBox["y", "1"]]]


0

Yes, use Mathematica's Notation package to define what your particular form means. Here I create a datatype G[] from sub- and superscripted letter e. Note in the final example that regular superscripting still means raise to a power.

0

When I asked people about this before, they wrote up: Displaying index as subscript on output: e.g. C[i] -> C_i with Notation[...] or Interpretation[..]? As with the other answers, this focuses on only output. But uses an Interpretation instead of just changing the functions themselves. You should just be able to copy/paste the code to try it out. To change from subscripts to superscripts for your code, it should just be a copy/replace.

As for why you shouldn't use subscripts/superscripts, it took me a while to figure it out, but basically it is because $x_i$ is not a symbol. Try to call FullForm on $x_i$ to get Subscript[x,i]` which is tough to work with.