4
$\begingroup$

For me, Indexed[x, n] is a much more useful and versatile construct than Subscript[x, n]. I would therefore like to modify Ctrl-- to insert Indexed expressions instead of Subscripts. Is this possible?

Note that I do, occasionally, need to work with code containing Subscripts, so simply redefining Subscript[x__] := Indexed[x] is not an option for me.

$\endgroup$
  • $\begingroup$ May I ask, why do you prefer one to another? Where do you used such indexed expressions: in Input cells? In DisplayFormula cells? Somewhere else? What is the advantage? $\endgroup$ – Alexei Boulbitch Feb 18 '15 at 9:14
  • $\begingroup$ @AlexeiBoulbitch I use them in input cells, primarily as a substitute for Part's ugly double-bracket notation. They're also useful for integration over multidimensional regions, e.g., Integrate[Indexed[x, 1]^2 Indexed[x, 2]^2, Element[x, Rectangle[]]]. $\endgroup$ – David Zhang Feb 19 '15 at 5:59
  • $\begingroup$ @Mr.Wizard This my question is to the whole community, by I just directed it to Mr.Wizard to address one. Previously it was known that Mma "does not like" to operate with expressions with subscripts. It was a general knowledge that it may occasionally evaluate an expression containing subscribed variables correctly, but may also return a mistake. So we preferred to use, say, x1 , rather than Subscript[x,1] . The question is, if this is not a problem for calculations with indexed variables, as OP states? $\endgroup$ – Alexei Boulbitch Feb 19 '15 at 8:50
  • $\begingroup$ @AlexeiBoulbitch Could I ask you for an example of Mathematica giving incorrect results in computations with Subscripts? I've never run into any such problems with either Subscript or Indexed, but my use cases of both are admittedly limited. $\endgroup$ – David Zhang Feb 19 '15 at 9:06
  • $\begingroup$ I never tried anything with Indexed before, in fact I did not know about its existence until now. So this I cannot. With Indexed, however, I and other people tried many times. When I started with Mma3 it was simply a "no go". It seemed that in the last versions (9 and 10) it is not the case, but I recently tried to make calculations in the 10.0.2 with the subscripts and it, indeed, made several operations correctly, but then returned an erroneous result which immediately turns into the right one, as soon as I removed the subscripts. I did not save this example though. $\endgroup$ – Alexei Boulbitch Feb 19 '15 at 9:16
4
$\begingroup$

You could use a Palette:

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

Or an Input Alias:

AppendTo[
 CurrentValue[$FrontEndSession, "InputAliases"],
 "idx" -> TemplateBox[{"\[SelectionPlaceholder]", "\[Placeholder]"}, "IndexedDefault"]
]

For a system-wide keyboard shortcut you can copy KeyEventTranslations.tr from SystemFiles\FrontEnd\TextResources\(your OS) within $InstallationDirectory to the like path within $UserBaseDirectory and replace the line containing "KeypadSubtract" with:

Item[KeyEvent["KeypadSubtract", Modifiers -> {Control}],
    FrontEndExecute[{
        FrontEnd`NotebookWrite[FrontEnd`InputNotebook[],
            TemplateBox[{"\[SelectionPlaceholder]", "\[Placeholder]"}, "IndexedDefault"], After]
    }]],

This will allow insertion of the same template as above. I am still seeking a way to emulate the default Subscript behavior where a template is inserted for a blank line, but the first element is auto-filled if an expression exists.


With the hint from Kuba to use NotebookApply we can get nearly perfect emulation with:

Item[KeyEvent["KeypadSubtract", Modifiers -> {Control}],
    FrontEndExecute[{
        FrontEnd`SelectionMove[FrontEnd`InputNotebook[], All, Word];
        FrontEnd`NotebookApply[FrontEnd`InputNotebook[],
          TemplateBox[{"\[SelectionPlaceholder]", "\[Placeholder]"}, "IndexedDefault"]]
    }]
],

Related:

$\endgroup$
  • $\begingroup$ @David The first snippet creates a Palette Notebook -- look for a small window with a button; on my system it appears in the upper right hand corner. Yes, it should be possible to map this to a key combination, however (1) I need to modify my configuration to test it, and (2) I was trying to think of a way to emulate the (default) behavior with Subscript, rather than inserting the double placeholder object. I shall update this answer later with whatever I can get working. $\endgroup$ – Mr.Wizard Feb 18 '15 at 0:09
  • $\begingroup$ @David Please see update $\endgroup$ – Mr.Wizard Feb 18 '15 at 0:39
  • 1
    $\begingroup$ @Mr.Wizard You can use this: SelectionMove[nb, Previous, Character]; SelectionMove[nb, All, Character]; NotebookApply[nb, TemplateBox[{"\[SelectionPlaceholder]", "\[Placeholder]"}, "IndexedDefault"]] $\endgroup$ – Kuba Feb 18 '15 at 9:02
  • $\begingroup$ @Kuba Thanks! NotebookApply is what I needed. However I get better behavior from All, Word as shown in the present answer; can you confirm? $\endgroup$ – Mr.Wizard Feb 19 '15 at 2:14
  • $\begingroup$ @Kuba This is fantastic! I'm surprised you found a way to emulate the selection behavior of Subscript. I have, however, noticed something strange: while I can override KeypadSubtract and _ just fine, I can't seem to modify the behavior of -. Is this intended? $\endgroup$ – David Zhang Feb 19 '15 at 5:32
1
$\begingroup$

One way to do what it seems me you would like to is to use the InputAliases function by evaluating in your notebook the following expression:

 SetOptions[InputNotebook[], 
 InputAliases -> {"ind" -> 
    "Evaluate[Indexed[\[SelectionPlaceholder],\[Placeholder]]]"}]

After that as soon as you type Esc+ind+Esc placeholders to type in an indexed expression will appear in the place of the cursor.

This is, however, boring, since you will need to evaluate such an operation in each notebook before its use.

One may also do it once for ever as follows:

  1. Having any of your notebooks open go to Menu/Format/OptionInspector (alternatively press Shift+Ctrl+O). This opens the OptionInspector.

  2. In its slot entitled "Show option values" choose Global Options.

  3. In the slot "Lookup" type InputAlaises, or alternatively in the left window choose Edit Options

  4. In the right window choose InputAlaises and click on the gray box at the right margin of the line.

  5. This opens a dialog box with a list of the already existing aliases. Press the button "Add".

  6. This opens the "Edit setting rules" dialog box enabling one to introduce a new rule. By default x->y stays there.

  7. Remove x and insert the key combination that you need, say, in. (do not put simply i, since such an aliase already exists, I checked).

  8. Instead of y insert TemplateBox[{"\[SelectionPlaceholder]", "\[Placeholder]"}, "IndexedDefault"]. Click the "OK" button on the both dialog windows, and "Apply" on top of the OptionInspector window.

Done. Now as soon as you type anywhere in any of your notebooks Esc+in+Esc you will get the placeholders enabling you to type in the expression which will appear to be indexed.

Have fun!

$\endgroup$
  • 1
    $\begingroup$ One can add a persistent input alias far more easily; simply evaluate AppendTo[ CurrentValue[$FrontEnd, "InputAliases"], "idx" -> TemplateBox[{"\[SelectionPlaceholder]", "\[Placeholder]"}, "IndexedDefault"] ]. For more information see: (72989) $\endgroup$ – Mr.Wizard Feb 19 '15 at 16:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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