1

I am working on a LaTex file.

Is it possible to bind a keybinding to add \textit or textbf for the word, where the cursor is pointing?

For example, cursor is on top of word hello:

  • hello converted into \textit{hello} # with M-o i
  • hello converted into \textbf{hello} # with M-o b
3
  • 1
    How about using the yasnippet library and writing your own snippet, or just copying an existing snippet that does something exactly the same or very similar thereto? I personally use a custom snippet that surrounds an active region with similar/same codes, or if there is no active region, then it leaves the cursor in between the wavy brackets to insert the text manually and then the tab key exits and places the cursor after the closing wavy bracket. – lawlist Nov 19 '20 at 21:29
  • I think M-o i is the default keybinding but not achieve any change on my end. I will look into yasnippet library – alper Nov 19 '20 at 21:39
  • 1
    Auctex already has this with the C-c C-f, which is the TeX-font command. it works like a prefix, thus you type something more to choose what you want. For bold you use C-c C-f C-b. It is also "smart" and if you use inside an equation it will use \mathbf instead. Type C-c C-f C-h to see all possibilities. You might set a different keybinding for TeX-font for easier typing. – darcamo Nov 19 '20 at 21:41
1

You can create commands to achieve the job you want and link them to the key you like.

(defun italic-word ()
 "your comment" 
 (interactive) 
 (easy-mark) (TeX-font nil 9))

(defun bold-word ()
 "your comment" 
 (interactive) 
 (easy-mark) (TeX-font nil 2))
 
 (bind-key (kbd "M-o i") #'italic-word LaTeX-mode-map)
 (bind-key  (kbd "M-o b") #'bold-word LaTeX-mode-map)

"M-o i" and "M-o b" are already bind. It is not a good idea to use them.

the function easy-mark come with the easy-kill package on elpa. Probably you need to install it.

edit : I don't really like this code that operates on the mark. I think easy-mark should be for interactive use only. So I suggest you replace with this more robust code:

(defun italic-word ()
 "your comment" 
 (interactive) 
 (save-excursion
   (beginning-of-thing 'word)
   (insert "\\textit{")
   (end-of-thing 'word)
   (insert "}")))

(defun bold-word ()
 "your comment" 
 (interactive)
 (save-excursion
   (beginning-of-thing 'word)
   (insert "\\textbf{")
   (end-of-thing 'word)
   (insert "}")))
10
  • Can I bind something like M-u i ? Not sure what is M-o i bind to – alper Nov 20 '20 at 5:09
  • M-u is bind to 'upcase-word. You can reveal what a key is bind to with C-h k – gigiair Nov 20 '20 at 5:19
  • It does {\bf doo} instead can I do \textbf{hello} ? – alper Nov 22 '20 at 16:53
  • (global-set-key (kbd "M-o i" #'italic-word) does not work on my end :-( – alper Nov 22 '20 at 16:55
  • 1
    I suspect you are in tex-mode. You should be in latex-mode. Check the display on the modeline – gigiair Nov 23 '20 at 6:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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