26

Suppose I want to edit a table

moon & 456456456423423534 &  2342523
sun  &             234324 &     3453

You see I have to enter a lot of spaces manually to align stuff. It is ok for a small table but for a large table this is inefficient.

I wonder there there is an editor that I can easily write tables in human readable script. I am familiar with vim and Ultraedit though I don't know which functionality specifically I should use. Perhaps there are software better than vim and Ultraedit for this purpose.

Werner
  • 603,163

9 Answers9

24

I am very happy with vim and the tabular plugin. To align the table, place the cursor anywhere inside the (nonaligned) table and type :Tabularize /&. The result is a nicely aligned table.

Before:

moon&456456456423423534&2342523
sun&234324&3453

Then a :Tabularize /&, after:

moon & 456456456423423534 & 2342523
sun  & 234324             & 3453

If you need this often, you can assign a custom shortcut for this.

Marco
  • 26,055
  • 1
    You beat me by 10 seconds. :) Nice plugin. There's a video tutorial on how to use it. :) – Paulo Cereda Mar 01 '12 at 14:43
  • Thank you, can you tell me how to assign a this function as 'control-E' in vim. I forgot the syntax of keyboard shortcut in vimrc – Zwingli Tannenbaum Mar 01 '12 at 15:07
  • @ZwingliTannenbaum You can assign a mapping in command mode with :nmap <Leader>at :Tabularize /&<CR> or in insert mode with :imap <Leader>at <ESC>:Tabularize /&<CR>i. I choose <Leader> (which is the backslash by default) to avoid interference with the scrolling (<CTRL>-e). at simply stands for “align table”, but you are free to pick another mnemonic. – Marco Mar 01 '12 at 15:33
  • Sorry I have a lot of trouble in installing tabularize.vim as a plugin. I use Mac and Windows and Linux, I want to install tabuliarze.vim on all three, the supposedly autoloaded plugin directory just doesn't get autoloaded, I tried a whole bunch different directories but it just doesn't get loaded, is there a way to "force load" tabularize.vim, say put in vimrc file . "loadplugin /.vim/plugin/after/tabularize.vim" – Zwingli Tannenbaum Mar 01 '12 at 16:01
  • On Unices the following should be enough: 1) git clone git://github.com/godlygeek/tabular.git 2) cp -r -u tabular/* ~/.vim – Marco Mar 01 '12 at 16:26
  • Sorry, the , stuff is literally type in or use the control-V trick. – Zwingli Tannenbaum Mar 01 '12 at 19:18
  • 1
    @ZwingliTannenbaum What about trying either of then and check which one works? A try will not nuke your system. – Marco Mar 01 '12 at 20:11
  • You could also use the Align plugin. – schmendrich Mar 02 '12 at 22:12
  • you could also include the line delimiters to the pattern: :Tab /&\|\\\\ will do the trick. – bmurauer Aug 22 '21 at 06:41
24

In emacs, AUCTeX includes align.el, which provides the command align-current: select the content of the table, and run M-x align-current, and it will auto-align the columns, producing the same result as in @Marco's answer

Ben Lerner
  • 7,082
  • Sorry I don't use EMACS, how to do it in VI or ultraedit? – Zwingli Tannenbaum Mar 01 '12 at 14:52
  • 3
    @ZwingliTannenbaum, I don't use VI or ultraedit :) Marco's presented one approach in vim, and your question just asked if there was some editor that did table-editing well. If vi or ultraedit are actually requirements in your question, you may want to edit it to clarify that a bit. – Ben Lerner Mar 01 '12 at 15:03
7

On a mac

With Texshop

1) You can use a matrix panel

enter image description here

2) Remark from Alan Munn

In TeXShop you can also use a spreadsheet for the cells and then paste them with Paste Spreadsheet Cells from the Macros menu

With TextMate

enter image description here

1) you begin with a selection like this

  12 15 13
  1 2 3 

then you select "convert selection to table" or you type "shift control command T" to get

\begin{tabular}{c}
\hline
12 15 13\\
\hline
1 2 3\\
\hline
\end{tabular}

2) you can use "Create Table" and you give the number of rows and column then with TAB you can move from cell to cell

Moriambar
  • 11,466
Alain Matthes
  • 95,075
6

Disclaimer: I also use the Tabular plugin in Vim (see Marco's answer). :)

There's a similar Vim plugin called Align which behaves pretty much the same as Tabular.

moon & 456456456423423534 & 2342523
sun & 234324 & 3453

By issuing :Align &:

moon & 456456456423423534 &  2342523
sun  &             234324 &     3453

You can also use multiple separators at once, e.g, :Align = + - \* /.

There's also another command called AlignCtrl which has more options, including text alignment (left, right, center). By using the > operator, you can right-justify the table just like the question example.

Lots of examples can be found in the GitHub repo.

Paulo Cereda
  • 44,220
4

The next function uses the tabular plugin in Vim to automatically align table columns as you type them (once you have typed the first row):

function! s:ealign()
    let p = '^.*&\s.*$'
    if exists(':Tabularize') && getline('.') =~# '^.*&' && (getline(line('.')-1) =~# p || getline(line('.')+1) =~# p)
        let column = strlen(substitute(getline('.')[0:col('.')],'[^&]','','g'))
        let position = strlen(matchstr(getline('.')[0:col('.')],'.*&\s*\zs.*'))
        Tabularize/&/l1
        normal! 0
        call search(repeat('[^&]*&',column).'\s\{-\}'.repeat('.',position),'ce',line('.'))
    endif
endfunction

To make it work only with tex files add the following to your vimrc file:

augroup ft_tex
    au!
    au Filetype tex inoremap <buffer> <silent> & &<Esc>:call <SID>ealign()<CR>a
augroup END
petobens
  • 3,306
4

For winedt, there is a plugin: TeXtab. You can find the installtion instructions in the page itself. To quote from there:

The macro formats selected lines with respect to & and \\. It disregards \&

There are two ways to format the selected lines:

formatting with blanks leading and trailing & and \\:

aaaaaaaaaa&bb\&bbbbbb&ccccccc&&\\
eeee&fffff&gggg&hh&ii&jjjjjjj&kkkkkk\

will be changed to

aaaaaaaaaa & bb\&bbbbbb & ccccccc &    & \\
eeee       & fffff      & gggg    & hh & ii  & jjjjjjj & kkkkkkkk \

formatting without blanks between text and separators:

aaaaaaaaaa&bb\&bbbbbb&ccccccc&  &\\
eeee      &fffff     &gggg   &hh&ii &jjjjjjj&kkkkkk\

How do you use the macro:

Select full lines containing lines of a tabular block. Choose in menu Edit one of the menu item Format TeX-Tables with blanks or Format TeX-Tables without blanks (ALT e x or ALT e o).

ShreevatsaR
  • 45,428
  • 10
  • 117
  • 149
4

When I studied Astronomy over 20 years ago knowledge of Unix was assumed.

$ more myfilename.csv
moon & 456456456423423534 & 2342523
sun  &234324 &        3453

and now

$ cat myfilename.csv | column  -t                     
moon  &  456456456423423534  &  2342523
sun   &  234324              &  3453

With little fiddling you can easily align columns to the right!

3

OpenOffice.org allows to use any character as column separator for CSV (comma-separated values file format), so if you write large tables, you can put their data in a separate file and edit it there.

Here is some how-to on this functionality.

Vivi
  • 26,953
  • 31
  • 77
  • 79
yo'
  • 51,322
3

1. LaTable and Tablas

On Windoz, you can use LaTable

LaTable at CTAN

or Tablas

Tablas 3.0

LaTable may have some small quirks under Win7. Both programs produce LaTex code to copy and paste into a LaTex manuscript, or saved as separate files, which you can input in your manuscript.

2. CSVed and uniCSVed

Sam Francke has written two very potent programs for manipulating CSV-files. You can easily adopted those to a LaTex workflow. Have a look at the home page of CSVed and uniCSVed

CSVed is the most feature rich of the two programs. You can manipulate CSV-files in more of less every thinkable ways. CSVed is especially capable for editing CSV-files with lot of records, which you can inputas table body in a longtable or tabular environment. The author is also looking into the possibility to add options to save to LaTex format (also an option to automatically escape LaTex special characters). Limitation: You cannot save in a UTF8 format (uniCSVedit can). Until the export filter is ready, you have to input the file between suitable table environments in your manuscript, after you have added \\. On the other hand, you can use Nicola Talbot's datatool-package to merge the resulting CSV-file into a LaTex table.

uniCSVed has less features than CSVed, but you can save your file in Unicode (also without BOM). Links to uniCSVed are:

uniCSVed with installer
uniCSVed portable

uniCSVed can save the CSV-files in a format usable for LaTex (i.e. with & and \\).

  1. Open a file to convert (has to be UTF8 with or without BOM), or create a new file
  2. Go to TAB Modify 2 and add the suffix \\ to the last column
  3. Go to first TAB and change separator at Other &
  4. Open menu File - Encode in and select UTF-8 wo BOM
  5. Save your file and is ready for LaTeX

TeXmaker

Also, the free cross-platform LaTeX editor TeXmaker has a very capable Table Wizard. But as most Wizards, this is a one-shot. When you have closed the Wizard, you are left with the usual LaTeX-code in the manuscript. It cannot be reloaded in the Wizard for further editing.

Sveinung
  • 20,355