我正在尝试为表格中的某些单元格着色,单元格内容垂直居中和水平居中。我似乎无法让数字(似乎与标题对齐)在单元格颜色创建的矩形内水平居中。数字似乎也没有在彩色矩形中垂直居中。我已将复杂的表格简化为这个 MWE:
\PassOptionsToPackage{table}{xcolor}
\documentclass{beamer}
\usepackage{ragged2e}
\usepackage{tabularx}
\newcolumntype{C}[1]{>{\Centering}m{#1}}
\usepackage{booktabs}
\begin{document}
\begin{frame}
\begin{tabularx}{\textwidth}{@{} l C{0.1cm} C{0.1cm}}
\toprule[1.5pt]
header & \multicolumn{2}{l}{header} \\
\midrule
label & \cellcolor{green!40!black} \textcolor{lightgray!50!white}{\textbf{6}} & \cellcolor{black} \textcolor{lightgray!50!white}{\textbf{16}} \\
label & \cellcolor{black} \textcolor{lightgray!50!white}{\textbf{7}} & \cellcolor{black} \textcolor{lightgray!50!white}{\textbf{18}} \\
\bottomrule[1.25pt]
\end{tabularx}
\end{frame}
\end{document}
以下是 MWE 得出的表格的相关部分:
你可以看到两位数甚至被截断了。有人能纠正对齐吗?还是我的期望\LaTeX{}
太高了?
答案1
您的列太窄(使用您的代码时您会收到Overfull \hbox
警告);只要增加宽度,您就会得到所需的结果,使用\extrarowheight
您可以改进垂直对齐方式:
\documentclass[xcolor=table]{beamer}
\usepackage{ragged2e}
\usepackage{tabularx}
\newcolumntype{C}[1]{>{\Centering}m{#1}}
\usepackage{booktabs}
\begin{document}
\begin{frame}
{
\setlength\extrarowheight{2pt}
\begin{tabular}{@{} l C{0.3cm} C{0.5cm}}
\toprule[1.5pt]
header & \multicolumn{2}{l}{header} \\
\midrule
label & \cellcolor{green!40!black}\textcolor{lightgray!50!white}{\textbf{6}} & \cellcolor{black} \textcolor{lightgray!50!white}{\textbf{16}} \\
label & \cellcolor{black} \textcolor{lightgray!50!white}{\textbf{7}} & \cellcolor{black} \textcolor{lightgray!50!white}{\textbf{18}} \\
\bottomrule[1.25pt]
\end{tabular}
}
{
\setlength\extrarowheight{2pt}
\begin{tabularx}{\textwidth}{@{} l C{0.3cm} C{0.5cm}}
\toprule[1.5pt]
header & \multicolumn{2}{l}{header} \\
\midrule
label & \cellcolor{green!40!black}\textcolor{lightgray!50!white}{\textbf{6}} & \cellcolor{black} \textcolor{lightgray!50!white}{\textbf{16}} \\
label & \cellcolor{black} \textcolor{lightgray!50!white}{\textbf{7}} & \cellcolor{black} \textcolor{lightgray!50!white}{\textbf{18}} \\
\bottomrule[1.25pt]
\end{tabularx}
}
\end{frame}
\end{document}
为什么使用tabularx
环境而不真正使用 X 列?