确定表格的高度

确定表格的高度

我正在小页面中工作,因为我想要横向模式下两列彼此相邻。

在第一列中,我想放置一个图形,然后放置一个表格。我正在缩放表格以使其完全适合小页面。大致轮廓如下:

\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}

我现在想做的是按照正确的比例绘制剩余的图形以填充页面的剩余部分 - 宽度相当容易,但高度是多少?问题还在于我事先不知道表格的行数。

所以我需要某种方法来确定表格的高度,从文本高度中减去它,以获得图形的剩余空间。

\settoheight不幸的是只适用于文本。

答案1

您必须记住\settoheight测量的是高度,但表格环境也有深度;所以我们还需要考虑深度。这里有一个建议;(调整大小的)表格的总高度存储在寄存器中\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}

在此处输入图片描述

答案2

这是为了回答

\settoheight不幸的是只适用于文本。

代码:

\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}

结果是26.5pt。因此\settoheight有效!

答案3

类似下面的操作是否有帮助:

\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}

请注意,这将打印表格的高度,在本例中为14.5pt

答案4

另一种方法是使用calc\totalheightof(计算高度和深度的总和):

\usepackage{calc}
...
\DeclareRobustCommand*{\mytable}{
    \begin{tabular}{...}
        [YOUR TABLE]
    \end{tabular}
}
...
\begin{document}
...
\newlength{\mytableheigth}
\setlength{\mytableheigth}{\totalheightof{\mytable}}

相关内容