MarkdownShortcutsForSE

insert latex commands quickly by keybindings

Stars
3

Markdown Shortcuts for StackExchange

This userscript gives users several quick key bindings to speed up the editing of mathjax/chem/etc. into StackExchange posts.

Example hotkeys

The hotkeys are customizable!

  1. Pressing Alt+Shift+P would insert \pi directly.
  2. Pressing Alt+R would insert \mathrm{} with the caret auto-placed in the middle. Pressing Alt-R again would unwrap the \mathrm{} back.
  3. Pressing Alt+R with some text pre-selected would insert \mathrm{text}, retaining the selection on the text. Pressing Alt-R again would unwrap the \mathrm{text} back to text, retaining the selection on text.
  4. Prepending Ctrl to any of the above keybindings will auto-surround the insertion text with $$ (or \$ on some other SE sites)
  5. The keybinding and its associated insertion text can be modified by the user. So, you can set it to insert \pi on Alt+A instead.

Moreover, there are certain special shortcuts too:

  • Alt-Z => dollar-ifying (text => $text$)
  • Alt-C => double dollar-ifying (text => $$text$$)
  • Alt-A => frac-ify (A/B => \frac{A}{B})
  • Alt-S => align-ify (image demo)

You can change their keycodes or disable them as well. The given Z/C/A/etc. keycombos are just the default values.

You may use the Alt-H hotkey to get a popup containing a list of your current keybindings, in case you forget any of them ;)


How to customize hotkeys?

Each hotkey is composed of three parts:

  • modifier: either an "altKey" or a "shiftKey+altKey" combo

  • key: the key to press.

  • LaTeX command: to be inserted on pressing modifier+key. These are of two types:

    • braced: like \mathrm{}
    • non-braced: like \pi.

To customize hotkeys, head over to line 40 where SHORTCUTS = is initialized. By default, it is set as (note it is important to escape the \, so we write \\):

SHORTCUTS = [
    ["altKey", "I", "\\pi"],
    ["altKey", "R", "\\mathrm{}"],
]

So, if you want to insert \large{} on Alt-L, just add a single line:

SHORTCUTS = [
    ["altKey", "I", "\\pi"],
    ["altKey", "R", "\\mathrm{}"],
    ["altKey", "H", "\\large{}"]
]

To customize special hotkeys (for frac-ify, align lines, etc.), head over to the declaration for SPECIAL_SHORTCUTS. To disable any of them, delete/comment the corresponding line.

To position the caret at a specific position, use the pipe (|) character. Usual wrapping rules apply. In the absence of the pipe character, the caret is inserted inside the last {}/()pair, or at the end of the string.

Note: some Alt+key combos might be system-reserved (check this list), so you may need to prepend Shift to avoid overriding system-reserved behavior.