![表格中的 hhline 和 cellcolor 问题](https://linux22.com/image/396830/%E8%A1%A8%E6%A0%BC%E4%B8%AD%E7%9A%84%20hhline%20%E5%92%8C%20cellcolor%20%E9%97%AE%E9%A2%98.png)
我正在尝试设置一个带有 和 的表格\hhline
。\cellcolor
到目前为止,我没有\hhline
在彩色单元格中看到 ,现在我缺少\hhline
应该将单元格分开的黑色 。它应该是这样的:
\documentclass{article}
% General document formatting
\usepackage[margin=0.7in]{geometry}
\usepackage[parfill]{parskip}
\usepackage[utf8]{inputenc}
\usepackage{multirow, hhline}
% Related to math
\usepackage{amsmath,amssymb,amsfonts,amsthm}
\usepackage{multirow}
\usepackage[table,xcdraw]{xcolor}
\begin{document}
\begin{table}[!ht]
\centering
\begin{tabular}{|c|c|c|}
\hhline{*{1}{-}}
{\cellcolor[HTML]{C0E5C6}}Modell-Überkategorien &\multicolumn{2}{c}{} \\
\cline{1-1}
statisch &\multicolumn{2}{c}{} \\
\cline{1-1}
zufällig (Monte-Carlo) &\multicolumn{2}{c}{} \\
\hhline{*{3}{-}}
\cellcolor[HTML]{E2EFD9} & \cellcolor[HTML]{E2EFD9} Kontinuirlich & \cellcolor[HTML]{E2EFD9} \\
\hhline{>{\arrayrulecolor[HTML]{E2EFD9}}->{\arrayrulecolor{black}}->{\arrayrulecolor[HTML]{E2EFD9}}-}
\cellcolor[HTML]{E2EFD9} & \cellcolor[HTML]{E2EFD9} Hybrid & \cellcolor[HTML]{E2EFD9}\\
\hhline{>{\arrayrulecolor[HTML]{E2EFD9}}->{\arrayrulecolor{black}}->{\arrayrulecolor[HTML]{E2EFD9}}-} \multirow{-3}{*}{{\cellcolor[HTML]{E2EFD9}}dynamisch}
& \cellcolor[HTML]{E2EFD9} Diskret & \cellcolor[HTML]{E2EFD9} \multirow{-3}{*}{Raum/Zeit} \\
\hline
\cellcolor[HTML]{E2EFD9} (Multi)-Agenten basiert &\multicolumn{2}{c}{} \\
\hhline{*{1}{-}}
\end{tabular}
\bigskip
\end{table}
\end{document}
我缺少两条 hhline,并且多行单元格的边框之间有一些小间隙。有办法解决这个问题吗?
答案1
我有两个 MWE:最后一个可以解决两个问题,第一个不能解决小绿地问题。
使用线
您忘记打开arrayrowcolor{black}
两个地方。此外, - 语法有一个地方是错误的。我还没有找到水平边框中小空间的解决方案(目前)。覆盖垂直线的是下面的hhline
绿色。arrayrule
multirow
不相关:您加载multirow
两次。
\documentclass{article}
% General document formatting
\usepackage[margin=0.7in]{geometry}
\usepackage[parfill]{parskip}
\usepackage[utf8]{inputenc}
\usepackage{multirow, hhline}
% Related to math
\usepackage{amsmath,amssymb,amsfonts,amsthm}
\usepackage[table,xcdraw]{xcolor}
\renewcommand{\arraystretch}{1.2}
\setlength{\extrarowheight}{1pt}
\begin{document}
\begin{table}[!ht]
\centering
\begin{tabular}{|c|c|c|}
\hhline{-~~}
{\cellcolor[HTML]{C0E5C6}}Modell-Überkategorien &\multicolumn{2}{c}{} \\
\hhline{-~~}
statisch &\multicolumn{2}{c}{} \\
\hhline{-~~}
zufällig (Monte-Carlo) &\multicolumn{2}{c}{} \\
\hhline{*{3}{-}}
\cellcolor[HTML]{E2EFD9} & \cellcolor[HTML]{E2EFD9} Kontinuirlich & \cellcolor[HTML]{E2EFD9} \\
\hhline{>{\arrayrulecolor[HTML]{E2EFD9}}->{\arrayrulecolor{black}}->{\arrayrulecolor[HTML]{E2EFD9}}-}\arrayrulecolor{black}
\cellcolor[HTML]{E2EFD9} & \cellcolor[HTML]{E2EFD9} Hybrid & \cellcolor[HTML]{E2EFD9}\\
\hhline{>{\arrayrulecolor[HTML]{E2EFD9}}->{\arrayrulecolor{black}}->{\arrayrulecolor[HTML]
{E2EFD9}}-}\arrayrulecolor{black}
\multirow{-3}{*}{{\cellcolor[HTML]{E2EFD9}}dynamisch}
& \cellcolor[HTML]{E2EFD9} Diskret & \cellcolor[HTML]{E2EFD9} \multirow{-3}{*}{Raum/Zeit} \\
\hhline{|---}
\cellcolor[HTML]{E2EFD9} (Multi)-Agenten basiert &\multicolumn{2}{c}{} \\
\hhline{|-~~}
\end{tabular}
\bigskip
\end{table}
\end{document}
使用卡路里
使用以下方式可以更轻松地构建表格:卡路里。
\documentclass{article}
% General document formatting
\usepackage[margin=0.7in]{geometry}
\usepackage[parfill]{parskip}
\usepackage[utf8]{inputenc}
\usepackage{cals}
% Related to math
\usepackage{amsmath,amssymb,amsfonts,amsthm}
\usepackage[table,xcdraw]{xcolor}
\let\nc=\nullcell % Shortcuts
\let\sc=\spancontent
\definecolor{darkGreen}{HTML}{C0E5C6}
\definecolor{lightGreen}{HTML}{E2EFD9}
\begin{document}
\begin{calstable}
% Defining 3 column relativ to each other and relativ to the margins
\colwidths{{\dimexpr(\columnwidth/16*4)\relax}
{\dimexpr(\columnwidth/16*3)\relax}
{\dimexpr(\columnwidth/16*3)\relax}
}
% The tabular fills 5/8 of the text area
% Set up the tabular
\makeatletter
\def\cals@framers@width{0.4pt} % Outside frame horizontal rules
\def\cals@framecs@width{0.4pt} % Outside frame horizontal rules, reduce
% thickness if you find them too heavy
\cals@setpadding{Ag}
\cals@setcellprevdepth{Al}
\def\cals@cs@width{0.4pt} % Inside rules, reduce if the rule is too heavy
\def\cals@rs@width{0.4pt}
\def\cals@bgcolor{} % Define cals@bgcolor to empty
\def\green{\ifx\cals@bgcolor\empty % Colour switch (on-off)
\def\cals@bgcolor{darkGreen}
\else \def\cals@bgcolor{} \fi}
\def\lgreen{\ifx\cals@bgcolor\empty % Colour switch (on-off)
\def\cals@bgcolor{lightGreen}
\else \def\cals@bgcolor{} \fi}
\def\tb{\ifx\cals@borderT\relax % Top border switch (off-on)
\def\cals@borderT{0pt}
\else \let\cals@borderT\relax\fi}
\def\bb{\ifx\cals@borderB\relax % Botton border switch (off-on)
\def\cals@borderB{0pt}
\else \let\cals@borderB\relax\fi}
\def\rb{\ifx\cals@borderR\relax % Right border switch (off-on)
\def\cals@borderR{0pt}
\else \let\cals@borderR\relax\fi}
% R1 Body
\brow
\green\alignC\cell{Modell-Überkategorien}\green
\tb\rb\nc{lt}
\nc{rt}\tb\rb
\erow
% R2 Body
\brow
\cell{statisch}
\rb\nc{l}
\nc{r}\rb
\erow
% R3 Body
\brow
\cell{zufällig (Monte-Carlo)}
\rb\nc{lb}
\nc{rb}\sc{}\rb
\erow
% R4 Body
\brow
\lgreen\nc{lrt}
\cell{Kontinuirlich}
\nc{lrt}
\erow
% R5 Body
\brow
\nc{lr}
\cell{Hybrid}
\nc{lr}
\erow
% R6 Body
\brow
\nc{lrb}\sc{\vfil dynamisch}
\cell{Diskret}
\nc{lrb}\sc{\vfil Raum/Zeit}
\erow
% R7 Body
\brow
\cell{(Multi)-Agenten basiert}\lgreen
\rb\bb\nc{ltb}
\nc{rtb}\sc{}\bb\rb
\erow
\makeatletter
\end{calstable}\par % \par needed to align the tabular
\end{document}
答案2
对第一个例子(+1)进行了小修改,涉及表格中颜色的使用規劃答案(练习用)):
\documentclass{article}
% General document formatting
\usepackage[margin=0.7in]{geometry}
\usepackage[parfill]{parskip}
\usepackage[utf8]{inputenc}
\usepackage{multirow, hhline}
% Related to math
\usepackage{amsmath,amssymb,amsfonts,amsthm}
\usepackage[table,xcdraw]{xcolor}
\definecolor{col1}{HTML}{C0E5C6} % <---
\definecolor{col2}{HTML}{E2EFD9} % <---
\renewcommand{\arraystretch}{1.2}
\setlength{\extrarowheight}{1pt}
\begin{document}
\begin{table}[!ht]
\centering
\begin{tabular}{|c|c|c|}
\hhline{-~~}
\cellcolor{col1}{Modell-Überkategorien} % <---
& \multicolumn{2}{c}{} \\
\hhline{-~~}
statisch & \multicolumn{2}{c}{} \\
\hhline{-~~}
zufällig (Monte-Carlo) & \multicolumn{2}{c}{} \\
\hhline{*{3}{-}}
\rowcolor{col2}
& Kontinuirlich & \\ % <---
\hhline{>{\arrayrulecolor{col2}}-%
>{\arrayrulecolor{black}}-%
>{\arrayrulecolor{col2}}-}
\arrayrulecolor{black}
\rowcolor{col2}
& Hybrid & \\
\hhline{>{\arrayrulecolor{col2}}-%
>{\arrayrulecolor{black}}-%
>{\arrayrulecolor{col2}}-}
\arrayrulecolor{black}
\rowcolor{col2}
\multirow{-3}{*}{dynamisch}
& Diskret & \multirow{-3}{*}{Raum/Zeit} \\
\hhline{|---}
\cellcolor{col2} (Multi)-Agenten basiert
& \multicolumn{2}{c}{} \\
\hhline{|-~~}
\end{tabular}
\end{table}
\end{document}
结果和他的回答一样。
答案3
{NiceTabular}
这是的解决方案。此环境与经典的nicematrix
类似,但具有附加功能。{tabular}
array
使用键
corners
,您可以nicematrix
考虑到空角(见下文)。您可以使用命令垂直和水平合并单元格
\Block
。关键
hvlines
是绘制所有规则,同时考虑到块和角落。有命令可以给单元格、列和行着色(在 中
\CodeBefore
)。角落里的单元格未填充。
使用nicematrix
,您将不会在某些 PDF 查看器中在某些缩放级别上看到伪影(就像使用 创建的 PDF 一样colortbl
)。
\documentclass{article}
\usepackage{nicematrix}
\renewcommand{\arraystretch}{1.2}
\setlength{\extrarowheight}{1pt}
\begin{document}
\begin{table}[!ht]
\centering
\begin{NiceTabular}{ccc}[corners,hvlines]
\CodeBefore
\cellcolor[HTML]{C0E5C6}{1-1}
\rowcolor[HTML]{E2EFD9}{4-*}
\Body
Modell-Überkategorien \\
statisch \\
zufällig (Monte-Carlo) \\
\Block{3-1}{dynamisch} & Kontinuirlich & \Block{3-1}{Raum/Zeit} \\
& Hybrid & \\
& Diskret \\
(Multi)-Agenten basiert
\end{NiceTabular}
\end{table}
\end{document}
您需要多次编译(因为nicematrix
在后台使用 PGF/Tikz 节点)。