Saturday, January 15, 2011

LaTeX table manipulation using Emacs/AUCTeX+align.el+rectangular selection

Tables in LaTeX are sometimes somewhat difficult to manage; in particular doing anything with columns---in contrast to most word processors, which allow for the manipulation of columns in certain environments (e.g. tables). However, the text editor Emacs in fact allows for rectangular selections anywhere, which means that using Emacs as an editor for (La)TeX files makes working with tables easier.

The AUCTeX package for Emacs adds a vast number of useful features; however, it does not automatically format tabular environments. Here I  offer some notes on how to deal with LaTeX tables in a Emacs/AUCTeX environment.

So let's say we have a LaTeX document containing a (big) table, like this:



















And let's say we want to switch the third and fourth columns. Easily done in a typical word processor, but not necessarily so for a LaTeX document. Let's look at the LaTex .tex source for this table:













A rectangular selection will work only if we have  our columns aligned in our source .tex file. The align.el package for Emacs is needed here (as of Emacs 22, this functionality is built in).

Place the cursor at the beginning of the tabular environment (in our current example, right below the first \hline command will work), and use the align-current function (press M-x and then type align-current RET):

 





[Make sure there are no blank lines in your tabular environment; "align-current" will stop when it hits a blank line.]

If you're working with a large table, as in this example, you still won't be able to see the entire width of the table, so hold down SHIFT and left-click the mouse and choose "Decrease Buffer Text Size" repeatedly until the entire width of the table is visible on your screen (maximizing the Emacs frame to the maximum width of your desktop will be helpful as well...). You should then  be able to view the table as so:















All of our columns are now aligned. Now we need simply to select a rectangular region. There are (at least) two ways of going about doing so. Firstly, the ability to select rectangular regions is a built-in feature of Emacs (see the relevant section of the Emacs manual). Simply select the columnar area (mark it using C-SPC) and press either C-x r r to copy the rectangular area to the register or C-x r k to cut ("kill") the rectangular area and place it in the register. And then use C-x r y to paste ("yank") the copied/cut rectangle into the position you want it (with the cursor in the position of its upper left-hand corner).

However, if your Emacs has the visible selection turned on (as is the default as of Emacs 23), then it is nicer and easier to be able to see the selected area as a rectangle. One nice option is to use the rectangular selection features of the CUA package (a built-in default as of Emacs 22). One of the main functions of the CUA-mode is to change Emacs' copy, kill/cut, and yank/paste keybindings (M-w, C-w, C-y) to the "common" C-c, C-x, C-v. If you, like me, don't want to change the default Emacs keybindings, then include the following in your .emacs configuration file:

(cua-selection-mode t)
(setq cua-auto-tabify-rectangles nil) 

[If you do want the "common" keybindings, then use (cua-mode t) in place of (cua-selection-mode t).]

With this configuration in place, we can now visibly select rectangular areas, and use the normal copy/kill/yank keybindings to copy, cut, and paste the rectangular selections. In CUA-mode rectangular selections are made using C-RET rather than C-SPC. Here is the rectangular selection in our example:











I then kill ("cut") this rectangular area with C-w. And then position the cursor at the point where I want the upper left-hand corner of this rectangular area to be inserted:











And then yank ("paste") the column in its new position with C-y:










That's it. Here is the resulting table (with columns 3 and 4 switched):





















Oh, and here is a nice video demonstration of the use of rectangular selection cut-and-pasting (with CUA-mode): Emacs Column Editing by Mark Mansour. It doesn't deal with LaTeX, but rather HTML; however, it illustrates the general use.

4 comments:

  1. Very nice post indeed, thank you very much. It is also possible to use the Emacs org mode within AUCTeX to manipulate the table as if you were in a spreadsheet editor. More information

    http://orgmode.org/manual/A-LaTeX-example.html
    http://orgmode.org/manual/Tables.html#Tables

    ReplyDelete
  2. Yes, using org-mode tables is really, really handy for this kind of situations. Just enable the orgtbl-mode, load the package comment and try something like this: http://pastebin.com/CmC4xYDt

    Update your table, press C-c C-c and your latex table will be updated.

    ReplyDelete
  3. Thanks - I'll have a look at orgmode tables. It sounds very convenient.

    ReplyDelete
  4. Now I've had a chance to try them: Org-Mode Tables are very cool. Many thanks Damien & Oscar for pointing these out.

    ReplyDelete