Message Boards Message Boards

GROUPS:

Put parentheses around selection

Posted 1 month ago
784 Views
|
5 Replies
|
17 Total Likes
|

Hello Suppose I wrote an expression, such as xy. Pressing Ctrl+. , I can select the expression. Now I want to put this expression in parentheses:

(xy)

This option works for putting a squre-root sign around an expression, by pressing Ctrl+2. What I am looking for is an equivalent shortcut for bracket, or the parentheses, punctuation mark.

5 Replies

I personally do that by adding this command to the file KeyEventTranslations.tr file inside EventTranslations[{

(* Matching parentheses*)
Item[KeyEvent["9",Modifiers->{Command,Shift}],
    FrontEndExecute[FrontEnd`NotebookApply[FrontEnd`InputNotebook[],
      "(\[SelectionPlaceholder])", Placeholder]]]

However, this is not documented or recommended by Wolfram, and the functionality may not be maintained in future releases. The analogous command for adding [] or {} or "" used to work some years ago but has deteriorated over time. The file is in the directory ~/Library/Mathematica/SystemFiles/FrontEnd/TextResources/Macintosh/ on my system, and was copied there from the parent unmodified file in the main Mathematica directory.

Limitations and problems with .tr files made me create live templates see community post or mm.se post.

Once installed you can set package's stylesheet or add templates menu to all notebooks by:

SetOptions[$FrontEnd, NotebookEventActions -> {
     {"MenuCommand", "InsertNewGraphic"} :> Block[{$ContextPath}
        , Needs["DevTools`"]    ; DevTools`OpenNotebookMenu["CodeTemplates"]
        ]
      , ParentList
      }
    ]

and you are ready to go, with a caret just after the symbol you can hit Ctrl+1 followed by ( and wrapped:

enter image description here

Posted 1 month ago

This is pretty simple to do without modifying any existing settings. After you've selected what you want wrapped in parentheses, just hit Ctrl-6 then Delete (or Backspace). It's not a single shortcut, but I've been using it for years and don't find it cumbersome.

enter image description here

I am pretty sure this was available in Mathematica 2.2, but it was removed from the GUI. This is still available as a FrontEndToken. I have a palette that activates it:

Button["(\[SelectionPlaceholder])",  
 FrontEndExecute[
  FrontEndToken[SelectedNotebook[], "SelectionParenthesize"]]]

In modern text editors like Sublime Text and Atom, when you have someting selected and type "(" the selection is parentized instead of replaced. I wish this behabior was adepted by Mathematica (and also for "{" and "[").

Related: https://mathematica.stackexchange.com/questions/84720/parenthesize-what-i-have-highlighted

Posted 1 month ago

Thanks a lot for all of the comments. I am sure about to adopt one of them.

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract