Ads by Google

Friday, July 4, 2008

Quickly Creating LaTeX Tables in Emacs

One of the "that sucks" part of using LaTeX is the part about creating tables. While it is powerful, the idea of all those ampersands, slashes and \hlines makes me go ape. And it takes a couple of edit-compile-view cycles to get into the groove of table writing in LaTeX.

Thankfully Emacs has the table mode as part of the standard distribution. Invoke table-mode as M-x table-insert and create the table. Answer the wizard questions and enter the data for the columns. Once you've written the data into the table, generate the LaTeX table by hitting C-^ and choosing the export format as latex.

Done.

Paste that back into your tex file and add the \usepackage{tabular} or \usepackage{tabularx} to the preamble and compile the file. Adjust the spacing by deleting off unwanted table rows.

This beats the earlier method by a large order in terms of productivity if you can't be bothered to learn LaTeX tables.

And one other tip: Create the emacs table in the tex file and comment it out. If you ever want to make extensive changes to the table, do the following.

  1. Uncomment the emacs table
  2. Move point to the table or highlight the region
  3. M-x table-recognize will make it ready to edited as an emacs table
  4. make the changes and export it again as latex
  5. comment out the emacs table
Keeping the emacs table inline saves some time in creating the table again if you ever need to make changes. Of course, you could edit the LaTeX table code but I don't venture there at unless the changes are cosmetic.

4 comments:

Claudio Vellage said...

There is actually no need to convert the tables to LaTeX format, the pgfplotstable package can automatically generate beautiful tables directly from CSV files, this is how I usually do it. There's a step by step guide on my website: http://www.latex-tutorial.com/tutorials/advanced/lesson-9/
I created a minor mode for emacs to insert latex snippets into my document, so tables can be included in the document very efficiently and I don't have to write a lot of boilerplate code when creating my documents.

sivaram said...

Claudio,

Thanks for the tip. I will check it out.

Sivaram

Anonymous said...

You can also produce the whole LaTeX document with org-mode:
- Yank this to a new buffer:
(require 'package)
(package-initialize)
;; Do not auto-load packages lists
(setq package-archives nil)
;; Add repositories
(add-to-list 'package-archives
'("gnu" . "http://elpa.gnu.org/packages/"))
- Evaluate the buffer
M-x eval-buffer RET (M-x usually means [Alt] + x; RET usually means [Enter])
- Install org
M-x package-install RET org RET
- Create a new buffer
- Type this:
|col 1|col 2|col 3|
|col a|col b|col c|
- C-c C-e l p (C-c usually means [Ctrl] + c; C-e = [Ctrl] + e; l is a lowercase L, not 1)
- Open your pdf and RTFM :) .

Salih Muhammed said...

This made it a lot easier for me. But it still sucks.