Control order of columns in table
Using @MartinScharrer's rather new collcell
package, we can collect the body of each cell and save it with a given label, then typeset the cells in a different order, calling each label. Namely, the code below defines two column types: s{<label>}
(for save) and o{<label>}
(for output).
One drawback is that you need to put a whole bunch of &
at the end of each line to allow typesetting to occur (as many &
as you have columns). This can also be used to e.g. double one column, or remove one column.
\documentclass{article}
\usepackage{collcell}
\usepackage{array}
\newcommand{\newcelltoksifnew}[1]{%
\expandafter\ifx\csname cell@toks@#1\endcsname\relax
\expandafter\newtoks\csname cell@toks@#1\endcsname
\fi}
\newcolumntype{s}[1]{%
>{\unskip\newcelltoksifnew{#1}%
\collectcell{\global\csname cell@toks@#1\endcsname}}%
c%
<{\endcollectcell}%
@{}%
}
\newcolumntype{o}[1]{%
>{\the\csname cell@toks@#1\endcsname}%
c%
}
\begin{document}
\begin{table}\centering
\begin{tabular}{|c|c|c|}
a & c & b \\
AAAAA&CCCCC&BBBBB
\end{tabular}
\begin{tabular}{s{A}s{B}s{C}|o{A}|o{C}|o{B}|}
a & b & c &&&\\
AAAAA&BBBBB&CCCCC&&&
\end{tabular}
\caption{Comparing two tables to see that the spacing is not affected.}
\end{table}
\end{document}
Another method would be to swap to column using a string manipulation package, let's say xstring
;). Of course, swapping 2 columns is not reorder all of them!
The \swapcol command must be written at the begining of every row in which you want to swap the content of 2 cells. The last row must be ended with a \\
.
Here is the tricky code:
\documentclass[a4paper]{article}
\usepackage{xstring}
\usepackage{array}
\def\expaddtocs#1#2{%
\expandafter\expandafter\expandafter\def\expandafter\expandafter\expandafter
#1\expandafter\expandafter\expandafter{\expandafter#1#2}}
\def\swapcol#1#2#3\\{%
\ifnum#1>#2 \def\colinf{#2}\def\colsup{#1}%
\else \def\colinf{#1}\def\colsup{#2}\fi
\noexpandarg \let\swaprow\empty \iffalse{\fi\ifnum0=`}\fi
\expandafter\StrBefore\expandafter[\number\numexpr\colinf-1]{#3&}&[\beforecol]%
\ifx\beforecol\empty\else\expaddtocs\swaprow{\beforecol&}\fi
\StrBehind[\colinf]{&}&[\aftercol]%
\expandafter\StrBefore\expandafter{\aftercol}&[\colA]%
\expandafter\StrBehind\expandafter{\aftercol}&[\aftercol]%
\expandafter\StrBefore\expandafter[\number\numexpr\colsup-\colinf-1\expandafter]\expandafter{\aftercol}&[\intercol]%
\StrBehind[\colsup]{}&[\aftercol]%
\expandafter\StrBefore\expandafter{\aftercol&}&[\colB]%
\expandafter\StrBehind\expandafter{\aftercol}&[\aftercol]%
\expaddtocs\swaprow{\colB&}%
\ifx\intercol\empty\else\expaddtocs\swaprow{\intercol&}\fi
\expaddtocs\swaprow{\colA}%
\ifx\aftercol\empty\else\expaddtocs\swaprow{\noexpand&}\fi
\expaddtocs\swaprow{\aftercol\\}%
\ifnum0=`{\fi\iffalse}\fi \swaprow}
\begin{document}
\begin{tabular}{ccccc}
1 & 2 & 3 & 4 & 5 \\
1 & 2 & 3 & 4 & 5 \\
1 & 2 & 3 & 4 & 5 \\
\end{tabular}
\qquad
\begin{tabular}{ccccc}
\swapcol{2}{5}1 & 2 & 3 & 4 & 5 \\
\swapcol{2}{5}1 & 2 & 3 & 4 & 5 \\
\swapcol{2}{5}1 & 2 & 3 & 4 & 5 \\
\end{tabular}
\begin{tabular}{|c|c|c|}
a & c & b \\
AAAAA&CCCCC&BBBBB
\end{tabular}
\qquad
\begin{tabular}{|c|c|c|}
\swapcol{1}{2} a & c & b \\
\swapcol{1}{2}AAAAA&CCCCC&BBBBB\\
\end{tabular}
\end{document}