在一个大型项目中工作,其中定义了一些用于表格的命令。我将它们列在下面。使用它们,我尝试创建一个包含多行且仍能正确对齐其他单元格的表格:
可以看出,我设法获得了多行,但没有获得正确的单元格行和/或 X 的居中。我做错了什么?
非常感谢!
以下是 MWE:
\documentclass{report}
\usepackage[utf8]{inputenc}
\usepackage{diagbox}
\usepackage{multirow}
% for loop - table 1
\usepackage{forloop}
\newcounter{loopcntr}
\newcommand{\rpt}[2][1]{%
\forloop{loopcntr}{0}{\value{loopcntr}<#1}{#2}%
}
\newcommand{\on}[1][1]{
\forloop{loopcntr}{0}{\value{loopcntr}<#1}{&\scriptsize X}
}
\newcommand{\off}[1][1]{
\forloop{loopcntr}{0}{\value{loopcntr}<#1}{&}
}
% extra table 1
\usepackage{array}
\usepackage[export]{adjustbox}
\newcolumntype{P}[1]{>{\centering\arraybackslash}p{#1}}
\newcommand{\centered}[1]{\begin{tabular}{l} #1 \end{tabular}}
% thick horizontal line (for table)
\makeatletter
\def\thickhline{%
\noalign{\ifnum0=`}\fi\hrule \@height \thickarrayrulewidth \futurelet
\reserved@a\@xthickhline}
\def\@xthickhline{\ifx\reserved@a\thickhline
\vskip\doublerulesep
\vskip-\thickarrayrulewidth
\fi
\ifnum0=`{\fi}}
\makeatother
\newlength{\thickarrayrulewidth}
\setlength{\thickarrayrulewidth}{2\arrayrulewidth}
\begin{document}
\renewcommand{\arraystretch}{0.8}
\begin{table}[h]
\noindent\begin{tabular}{|p{0.4\textwidth}
!{\vrule width 0.4mm}P{0.01\textwidth}*{1}{|P{0.01\textwidth}}
!{\vrule width 0.4mm}P{0.01\textwidth}*{3}{|P{0.01\textwidth}}
!{\vrule width 0.4mm}P{0.01\textwidth}*{3}{|P{0.01\textwidth}}
!{\vrule width 0.4mm}P{0.01\textwidth}*{1}{|P{0.01\textwidth}}
!{\vrule width 0.4mm}P{0.01\textwidth}*{1}{|P{0.01\textwidth}}
||}
\thickhline
\textbf{\vspace{-20pt}\diagbox[width=\dimexpr \textwidth/4+2\tabcolsep\relax, height=0.75cm]{ Package }{\centering Timeline }}
& \multicolumn{2}{c!{\vrule width 0.4mm}}{\scriptsize 2024}
& \multicolumn{4}{c!{\vrule width 0.4mm}}{\scriptsize 2025}
& \multicolumn{4}{c!{\vrule width 0.4mm}}{\scriptsize 2026}
& \multicolumn{2}{c!{\vrule width 0.4mm}}{\scriptsize 2027}
& \multicolumn{1}{c!{\vrule width 0.1mm}}{\scriptsize Involved people} \\
\cline{2-13}
\hspace{5.6cm} \scriptsize Quarters \rpt[1]{& \scriptsize 3 & \scriptsize 4} \rpt[2]{& \scriptsize 1 & \scriptsize 2 & \scriptsize 3 & \scriptsize 4} \rpt[1]{& \scriptsize 1 & \scriptsize 2} \rpt[1]{&} \\
\thickhline
\scriptsize Package1: \\ \scriptsize blabla \on[4] \off[9] \scriptsize{Some Guy} \\[-10pt]
\hline
\scriptsize Package2: \\ \scriptsize more blabla \on[4] \off[9] \scriptsize{Some Other Guy} \\[-10pt]
\hline
\scriptsize Package3: \\ \scriptsize more blabla \off[2] \on[6] \off[5] \\[-10pt]
\hline
\scriptsize Package4: \\ \scriptsize more blabla \off[6] \on[6] \off[1] \scriptsize{Some Guy} \\[-10pt]
\hline
\scriptsize Package5: \\ \scriptsize more blabla \on[4] \off[4] \on[4] \off[1] \\
\thickhline
\end{tabular}
\caption{bla bla bla bla bla bla bla.}
\label{milestones}
\end{table}
\end{document}
答案1
通过使用tabularray
包并x
在需要的地方插入,表的代码可以大大简化:
\documentclass{report}%
%---------------- show page layout. don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%
\usepackage{tabularray}
\UseTblrLibrary{booktabs}
\begin{document}
\begin{table}[ht]
\begin{tblr}{hline{4-Y},
vline{1,4,6-8,10-12,14,Z},
vline{3,5,9,13,15}=1pt,
colsep = 5pt,
colspec = {X[l,m] r *{2}{Q[c,m]}
*{4}{Q[c,m]}
*{4}{Q[c,m]}
*{2}{Q[c,m]}
X[l,m]},
cell{4-Z}{1} = {c=2}{},
cell{4-Z}{2-Y} = {font=\sffamily},
hspan = minimal,
row{1} = {font=\bfseries}}
\toprule
& & \SetCell[c=12]{c} Timeline
& & & & & & & & & & & & \\
\cmidrule{3-14}
& year:
& \SetCell[c=2]{c} 2024
& & \SetCell[c=4]{c} 2025
& & & & \SetCell[c=4]{c} 2026
& & & & \SetCell[c=2]{c} 2027
& &\SetCell[r=2]{f}
Involved people
\\
\cmidrule{3-14}
packages
& quoter:
& 3 & 4 & 1 & 2 & 3 & 4 & 1 & 2 & 3 & 4 & 1 & 2 & \\
{Package 1:\\ \footnotesize blabla}
& & x & x & x & x & & & & & & & & & Some Guy \\
{Package 2:\\ \footnotesize more blabla}
& & x & x & x & x & & & & & & & & & Some Guy \\
{Package 3:\\ \footnotesize more blabla}
& & & x & x & x & x & x & & & & & & & Some Guy \\
{Package 4:\\ \footnotesize a brief summary of the tasks of this package}
& & & & x & x & x & x & x & & & & & & Some Guy \\
{Package 5:\\ \footnotesize more blabla}
& & x & x & x & x & & & & & x & x & x & x & Some Guy \\
\bottomrule
\end{tblr}
\caption{bla bla bla bla bla bla bla.}
\label{milestones}
\end{table}
\end{document}
(红线表示页面布局)
编辑:将指令更改cell{3-Z}{2-Y}
为cell{4-Z}{2-Y}
。