
私は、ヘッダーを含む最初の行を太字にして、水平方向と垂直方向の両方で中央揃えにする表を印刷しようとしています。さらに、私はlongtable
環境を使用しています。このパッケージ数ページに渡る非常に長い表があるためです。水平方向のストレスのため、表のヘッダーの一部を2行に収めたいのですが(すべてではありません)、これを実現するためにshortstack
そうは言っても、一部のセルには 2 行あり、他のセルには 1 行しかないため、ヘッダー コンテンツを垂直方向に揃えることはできません。以下の MWE を参照してください。
\documentclass{report}
\usepackage{booktabs}
\usepackage{longtable}
\newcommand*{\thead}[1]{\multicolumn{1}{c}{\bfseries #1}}
\begin{document}
\begin{center}
\begin{longtable}{rcrrcc}
\toprule
\thead{ID} & \thead{Database name} & \thead{\shortstack{Size\\(MB)}} & \thead{\shortstack{No. of\\records}} & \thead{\shortstack{Time stamp\\1st record}} & \thead{\shortstack{Time stamp\\last record}} \\
\midrule
%\input{tab-metadata} Really long table
1 & dummie & 2.1 & 33 & dummie & dummie \\
2 & dummie & 4.3 & 67 & dummie & dummie \\
\bottomrule
\end{longtable}
\end{center}
\end{document}
このコードは次の表から生成されます。
この問題を解決し、すべてのヘッダー セルを垂直方向に中央揃えにする最もシンプルで正確かつエレガントな方法を見つけるために、皆様のご協力をお願いします。
答え1
見出しを垂直方向に中央揃えにするのが最も簡単ですtabular
が、長い表を中央揃えにしないため削除しましたcenter
。表がページに対して少し幅が広すぎるため、列間の間隔を少し狭めました。
\documentclass{report}
\usepackage{booktabs}
\usepackage{longtable}
\newcommand*{\thead}[1]{%
\multicolumn{1}{c}{\bfseries\begin{tabular}{@{}c@{}}#1\end{tabular}}}
\begin{document}
\setlength\tabcolsep{5pt}
\begin{longtable}{@{}rcrrcc@{}}
\toprule
\thead{ID} &
\thead{Database name} &
\thead{Size\\(MB)} &
\thead{No. of\\records} &
\thead{Time stamp\\1st record} &
\thead{Time stamp\\last record} \\
\midrule
%\input{tab-metadata} Really long table
1 & dummie & 2.1 & 33 & dummie & dummie \\
2 & dummie & 4.3 & 67 & dummie & dummie \\
\bottomrule
\end{longtable}
\end{document}
答え2
パッケージのもう 1 つのソリューションは、コマンドとコマンドmakecell
を使用して、セル内の垂直方向と水平方向の配置と共通の書式設定の両方を正確に選択できるように設計されています。また、テーブル内の垂直方向の間隔を狭くしないようにロードしました。\thead
\makecell
cellspace
\documentclass{report}
\usepackage{booktabs}
\usepackage{longtable}
\usepackage{makecell}
\renewcommand\theadfont{\bfseries}
\usepackage{cellspace}
\setlength\cellspacetoplimit{4pt}
\setlength\cellspacebottomlimit{4pt}
\begin{document}
\begin{center}
\begin{longtable}{Srcrrcc}
\toprule
\thead{ID} & \thead{Database name} & \thead{\shortstack{Size & & & \\(MB)}} & \thead{\shortstack{No. of\\records}} & \thead{\shortstack{Time stamp\\1st record}} & \thead{\shortstack{Time stamp\\last record}} \\
\midrule
%\input{tab-metadata} Really long table
1 & dummie & 2.1 & 33 & dummie & dummie \\
2 & dummie & 4.3 & 67 & dummie & dummie \\
\bottomrule
\end{longtable}
\end{center}
\end{document}