答案1
\arraystretch
您也许可以通过嵌套表格和使用、 \hphantom
和来实现您的需要\rlap
。
\RequirePackage{array}
\documentclass[12pt]{article}
\usepackage[table]{xcolor}
\newlength\scratchlength
\definecolor{LightGrayCellColor}{RGB}{251 251 251}
\definecolor{DarkGrayCellColor}{RGB}{245 245 245}
\definecolor{RuleColor}{RGB}{232 232 232}
\definecolor{TextColor}{RGB}{64 64 64}
\begin{document}
\begingroup
\arrayrulecolor{RuleColor}%
\color{TextColor}%
\renewcommand{\arraystretch}{2}%
%\arrayrulewidth=2\arrayrulewidth % <- increase thickness of lines in
% case by your pdf-viewer they are not displayed correctly with
% small zoom-factors.
\noindent
\begin{tabular}{@{}llllll@{}}%
\hline
\multicolumn{2}{|@{}l@{}}{%
\begin{tabular}[c]{l|l}%
\multicolumn{2}{l}{\cellcolor{LightGrayCellColor}\textsf{Vegetables}}\\%
\hline
\cellcolor{LightGrayCellColor}\hbox{\rlap{\textsf{Annual}}\hphantom{Beetroot}}&%
\cellcolor{LightGrayCellColor}\hbox{\rlap{\textsf{Perennial}}\hphantom{Capsicum}}%
\end{tabular}%
}%
&%
\multicolumn{2}{|@{}l@{}}{%
\begin{tabular}[c]{l|l}%
\multicolumn{2}{l}{\cellcolor{LightGrayCellColor}\textsf{Herbs}}\\%
\hline
\cellcolor{LightGrayCellColor}\rlap{\textsf{Annual}}\hphantom{Fenugreek}&%
\cellcolor{LightGrayCellColor}\rlap{\textsf{Perennial}}\hphantom{Holy Basil}%
\end{tabular}%
}%
&%
\multicolumn{1}{|l}{%
\cellcolor{LightGrayCellColor}%
\begin{tabular}[c]{@{}l@{}}%
\textsf{Fruits}%
\end{tabular}%
}%
&%
\multicolumn{1}{|l|}{%
\cellcolor{LightGrayCellColor}%
\begin{tabular}[c]{@{}l@{}}%
\textsf{Flowers}%
\end{tabular}%
}%
\\%
\hline
\multicolumn{1}{|l}{%
\cellcolor{DarkGrayCellColor}%
\begin{tabular}[t]{@{}l@{}}%
Beans\\%
\cellcolor{LightGrayCellColor}Beetroot\settowidth\scratchlength{Melon}\global\scratchlength\scratchlength\\%
\multicolumn{1}{@{}p{\scratchlength}}{Bitter\hfill\null\linebreak Melon\strut}\\%
\cellcolor{LightGrayCellColor}Carrot\settowidth\scratchlength{Gourd}\global\scratchlength\scratchlength\\%
\multicolumn{1}{@{}p{\scratchlength}}{Bottle\hfill\null\linebreak Gourd\strut}\\%
\end{tabular}%
}%
&%
\multicolumn{1}{|l}{%
\cellcolor{LightGrayCellColor}%
\begin{tabular}[t]{@{}l@{}}%
\cellcolor{DarkGrayCellColor}Capsicum\\%
Chilli\\%
\cellcolor{DarkGrayCellColor}Pepper%
\end{tabular}%
}%
&%
\multicolumn{1}{|l}{%
\cellcolor{LightGrayCellColor}%
\begin{tabular}[t]{@{}l@{}}%
\cellcolor{DarkGrayCellColor}Coriander\\%
Fenugreek\\%
\cellcolor{DarkGrayCellColor}Garlic\\%
Basil\\%
\cellcolor{DarkGrayCellColor}Carom\\%
Dill%
\end{tabular}%
}%
&%
\multicolumn{1}{|l}{%
\cellcolor{LightGrayCellColor}%
\begin{tabular}[t]{@{}l@{}}%
\cellcolor{DarkGrayCellColor}Ginger\\%
Saffron\\%
\cellcolor{DarkGrayCellColor}Holy Basil\\%
Thyme\\%
\cellcolor{DarkGrayCellColor}Turmeric\\%
Rosemary%
\end{tabular}%
}%
&%
\multicolumn{1}{|l}{%
\cellcolor{LightGrayCellColor}%
\begin{tabular}[t]{@{}l@{}}%
\cellcolor{DarkGrayCellColor}Strawberry\\%
Blueberry\\%
\cellcolor{DarkGrayCellColor}Fig\\%
Tomato\\%
\cellcolor{DarkGrayCellColor}Pineapple\\%
Cantaloupe%
\end{tabular}%
}%
&%
\multicolumn{1}{|l|}{%
\cellcolor{LightGrayCellColor}%
\begin{tabular}[t]{@{}l@{}}%
\cellcolor{DarkGrayCellColor}Petunia\\%
Geranium\\%
\cellcolor{DarkGrayCellColor}Begonia\\%
Lobelia\\%
\cellcolor{DarkGrayCellColor}Pansy\\%
Impatiens%
\end{tabular}%
}\\%
\hline
\end{tabular}%
\endgroup
\end{document}
使用 pgfmath 您可以计算\arraystretch
,\extrarowheight
使得单元格顶部规则和大写字母之间的垂直间隙大约与单元格小写字母和底部规则之间的垂直间隙相同,即 strutbox 的深度和字母深度之间的差异按比例yjpqfgj
缩放,使得单行表格单元格的总高度和深度为2\baselineskip
而支撑架和\baselineskip
可通过 进行调整\fontsize
。
(单行表格单元格的总高度和深度必须为,2\baselineskip
因为最左边的列有 5 行,其中两行垂直方向较大,\baselineskip
而其他列有 6 行,因此 5rows+2 \baselineskip
=6rows→2 \baselineskip
=1row...)
\RequirePackage{array}
\documentclass[12pt]{article}
\usepackage[table]{xcolor}
\usepackage{pgfmath}
\newlength\scratchlength
\definecolor{LightGrayCellColor}{RGB}{251 251 251}
\definecolor{DarkGrayCellColor}{RGB}{245 245 245}
\definecolor{RuleColor}{RGB}{232 232 232}
\definecolor{TextColor}{RGB}{64 64 64}
\begin{document}
\makeatletter
\begingroup
\arrayrulecolor{RuleColor}%
\color{TextColor}%
\fontsize{12pt}{12pt}\selectfont
\settoheight\scratchlength{ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz}%
\extrarowheight=\scratchlength
\settodepth\scratchlength{ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz}%
\advance\extrarowheight-\scratchlength
\pgfmathparse{((2\baselineskip-\extrarowheight)/(2\dp\strutbox))}%
\let\arraystretch\pgfmathresult
\pgfmathparse{(1/(\arraystretch))}%
\extrarowheight=\pgfmathresult\extrarowheight
\advance\extrarowheight\dp\strutbox
\advance\extrarowheight-\ht\strutbox
%\arrayrulewidth=2\arrayrulewidth % <- increase thickness of lines in
% case by your pdf-viewer they are not displayed correctly with
% small zoom-factors.
\noindent
\begin{tabular}{@{}llllll@{}}%
\hline
\multicolumn{2}{|@{}l@{}}{%
\begin{tabular}[c]{l|l}%
\multicolumn{2}{l}{\cellcolor{LightGrayCellColor}\textsf{Vegetables}}\\%
\hline
\cellcolor{LightGrayCellColor}\hbox{\rlap{\textsf{Annual}}\hphantom{Beetroot}}&%
\cellcolor{LightGrayCellColor}\hbox{\rlap{\textsf{Perennial}}\hphantom{Capsicum}}%
\end{tabular}%
}%
&%
\multicolumn{2}{|@{}l@{}}{%
\begin{tabular}[c]{l|l}%
\multicolumn{2}{l}{\cellcolor{LightGrayCellColor}\textsf{Herbs}}\\%
\hline
\cellcolor{LightGrayCellColor}\rlap{\textsf{Annual}}\hphantom{Fenugreek}&%
\cellcolor{LightGrayCellColor}\rlap{\textsf{Perennial}}\hphantom{Holy Basil}%
\end{tabular}%
}%
&%
\multicolumn{1}{|l}{%
\cellcolor{LightGrayCellColor}%
\begin{tabular}[c]{@{}l@{}}%
\textsf{Fruits}%
\end{tabular}%
}%
&%
\multicolumn{1}{|l|}{%
\cellcolor{LightGrayCellColor}%
\begin{tabular}[c]{@{}l@{}}%
\textsf{Flowers}%
\end{tabular}%
}%
\\%
\hline
\multicolumn{1}{|l}{%
\cellcolor{DarkGrayCellColor}%
\begin{tabular}[t]{@{}l@{}}%
Beans\\%
\cellcolor{LightGrayCellColor}Beetroot\settowidth\scratchlength{Melon}\global\scratchlength\scratchlength\\%
\multicolumn{1}{@{}p{\scratchlength}}{Bitter\hfill\null\linebreak Melon}\\%
\cellcolor{LightGrayCellColor}Carrot\settowidth\scratchlength{Gourd}\global\scratchlength\scratchlength\\%
\multicolumn{1}{@{}p{\scratchlength}}{Bottle\hfill\null\linebreak Gourd}\\%
\end{tabular}%
}%
&%
\multicolumn{1}{|l}{%
\cellcolor{LightGrayCellColor}%
\begin{tabular}[t]{@{}l@{}}%
\cellcolor{DarkGrayCellColor}Capsicum\\%
Chilli\\%
\cellcolor{DarkGrayCellColor}Pepper%
\end{tabular}%
}%
&%
\multicolumn{1}{|l}{%
\cellcolor{LightGrayCellColor}%
\begin{tabular}[t]{@{}l@{}}%
\cellcolor{DarkGrayCellColor}Coriander\\%
Fenugreek\\%
\cellcolor{DarkGrayCellColor}Garlic\\%
Basil\\%
\cellcolor{DarkGrayCellColor}Carom\\%
Dill%
\end{tabular}%
}%
&%
\multicolumn{1}{|l}{%
\cellcolor{LightGrayCellColor}%
\begin{tabular}[t]{@{}l@{}}%
\cellcolor{DarkGrayCellColor}Ginger\\%
Saffron\\%
\cellcolor{DarkGrayCellColor}Holy Basil\\%
Thyme\\%
\cellcolor{DarkGrayCellColor}Turmeric\\%
Rosemary%
\end{tabular}%
}%
&%
\multicolumn{1}{|l}{%
\cellcolor{LightGrayCellColor}%
\begin{tabular}[t]{@{}l@{}}%
\cellcolor{DarkGrayCellColor}Strawberry\\%
Blueberry\\%
\cellcolor{DarkGrayCellColor}Fig\\%
Tomato\\%
\cellcolor{DarkGrayCellColor}Pineapple\\%
Cantaloupe%
\end{tabular}%
}%
&%
\multicolumn{1}{|l|}{%
\cellcolor{LightGrayCellColor}%
\begin{tabular}[t]{@{}l@{}}%
\cellcolor{DarkGrayCellColor}Petunia\\%
Geranium\\%
\cellcolor{DarkGrayCellColor}Begonia\\%
Lobelia\\%
\cellcolor{DarkGrayCellColor}Pansy\\%
Impatiens%
\end{tabular}%
}\\%
\hline
\end{tabular}%
\endgroup
\end{document}
答案2
使用新的 LaTeX3 包可以更轻松地创建此类表格tabularray
:
\documentclass{article}
\usepackage{tabularray,xcolor}
\begin{document}
\begin{tblr}{
vspan=even, hlines={white,1pt}, vlines = {white,1pt},
row{1-2} = {font=\sffamily,azure9},
row{3} = {gray9}, row{4-12} = {gray9!50},
cell{1}{1,3} = {c=2}{}, cell{1}{5,6} = {r=2}{},
cell{5,10}{1} = {r=3}{gray9}, cell{8}{1} = {r=2}{},
cell{5,7,9,11}{2-6} = {r=2}{},
cell{5}{2-6} = {gray9}, cell{9}{3-6} = {gray9},
}
Vegetables & & Herbs & & Fruits & Flowers \\
Annual & Perennial & Annual & Perennial & & \\
Beans & Capsicum & Coriander & Ginger & Strawberry & Petunia \\
Beetroot & Chilli & Fenugreek & Saffron & Blueberry & Geranium \\
{Bitter \\ Melon} & Pepper & Garlic & Holy Basil & Fig & Begonia \\
& & & & & \\
& & Basil & Thyme & Tomato & Lobelia \\
Carrot & & & & & \\
& & Carom & Turmeric & Pineapple & Pansy \\
{Bottle \\ Gourd} & & & & & \\
& & Dill & Rosemary & Cantaloupe & Impatiens \\
& & & & & \\
\end{tblr}
\end{document}
答案3
这是一个{NiceTabular}
使用 的解决方案nicematrix
。
无论使用什么 PDF 查看器,规则似乎都不会消失。
\documentclass{article}
\usepackage{nicematrix,tikz,makecell}
\begin{document}
\definecolor{gray1}{gray}{0.8}
\definecolor{gray2}{gray}{0.9}
\newcommand{\headstyle}{\RowStyle{\sffamily}\rowcolor{gray2}}
\begin{NiceTabular}{llllll}[hvlines,cell-space-limits=7pt,colortbl-like]
\CodeBefore
\rowcolors{3}{gray1}{gray2}
\tikz \fill [gray1] (5-|1) rectangle (6.5-|2) (7.5-|1) rectangle (9-|2) ;
\tikz \fill [gray2] (6.5-|1) rectangle (7.5-|2) ;
\Body
\headstyle
\Block[l]{1-2}{Vegetable} & & \Block[l]{1-2}{Herbs} & & \Block{2-1}{Fruits} & \Block{2-1}{Flowers} \\
\headstyle
Annual & Perennial & Annual & Perennial & \\
Beans & Capsicum & Coriander & Ginger & Strawberry & Petunia \\
Beetroot & Chilli & Fenugreek & Saffron & Blueberry & Geranium \\
\smash{\makecell[t]{Bitter\\ Melon}}
\Block{4-1}{}
& Pepper & Garlic & Holy Basil & Fig & Begonia \\
\Block{2-1}{Carrot}
& \Block[fill=gray2]{3-1}{}
& Basil & Thyme & Tomato & Lobelia \\
& & Carom & Turmeric & Pineapple & Pansy \\
\smash{\makecell[b]{Bottle\\ Gourd}}
& & Dill & Rosemary & Cantaloupe & Impatiens
\CodeAfter
\tikz \draw (6.5-|1) -- (6.5-|2) (7.5-|1) -- (7.5-|2) ;
\end{NiceTabular}
\end{document}
您需要多次编译(因为 PGF/Tikz 节点)。
答案4
虽然这两个答案都有效,但(在我看来)它们太难了。为什么不直接使用简单的在线表格生成器呢:https://www.tablesgenerator.com/?
它允许您创建表格(几乎是所见即所得),然后复制/粘贴 latex 源代码。比手动输入所有 latex 代码要容易得多(在我看来,也更容易调试)。