Over the years of using Mathematica, I find myself doing something along the lines of rewriting this:

$\frac{[A^-_F]}{[A^-_U] + \frac{[H^+][A^-_U]}{\kappa_1}}$

as this:

$\frac{[A^-_U] + \frac{[H^+][A^-_U]}{\kappa_1}}{[A^-_F]}$

on a constant basis. In other words, just switching the numerator and the denominator of a fraction. Copying and pasting is kind of a drag, especially when you have to do it many times, and Mathematica can be kind of persnickety when it comes to highlighting things. Is there a built in keyboard shortcut that can invert fractions automatically? If not, is there a way for me to build a custom keyboard shortcut in order to do so?

  • 2
    $\begingroup$ Perhaps Ctrl+6 and -1(inside the placeholder)? $\endgroup$ – kglr Dec 10 '12 at 14:23
  • $\begingroup$ The point is to come up with something more convenient than cut-paste. Evaluation would definitely be less. $\endgroup$ – tel Dec 13 '12 at 0:12
  • $\begingroup$ Also, with evaluation, if any of the variables have values assigned, the output would be the wrong thing, and it might be scrambled by Mathematica's default simplification routine. $\endgroup$ – tel Dec 13 '12 at 0:16

Surely I'm missing something, but...

expr = (x - 1) (x - 2)/(x - 3)^2

Mathematica graphics

1 / expr

Mathematica graphics

Try menu Evaluation > Evaluate In Place


Since the above is trivial, here is a palette to flip a selected fraction, assuming that it is in the form of a FractionBox. This has the advantage of not evaluating anything, which I suppose could change your expression in an undesired way.

Button["Flip Fraction",
 With[{nb = SelectedNotebook[]},
    NotebookRead @ nb,
    x_FractionBox :> Reverse @ x

Evaluate the code, then select the Button and choose Palettes > Generate Palette from Selection. You must make a Palette; the raw button will not work.

  • $\begingroup$ a nice simple example of button ;-) $\endgroup$ – chris Dec 10 '12 at 14:27
  • $\begingroup$ @Mr.Wizard Is there any particular reason why you state the button alone would not work? It seems to work just fine here, as I would expect it to. $\endgroup$ – jVincent Dec 10 '12 at 14:51
  • $\begingroup$ Is there any way to assign a keyboard shortcut to a custom button like the one you have above? I use Mathematica mostly as a typesetting program, and I'd like to have a way to invert that doesn't break my flow. Also, what would I add to the button code to get it to automatically select whatever fraction the cursor is currently in (without having to highlight it)? $\endgroup$ – tel Dec 10 '12 at 15:28
  • $\begingroup$ @jVincent at least in v7 the window with the fraction loses focus the button in another regular Notebook is clicked, so SelectedNotebook[] fails. Perhaps that has been changed in later versions. $\endgroup$ – Mr.Wizard Dec 10 '12 at 15:48
  • 1
    $\begingroup$ You could wrap it in CreatePalette and use InputNotebook[] instead, at least in v8 and v9. +1 $\endgroup$ – Rojo Dec 10 '12 at 19:56

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.