Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

What is the best way of editing an existing LaTeX table? I need to swap the order of two columns and can't think of an efficient way of doing this.

My table is a long creature of the following structure:

A &     -0.523 &      26.94 &     0.8243 &     0.0000 &     -12.67 \\
B &     -0.614 &      30.02 &     0.8509 &     0.0000 &     -13.12 \\
C &     -0.630 &      32.92 &     0.9254 &     0.0000 &     -21.45 \\

I need to change the values so that it is:

A &     26.94 &      -0.523 &     0.8243 &     0.0000 &     -12.67 \\
(etc)

The thoughts that I had were to import it into Excel and to drag columns around, and then use the concatenate() function to produce the number & number structure. I feel like there might be a better way of doing this that I am unaware of.

share|improve this question
4  
There are plenty of similar questions already on the site: search for "table editor" to see a selection: e.g. Tool for manipulating LaTeX tables (HTML to LaTeX) and Comprehensive list of tools that simplify the generation of LaTeX tables –  Alan Munn May 16 '12 at 1:44
1  
@AlanMunn I don't see this ability in any of the tools mentioned. I'm familiar with some of them, but generally they seem to be useful for generating the table initially, not for editing an existing Latex table (though maybe I have not looked thoroughly enough). –  celenius May 16 '12 at 1:55
    
I guess that's true. –  Alan Munn May 16 '12 at 2:08
2  
I recommend renaming the question to be more specific to moving columns around, which would allow it to stand on its own and make it more useful for those googling. –  qubyte May 16 '12 at 6:34
1  
In Texmaker you can use Alt + Shift to select columns. Then just copy & paste. –  leo May 20 '12 at 18:07

10 Answers 10

up vote 27 down vote accepted

Any editor with regular expression replace can easily swap columns. For example on the command line if tab.tex contains your input this regexp will swap column 2 and 3. (You may need more or less backslashes in the regexp depending on the command line shell in use)

 sed -e "s/^\([^\&]*\)\&\([^\&]*\)\&\([^\&]*\)/\1\3\2/" tab.tex 
A       26.94      -0.523 &     0.8243 &     0.0000 &     -12.67 \\
B       30.02      -0.614 &     0.8509 &     0.0000 &     -13.12 \\
C       32.92      -0.630 &     0.9254 &     0.0000 &     -21.45 \\
share|improve this answer
3  
Thanks David - I'll try it out. That is a mind-melting regular expression. –  celenius May 16 '12 at 1:44
1  
the regexp is really just three ([^&])& groups that is, sequence of not-& followed by an &. It just needs the backslashes to hide the special characters from the shell (I was using bash shell, other command lines may be different) –  David Carlisle May 16 '12 at 1:47
    
I'm starting to understand it; thanks again. regexp is something I have tried to learn several times. It did work (though stripped out some of the &). –  celenius May 16 '12 at 1:56
2  
oh sorry you need \1&\3&\2& to put the &'s back in the replacement or possibly \&\&\3\&\2\& depending. –  David Carlisle May 16 '12 at 2:01
    
@David: lol, your last comment is a nice illustration of how your earlier "... regexp is really just ... " is very optimistic. I only use regexes only when I have to. If you don't use them every day, I think the syntax is very hard to remember and get right. –  Rabarberski Jun 8 '12 at 13:09

+1 David for sed one-liner but with all due respect there is no better tool for working with tables than AWK. This is the solution for the above problem!

awk  'BEGIN {FS="&";OFS="&"}{print $1,$3,$2,$4,$5}' file.csv > newfile.csv
share|improve this answer
3  
Very cool. Thanks. –  Alan Munn May 16 '12 at 2:06
    
Great answer, thanks! I need one more tip to get this to work right, though. I understand that FS sets the separator, but how would I get awk to understand that the \\ at the end of each line is not part of the last field, but the end of line? –  altabq May 11 at 1:16

As a tip for the future:

When creating big tables, I make a command to encode the row.

\newcommand{\resultrow}[4]{#1 & #2 & #3 & #4 \\}

If you then want to change the order/layout of something you only have to change the command definition.

You should take care in making the command as descriptive as possible, and that the arguments are in the order that they are logical to the command not to the table. This will make later changes to the order simpler.

As per request, an example. Take this table:

\begin{tabular}{l l l l}
  a & b & c & d \\
  2.33 & 4.55 & 5.66 & 7.88 \\
  3.44 & 5.66 & 6.77 & 9.00 \\
  .......
\end{tabular}

If you then want to change the order of the columns, well see the other answers to this question. I would however solve it like this:

\newcommand{\abcdresults}[4]{ #1 & #2 & #3 & #4 \\}
\begin{tabular}{l l l l}
  a & b & c & d \\
  \abcresults{2.33}{4.55}{5.66}{7.88}
  \abcresults{3.44}{5.66}{6.77}{9.00}
  .......
\end{tabular}

If you then want to change the order, you can just simply change the body of \abcdresults to for instance: {\textbf{#3} & #1 & #2 & #4 \\}

share|improve this answer
7  
+1: for a simple, but nice approach to decouple presentation order from data order! –  Daniel May 16 '12 at 12:14
    
@DavyLandman Could you elaborate, please? I don't understand how to use your tip to solve the problem (though I'm using tabular quite frequently). Thanks! –  Keks Dose May 24 '12 at 8:44

There are already good answers here, but there's a moral imperative to point out that, of course, there's an easy way to do this in emacs (or xemacs), without using regular expressions. You can copy a column to a register using copy-rectangle-to-register, paste it in as a new column using insert-register, and then delete the unwanted column using delete-rectangle. It's less fancy than using a regular expression, but easier to do one step at a time, making sure it's doing exactly what you want.

For the sake of completeness, use C-Space to mark one corner of the rectangle, and move to the opposite one. Then you can invoke the command you want on the rectangle defined by the two corners.

share|improve this answer
1  
Definitely +1 for using "column edit mode" in your text editor. I think column edit mode would be faster than the awk/sed regex because it would take me at least 5 minutes to create from scratch the above regex... when column edit would be done in 2 minutes. –  Trevor Boyd Smith May 16 '12 at 11:59
    
Alternatively, you can use Emac's Org mode to convert the data to a table with (org-table-create-or-convert-from-region) and move the columns around with M-<left> and M-<right>. Then you can either export it in LaTeX format or save it as a text file. –  Ricardo May 16 '12 at 12:55
    
Yes I nearly gave an emacs rectangle answer rather than sed, so +1, although someone claimed in a comment to an answer I gave to a different question that not everyone uses emacs, and also the rectangle comamnds require the input is nicely lined up as in the MWE, whereas the sed or awk field based versions will match fields even if the input file is less well padded for alignment of the entries. –  David Carlisle May 16 '12 at 12:56
3  
@DavidCarlisle, in emacs, if the input is not as nicely aligned as in the MWE, align and align-current work well and can take care of that. –  Ricardo May 16 '12 at 15:24
1  
@PhilHirschhorn I also hear that there are some strange people who use something called "Microsoft Word" instead of LaTeX. The world never ceases to amaze. –  Seamus May 17 '12 at 12:29

Some TeX editors, e.g. WinEdt and TeXnicCenter and also text editors, e.g. Notepad++, offer a rectangular selection feature which is highly useful for editing tables.

For the WinEdt users, this nice and often overlooked functionality is in the Status Line.

enter image description here

If you click on LINE it becomes Block and that switches to the rectangle mode. This allows the user to select rectangular regions and perform copy paste options as usual.

enter image description here

This is especially useful to fill in the missing & columns to tabular data imported from Excel, MATLAB etc. via copying one column of & and pasting it between columns.

TeXnicCenter 2 makes rectangular selections if keys ALT + SHIFT are pressed while selecting. The same is true for Notepad++.

share|improve this answer
    
Also very useful to know! Apparently you can do this in TextWrangler using a "rectangular selection" function with the command Option-drag or shift-option-click (though I can't get it to work). –  celenius May 17 '12 at 20:53
    
@dgs Thanks a bunch for the addition. I didn't know that TC2 have that option too. –  percusse May 20 '12 at 12:05
    
You're welcome. Actually it was this very feature which made me dump TC1, even though TC2 still is an alpha release. –  dgs May 20 '12 at 12:39

In TeXShop (for Mac Users)
(1) Hold the <option> key down and then click and drag the mouse to select a column.
(2) Cut <command><X> and
(3) Paste <command><V>
to place it as a new column of your table where desired.

enter image description here

share|improve this answer

In Excel you don't need to use concatenate to keep the table structure. If you import the table as in your MWE, you should be able to keep the & separators and the \\ line breaks in separate columns. Once you swap the columns, you can save it as a tab-delimited .txt file and you are done. The & and \\ will be separated from the data by a tab, but that shouldn't be an issue.

share|improve this answer
    
That's very useful to know. Do you choose a space as your delimiter when importing? –  celenius May 16 '12 at 16:09
    
I use space and tab when importing; most importantly, I look for the visual cue in the Excel import-window that tells you how the data will be imported, and choose the delimiters accordingly. –  Ricardo May 19 '12 at 23:52

Like emacs, Vim has a column selection mode (^V) which will allow you to select columns and move them about. So ^v {select column} d {move to new location} p. And there's an extension called Tabular which will allow you to reformat tables with ease as well.

share|improve this answer

You can also use one of Sam Franke’s excellent tools for manipulating delimited text files, CSVed and uniCSVed. Those are true graphical interfaces. You find the programs at this web-page.

share|improve this answer

Here is a Excel Macro for exporting table content to a latex table. It is not perfect but when prototyping large tables and reordering columns this is quite helpful.

share|improve this answer
    
The official homepage of excel2latex would either be ctan.org/pkg/excel2latex or launchpad.net/excel2latex –  matth May 24 '12 at 20:01

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.