3
$\begingroup$

Using ctrl/ you can make a fraction. If you have selected something it will appear in the numerator.

Does there exist a shortcut to make the selected text appear in the denominator instead? If not, is it possible to create a shortcut that does this?

$\endgroup$

2 Answers 2

2
$\begingroup$

Update: Just reverse the output of usual Ctrl+/!

Add the following lines to KeyEventTranslations.tr:

Item[KeyEvent["?", Modifiers -> {Control}],
    FrontEndExecute[{
        FrontEndTokenExecute["Fraction"],
        FrontEndTokenExecute["ExpandSelection"],
        NotebookWrite[InputNotebook[], Reverse@NotebookRead@InputNotebook[], Placeholder]
    }], 
    MenuEvaluator -> Automatic       
  ],

and press Ctrl+Shift+/ (? is Shift+/ on my keyboard).

This method is fully compatible with brackets, functions, etc. When nothing is selected the cursor is in the numerator. If you prefer the denominator than use the following

Item[KeyEvent["?", Modifiers -> {Control}],
    FrontEndExecute[{
        FrontEndTokenExecute["Fraction"],
        FrontEndTokenExecute["ExpandSelection"],
        NotebookWrite[InputNotebook[], Reverse@NotebookRead@InputNotebook[]],
        NotebookFind[InputNotebook[], "\[Placeholder]", Previous, AutoScroll -> False]
    }], 
    MenuEvaluator -> Automatic       
  ],    

Previous method:

Item[KeyEvent["?", Modifiers -> {Control}],
    FrontEndExecute@Module[{den=NotebookRead@InputNotebook[]},
        If[den=={},
           SelectionMove[InputNotebook[], All, Expression];
           den=NotebookRead@InputNotebook[]];
        If[den=={},den="\[Placeholder]"];
        NotebookWrite[InputNotebook[],FractionBox["\[Placeholder]",den]];
        SelectionMove[InputNotebook[], Previous, Word];
        SelectionMove[InputNotebook[], Next, Character]; 
        SelectionMove[InputNotebook[], All, Character]
    ],
    MenuEvaluator -> Automatic 
  ], 

MenuEvaluator -> Automatic is important because without it an unevaluated expression is written.

See also: Customizing Mathematica shortcuts

$\endgroup$
5
  • $\begingroup$ Seems you were a few seconds quicker :P $\endgroup$
    – ssch
    Oct 4, 2013 at 11:23
  • 1
    $\begingroup$ Nice! You could replace the SelectionMove stuff with Placeholder as third argument of NotebookWrite $\endgroup$
    – ssch
    Oct 4, 2013 at 15:46
  • $\begingroup$ If it works perfectly except one thing: When nothing is selected the cursor should be in the denominator. $\endgroup$
    – Tyilo
    Oct 7, 2013 at 12:40
  • $\begingroup$ @Tyilo I added the version with the cursor in the denominator. $\endgroup$
    – ybeltukov
    Oct 7, 2013 at 13:04
  • $\begingroup$ @ybeltukov Thanks! $\endgroup$
    – Tyilo
    Oct 7, 2013 at 15:53
2
$\begingroup$

Using same approach as Rolf did here

Putting this in KeyEventTranslations.tr does the job:

Item[KeyEvent["?", Modifiers -> {Control}],
 FrontEndExecute@With[
  {nb = InputNotebook[]},
  If[NotebookRead@nb === {},
    SelectionMove[nb, All, Expression];
    If[NotebookRead@nb =!= {}, FrontEndTokenExecute["ExpandSelection"]]
  ];
  NotebookApply[
    nb,
    FractionBox["\[Placeholder]", "\[SelectionPlaceholder]"],
    Placeholder]
 ], MenuEvaluator->Automatic],

Here I used Ctrl+Shift+/ as it for some reason didn't work with Ctrl+\ (I tried with "\\" and "Backslash" perhaps it has another name)

Edit: Took keyboard shortcut from ybeltukov
Edit2: Also "ExpandSelection"

$\endgroup$
9
  • $\begingroup$ I enjoy your approach with NotebookApply (+1). Do you know how to modify it for cases 2 and 3 in my answer? $\endgroup$
    – ybeltukov
    Oct 4, 2013 at 12:24
  • $\begingroup$ @ybeltukov No, having trouble with SelectionMove. I think I'll add a check and only use SelectionMove if den == {} $\endgroup$
    – ssch
    Oct 4, 2013 at 12:28
  • $\begingroup$ @ybeltukov Now it works as it should, where did you find out about MenuEvaluator? Without it I couldn't use If $\endgroup$
    – ssch
    Oct 4, 2013 at 12:49
  • $\begingroup$ @ybeltukov Any ideas how to make things like f[x] work? Currently only ] is selected $\endgroup$
    – ssch
    Oct 4, 2013 at 13:03
  • 1
    $\begingroup$ I could do nothing, until I accidentally came across this post. Then I found many other examples: mathematica.stackexchange.com/search?q=KeyEvent+MenuEvaluator $\endgroup$
    – ybeltukov
    Oct 4, 2013 at 13:42

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.