1

This question already has an answer here:

I have write the code of a table with more than hundred rows, (in fact set of tables, with more than hundres rows at all !) and five columns.

Let's call the columns from left to right by A, B, C, D, E;
I want to reverse the order of the columns,
i.e. to get a table with columns E, D, C, B, A, respectively from left to right.

Is there any code or package to do this for me?

My code is something like the following:

\begin{table}
\caption‎‎
{computations of the first class}
‎\begin{center}‎
‎\scalebox{1}‎
{ 
‎\begin{tabular}{| c || c | c | c | c |}‎ 
‎\hline‎
A0 & C0 & B0 & D0 & E0 \\ 
‎\hline‎ \hline 
A1 & C1 & B1 & D1 & E1 \\ 
‎\hline‎
A2 & C2 & B2 & D2 & E2 \\ 
‎\hline‎
A3 & C3 & B3 & D3 & E3 \\ 
‎\hline‎
A4 & C4 & B4 & D4 & E4 \\ 
‎\hline‎
A5 & C5 & B5 & D5 & E5 \\ 
‎\hline‎
A6 & C6 & B6 & D6 & E6 \\ 
‎\hline‎
A7 & C7 & B7 & D7 & E7 \\ 
‎\hline‎
A8 & C8 & B8 & D8 & E8 \\ 
‎\hline‎
A9 & C9 & B9 & D9 & E9 \\ 
‎\hline‎
‎\end{tabular}‎
}
‎\end{center}‎‎
‎\end{table}‎


As @Alan Munn has been guided me to this question,
I have added one line to my code(befor \begin{tabular}):

\def\1#1&#2&#3&#4&#5\\{#5 &#4 &#3 &#2 &#1 \\} 

But this does not changes any thing. Where is the problem?

marked as duplicate by Alan Munn, Stefan Pinnow, Mensch, Zarko tables Sep 23 '17 at 19:23

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Welcome to TeX.SX! Please post a minimal working code example (MWE) to help us help you. – TeXnician Sep 23 '17 at 15:52
  • 1
    For such cases, it's probably best to create the table in some other software to begin with (e.g. Excel), and then import it to LaTeX. See e.g. excel2latex. – Sverre Sep 23 '17 at 16:16
  • @TeXnician ; I have added my sample code. – Jungle Boy Sep 23 '17 at 16:40
  • 2
    The solution here will work for up to 9 columns. How can I reverse (flip horizontal) a table?. But it would probably be faster to cut and paste into Excel and reverse them there then paste back. – Alan Munn Sep 23 '17 at 16:43
  • @Alan Munn ; I have added one line to my code: \def\1#1&#2&#3&#4&#5\\{#5 &#4 &#3 &#2 &#1 \\} But this changes only the B-column by C-column. Where is the problem? – Jungle Boy Sep 23 '17 at 17:40
3

This method is the idea of https://tex.stackexchange.com/a/317425/4686 in a way which needs no redefinition depending on number of columns.

  • advantage 1: it only needs each tabular line is ended by \\, (i.e. it makes no supposition about \hline being there to serve as delimiter)

  • advantage 2: no extra package (admittedly this is moot point, as the said extra packages are pre-destined to rule the 21st century ;-))

  • disadvantage 1: limited to 8 columns (I was lazy) It now works with up to nine columns.

  • disadvantage 2 (a big one!): it needs the extra \1 (or whatever) mark-up. Sadly the >{tokens} construct of array package is in no help here. But I took the snapshot with compilation using array so I leave it there.

Code:

\documentclass{article}
\usepackage{array}

\makeatletter
\long\def\REVERSEIT #1\\%
  {\@REVERSEIT 
  #1&\@gobble&\@gobble&\@gobble&\@gobble&\@gobble&\@gobble&\@gobble&\@gobble\\}

\long\def\@REVERSEIT #1&#2&#3&#4&#5&#6&#7&#8&#9\\%
  {#9&#8&#7&#6&#5&#4&#3&#2&#1\\}
\makeatother

\let\1\REVERSEIT

\begin{document}

\begin{tabular}{| c || c | c | c | c |}
\hline
\1 A0 & C0 & B0 & D0 & E0 \\
\hline\hline
\1 A1 & C1 & B1 & D1 & E1 \\
\hline
\1 A2 & C2 & B2 & D2 & E2 \\
\hline
\1 A3 & C3 & B3 & D3 & E3 \\
\1 A4 & C4 & B4 & D4 & E4 \\
\1 A5 & C5 & B5 & D5 & E5 \\
\hline
\1 A6 & C6 & B6 & D6 & E6 \\
\1 A7 & C7 & B7 & D7 & E7 \\
\1 A8 & C8 & B8 & D8 & E8 \\
\1 A9 & C9 & B9 & D9 & E9 \\
\hline
\end{tabular}‎
\quad
\begin{tabular}{| c || c | c | c |}
\hline
\1 A0 & C0 & B0 & D0 \\
\hline\hline
\1 A1 & C1 & B1 & D1 \\
\hline
\1 A2 & C2 & B2 & D2 \\
\hline
\1 A3 & C3 & B3 & D3 \\
\1 A4 & C4 & B4 & D4 \\
\1 A5 & C5 & B5 & D5 \\
\hline
\1 A6 & C6 & B6 & D6 \\
\1 A7 & C7 & B7 & D7 \\
\1 A8 & C8 & B8 & D8 \\
\1 A9 & C9 & B9 & D9 \\
\hline
\end{tabular}‎
\quad
\begin{tabular}{| c || c | c |}
\hline
\1 A0 & C0 & B0  \\
\hline\hline
\1 A1 & C1 & B1  \\
\hline
\1 A2 & C2 & B2  \\
\hline
\1 A3 & C3 & B3  \\
\1 A4 & C4 & B4  \\
\1 A5 & C5 & B5  \\
\hline
\1 A6 & C6 & B6  \\
\1 A7 & C7 & B7  \\
\1 A8 & C8 & B8  \\
\1 A9 & C9 & B9  \\
\hline
\end{tabular}‎

\end{document}

enter image description here

Compared to OP I deliberately removed some \hline's to show method did not need them. Notice that in the above I copied from original, and there B and C columns are already permuted!

To test with nine columns:

\begin{tabular}{| c || c | c | c | c |c | c | c | c |}
\hline
\1 A1 & B2 & C3 & D4 & E5 & F6 & G7 & H8 & I9 \\
\1 A1 & B2 & C3 & D4 & E5 & F6 & G7 & H8 & I9 \\
\1 A1 & B2 & C3 & D4 & E5 & F6 & G7 & H8 & I9 \\
\1 A1 & B2 & C3 & D4 & E5 & F6 & G7 & H8 & I9 \\
\1 A1 & B2 & C3 & D4 & E5 & F6 & G7 & H8 & I9 \\
\1 A1 & B2 & C3 & D4 & E5 & F6 & G7 & H8 & I9 \\
\hline
\end{tabular}‎
  • I have used your code but nothing changes. – Jungle Boy Sep 23 '17 at 18:49
  • post some example illustrating the problem. "I have used your code but nothing changes" will not help me much help you. By the way, I notice now that B and C columns are permuted in the original! – user4686 Sep 23 '17 at 18:53
  • 1
    are you actually inserting the extra \1 in each row ? – user4686 Sep 23 '17 at 18:57
  • I add \1 in each row; also I have tested your code (I have copy your code and paste it) for 9 rows; but it does not reverse anything, the result has the usual order. – Jungle Boy Sep 23 '17 at 20:19
  • Mr Spock would be in state of shock to hear that. Are you actually compiling the document? – user4686 Sep 23 '17 at 22:34
2

This assumes you use rules (but you shouldn't). No limit on the number of rows.

\documentclass{article}
\usepackage{xparse,environ}

\ExplSyntaxOn
\NewEnviron{reversetabular}[1]
 {
  \famke_revtab_main:nV { #1 } \BODY
 }

\seq_new:N \l__famke_revtab_rows_seq
\seq_new:N \l__famke_revtab_out_seq
\seq_new:N \l__famke_revtab_row_seq
\tl_new:N \l__famke_revtab_temp_tl

\cs_new_protected:Nn \famke_revtab_main:nn
 {
  \seq_set_split:Nnn \l__famke_revtab_rows_seq { \hline } { #2 }
  \seq_clear:N \l__famke_revtab_out_seq
  \seq_map_inline:Nn \l__famke_revtab_rows_seq
   {
    \tl_if_blank:nTF { ##1 }
     {
      \seq_put_right:Nn \l__famke_revtab_out_seq { }
     }
     {
      \tl_set:Nn \l__famke_revtab_temp_tl { ##1 }
      \tl_remove_once:Nn \l__famke_revtab_temp_tl { \\ }
      \seq_set_split:NnV \l__famke_revtab_row_seq { & } \l__famke_revtab_temp_tl
      \seq_reverse:N \l__famke_revtab_row_seq
      \seq_put_right:Nx \l__famke_revtab_out_seq
       {
        \seq_use:Nn \l__famke_revtab_row_seq { & } \exp_not:N \\
       }
     }
   }
  \begin{tabular}{#1}
  \seq_use:Nn \l__famke_revtab_out_seq { \hline }
  \end{tabular}
 }
\cs_generate_variant:Nn \famke_revtab_main:nn { nV }
\ExplSyntaxOff

\begin{document}

\begin{reversetabular}{| c || c | c | c | c |}
\hline
A0 & C0 & B0 & D0 & E0 \\
\hline\hline
A1 & C1 & B1 & D1 & E1 \\
\hline
A2 & C2 & B2 & D2 & E2 \\
\hline
A3 & C3 & B3 & D3 & E3 \\
\hline‎
A4 & C4 & B4 & D4 & E4 \\
\hline
A5 & C5 & B5 & D5 & E5 \\
\hline
A6 & C6 & B6 & D6 & E6 \\
\hline
A7 & C7 & B7 & D7 & E7 \\
\hline
A8 & C8 & B8 & D8 & E8 \\
\hline
A9 & C9 & B9 & D9 & E9 \\
\hline
\end{reversetabular}‎

\end{document}

enter image description here

  • I am new to the latex; which part of your code I should copy? your code contains some phrases that contains my name; what are they? that confused me. – Jungle Boy Sep 23 '17 at 18:03
  • 1
    @Famke The calls to the packages and all code between \ExplSyntaxOn and \ExplsyntaxOff inclusive. – egreg Sep 23 '17 at 20:46

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