# Quick way to invert a fraction

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?

-
Perhaps Ctrl+6 and -1(inside the placeholder)? – kglr Dec 10 '12 at 14:23
The point is to come up with something more convenient than cut-paste. Evaluation would definitely be less. – tel Dec 13 '12 at 0:12
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. – tel Dec 13 '12 at 0:16

Surely I'm missing something, but...

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


1 / expr


Try menu Evaluation > Evaluate In Place

## Palette

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[]},
NotebookWrite[nb,
Replace[
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.

-
a nice simple example of button ;-) – chris Dec 10 '12 at 14:27
@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. – jVincent Dec 10 '12 at 14:51
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)? – tel Dec 10 '12 at 15:28
@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. – Mr.Wizard Dec 10 '12 at 15:48
You could wrap it in CreatePalette and use InputNotebook[] instead, at least in v8 and v9. +1 – Rojo Dec 10 '12 at 19:56