[Tex/LaTex] Determine height of tabular

heighttables

I am working in a minipage, because I want two columns next to each other in landscape mode.

In the first column I would like to place a figure, and then a tabular. I am scaling the tabular to exactly fit the minipage. This is roughly the outline:

\newpage
\begin{minipage}{0.50\textwidth}
\begin{center}
\resizebox{\columnwidth}{!}{%
% latex table generated in R 2.14.1 by xtable 1.6-0 package
% Thu Nov  8 13:31:32 2012
\begin{tabular}
...
\end{tabular}%
}
\end{center}
\end{minipage}
\begin{minipage}{0.50\textwidth}
\end{minipage}

What I now would like to do is plot the remaining figure in exactly the correct proportions to fill the remainder of the page – the width is fairly easy, but what is the height? The problem is also that I don't know the number of rows of the table in advance.

So I need some way to determine the height of the tabular, subtract it from the textheight, to obtain my remaining space for the figure.

\settoheight unfortunately only works on text.

Best Answer

You have to remember that \settoheight measures the height, but tabular environments also have a depth; so we need to take also the depth into account. Here's a proposal; the total height of the (resized) table is stored in the register \tableheight:

\documentclass{article}
\usepackage[demo]{graphicx}
\newsavebox{\tablebox}
\newlength{\tableheight}
\newenvironment{resizedtabular}[1]
 {\begin{lrbox}{\tablebox}\begin{tabular}{#1}}
 {\end{tabular}\end{lrbox}%
  \sbox{\tablebox}{\resizebox{\textwidth}{!}{\usebox{\tablebox}}}%
  \global\tableheight=\ht\tablebox
  \global\advance\tableheight\dp\tablebox
  \usebox{\tablebox}}


\begin{document}

\noindent
\begin{minipage}{0.50\textwidth}
\centering
\begin{resizedtabular}{lll}
abcabcabc & defdefdef & ghighighi \\
abcabcabc & defdefdef & ghighighi \\
abcabcabc & defdefdef & ghighighi \\
abcabcabc & defdefdef & ghighighi \\
abcabcabc & defdefdef & ghighighi
\end{resizedtabular}
\end{minipage}%
\begin{minipage}{0.50\textwidth}
\includegraphics[width=\textwidth,height=\tableheight]{a}
\end{minipage}

\bigskip

\noindent
\begin{minipage}{0.50\textwidth}
\centering
\begin{resizedtabular}{llllll}
abcabcabc & defdefdef & ghighighi & abcabcabc & defdefdef & ghighighi \\
abcabcabc & defdefdef & ghighighi & abcabcabc & defdefdef & ghighighi \\
abcabcabc & defdefdef & ghighighi & abcabcabc & defdefdef & ghighighi \\
abcabcabc & defdefdef & ghighighi & abcabcabc & defdefdef & ghighighi \\
abcabcabc & defdefdef & ghighighi & abcabcabc & defdefdef & ghighighi \\
abcabcabc & defdefdef & ghighighi & abcabcabc & defdefdef & ghighighi \\
abcabcabc & defdefdef & ghighighi & abcabcabc & defdefdef & ghighighi \\
abcabcabc & defdefdef & ghighighi & abcabcabc & defdefdef & ghighighi \\
abcabcabc & defdefdef & ghighighi & abcabcabc & defdefdef & ghighighi \\
abcabcabc & defdefdef & ghighighi & abcabcabc & defdefdef & ghighighi \\
abcabcabc & defdefdef & ghighighi & abcabcabc & defdefdef & ghighighi
\end{resizedtabular}
\end{minipage}%
\begin{minipage}{0.50\textwidth}
\includegraphics[width=\textwidth,height=\tableheight]{a}
\end{minipage}

\end{document}

enter image description here

Related Question