Skip to content

Instantly share code, notes, and snippets.

@zmwangx
Last active May 20, 2016 19:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zmwangx/11199126 to your computer and use it in GitHub Desktop.
Save zmwangx/11199126 to your computer and use it in GitHub Desktop.
Some tips regarding AUCTeX/Emacs/LaTeX, from an email to Tri Dao on 04/22/2014.

Hi Tri,

The other builtin way to insert fraction is to use C-c RET, which is bound, in LaTeX mode (AUCTeX), to TeX-insert-macro, which is an interactive function. According to its help (C-h f TeX-insert-macro RET),

TeX-insert-macro is an interactive compiled Lisp function in `tex.el'.

It is bound to C-c RET, <menu-bar> <LaTeX> <Macro...>.

(TeX-insert-macro SYMBOL)

Insert TeX macro SYMBOL with completion.

AUCTeX knows of some macros and may query for extra arguments, depending on
the value of `TeX-insert-macro-default-style' and whether `TeX-insert-macro'
is called with C-u.

In the case of fractions, the keystroke sequence is C-c RET frac RET, which enters

\frac{.}{}

where I use a dot to indicate the position of the point (Emacs term for cursor) after calling the function. It's not very efficient for \frac (well, it might be somewhat efficient if you hate manually typing in curly braces), but it's good to know the standard solution. (I personally hate unnecessary interactive commands.)

As I told you before, in Emacs you can always code up functions yourself. (Emacs Lisp is Turing complete, so yeah, you can do anything—for instance, Emacs comes bundled with several mail clients, several web browsers, several tty games, etc.) For instance, you could consider the following code to insert \frac:

(defun TeX-insert-fraction()
  "Insert \"frac{}{}\" and move point to after the first \"{\"."
  (interactive)
  (insert "\\frac{}{}")
  (backward-char 3))
  
(add-hook 'TeX-mode-hook
          '(lambda () (local-set-key (kbd "KEYBINDING")
                                     'TeX-insert-fraction)))

And possibly another keybinding to move two points to the next curly braces. Same thing. (The canonical way is to use a prefix argument 2 to forward-char, namely, C-u 2 C-f; you certainly don't want to do that for moving two points right.)

By the way, KEYBINDING in the code snippet above should be something like C-c C-f. Check if a keybinding already exists by C-h c in the corresponding mode. For instance, C-h c C-c C-f in LaTeX-mode tells me C-c C-f runs the command TeX-font. If you don't care about that TeX-font (I do use it, though), simply overwrite it.

I've been discussing how to insert \frac. However, I found myself using \frac rare enough so I prefer typing it myself. Moreover, \frac's, as opposed to macros for symbols, sort of structure your equations, so I find them not so intrusive as compared to e.g. \alpha + \beta + \gamma + \delta (you can you TeX-fold-mode to get pretty print right inside Emacs for a lot of symbols, but that's a whole new story). If I find some recurring macro or macro set tiring to type, I'd use an alias for that, you know, like \renewcommand{\a}{\alpha}. One great thing about Emacs is Emacs allows you to auto insert, i.e., pre-fill a file according to a template at the time you creates it (not sure if there's a vim equivalent). For instance,

(require 'autoinsert)
(auto-insert-mode)
(setq auto-insert-directory "~/.templates")
(setq auto-insert-query nil)
(define-auto-insert "\\.tex\\'" "latex.tex")

automatically fills every one of your new FILENAME.tex with contents from ~/.templates/latex.tex. Auto insert mode is actually more powerful than this simple application. See Doc and Emacs Wiki for details. So I could drop all those aliases in the template file, and carry them around without worrying about them in the future. (Currently I have some thirty heavily used aliases along with common \usepackage's, \newtheorem's, \title, \author, begin/end documents, etc. in my template. Pretty handy.) If you dislike extra aliases in every single source file, you could also write separate template files and \input them in source files (I have a couple of these for discipline-specific aliases/special macro defs), but then you risk portability. Anyway, the point is I find aliases more convenient than keybindings for inserting full macros (I could type faster and won't get an Emacs pinky). You mileage might vary.

By the way, here are a bunch of LaTeX-mode configs in my .emacs that I consider essential for everyone. You might find them useful.

;;; get information on loaded packages
;;; (which leads to better parsing)
(setq-default TeX-auto-save t)
(setq-default TeX-parse-self t)

;;; pdf mode
;;; (generate pdf output instead of dvi)
(setq-default TeX-PDF-mode t)

;;; rename default auto directory to ~/.auctex-auto
;;; (if you don't want to see an ./auto directory in every single
;;; source file directory, use this)
(setq-default TeX-auto-local "~/.auctex-auto")

;;; auto-fill-mode
;;; (automatic line break; you could set the variable 'fill-column' to 
;;; adjust the number of columns -- default is 70)
(add-hook 'TeX-mode-hook 'auto-fill-mode)

;;; auto indent with RET
;;; (you don't need to constantly press TAB to get your indentation 
;;; right)
(add-hook 'TeX-mode-hook
          '(lambda () (local-set-key (kbd "RET") 'newline-and-indent)))

Sorry about the longish email. I'm proud of Emacs (as well as LaTeX), so I couldn't stop talking about it once I begin! ;)

Thanks,
Zhiming

You're welcome. In comparison, Vim does fewer things than Emacs, so it does better in terms of text editing per se.

However, in terms of (temporary) expansion, Emacs could also do the (same?) job. There are two ways:

  1. The good old keybinding way, which is a bit complicated, and the outcome is not very desirable, but illustrates some important mechanisms of Emacs and Elisp (Emacs Lisp). Use M-: (which is bound to eval-expression by default) to bring up the elisp eval minibuffer. Then type in your command, for instance,

    (local-set-key (kbd "C-a a n") '(lambda () (interactive) (insert "\\left\\{ a_n \\right\\}")))
    

    You need to hold down the control, which is rather annoying. But Emacs is all about control; more precisely, Escape-Meta-Alt-Control-Shift — EMACS.

  2. There is a more fancy and much more efficient way — Abbrev Mode (Doc and Wiki). I use it myself mainly for auto correction; I guess I underestimated its power for Vim users. Basically, first you enable abbrev-mode by M-x abbrev-mode to bind \left\{ a_n \right\} (which contains three "words") to aan, the key sequence is

    \left\{ a_n \right\} C-u 3 C-x a g aan RET
    

    (I'll assume you know C-u and C-x full well, since the notation is explained in the first few pages of the built in tutorial.) That's it. Six more key strokes: C-u 3 C-x a g and RET. If you use it often, you can bind add-global-abbrev (default to C-x a g) to something simpler. From now on you can type ann followed by whitespace, which automatically expands to \left\{ a_n \right\}.

    If you like abbrev mode (a minor mode), you can add a hook in your .emacs so that it is automatically enabled every time you enter TeX mode:

    (add-hook 'TeX-mode-hook 'abbrev-mode)
    

    If you really like it, since TeX-mode is based on text-mode, you can add a hook to all derivatives of text mode:

    (add-hook 'text-mode-hook 'abbrev-mode)
    

    If you really really like it, you can also add it to programming modes:

    (add-hook 'text-mode-hook 'abbrev-mode)
    (add-hook 'prog-mode-hook 'abbrev-mode)
    

If you decide to switch to Emacs, I can't guarantee that you'll be more satisfied with it. in the end. However, one thing is certain: Emacs is more powerful, and you can build all kinds of workflows around it if you dig deep enough. Especially if you are prepared to take on the task of hacking elisp yourself. After all (from vim.org):

7800W.

Feel free to email me with any questions. (I'm not an expert, but I'll try to help with my limited experience.)

Best, Zhiming

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment