4
$\begingroup$

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?

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

1 Answer 1

6
$\begingroup$

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


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[
    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.

$\endgroup$
9
  • $\begingroup$ a nice simple example of button ;-) $\endgroup$
    – chris
    Commented Dec 10, 2012 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
    Commented Dec 10, 2012 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
    Commented Dec 10, 2012 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
    Commented Dec 10, 2012 at 15:48
  • 1
    $\begingroup$ You could wrap it in CreatePalette and use InputNotebook[] instead, at least in v8 and v9. +1 $\endgroup$
    – Rojo
    Commented Dec 10, 2012 at 19:56

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.