我使用tabu
包轻松排版表格。现在我需要排版长表格。所以我使用了longtabu
。表格的头部由多列组成。此外,每个单元格中都有很多文本,有些单元格应该跨越。我认为,居中的文本(格式化为段落)看起来会更好。
最小示例
\documentclass[14pt,twoside]{extreport}
\usepackage{type1ec}
\usepackage[T2A]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{multirow}
\usepackage{array}
\usepackage{longtable}
\usepackage{tabu}
\usepackage{xparse}
\usepackage[unicode=true]{hyperref}
\NewDocumentCommand{\rot}{O{90} O{1em} m}{\makebox[#2][l]{\rotatebox{#1}{#3}}}%
\begin{document}
\newlength{\firsttoprow}
\setlength{\firsttoprow}{3cm}
\begin{longtabu} to \linewidth%
{|X[25,C]%
|X[15,C]|X[15,C]|X[15,C]%
|X[25,C]%
|X[25,C]|}
\caption{First table caption%
\label{tab:first}}\\\hline
\multirow{2}*{\parbox{\linewidth}{\centering Column A (first page head)}}%
& \rot{\parbox{\firsttoprow}{\centering Column B}}%
& \rot[90][5ex]{\parbox{\firsttoprow}{\centering Column C (two lines)}}%
& \rot[90][5ex]{\parbox{\firsttoprow}{\centering Column D (two lines)}}%
& \multirow{2}*{\rot[90][7.5ex]{\parbox{\firsttoprow}{\centering Column E (consists of three lines)}}}%
& \multirow{2}*{\rot[90][5ex]{\parbox{\firsttoprow}{\centering Column F (two lines)}}}\\\cline{2-4}
& B (two lines)%
& C (three lines)%
& D & &\\\hline
\endfirsthead
\caption*{Table \ref{tab:first} (Cont.)}\\\hline
Column A (head)%
& B & C & D%
& Col. E%
& Col. F\\\hline
\endhead
Cont A%
& Cnt.B & Cnt.C & Cnt.D%
& Cnt.E%
& Cnt.F\\\hline
\end{longtabu}
\end{document}
旋转是通过xparse
(见https://tex.stackexchange.com/a/32687/33087)。存在的问题:
- 在 B、C 和 D 列的顶行中,框和水平线之间的底部垂直空间较大。我希望内容位于单元格的中间。
- 跨头部单元格(A、E 和 F 列)的内容垂直对齐,
\multirow
因此框的中心位于头部行之间的线上。我希望框的中心位于跨单元格的中心。 - 本例产生了 6 个坏盒(
\vbox
由于而过满\multirow
)。
第二个示例提供了丑陋的解决方法,需要手动垂直修复,并且不需要\multirow
:
\documentclass[14pt,twoside]{extreport}
\usepackage{type1ec}
\usepackage[T2A]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{multirow}
\usepackage{array}
\usepackage{longtable}
\usepackage{tabu}
\usepackage{xparse}
\usepackage[unicode=true]{hyperref}
\newcolumntype{Y}[1]{>{\strut\hspace{0pt}}X[#1]<{\hspace{0pt}\strut}}
\tabucolumn Y
\NewDocumentCommand{\rot}{O{90} O{1em} m}{\makebox[#2][l]{\rotatebox{#1}{#3}}}%
\begin{document}
\newlength{\firsttoprow}
\newlength{\fixuptoprow}
\setlength{\firsttoprow}{3cm}
\setlength{\fixuptoprow}{2cm}
\begin{longtabu} to \linewidth%
{|Y{25,C}%
|Y{15,C}|Y{15,C}|Y{15,C}%
|Y{25,C}%
|Y{25,C}|}
\caption{First table caption%
\label{tab:first}}\\\hline
\begin{minipage}[t][0pt]{\linewidth}\vspace{-\fixuptoprow}\vspace{3.5ex}%
\parbox{\linewidth}{\centering Column A (first page head)}%
\end{minipage}%
& \rot{\parbox{\firsttoprow}{\centering Column B}}%
& \rot[90][5ex]{\parbox{\firsttoprow}{\centering Column C (two lines)}\hspace{1ex}}%
& \rot[90][5ex]{\parbox{\firsttoprow}{\centering Column D (two lines)}}%
& \begin{minipage}[t][0pt]{7.5ex}\vspace{-\fixuptoprow}%
\rot[90][7.5ex]{\parbox{\firsttoprow}{\centering Column E (consists of three lines)}}%
\end{minipage}%
& \begin{minipage}[t][0pt]{5ex}\vspace{-\fixuptoprow}%
\rot[90][5ex]{\parbox{\firsttoprow}{\centering Column F (two lines)}}%
\end{minipage}\\\cline{2-4}
& B (two lines)%
& C (three lines)%
& D & &\\\hline
\endfirsthead
\caption*{Table \ref{tab:first} (Cont.)}\\\hline
Column A (head)%
& B & C & D%
& Col. E%
& Col. F\\\hline
\endhead
Cont A%
& Cnt.B & Cnt.C & Cnt.D%
& Cnt.E%
& Cnt.F\\\hline
\end{longtabu}
\end{document}
我希望有人能提出一个漂亮的决策,它可以自动计算修复。另外,有关minipage
决策,请参阅https://tex.stackexchange.com/a/99684/33087。
答案1
我会使用更简单的标记,并取消垂直规则。
\documentclass[14pt,twoside]{extreport}
\usepackage{type1ec}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{array,graphicx}
\usepackage{longtable}
\newcommand\rot[1]{\rotatebox{90}{ \begin{tabular}{@{}c@{}}#1\end{tabular} }}
\begin{document}
\begin{longtable}{
>{\centering}m{2cm}
>{\centering}m{2cm}
>{\centering}m{2cm}
>{\centering}m{2cm}
>{\centering}m{2cm}
>{\centering\arraybackslash}m{2cm}
}
\caption{First table caption%
\label{tab:first}}\\\hline
Column A (first page head)
& \rot{Column B}\par\hrule\smallskip B (two lines)
& \rot{Column C\\ (two lines)}\par\hrule\smallskip C (three lines)
& \rot{Column D\\ (two lines)}\par\hrule\smallskip D\\\mbox{}
& \rot{Column E\\ (consists of\\ three lines)}
& \rot{Column F\\ (two lines)}\\
\hline
\endfirsthead
\caption*{Table \ref{tab:first} (Cont.)}\\\hline
Column A (head)%
& B & C & D%
& Col. E%
& Col. F\\\hline
\endhead
Cont A%
& Cnt.B & Cnt.C & Cnt.D%
& Cnt.E%
& Cnt.F\\\hline
\end{longtable}
\end{document}