Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

The solution to this question on how to get latex code for a text cell (which Mathematica doesn't do correctly) was formulated as a palette but I need it as a keyboard shortcut.

How can I add an item to that executes this solution's code? And in general, how does one construct a key command that executes arbitrary code, not just front-end packets and tokens?

share|improve this question
Did you see this ? Should work similarly for – Rolf Mertig Nov 30 '12 at 14:30
up vote 7 down vote accepted

Think I figured it out: you can't use special characters like _ or ~~ in, everything has to be full-form.

    Item[KeyEvent["y", Modifiers -> {Control}], 
        FrontEndExecute @ Module[{nb, t},
            nb = NotebookCreate[Visible -> False];
            t = ExportString[NotebookRead[SelectedNotebook[]], "LaTeX"];
            t = StringTrim[StringReplace[t, 
                StringExpression[BlankSequence[], "\\begin{document}", 
              Pattern[txt, BlankSequence[]], "\\end{document}", BlankNullSequence[]] :> txt]];
            NotebookWrite[nb, Cell[t, "Text"]];
            SelectionMove[nb, All, Notebook];
            FrontEndTokenExecute[nb, "Copy"];
        MenuEvaluator -> Automatic 
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.