[Tex/LaTex] Keep first column on every page of long sideways-table

longtablerotatingtables

I have a table with a lot of columns, so I am going to make it landscape.The very first column is a sort of header, so I would like to repeat it on every page. Is there a way to do that?

Best Answer

I'll try to figure out what the problem is. You have a table with a big number of colums, and presumibly few rows, as for example this one:

Big table

You tried rotating it, but still it doesn't fit in one page. So you want to split the table among several pages, and repeat the first column in each page, all of this automatically.

Unfortunately, there is no package which allow you to repeat the first column as a kind of "header". longtable package is the closest, but it only allows the first row to be used as a header (a reasonable choice).

So the first question is, can you transpose your table, converting files into rows and vice-versa? The new big table would look like this (numbers are different because I'm generating them randomly, but this is not important):

Long table

First, note that the table is more compact this way, so there are better chances of fitting it into a single page. But even if it is too large, you can leave longtable package to take care of splitting it and repeat the header, using the code provided in other solutions. This will give:

lontable split

But, if for some reason, you still need that the header is a column instead of a row (I cannot imagine why), then you can use the following dirty trick. Use longtable as in the last example, but rotate 90 degrees each cell. This way the table would look like a "sideways table", and the first row will look like the first column. This is the code:

\documentclass{article}
\usepackage{longtable}
\usepackage{graphicx}
\usepackage{array}
\usepackage{fancybox}
\usepackage[a4paper, margin=3cm]{geometry}

\def\beginrotate{\begin{Sbox}}
\def\endrotate{\end{Sbox}\rotatebox{-90}{\TheSbox~~}}

\begin{document}\footnotesize
\newcolumntype{R}{>{\beginrotate}c<{\endrotate}}
\begin{longtable}{|R|R|R|R|R|R|R|}
\hline
\textbf{Foo} &  \textbf{Bar} &  \textbf{Foobar} & \textbf{Barfoo} & 
\textbf{FooFoo} & \textbf{Barbar} & \textbf{Foobaz} \endhead\hline
\input{bigtable}
\\\hline
\end{longtable}
\end{document}

Where bigtable is a file containing the data of a table with 30 rows and 7 colums. Its content is as follows (in order to complete the MWE):

0.6758 & 0.7150 & 0.6982 & 0.3166 & 0.9916 & 0.4469 & 0.6663\\\hline
0.5192 & 0.3351 & 0.1848 & 0.2907 & 0.9938 & 0.9485 & 0.8826\\\hline
0.8872 & 0.9911 & 0.5904 & 0.5111 & 0.8150 & 0.9460 & 0.6106\\\hline
0.6567 & 0.0251 & 0.0072 & 0.2924 & 0.0119 & 0.5548 & 0.8066\\\hline
0.8708 & 0.5457 & 0.6155 & 0.8238 & 0.5979 & 0.5184 & 0.9628\\\hline
0.1505 & 0.3449 & 0.8085 & 0.9886 & 0.5966 & 0.0804 & 0.4170\\\hline
0.6807 & 0.4711 & 0.6918 & 0.4803 & 0.7829 & 0.9600 & 0.1042\\\hline
0.8812 & 0.6242 & 0.7196 & 0.6065 & 0.3812 & 0.0360 & 0.0062\\\hline
0.2533 & 0.4434 & 0.5977 & 0.0679 & 0.7181 & 0.3089 & 0.9971\\\hline
0.3002 & 0.8496 & 0.0992 & 0.5157 & 0.3220 & 0.5868 & 0.9570\\\hline
0.1678 & 0.5196 & 0.6335 & 0.5228 & 0.2306 & 0.8910 & 0.4287\\\hline
0.6802 & 0.5555 & 0.3534 & 0.5206 & 0.1367 & 0.9867 & 0.8026\\\hline
0.7301 & 0.8947 & 0.5710 & 0.0133 & 0.4933 & 0.9754 & 0.1996\\\hline
0.8284 & 0.7035 & 0.3541 & 0.5774 & 0.4192 & 0.4091 & 0.5287\\\hline
0.9641 & 0.2976 & 0.2910 & 0.7979 & 0.5771 & 0.6541 & 0.8497\\\hline
0.2666 & 0.6168 & 0.4662 & 0.2921 & 0.4760 & 0.3647 & 0.8961\\\hline
0.4583 & 0.9469 & 0.4631 & 0.3696 & 0.5438 & 0.9070 & 0.4259\\\hline
0.4572 & 0.6667 & 0.5216 & 0.4976 & 0.2914 & 0.7089 & 0.0738\\\hline
0.6888 & 0.5940 & 0.8692 & 0.0071 & 0.5753 & 0.3133 & 0.4880\\\hline
0.6797 & 0.4619 & 0.3415 & 0.8531 & 0.6565 & 0.4343 & 0.3206\\\hline
0.2041 & 0.6366 & 0.5736 & 0.5109 & 0.6069 & 0.3847 & 0.0896\\\hline
0.8915 & 0.4274 & 0.3350 & 0.4032 & 0.4010 & 0.9764 & 0.7961\\\hline
0.0209 & 0.7044 & 0.5719 & 0.9619 & 0.5674 & 0.2458 & 0.5487\\\hline
0.7091 & 0.8960 & 0.1819 & 0.4002 & 0.4607 & 0.3069 & 0.5067\\\hline
0.1857 & 0.8420 & 0.7223 & 0.7178 & 0.6437 & 0.7449 & 0.6483\\\hline
0.4034 & 0.9813 & 0.0288 & 0.6326 & 0.7765 & 0.6866 & 0.5010\\\hline
0.3131 & 0.3296 & 0.6957 & 0.9979 & 0.3438 & 0.6381 & 0.6289\\\hline
0.7243 & 0.0523 & 0.7895 & 0.7588 & 0.3630 & 0.3918 & 0.9185\\\hline
0.7184 & 0.6831 & 0.5795 & 0.8660 & 0.2125 & 0.3705 & 0.5142\\\hline
0.9295 & 0.6789 & 0.8640 & 0.6584 & 0.1385 & 0.8579 & 0.1618

And the result:

enter image description here

UPDATE

Another crazy idea. You can enclose my last proposed solution into a landscape environment, and thus the table is rotated again 90 degrees counter-clockwise, and it looks portrait, with the effect of the first column being repeated in each page:

\documentclass{article}
\usepackage{longtable,lscape}
\usepackage{graphicx}
\usepackage{array}
\usepackage{fancybox}
\usepackage[a4paper, margin=3cm]{geometry}

\def\beginrotate{\begin{Sbox}}
\def\endrotate{\end{Sbox}\rotatebox{-90}{\TheSbox~~}}

\begin{document}\footnotesize
\newcolumntype{R}{>{\beginrotate}c<{\endrotate}}
\begin{landscape}
\begin{longtable}{|R|R|R|R|R|R|R|}
\hline
\textbf{Foo} &  \textbf{Bar} &  \textbf{Foobar} & \textbf{Barfoo} &
\textbf{FooFoo} & \textbf{Barbar} & \textbf{Foobaz} \endhead\hline
\input{bigtable}
\\\hline
\end{longtable}
\end{landscape}
\end{document}

Resulting in three pages. The result is like cutting the table at the top of this answer into three chunks, and having the first column repeated at each chunk.

Pag1 Pags2-3