我的文档中有几个表格只是垂直方向看,有点太小,无法填满整个页面。
我想拉伸表格,通过在底部添加空行,或者通过拉伸行跳过,以便表格填充文本区域。
我该如何处理这个问题?
\documentclass{scrbook} % because that is what I use
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{tabularx}
\usepackage{showframe}
\usepackage{lipsum}
\begin{document}
\begin{table*}
\begin{tabularx}{\linewidth}{|X|X|}
\hline
\textbf{Column A} & \textbf{Column B}\\
\hline
\lipsum[1] & \lipsum[2] \\
\hline
\end{tabularx}
\end{table*}
\end{document}
我希望红色标记的空白消失,即表格边框与文本区域边框相匹配。
请注意,这是人为的。在实际文档中,这是为了sidewaystable
- 使非文本区域的高度对整体布局造成更大的破坏,并删除让周围文本占据空白的选项。
答案1
一个解决方案,将标题放在表格内(示例表格取自 David Carlisle 和 DevSolar 的答案):
\documentclass[captions=tableheading]{scrbook}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{tabularx}
\usepackage{rotating}
\usepackage[vlines]{tabularht}
\usepackage{showframe}
\usepackage{lipsum}
\newdimen\orgtextwidth
\setlength{\orgtextwidth}{\textwidth}
\begin{document}
\makeatletter
\begin{table*}
\begin{tabularhtx}{\textheight}{\linewidth}{|X|X|}
\noalign{%
\caption{This is an example}%
}
\hline
\interrowfill
\textbf{Column A} & \textbf{Column B}\\
\interrowfill
\hline
\interrowfill
\lipsum[2] & \lipsum[2]\\
\interrowfill
\hline
\end{tabularhtx}
\end{table*}
\begin{table*}
\centering
\begin{tabularht}{\textheight}{|l|}
\multicolumn{1}{@{}c@{}}{%
\hbox to 0pt{%
\hss
\centerline{%
\parbox{\linewidth}{%
\caption{This is an example}%
}%
}%
\hss
}%
}\\
\hline
\interrowfill
\textbf{Col.}\\
\interrowfill
\hline
\interrowfill
abc\\
\interrowfill
def\\
\interrowfill
ghi\\
\interrowfill
\hline
\end{tabularht}
\end{table*}
\clearpage
\begin{sidewaystable*}
\begin{tabularhtx}{\orgtextwidth}{\linewidth}{|X|X|}
\hline
\noalign{\captionabove{This is an example}}
\hline
\interrowfill
\textbf{Column A} & \textbf{Column B}\\
\interrowfill
\hline
\interrowfill
\lipsum[2] & \lipsum[2]\\
\interrowfill
\hline
\end{tabularhtx}
\end{sidewaystable*}
\end{document}
评论:
内部的水平位置
\noalign
是表格的左边框。由于这里不知道表格的宽度,因此只有当表格跨越整个宽度时才有可能正确居中。添加KOMA-Script 选项
captions=tableheadings
以获取表格标题的正确间距。使用Inside
sidewaystable*
\captionabove
,因为 KOMA-Script 无法看出这也是一个表格。
答案2
或者与我看到@Heiko 刚才发表的评论完全相反,这会影响单元格但不会影响表格行。
\documentclass{scrbook} % because that is what I use
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{tabularx}
\makeatletter
\def\ugg#1{{%
\sbox{0}{\tx}
\dimen0\dimexpr\ht0+\dp0
\sbox{0}{\advance\normalbaselineskip1pt \tx}
\count@\strip@pt\dimexpr\ht0+\dp0 - \dimen0 \relax\relax
\dimen@\dimexpr\textheight-\ht0-\dp0\relax
\divide\dimen@ \count@
\advance\normalbaselineskip\dimen@ \tx
}}
\makeatother
\usepackage{showframe}
\usepackage{lipsum}
\begin{document}
\def\tx{%
\begin{tabularx}{\linewidth}{|X|X|}
\hline
\textbf{Column A} & \textbf{Column B}\\
\hline
\lipsum[1] & \lipsum[2] \\
\hline
\end{tabularx}}
\begin{table*}
\ugg{\tx}
\end{table*}
\end{document}
答案3
Heiko 的评论tabularht
让我走上正确的道路,egreg 帮助我弄清楚如何考虑标题高度。这是完整的解决方案。(也许可以考虑将其纳入tabularht
?)
\documentclass{scrbook}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{tabularx}
\usepackage{rotating}
% These are the packages required by the solution
\usepackage[vlines]{tabularht}
\usepackage{calc}
% The sidewaystable environment screws up \textwidth,
% so we have to store its value beforehand.
\newlength{\SidewaysTextHeight}
\setlength{\SidewaysTextHeight}{\textwidth}
% Full-page height minus the vertical size of the caption
\newlength{\TabularhtHeight}
\makeatletter
% \captionht - typeset a caption and simultaneously set \TabularhtHeight
\newcommand{\captionht}[2][]{%
\stepcounter{table}%
\setbox\z@=\vbox{\@makecaption\fnum@table{#2}}%
\addtocounter{table}{-1}%
\setlength{\TabularhtHeight}{\textheight-\ht\z@-\lineskip}%
\if\relax\detokenize{#1}\relax
\caption{#2}%
\else
\caption[#1]{#2}%
\fi
}
% \captionwd - as \captionht, but for sidewaystable
\newcommand{\captionwd}[2][]{%
\stepcounter{table}%
\setbox\z@=\vbox{\@makecaption\fnum@table{#2}}%
\addtocounter{table}{-1}%
\setlength{\TabularhtHeight}{\SidewaysTextHeight-\ht\z@-\lineskip}%
\if\relax\detokenize{#1}\relax
\caption{#2}%
\else
\caption[#1]{#2}%
\fi
}
\makeatother
\usepackage{showframe}
\usepackage{lipsum}
\begin{document}
\begin{table*}
\captionht{Caption} % \captionht for table
\begin{tabularhtx}{\TabularhtHeight}{\linewidth}{|X|X|}
\hline
\interrowfill
\textbf{Column A} & \textbf{Column B}\\
\interrowfill
\hline
\interrowfill
\lipsum[2] & \lipsum[2]\\
\interrowfill
\hline
\end{tabularhtx}
\end{table*}
\clearpage
\begin{sidewaystable*}
\captionwd{Caption} % \captionwd for sidewaystable
\begin{tabularhtx}{\TabularhtHeight}{\linewidth}{|X|X|}
\hline
\interrowfill
\textbf{Column A} & \textbf{Column B}\\
\interrowfill
\hline
\interrowfill
\lipsum[2] & \lipsum[2]\\
\interrowfill
\hline
\end{tabularhtx}
\end{sidewaystable*}
\end{document}
sidewaystable
你可能会注意到不是完全适合文本区域。我欢迎任何关于为什么会这样提示。
编辑:rotating
已就布局问题联系维护人员。如果我收到反馈,将在此更新。
答案4
我编写了此代码,试图实现 tabularx 垂直填充页面。评论?在什么情况下可能会失败?如果要填充的空间少于一行,则不会绘制最后的 \hline!
我确实放了一个 \framebox 来明确地看到填充的空间。
\documentclass{article}
\usepackage{tabularx}
\usepackage{zref-savepos}
\usepackage{calc}
\usepackage{geometry}
\newlength{\Start}
\newlength{\End}
\newlength{\Diff}
\newbox{\ParBox}
\geometry{paperheight=300pt,vmargin={0pt,0cm},hmargin{0cm,0cm},footskip=0pt,headsep=0pt,headheight=0pt}
\begin{document}
\setlength{\parindent}{0cm}
\setlength{\Start}{\zposy{start}sp}%
\setlength{\End}{\zposy{end}sp}%
\setlength{\Diff}{\Start-\End}
\savebox{\ParBox}{\parbox[t][\Diff]{2cm}{\mbox{}}}%
\begin{tabularx}{10cm}{|lXl|}\hline
. & start \the\Start{} - end \the\End & . \\
. & filled space \the\dp\ParBox & . \\
.&.&.\\
.&.&.\\
.&.&.\\
.&.&.\\
.&.&.\\
.&.&.\\
.&.&.\\
.&.&.\\
.&.&.\\
.&.&.\\
.&.&.\\
.&.&.\\
& \framebox{\usebox{\ParBox}} &\\
\hline
\end{tabularx}%
\vskip-\Start
\vskip-\End
\zsaveposy{start}%
\vfill\zsaveposy{end}%
\end{document}