# Reflect / flip / rotate a table or array without rotating the elements

I have been working on an array environment, but I have realized that it would take up less space if it was reflected across the diagonal (I suppose this is not quite the same as flipped or rotated, but I thought I would include those keywords for similarity). This realization has come too late, because it will take me a long time to rewrite everything in the reflected manner. I was wondering, is there a quick fix to my laziness? I would like a way to reflect the array below without rotating any of the elements. That is, I would like x_1,...,x_8 across the top from left to right in the first row, b_{1,1},...,b_{8,1} across the second row from left to right, and so on. Rotated would be x_8,...,x_1 across from left to right, and I guess that would be an okay solution (though I would like the reflected one better).

I tried rotatebox, adjustbox, and sidewaystable, but all those also rotate the elements. Here is my code:

\documentclass{standalone}

\usepackage{tikz}
\usetikzlibrary{shapes}

\newcommand\squared[1]{\noindent\framebox{\bf{#1}}}
\newcommand*\circled[1]{\tikz[baseline=(char.base)]{
\node[shape=circle,draw,inner sep=2pt] (char) {#1};}}
\newcommand*\triangled[1]{\tikz[baseline=(char.base)]{
\node[shape=regular polygon,regular polygon sides=3,draw,inner sep=0pt] (char) {#1};}}

\begin{document}

$\begin{array}{c|c c c c c c c c c} x_1 & \squared{$b_{1,1}$} & \circled{$b_{1,2}$} & b_{1,3} & b_{1,4} & b_{1,5} & b_{1,6} & b_{1,7} & b_{1,8} & \cdots \\ x_2 & b_{2,1} & b_{2,2} & b_{2,3} & b_{2,4} & b_{2,5} & b_{2,6} & b_{2,7} & b_{2,8} & \cdots \\ x_3 & \squared{$b_{3,1}$} & \circled{$b_{3,2}$} & \triangled{$b_{3,3}$} & b_{3,4} & b_{3,5} & b_{3,6} & b_{3,7} & b_{3,8} & \cdots \\ x_4 & \squared{$b_{4,1}$} & b_{4,2} & b_{4,3} & b_{4,4} & b_{4,5} & b_{4,6} & b_{4,7} & b_{4,8} & \cdots \\ x_5 & b_{5,1} & b_{5,2} & b_{5,3} & b_{5,4} & b_{5,5} & b_{5,6} & b_{5,7} & b_{5,8} & \cdots \\ x_6 & \squared{$b_{6,1}$} & b_{6,2} & b_{6,3} & b_{6,4} & b_{6,5} & b_{6,6} & b_{6,7} & b_{6,8} & \cdots \\ x_7 & \squared{$b_{7,1}$} & \circled{$b_{7,2}$} & \triangled{$b_{7,3}$} & b_{7,4} & b_{7,5} & b_{7,6} & b_{7,7} & b_{7,8} & \cdots \\ x_8 & \squared{$b_{8,1}$} & \circled{$b_{8,2}$} & b_{8,3} & b_{8,4} & b_{8,5} & b_{8,6} & b_{8,7} & b_{8,8} & \cdots \\ \vdots & \end{array}$

\end{document}


Thanks for any help.

Here is an approach that's a bit different from those that I linked to. You will probably want to tweak this a bit to get it to produce what you want:

\documentclass{standalone}
\usepackage{environ}
\usepackage{etoolbox}

\usepackage{tikz}
\usetikzlibrary{shapes}

\newcommand\squared[1]{\noindent\framebox{\bf{#1}}}
\newcommand*\circled[1]{\tikz[baseline=(char.base)]{
\node[shape=circle,draw,inner sep=2pt] (char) {#1};}}
\newcommand*\triangled[1]{\tikz[baseline=(char.base)]{
\node[shape=regular polygon,regular polygon sides=3,draw,inner sep=0pt] (char) {#1};}}

%-@-(1)------------------------------------------------------------------------------------
\makeatletter

\def\ae@transposed@array{}

\NewEnviron{tarray}[1]
{\let\ae@transposed@array\relax
\def\ae@column@max{0}%%
\expandafter\ae@build@array\BODY\\\@nil
\def\ae@column@cnt{0}%%
\ae@assemble@array
\edef\ae@transposed@array{%%
\noexpand\begin{array}{#1}
\expandonce\ae@transposed@array
\noexpand\end{array}}%%
\typeout{====>\detokenize\expandafter{\ae@transposed@array}}%%
\ae@transposed@array
}

\def\ae@build@array#1\\#2\@nil{%%
%\typeout{==> row --> NUM1:\detokenize{#1}}%%
%\typeout{==> row --> NUM2:\detokenize{#2}}%%
\def\ae@continue@array{}%%
\def\ae@column@cnt{0}%%
\expandafter\ifx\expandafter\relax\detokenize{#2}\relax
\ae@parse@row@into@columns#1&\@nil
\else
\ae@parse@row@into@columns#1&\@nil
%\typeout{@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@}%%
\def\ae@continue@array{\ae@build@array#2\@nil}%%
\fi
%\typeout{====> columns:\ae@column@cnt:\ae@column@max}%%
\ae@continue@array
}

\def\ae@parse@row@into@columns#1&#2\@nil{%%
\def\ae@continue@row{}%%
\edef\ae@column@cnt{\number\numexpr\ae@column@cnt+1\relax}%%
\ifnum\ae@column@cnt>\ae@column@max\relax
\edef\ae@column@max{\ae@column@cnt}%%%
\fi
%\typeout{==> row ==> \ae@column@cnt ==> \detokenize{#1}}%%
%\typeout{==> row ==> \ae@column@cnt ==> \detokenize{#2}}%%
\expandafter\ifx\expandafter\relax\detokenize{#2}\relax
\else
\@ifundefined{ae@row@\ae@column@cnt}
{\def\ae@tmp{#1}%%
\expandafter\edef\csname ae@row@\ae@column@cnt\endcsname{\expandonce\ae@tmp}}%%
{\def\ae@tmp{#1}%%
\expandafter\edef\csname ae@row@\ae@column@cnt\endcsname{\expandafter\expandonce\csname ae@row@\ae@column@cnt\endcsname&\expandonce\ae@tmp}}
\def\ae@continue@row{\ae@parse@row@into@columns#2\@nil}%%
%\typeout{-----------------------------------}%%
\fi
\ae@continue@row
}

\def\ae@assemble@array{%%
\def\ae@continue{}%%
\ifnum\ae@column@cnt<\ae@column@max\relax
\typeout{==:\ae@column@cnt:}%%
\edef\ae@column@cnt{\number\numexpr\ae@column@cnt+1\relax}%%
\typeout{==:\ae@column@cnt:}%%
\@ifundefined{ae@transposed@array}
{\edef\ae@transposed@array{\expandafter\expandonce\csname ae@row@\ae@column@cnt\endcsname\noexpand\\}}%%
{\edef\ae@transposed@array{\expandonce\ae@transposed@array\expandafter\expandonce\csname ae@row@\ae@column@cnt\endcsname\noexpand\\}}
\def\ae@continue{\ae@assemble@array}%%
\fi
\typeout{==> \ae@column@cnt}%%
\ae@continue
}

\makeatother
%-@-(2)------------------------------------------------------------------------------------

\begin{document}

$\begin{tarray}{c|c c c c c c c c c} x_1 & \squared{$b_{1,1}$} & \circled{$b_{1,2}$} & b_{1,3} & b_{1,4} & b_{1,5} & b_{1,6} & b_{1,7} & b_{1,8} & \cdots \\ x_2 & b_{2,1} & b_{2,2} & b_{2,3} & b_{2,4} & b_{2,5} & b_{2,6} & b_{2,7} & b_{2,8} & \cdots \\ x_3 & \squared{$b_{3,1}$} & \circled{$b_{3,2}$} & \triangled{$b_{3,3}$} & b_{3,4} & b_{3,5} & b_{3,6} & b_{3,7} & b_{3,8} & \cdots \\ x_4 & \squared{$b_{4,1}$} & b_{4,2} & b_{4,3} & b_{4,4} & b_{4,5} & b_{4,6} & b_{4,7} & b_{4,8} & \cdots \\ x_5 & b_{5,1} & b_{5,2} & b_{5,3} & b_{5,4} & b_{5,5} & b_{5,6} & b_{5,7} & b_{5,8} & \cdots \\ x_6 & \squared{$b_{6,1}$} & b_{6,2} & b_{6,3} & b_{6,4} & b_{6,5} & b_{6,6} & b_{6,7} & b_{6,8} & \cdots \\ x_7 & \squared{$b_{7,1}$} & \circled{$b_{7,2}$} & \triangled{$b_{7,3}$} & b_{7,4} & b_{7,5} & b_{7,6} & b_{7,7} & b_{7,8} & \cdots \\ x_8 & \squared{$b_{8,1}$} & \circled{$b_{8,2}$} & b_{8,3} & b_{8,4} & b_{8,5} & b_{8,6} & b_{8,7} & b_{8,8} & \cdots \\ \vdots & \end{tarray}$

\end{document}


A couple of comments are probably in order:

The primary idea here is to parse the table based upon rows ending in \\ and column entries separated by & and rebuild the table so that rows and columns are transposed.

There are two primary macros: the first is \ae@build@array which peels away each row and sends the row to the macro \ae@parse@row@into@columns. I use pseudo counters (so as not to clutter up counter registers---don't know whether I should really be worried about this) to keep track of the row the current column is being added to.

Having parsed the entire table, I reassemble the new table. It just so happens that you have as many rows and columns, but the first argument should be formatting for each column of the transposed table. (I think this makes this philosophically different from those of the linked question.)

Getting rules into the table is probably a bit more tricky. For now, I'll leave the headache unsolved.

I've left in the \typeout macros I used to help me track what I was doing when assembling the table. That way you can see a bit better (if you so desire) how the assembly is progressing (particularly if you try tweaking and things break on you).

I unabashedly use the resources of etoolbox. You might want to check whether there are any counterindications against other packages: I don't know of any off the top of my head. I also use the environ package which makes it particularly easy to pass the contents of the table to a macro for parsing and restyling. (Once again you might want to check for conflicts with other packages you may be using.)