![粗體、水平和垂直對齊的多行表格標題](https://rvso.com/image/305749/%E7%B2%97%E9%AB%94%E3%80%81%E6%B0%B4%E5%B9%B3%E5%92%8C%E5%9E%82%E7%9B%B4%E5%B0%8D%E9%BD%8A%E7%9A%84%E5%A4%9A%E8%A1%8C%E8%A1%A8%E6%A0%BC%E6%A8%99%E9%A1%8C.png)
我正在嘗試列印一個表格,其中包含標題的第一行應為粗體,並且水平和垂直居中。另外,我使用的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
\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}