# Section header and rotated table on same page

I have a table which has to be rotated given its size. Moreover, in my document it is the first table in the Appendix and hence has to follow the section header. Sidewaystable does not allow to have both the section header as well as the rotated table on the same page. Hence, I have tried the packages hvfloat as well as rotfloat / varwidth, however both don't lead to a perfect solution:

\documentclass[12pt,a4paper]{article}

\usepackage{hvfloat}
\usepackage{rotfloat}
\usepackage{varwidth}

\begin{document}

\section{Test Section 1}
\begin{center}
\hvFloat[nonFloat=true, capPos=t, rotAngle=90, objectPos=c]%
{table}%
{\begin{tabular}{lll}
column 1a & column 2a & column 3a \\
column 1b & column 2b & column 3b \\
column 1c & column 2c & column 3c \\
\end{tabular}}
{A rotated table}
{tab:test1}
\end{center}

\newpage
\section{Test Section 2}
\begin{table}[H]
\begin{center}
\rotatebox{90}{%
\begin{varwidth}{\textheight}
\caption{Another rotated table}\label{tab:test2}
\begin{tabular}{lll}
column 1a & column 2a & column 3a \\
column 1b & column 2b & column 3b \\
column 1c & column 2c & column 3c \\
\end{tabular}
\captionsetup{font=footnotesize}
\caption*{This table is just a test.}
\end{varwidth}}
\end{center}
\end{table}

\end{document}


In both cases I achieve that the tables are on the same page like the section header but:

In the case of hvfloat, (1) despite using the option capPos=t the caption remains always on the left hand side of the table. I've read the documentation but I have no idea what I'm doing wrong here. (2) I can't find an option to add table notes (in the table environment I'd use \caption*{Table Notes} but that doesn't work here). The table as such is positioned vertically and horiontally like I would like to have it.

In the case of the rotatebox, I manage to get the caption on top of the table and table notes below the table but whatever I do the table remains at the bottom of the page (while it should be centered horizontally and following the section header vertically).

Any hints would be highly appreciated. Thank you.

• How exactly should your table notes look like? How wide are they supposed to be with respect to the tabular? Do you need multiple lines for them? – leandriis Sep 19 '20 at 13:07
• Ideally, the table notes have the same width like the tabular. Yes, I need multiple lines for them. – tk79 Sep 19 '20 at 13:13
• Do you want to stick to a tabular environment or would you be willing to fix the width of the tabular using either tabularx or tabular*? – leandriis Sep 19 '20 at 13:15
• I'm open to using other environments than tabular – tk79 Sep 19 '20 at 13:16
• I have edited my answer to include a version using tabular* Which environment to use depends on the type of contents in your actual table. – leandriis Sep 19 '20 at 13:27

Using capPos=top instead of capPos=t results in the following output:

\documentclass[12pt,a4paper]{article}

\usepackage{hvfloat}
\usepackage{rotfloat}
\usepackage{varwidth}

\begin{document}

\section{Test Section 1}
\begin{center}
\hvFloat[nonFloat=true, capPos=top, rotAngle=90, objectPos=c]%
{table}%
{\begin{tabular}{lll}
column 1a & column 2a & column 3a \\
column 1b & column 2b & column 3b \\
column 1c & column 2c & column 3c \\
\end{tabular}}
{A rotated table}
{tab:test1}
\end{center}
\end{document}


With tabular* and a \multicolumn for the table ntes:

\documentclass[12pt,a4paper]{article}

\usepackage{hvfloat}
\usepackage{booktabs}

\begin{document}

\section{Test Section 1}
\begin{center}
\hvFloat[nonFloat=true, capPos=top, rotAngle=90, objectPos=c]%
{table}%
{\begin{tabular*}{7cm}{@{\extracolsep{\fill}}lll@{}}
\toprule
column 1a & column 2a & column 3a \\
column 1b & column 2b & column 3b \\
column 1c & column 2c & column 3c \\
\bottomrule
\multicolumn{3}{@{}p{7cm}@{}}{these are some table notes these are some table notes these are some table notes these are some table notes these are some table notes}\\
\end{tabular*}}
{A rotated table}
{tab:test1}

\end{center}
\end{document}


The main problem with adding a \section heading is that it reduces the remaining space (width before rotating). Also, it is easier to make a minipage act like a table (see \setcaptype) than to make a table act like a minipage.

Note that \centering affects the vertical position (after rotating) while the third optional argument of the minipage affects the horizontal positioning (after rotating).

\documentclass[12pt,a4paper]{article}

\usepackage{threeparttable}
\usepackage{showframe}% MWE only

\newsavebox{\tempbox}

\newcommand{\setcaptype}[1]% #1 = figure or table
{\expandafter\def\csname @captype\endcsname{#1}\ignorespaces}

\begin{document}

\setbox\tempbox=\vbox{\section{Test Section 2}}% measure size of section title
\usebox\tempbox

\rotatebox{90}{\begin{minipage}[c][\textwidth][c]{\dimexpr \textheight-\ht\tempbox-\dp\tempbox-\baselineskip-\parskip}
\setcaptype{table}\centering
\begin{threeparttable}
\caption{Another rotated table}\label{tab:test2}
\begin{tabular}{lll}
column 1a & column 2a & column 3a \\
column 1b & column 2b & column 3b \\
column 1c & column 2c & column 3c \\
\end{tabular}
\begin{tablenotes}
\item The first note.
\end{tablenotes}
\end{threeparttable}
\end{minipage}}

\end{document}