粗體、水平和垂直對齊的多行表格標題

粗體、水平和垂直對齊的多行表格標題

我正在嘗試列印一個表格,其中包含標題的第一行應為粗體,並且水平和垂直居中。另外,我使用的longtable環境來自這個包,因為我有一個非常長的表格,跨越幾頁。由於水平壓力,我還想將一些表標題分成兩行(但不是全部),我使用了shortstack

也就是說,我仍然無法垂直對齊標題內容,因為某些單元格有 2 行,而其他單元格只有 1 行。

\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

該套件的另一個解決方案,它被精確設計為使用和命令makecell選擇垂直和水平對齊方式以及單元格中的常見格式。我還加載了表格中不太緊的垂直間距:\thead\makecellcellspace

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

在此輸入影像描述

相關內容