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.

3 comments:

Spencer Thiel said...

Thanks! This is pretty awesome. I found you can open your CSV files with emacs, use your above steps, but then instead of doing table-insert, you can do table-capture and automatically convert your tables to emacs-table format.

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