[Tex/LaTex] Useful shortcuts or key bindings or predefined commands for emacs+AUCTeX

auctexemacs

One of the problems I've encountered with emacs in general, and with the defaults key bindings in particular, is the diversity. There are simply so many of them that sometimes, at the end of the day, you simply don't know that you should look for something which is already there.

Are you aware of a place where these can be found? I'm not referring to a listing of all possible key bindings etc. (which can probably be found in AUCTeX documentation), rather than sort of the top 10/20/50 or so. If something like this is not around, maybe having a wiki here would be a nice idea.

Well… Now it's your turn…

Best Answer

Summary answer

This answer is CW. Please add the best tricks to it!

First things first: these cheat sheets are very useful:

C-h m will show information on the current major mode.

C-c C-h shows all bindings that start with C-c

C-h b shows all bindings currently available.

Now for some details.

Macros

C-c C-m (or C-c RET) insert macro (warning: pressing TAB at this point will take a while because AUCTeX will load a list of all the macros it knows about. If the macro you pick is \usepackage and then TAB AUCTeX will load a list of all the packages it knows about for you to choose from.)

C-c C-f C-e Add an \emph{} and place cursor between the braces. Or if you have some text marked, wrap it in \emph replace C-e by C-b, C-c C-i, C-t for \textbf \textsc \textit or \texttt respectively. C-c C-f ? will give you a list of all the possibilities. This command knows about mathmode and will behave accordingly inside dollars or in equation environment. Also, with a prefix argument C-u, it will change the innermost surrounding font command accordingly, so if you have point at * in \textit{This is* bold} and press C-u C-c C-f C-b, you get \textbf{This is* bold}. Even though it's four keystrokes, it can be very useful.

Sections/Environments

C-c C-s create section with optional label

C-c C-e create environment (\begin and \end tags) choose from an autocomplete list or type your own. (If you have LaTeX mode on in an empty file, this will default to the document environment, and prompt you for a documentclass.

C-u C-c C-e change the type of the current (innermost) environment

C-c . mark current environment

C-c * mark current section/subsection

C-c ] close current environment

C-M-a find matching begin environment, C-M-e find matching end

Compilation

C-c C-a runs LaTeX/TeX, BibTeX/Biber, Makeindex, etc... as appropriate until the document is ready, and eventually opens the output document in the viewer. It is like running C-c C-c (see below) repeatedly, but with just a single key binding. This is what you want to use most of the time if you trust the advanced ability of AUCTeX to guess the right commands to run.

C-c C-c do most appropriate compilation activity (LaTeX, BibTeX, View...) It's pretty smart, and you can override the default to pick what action you want to do. You can use this key binding also to delete all auxiliary files created during compilation (Clean and Clean all actions).

C-c C-r do most appropriate compilation activity (LaTeX, BibTeX, View...) to the region that has been pinned by C-c C-t C-r. Of course one wants to use C-c . or C-c * to ease the select a region.

C-c C-v view document (without compiling)

RefTeX

RefTeX, while not strictly part of AUCTeX is an essential part of your emacs/TeX working life.

C-c ( add label. Auto-suggests names based on current section.

C-c ) add \ref you can select from the list of currently defined labels. If you customise a certain variable (whose name escapes me) you can add hyperref, varioref and cleverref reference commands to the list of types of reference available.

C-c [ add citation. RefTeX understands bibliography commands and will search your .bib for references that match the pattern you supply to this command. Can be customised to have harvard, chicago and other kinds of reference commands available. (No BibLaTeX citation style support yet as far as I know.)

C-c = jump to section: opens list of sections/subsections etc you can jump to.

When in the toc buffer :

RET Go to the selected header and hide the toc buffer. If the local variable TeX-Master is correctly set (see AUCTeX documentation), it is a very convenient way of jumping in any other file of your project.

Space Go to the selected header, don't hide the toc buffer.

< Increase the level of the selected header or region (section becomes chapter, subsection becomes section, etc)

> Reduce the level of the selected header or section.

M-% Search-and-replace regexp in the whole document

RefTeX also provides a lot of convenient options through its menu, including conflicting label detection and fix (runs a search-and-replace query), document-scaled grep, document-scaled search and replace.

Miscellaneous

LaTeX-math-abbrev-prefix is a useful thing which is ` by default.

This gives you access to a bunch of mathmode symbols in two keystrokes. For example, you can get \subset by typing ` {

By customising LaTeX-math-list you can get whatever you like out of this prefix command. A bunch of the most useful are on the refcard.

C-c ; comment/uncomment region

C-c C-o C-b for folding TeX code

C-M-S f or C-M-S b to mark the content between a pair of balanced braces.

M-RET or C-c C-j has different effects depending on the current environment:

  • In list environments like itemize, enumerate and description, it inserts a new line and an \item macro. In this example, *!* indicates the position of cursor (point in Emacs jargon).

    \begin{itemize}
    \item Text *!*
    \end{itemize}
    

    Hitting M-RET results in:

    \begin{itemize}
    \item Text
    \item *!*
    \end{itemize}
    
  • In tabular like environments, it includes a \\ to start a new line and necessary number of & by parsing the table specification argument. Again, *!* indicates the position of cursor (point)

    \begin{tabular}{lll}
      1 & 2 & 3 *!*
    \end{tabular}
    

    Hitting M-RET results in:

    \begin{tabular}{lll}
      1 & 2 & 3 \\
     *!*&&
    \end{tabular}
    
  • In some math environments, it also includes a \\ and necessary number of &. Example for empheq environment provided by the package by the same name. Before:

    \begin{empheq}{alignat=2}
      &&& *!*
    \end{empheq}
    

    After:

    \begin{empheq}{alignat=2}
      &&& \\
    *!*&&&
    \end{empheq}