Keybinding for italic/bold text in Latex


#1

Hi,

I recently switched from Textmate to Atom in order to write scientific papers in Latex. I am working on OSX and desperately missing the CMD+I or CMD+B shortcut to format the selected text italic or bold in Atom.

Thus, I wonder how to define a keybinding to add the \emph{} or \textbf{} environment to a selected text. I tried something like

'atom-text-editor':
  'cmd-r': 'latex:build'
  'cmd-i': 'latex:toggle-italic'

in the keymap.cson. While compiling works, italic doesn’t. As you see I’m a newbie.

Thanks in advance for helpful advices.


Creating a keybinding for bold <strong> and italics <em>
#2

Have you tried using the Keybinding Resolver (Cmd+. on OS X and Ctrl+. on other platforms)? It will show you what key Atom saw you press and the command that was triggered because of it.


#3

Thanks for your answer. The keybinding resolver shows exactly the command 'latex:toggle-italic' but my problem is that I have actually invented this command and I am not sure what working function to call instead of 'latex:toggle-italic'.

To be precise, I would like to select a word Test in a Latex document, press Cmd+I and Atom translates this into \emph{Test}.


#4

There aren’t functions for this built-in to Atom. You would have to write your own … or perhaps a package already offers something similar.


#5

I finally had a couple of hours time to work on this issue and somehow solved it. Maybe someone with more coding experience might suggest a solution more simply - however some might find it useful.

Open Atom > Init Script and insert

atom.commands.add 'atom-text-editor', 'latex:toggle-bold', ->
  return unless editor = atom.workspace.getActiveTextEditor()
  selection = editor.getLastSelection()
  selection.insertText("\\textbf{#{selection.getText()}}")

atom.commands.add 'atom-text-editor', 'latex:toggle-italic', ->
  return unless editor = atom.workspace.getActiveTextEditor()
  selection = editor.getLastSelection()
  selection.insertText("\\textit{#{selection.getText()}}")

and in Atom > Keymap include

'atom-text-editor':
  'cmd-b': 'latex:toggle-bold'
  'cmd-i': 'latex:toggle-italic'

This works for me.


#6

I tried this, but it doesn’t work for me. Does it still work for you?


#7

Never mind, I disabled the latextools package and it started working for me. It’d be nice if I didn’t have to disable latextools. I had to unset latextools’ own command I and b shortcuts.


#8

Thanks @sma. I modified it slightly so that the cursor is placed inside of the braces if ctrl+i (or ctrl+b) is used with nothing selected.

atom.commands.add 'atom-text-editor',
  'latex:toggle-bold', ->
      return unless editor = atom.workspace.getActiveTextEditor()
      selection = editor.getLastSelection()
      selection.insertText("\\textbf{#{selection.getText()}}")
      if not editor.getSelectedText()
          editor.moveLeft()

atom.commands.add 'atom-text-editor',
  'latex:toggle-italic', ->
      return unless editor = atom.workspace.getActiveTextEditor()
      selection = editor.getLastSelection()
      selection.insertText("\\textit{#{selection.getText()}}")
      if not editor.getSelectedText()
          editor.moveLeft()

#9

thanks!
I also limited the scope of the shortcut to latex only in the keymap.cson like this

"atom-text-editor[data-grammar='text tex latex']":
  'alt-shift-i': 'latex:emph'

#10

I added editor.selectWordsContainingCursors() to the code from @ihincks, so the word at the current cursor position will be automatically selected.

atom.commands.add 'atom-text-editor',
    'latex:toggle-bold', ->
        return unless editor = atom.workspace.getActiveTextEditor()
        editor.selectWordsContainingCursors()
        selection = editor.getLastSelection()
        selection.insertText("\\textbf{#{selection.getText()}}")
        if not editor.getSelectedText()
             editor.moveLeft()

atom.commands.add 'atom-text-editor',
    'latex:toggle-italic', ->
        return unless editor = atom.workspace.getActiveTextEditor()
        editor.selectWordsContainingCursors()
        selection = editor.getLastSelection()
        selection.insertText("\\textit{#{selection.getText()}}")
        if not editor.getSelectedText()
             editor.moveLeft()