# How to get vertical size of table captions?

I found that the tabularht package, which allows to give a fixed vertical size for a tabular, does not take into account the vertical space taken up by a table's caption.

It stands to reason, the caption not actually being part of the tabular but the surrounding table.

However, this effectively keeps me from specifying \textheight as the size of the tabularht.

Now I would like to, as a workaround, substract the height of the caption from \textheight. However, I couldn't figure out how to get that particular measure (font height plus spacing above plus spacing below)?

\documentclass{scrbook} % because that is what I use

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{tabularht}
\usepackage{tabularx}

\newlength{\CaptionHeight}
\settoheight{...}          % this is where I am stuck
\newlength(\TabularhtHeight}
\setlength{\TabularhtHeight}{\textheight - \CaptionHeight}

\usepackage{showframe}
\usepackage{lipsum}

\begin{document}

\begin{table*}
\caption{Test}
\begin{tabularhtx}{\TabularhtHeight}{\linewidth}{|X|X|}
\hline
\textbf{Column A} & \textbf{Column B}\\
\hline
\lipsum[2] & \lipsum[2] \\
\interrowfill
\lipsum[2] & \lipsum[2] \\
\hline
\end{tabularhtx}
\end{table*}

\end{document}
-
For the sake of both the community and yourself, please provide a working minimal example that members can start with. –  Werner Nov 17 '12 at 14:54
sigh... you're a bit OCD about this MWE stuff, you know? The point being, I don't know how to get the value, i.e. the example does not and can not "work". –  DevSolar Nov 17 '12 at 15:01
"While solving problems is fun, setting them up is not." –  lockstep Nov 17 '12 at 15:02
@lockstep: Yes, but this is about getting a friggin' length. Either you know the magic statement that gives the height of a caption, or you don't. I simply don't see where having a tabular exceeding or not exceeding a page height has anything to do with "how to get vertical size of table captions", it's just a bit of fluff to explain why I would want that particular value. Anyway, I added the example. –  DevSolar Nov 17 '12 at 15:06
Well, if there isn't a magic statement, then someone might have taken the time to code something for you in a standard document class with a standard tabular environment. In addition to having to input a dummy table, the solution then might not have worked with scrbook or with tabularx and then they would have done all the work for nothing. That's why it's important to give the MWE. –  Scott H. Nov 17 '12 at 18:07

You have to set the length locally, because the height of the caption depends on the caption text. Here's a way: we typeset the caption and measure it. The counter must be stepped up and down because the label width can influence the typesetting on one or more lines.

\documentclass{scrbook}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\usepackage{tabularht}
\usepackage{tabularx,calc}

\newlength{\TabularhtHeight}

\makeatletter
\newcommand{\captionht}[2][]{%
\stepcounter{table}%
\setbox\z@=\vbox{\@makecaption\fnum@table{#2}}%
\setlength{\TabularhtHeight}{\textheight-\ht\z@-\lineskip}%
\if\relax\detokenize{#1}\relax
\caption{#2}%
\else
\caption[#1]{#2}%
\fi
}
\makeatother

\usepackage{showframe}
\usepackage{lipsum}

\begin{document}

\pagestyle{empty}
\begin{table*}
\captionht{Test}
\begin{tabularhtx}{\TabularhtHeight}{\linewidth}{|X|X|}
\hline
\textbf{Column A} & \textbf{Column B}\\
\hline
\lipsum[2] & \lipsum[2] \\
\interrowfill
\lipsum[2] & \lipsum[2] \\
\hline
\end{tabularhtx}
\end{table*}

\end{document}
-
Works like a charm... as long as the table is in "normal" orientation. +1 and "correct answer" for that. However, it breaks if you try this in a sidewaystable - even if \textheight is replaced with \textwidth in the macro. The table ends up being much too high for the page width. Any idea why? –  DevSolar Nov 19 '12 at 8:58
@DevSolar I should add that there are some limitations in that code: it's specific for table and the caption must go before the table. Can you add an example with sidewaystable? –  egreg Nov 19 '12 at 10:11
Found the problem with sidewaystable: Apparently, the sidewaystable environment changes \textwidth to some larger value (\textheight perhaps?). The reverse is not true - \textheight is not set to \textwidth. So, for the above to work with sidewaystable, you have to 1) take the value of \textwidth beforehand, and 2) write a second version of \captionht (I chose \captionwd) that uses that pre-taken value instead of \textheight for its calculations. Another funny thing is that forgetting to include the calc package makes this fail, silently. –  DevSolar Nov 20 '12 at 8:02