Some custom emacs keybindings for LaTeX to assist in the writing process

Here are some habits I think will assist in the writing of LaTeX documents:

  1. Always encapsulate your superscripts and subscripts with curly braces, even if they consist only of 1 characters. For example, it should x_{1} rather than x_1. The reason is that lot’s of time, I go back to changing my subscripts, and I often use a command like replace-string in emacs to help do it for the entire document. If I change the subscript from 1 character to multiple characters, thing gets messed up. For example, x_1 to x_i1 where it should really be x_{i1}. Using curly braces just help prevent things from going wrong and having to go back to the errors.
  2. As mentioned on this post, it is best to keep each sentence on its own line to facilitate version controlling the document.

To help with these two tasks, I’ve binded my “.”, “\^”, and “_” keys to smart functions that will do what I want. Pressing the keys again will go back to the plain character.

 <pre class="example">;; following for latex, adapted from ess-smart-underscore

;; can also be implemented using sequential command http://www.emacswiki.org/emacs/SequentialCommand (defun tex-smart-underscore () “Smart \”\” key: insert \”{}\”. If the underscore key is pressed a second time, \”{}\” is removed and replaced by the underscore.” (interactive) (let ((assign-len (length “{“))) (if (and (>= (point) (+ assign-len (point-min))) ;check that we can move back (save-excursion (backward-char assign-len) (looking-at “{}”))) ;; If we are currently looking at ess-S-assign, replace it with _ (progn (forward-char) (delete-backward-char (+ 1 assign-len)) (insert ““)) (delete-horizontal-space) (insert “_{}”) (backward-char))))

(defun tex-smart-caret () “Smart \”\^\” key: insert \”\^{}\”. If the caret key is pressed a second time, \”\^{}\” is removed and replaced by the caret.” (interactive) (let ((assign-len (length “\^{“))) (if (and (>= (point) (+ assign-len (point-min))) ;check that we can move back (save-excursion (backward-char assign-len) (looking-at “\\^{}”))) ;; looking-at reads regexp, so need to escape the caret character ;; If we are currently looking at ess-S-assign, replace it with \^ (progn (forward-char) (delete-backward-char (+ 1 assign-len)) (insert “\^”)) (delete-horizontal-space) (insert “\^{}”) (backward-char))))

(defun tex-smart-period () “Smart \”.\” key: insert \”. \n\”. If the period key is pressed a second time, \”. \n\” is removed and replaced by the period.” (interactive) (let ((assign-len (length “. %%\n”))) (if (and (>= (point) (+ assign-len (point-min))) ;check that we can move back (save-excursion (backward-char assign-len) (looking-at “\. %%”))) ;; If we are currently looking at ess-S-assign, replace it with _ (progn (delete-backward-char assign-len) (insert “.”)) (delete-horizontal-space) (insert “. %%\n”))))

;; http://stackoverflow.com/questions/5500035/set-custom-keybinding-for-specific-emacs-mode ;; eval-after-load didn’t fix this for me ;; http://www.emacswiki.org/emacs/LaTeX (add-hook ‘LaTeX-mode-hook (lambda () (define-key LaTeX-mode-map (kbd “_”) ‘tex-smart-underscore) (define-key LaTeX-mode-map (kbd “\^”) ‘tex-smart-caret) (define-key LaTeX-mode-map (kbd “.”) ‘tex-smart-period) ) )

Byobu, an enhanced screen

I use screen on a daily basis. I recently discovered byobu, an enhanced add-on for screen that makes it even more customizable. A screencast can be found here. After reading some descriptions of byobu here and here, I re-discovered screen’s ability to split screens. I’ll probably begin to use this feature. Not sure if I’ll need to use byobu. I do like the row on the bottom describing the system resources.

emacs keybindings in xpdf and xdvi

I’ve been using xpdf and xdvi for reading documents I’ve downloaded or my LaTeX-generated documents on the laptop more and more these days due to their speed. The one thing I require (desire) in all programs I use on a day to day basis, especially when I have to navigate the file, is to have emacs keybindings.

To do so for the two readers, first, add the following to ~/.Xresources:

 <pre class="src src-sh">! look<span style="color: #00ffff;"> in</span> man xdvi

xdvi.mainTranslations: #override Ctrlv: down-or-next()n Altv: up-or-previous()n Alt Shift <: goto-page(1)n Alt Shift >: goto-page()n Ctrlf: right(0.015)n Ctrlb: left(0.015)n Ctrln: down(0.015)n Ctrlp: up(0.015)n l: right(0.015)n h: left(0.015)n j: down(0.015)n k: up(0.015)n Ctrls: find()n xdvigeometry: 1350×700 xdvishrinkFactor: 4

! look in man xpdf and xpdfrc; ~/.xpdfrc xpdfgeometry: 1350×700 xpdfinitialZoom: width

Also, create and add the following to ~/.xpdfrc:

 <pre class="src src-sh">initialZoom width

continuousView yes bind ctrl-v any pageDown bind alt-v any pageUp bind alt-shift-< any gotoPage(1) bind alt-shift-> any gotoLastPage bind ctrl-n any scrollDown(16) bind ctrl-p any scrollUp(16) bind ctrl-f any scrollRight(16) bind ctrl-b any scrollLeft(16) bind h any scrollLeft(16) bind l any scrollRight(16) bind k any scrollUp(16) bind j any scrollDown(16) bind ctrl-s any find

Now I can at least navigate the file with emacs keybindings.

I would also like to get this to work on ghostview (for postscript files) or djview (for djvu files), but have yet to find out how to do so on these programs (or found programs for these formats that allow custom keybindings). I’ve written about using emacs’ doc-view to view all my files, but it can be sub-optimal:

  • the conversion process on large files can take a long time,
  • no “continuous” view mode,
  • searching the text does not highlight the text,
  • and the file doesn’t update when the dvi or pdf file is updated (or have a keybinding to update) for use with LaTeX (editing and updating files). UPDsATE: actually, you can refresh using the r keybinding; it works quite fast and stays on the same page you are on.

Please do let me know if you know how to make custom keybindings on ghostview or djview (or similar, fast programs). Also let me know if I am not aware of any features in doc-view that would make my life easier. Thanks!