TableMode

Available at http://table.sourceforge.net/ together with a tutorial. Provided by default with emacs version >= 22.

Features of TableMode

Table.el provides EMACS with text based table creation and editing capabilities.

Be sure to use C-h b to see the default bindings.

A worked-out example

Let us give an example. Assume we want to convert the following to a html table.

  Name  cmd calls  Percentage
  rgb  93 534       46%
  Xah  82 090          40%
     total    203 118   100%

– First, we select the text and capture the table with the M-x table-capture command.

– We must then specify a column delimiter regexp. SPC SPC is ok for this table.

– For the row delimiter, we want a newline. This can be entered either with C-o or C-q C-j.

– We just type RET when asked for justification, this will select the default.

– For Minimum Cell Width, we can take a value of 10. Here is the final result:

                                      +----------+----------+----------+ 
                                      |Name      |cmd calls |Percent   |
  Name  cmd calls  Percent            +----------+----------+----------+
  rgb  93 534       46%               |rgb       |93 534    |46%       |
  Xah  82 090          40%      =>    +----------+----------+----------+
     total    203 118   100%          |Xah       |82 090    |40%       |
                                      +----------+----------+----------+
                                      |total     |203 118   |100%      |
                                      +----------+----------+----------+

The point should be in the first cell. Press TAB to go to the next cell, S-TAB to come back to the previous cell. Columns can be widened/shrunk with C-> and C-<. You can write in the cells, the table will be expanded or redrawn as needed.

Commands available with tables are prefixed with table. They can be listed by typing M-x table- TAB . There name is self-explanatory. For example, we can center the second column by putting the cursor on that column and typing M-x table-justify. Just answer column then center when asked. The following gives additional information on these commands:

      C-h f table-insert

When finished with editing, several options are available.

     +--------+-----------+---------+
     | Name   | cmd calls | Percent |
     +--------+           +---------+
     | rgb    |  93 534   |   46%   |
     | Xah    |  82 090   |   40%   |
     +--------+           +---------+
     | total  |  203 118  |  100%   |
     +--------+-----------+---------+
     +--------+-----------+---------+      
     | Name   | cmd calls | Percent |       Name     cmd calls   Percent  
     +--------+-----------+---------+                                     
     | rgb    |  93 534   |   46%   |       rgb       93 534       46%    
     +--------+-----------+---------+   =>                                   
     | Xah    |  82 090   |   40%   |       Xah       82 090       40%    
     +--------+-----------+---------+                                     
     | total  |  203 118  |  100%   |       total     203 118     100%    
     +--------+-----------+---------+
     <!-- This HTML table template is generated by emacs 22.1.1 -->
     <table border="1">
       <caption>Table</caption>
       <tr>
         <td align="left" valign="top">
           &nbsp;Name&nbsp;&nbsp;&nbsp;
         </td>
         <td align="center" valign="top">
           &nbsp;cmd&nbsp;calls&nbsp;
         </td>
         <td align="center" valign="top">
           &nbsp;Percent&nbsp;
         </td>
       </tr>
       <tr>
         <td align="left" valign="top">
           &nbsp;rgb&nbsp;&nbsp;&nbsp;&nbsp;
         </td>
         <td align="center" valign="top">
           &nbsp;&nbsp;93&nbsp;534&nbsp;&nbsp;&nbsp;
         </td>
         <td align="center" valign="top">
           &nbsp;&nbsp;&nbsp;46%&nbsp;&nbsp;&nbsp;
         </td>
       </tr>
       <tr>
         <td align="left" valign="top">
           &nbsp;Xah&nbsp;&nbsp;&nbsp;&nbsp;
         </td>
         <td align="center" valign="top">
           &nbsp;&nbsp;82&nbsp;090&nbsp;&nbsp;&nbsp;
         </td>
         <td align="center" valign="top">
           &nbsp;&nbsp;&nbsp;40%&nbsp;&nbsp;&nbsp;
         </td>
       </tr>
       <tr>
         <td align="left" valign="top">
           &nbsp;total&nbsp;&nbsp;
         </td>
         <td align="center" valign="top">
           &nbsp;&nbsp;203&nbsp;118&nbsp;&nbsp;
         </td>
         <td align="center" valign="top">
           &nbsp;&nbsp;100%&nbsp;&nbsp;&nbsp;
         </td>
       </tr>
     </table>

Specifying delimiters

Characters used to recognize/draw the borders of the table can be specified with the help of the following variables:

 table-cell-horizontal-chars    

Contains a string. All characters in string are valid horizontal delimiters for the table. The first character of the string is used for output.

 table-cell-horizontal-char    

Contains the character used as the vertical delimiter. “|” by default.

 table-cell-intersection-char  

Contains the character that appears at the intersection of rows and columns. “+” is the default.

The following settings give interesting results. They use utf-8 characters to draw continuous lines for the border of the table. Arguably, your font should support these character to display the desired effect.

 (setq table-cell-horizontal-chars "\u2501")
 (setq table-cell-vertical-char ?\u2503)
 (setq table-cell-intersection-char ?\u254B)

Here is the result. The vertical lines may not be lined up, depending on your browser configuration. Expect perfect alignment with no gap from inside emacs.

    ╋━━━━━━╋━━━━━━╋━━━━━╋
    ┃Name      ┃cmd calls ┃Percent ┃
    ╋━━━━━━╋━━━━━━╋━━━━━╋
    ┃rgb       ┃93 534    ┃46%     ┃
    ╋━━━━━━╋━━━━━━╋━━━━━╋
    ┃Xah       ┃82 090    ┃40%     ┃
    ╋━━━━━━╋━━━━━━╋━━━━━╋
    ┃total     ┃203 118   ┃100%    ┃
    ╋━━━━━━╋━━━━━━╋━━━━━╋

Some issues to cope with

Tables can be pretty memory intensive since several variables are allocated to each cell, so as to record their properties. This means that you will probably run into error messages if the table has more than an hundred rows.

On a modern computer, it is however safe to increase by a factor of 10 the variables max-lisp-eval-depth and max-specpdl-size, so that emacs can use more memory and handle bigger tables. Try for example:

   M-x set-variable RET max-lisp-eval-depth RET 40000 RET
   M-x set-variable RET max-specpdl-size RET 100000 RET

Or put in your InitFile

  (setq max-lisp-eval-depth '40000)
  (setq max-specpdl-size '100000)

For my Emacs (unicode-2 branch), I needed to change the lambda on line 5176 to

  (lambda (key value) (set-char-table-range elt key (table--replace-binding1 value)))

– GeorgBrandl?


CategoryModes CategoryTables