Determine height of tabular

This is to answer

\settoheight unfortunately only works on text.

Code:

\documentclass{article}

\newlength{\mylength}
\settoheight{\mylength}{%
\begin{tabular}{|c|c|}
test & test\\
test & test\\
test & test\\
test & test
\end{tabular}
}

\begin{document}
\the\mylength
\end{document}

The result is 26.5pt. Hence \settoheight works!


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


Does something like the following help:

\documentclass{article}

\newcommand{\testtbl}{%
\begin{tabular}{|c|c|}
test & test\\
test & test
\end{tabular}
}
\newcommand{\ntabht}{%
  \setbox0=\hbox{\testtbl} \the\ht0%
}

\begin{document}
\ntabht
\end{document}

Notice that this will print the height of the table which in this case is 14.5pt.

Tags:

Height

Tables