在具有合并单元格的 LaTeX 表中,如何为每个单元格赋予不同的颜色?
%% LyX 1.6.7 created this file. For more info, see http://www.lyx.org/.
%% Do not edit unless you really know what you are doing.
\documentclass[english]{article}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}
\makeatletter
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% LyX specific LaTeX commands.
%% Because html converters don't know tabularnewline
\providecommand{\tabularnewline}{\\}
\makeatother
\usepackage{babel}
\begin{document}
\begin{tabular}{|c|c|c|c|}
\cline{3-4}
\multicolumn{1}{c}{} & & \multicolumn{2}{c|}{Overexpression}\tabularnewline
\cline{3-4}
\multicolumn{1}{c}{} & & Cancer & Control \tabularnewline
\hline
Marked & Cancer & 45 & 61\tabularnewline
\cline{2-4}
Cell Line & Control & 42 & 52\tabularnewline
\hline
\end{tabular}
\end{document}
答案1
\usepackage[table]{xcolor}
...
\begin{tabular}{...}
\cellcolor{blue} foo & \cellcolor{red} bar\\
...
还有一个用于为整列和整行着色的命令。
答案2
放入\usepackage[table]{xcolor}
序言中,使用嵌套表格和这些命令:
\rowcolor{}
,\columncolor{}
答案3
利用,您可以轻松地创建表格、轻松地为单元格添加颜色,还可以合并单元格并在 PDF 查看器中获得完美的效果{NiceTabular}
。nicematrix
\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{nicematrix}
\begin{document}
\begin{NiceTabular}{cccc}[hvlines,corners=NW,colortbl-like]
& & \Block[fill=red!15]{1-2}{Overexpression} \\
& & Cancer & Control \\
\Block[fill=red!15]{2-1}{}%
Marked & Cancer & 45 & 61 \\
Cell Line & Control & 42 & \cellcolor{blue!10} 52 \\
\end{NiceTabular}
\end{document}
您需要进行多次编译(因为nicematrix
在后台使用了 PGF/Tiks 节点)。