我搜索了很多,发现了很多其他类似的问题,但没有答案有帮助。我有以下表格代码:
\renewcommand{\arraystretch}{3}
\begin{table}[h!]
\resizebox{\columnwidth}{!}{
\centering
\begin{tabular}{||c c c c c c||}
\hline
\multicolumn{1}{||>{\centering\columncolor{lightgray}}c}
{Number} & \multicolumn{1}{>{\centering\columncolor{lightgray}[\dimexpr\tabcolsep + 1pt\relax]}c} {List A} & \multicolumn{1}{>{\centering\columncolor{lightgray}[\dimexpr\tabcolsep + 1pt\relax]}c} {List B} & \multicolumn{1}{>{\centering\columncolor{lightgray}[\dimexpr\tabcolsep + 1pt\relax]}c} {List C} & \multicolumn{1}{>{\centering\columncolor{lightgray}[\dimexpr\tabcolsep + 1pt\relax]}c} {List D} & \multicolumn{1}{>{\centering\columncolor{lightgray}}c||} {\makecell{List E\\ List F}}\\ [0.7ex]
\hline\hline
1 & AAAA & BBBB & CCCC & DDDD & EF\\\hline
2 & aaaa & bbbb & cccc & dddd & ef\\\hline
3 & \makecell{1111\\2222} & \makecell{3333\\4444} & 5555 & 6666 & 7788\\\hline
\end{tabular}
}
\end{table}
可以看出,每个单元格内容都不是垂直居中的。如何让每个单元格内容垂直和水平居中?
答案1
欢迎来到 TeX.SE!
- 请始终提供 MWE(最小工作示例),一份完整的小文档,以重现您的问题
cellspace
在和包的帮助下tabularx
,您清理代码后可以获得:
\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{cellspace, makecell, tabularx}
\newcolumntype{C}{>{\centering\arraybackslash}X}
\setlength\cellspacetoplimit{7pt}
\setlength\cellspacebottomlimit{7pt}
\addparagraphcolumntypes{C}
\begin{document}
\begin{table}[ht]
\begin{tabularx}{\linewidth}{|| *6{SC} ||}
\hline
\rowcolor{lightgray}
Number
& List A & List B & List C & List D & \makecell{List E\\ List F} \\
\hline\hline
1 & AAAA & BBBB & CCCC & DDDD & EF \\ \hline
2 & aaaa & bbbb & cccc & dddd & ef \\ \hline
3 & \makecell{1111\\2222}
& \makecell{3333\\4444}
& 5555 & 6666 & 7788 \\
\hline
\end{tabularx}
\end{table}
\end{document}