Methods to edit column order in an existing LaTeX table?

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 \\}


+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

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 \

Tags:

Editors

Tables