我需要帮助将使用 tabularx 创建的表格中日期/条款标题中的文本水平居中。第一列中的文本不应该居中。我尝试了多种方法,但每次尝试都会丢失两个不同的列间距,所有列最终都具有相同的宽度,或者最终在表格右侧留下空白(尽管表格应该填满整个文本宽度)。另外,我似乎无法弄清楚为什么有些垂直线比其他垂直线更暗。如果您也能提供帮助,我将不胜感激。感谢您的帮助!
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tabularx}
\usepackage[table]{xcolor}
\usepackage{float}
\usepackage{ragged2e}
\usepackage{array}
\usepackage{booktabs}
\begin{document}
\newcolumntype{s}{>{\hsize=0.5\hsize}X}
\newcolumntype{b}{>{\hsize=5.5\hsize}X}
\begin{center}
\begin{scriptsize}
\begin{tabularx}{\textwidth}{| b | s | s | s | s | s | s | s | s | s | s | s |}
\cline{2-12}
\multicolumn{1}{b|}{} & \multicolumn{1}{s|}{2018} & \multicolumn{3}{s|}{2019} & \multicolumn{3}{s|}{2020} & \multicolumn{3}{s|}{2021} & \multicolumn{1}{s|}{2022} \\
\cline{2-12}
\multicolumn{1}{b|}{} & Fall & Spr & Sum & Fall & Spr & Sum & Fall & Spr & Sum & Fall & Spr \\
\hline
asdfasdfasdf & \cellcolor[rgb]{0.2,0.4,0.8} &\cellcolor[rgb]{0.2,0.4,0.8} & \cellcolor[rgb]{0.2,0.4,0.8} &\cellcolor[rgb]{0.2,0.4,0.8} &\cellcolor[rgb]{0.2,0.4,0.8} &\cellcolor[rgb]{0.2,0.4,0.8} & & & & & \\
\cline{1-1}
asdfasdfasdfasdfasdfasdf & \cellcolor[rgb]{0.2,0.4,0.8} & & & & & & & & & & \\
\cline{1-1}
asdfasdfasdfasdfasdfadsf & \cellcolor[rgb]{0.2,0.4,0.8} & \cellcolor[rgb]{0.2,0.4,0.8} & \cellcolor[rgb]{0.2,0.4,0.8} & & & & & & & & \\
\cline{1-1}
asdfasdfasdasdfasdfadsfasdff & & \cellcolor[rgb]{0.2,0.4,0.8} & \cellcolor[rgb]{0.2,0.4,0.8} & \cellcolor[rgb]{0.2,0.4,0.8} & \cellcolor[rgb]{0.2,0.4,0.8} & \cellcolor[rgb]{0.2,0.4,0.8} & & & & & \\
\cline{1-1}
asdfasasdfadsfasdfdfasdf & \cellcolor[rgb]{0.8,0.4,0.3} & \cellcolor[rgb]{0.8,0.4,0.3} & \cellcolor[rgb]{0.8,0.4,0.3} & \cellcolor[rgb]{0.8,0.4,0.3} & \cellcolor[rgb]{0.8,0.4,0.3} & \cellcolor[rgb]{0.8,0.4,0.3} & & & & & \\
\cline{1-1}
asdfasdfasdfasdfasdfasdfasdf & \cellcolor[rgb]{0.8,0.4,0.3} & & & & & & & & & & \\
\cline{1-1}
asdfasdfasdfasdfasdfafds & & \cellcolor[rgb]{0.8,0.4,0.3} & & & & & & & & & \\
\hline
\end{tabularx}
\end{scriptsize}
\end{center}
\end{document}
答案1
这是有效的代码。您应该记住,如果X
列的宽度不同(带有\hsize=some coefficient \hsize
),则系数的总和必须等于X
类型列的总数;
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{array}\usepackage{tabularx}
\usepackage[table]{xcolor}
\definecolor{myblue}{rgb}{0.2,0.4,0.8}
\definecolor{myred}{rgb}{0.8,0.4,0.3}
\newcolumntype{s}{>{\hsize=0.7\hsize\centering\arraybackslash}X}
\newcolumntype{b}{>{\hsize= 4.3\hsize\arraybackslash}X}
\begin{document}
\begin{center}
\setlength{\tabcolsep}{3pt}
\setlength{\extrarowheight}{2pt}
\begin{scriptsize}
\begin{tabularx}{\textwidth}{| b|*{11}{s|}}
\cline{2-12}
\multicolumn{1}{b|}{} & \multicolumn{1}{c|}{2018} & \multicolumn{3}{c|}{2019} & \multicolumn{3}{c|}{2020} & \multicolumn{3}{c|}{2021} & \multicolumn{1}{c|}{2022} \\
\cline{2-12}
\multicolumn{1}{b|}{} & Fall & Spr & Sum & Fall & Spr & Sum & Fall & Spr & Sum & Fall & Spr \\
\hline
asdfasdfasdf & \multicolumn{1}{c|}{\cellcolor{myblue}} &\cellcolor{myblue} & \cellcolor{myblue} &\cellcolor{myblue}&\cellcolor{myblue} &\cellcolor{myblue} & & & & & \\
\cline{1-1}
asdfasdfasdfasdfasdfasdf & \multicolumn{1}{c|}{\cellcolor{myblue}} & & & & & & & & & & \\
\cline{1-1}
asdfasdfasdfasdfasdfadsf & \multicolumn{1}{c|}{\cellcolor{myblue}} & \cellcolor{myblue} & \cellcolor{myblue} & & & & & & & & \\
\cline{1-1}
asdfasdfasdasdfasdfadsfasdff & & \cellcolor{myblue} & \cellcolor{myblue} & \cellcolor{myblue} & \cellcolor{myblue} & \cellcolor{myblue} & & & & & \\
\cline{1-1}
asdfasasdfadsfasdfdfasdf & \multicolumn{1}{c|}{\cellcolor{myred}} & \cellcolor{myred} & \cellcolor{myred} & \cellcolor{myred} & \cellcolor{myred} & \cellcolor{myred} & & & & & \\
\cline{1-1}
asdfasdfasdfasdfasdfasdfasdf & \multicolumn{1}{c|}{\cellcolor{myred}} & & & & & & & & & & \\
\cline{1-1}
asdfasdfasdfasdfasdfafds & & \cellcolor{myred} & & & & & & & & & \\
\hline
\end{tabularx}
\end{scriptsize}
\end{center}
\end{document}