如何使单元格颜色不完全填充单元格

如何使单元格颜色不完全填充单元格
\documentclass[]{article}
\usepackage[utf8x]{inputenc}
\usepackage[english]{babel}
\usepackage[T1]{fontenc}
\usepackage{amsmath,amsthm,amsfonts,amssymb}
\usepackage{graphicx}
\usepackage{floatrow}
\usepackage{subfig}
\usepackage{microtype}
\usepackage{braket}
\usepackage{physics}
\usepackage{bm}
\usepackage{scalefnt}
\usepackage{asymptote}
\usepackage{asypictureB}
\usepackage{float}
\usepackage{makecell}

\usepackage[all,cmtip]{xy}
\usepackage{thmtools,thm-restate}
\usepackage[shortlabels]{enumitem}
\usepackage{xcolor,colortbl}
\makeatletter 
\def\l@subsubsection#1#2{}
\makeatother
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{zref-savepos}
\usepackage{tabu}
\newcommand*\circled[1]{\tikz[baseline=(char.base)]{% <---- BEWARE
\node[shape=circle,draw,inner sep=2pt] (char) {#1};}}

\begin{document}

\begin{table}[H]
    \centering
\begin{tabular}{c|c|c}
    \circled{A}
    & \circled{B}
    & \circled{C}
          \\ \hline
    \cellcolor{red!25}{p$_1$} & \cellcolor{red!25}{p$_1$}   & \cellcolor{red!25}{p$_1$}          \\ \hline
    \cellcolor{red!25}{p$_2$}& \cellcolor{red!25}{p$_2$}    & \cellcolor{red!25}{p$_2$}  \\ \hline
    \cellcolor{red!25}{p$_3$}  & 
 \cellcolor{red!25}{p$_3$}   & \cellcolor{red!25}{p$_3$}
\end{tabular}
\end{table}
\end{document}

上述代码创建下表。 在此处输入图片描述

是否可以使单元格颜色不与相邻单元格合并,使得单元格中的文本具有彩色背景,但在垂直和水平方向上相邻单元格之间仍然有一些白色间距?

答案1

更新:这是一个更简短的解决方案。

\documentclass{article}
\usepackage{nicematrix,tikz}

\newcommand*\circled[1]{\tikz[baseline=(char.base)]{% <---- BEWARE
\node[shape=circle,draw,inner sep=2pt] (char) {#1};}}

\begin{document}

\NiceMatrixOptions
  { custom-line = { command = TrimmedHline , 
                    tikz = {shorten < = 1pt, shorten > = 1.2pt} } , 
    custom-line = { letter = I , 
                    tikz = {shorten < = 1pt, shorten > = 1pt} } }

\begin{NiceTabular}{cIcIc}[cell-space-bottom-limit=2pt]
\CodeBefore
   \TikzEveryCell{offset=1pt,fill=red!15}
   \rowcolor{white}{1}
\Body
  \circled{A} & \circled{B} & \circled{C} \\ \TrimmedHline
    p$_1$     & p$_1$       & p$_1$       \\ \TrimmedHline
    p$_2$     & p$_2$       & p$_2$       \\ \TrimmedHline
    p$_3$     & p$_3$       & p$_3$       \\ 
\end{NiceTabular}

\end{document}

第一个代码的输出


与。{NiceTabular}nicematrix

\documentclass{article}
\usepackage{nicematrix,tikz}

\newcommand*\circled[1]{\tikz[baseline=(char.base)]{% <---- BEWARE
\node[shape=circle,draw,inner sep=2pt] (char) {#1};}}

\begin{document}

\begin{NiceTabular}{ccc}[cell-space-limits=1mm]
\CodeBefore 
  \begin{tikzpicture}
  \fill [red!15] (2-|1) rectangle (5-|4) ; 
  \begin{scope} [color = white, line width = 2 pt]
    \draw (2-|1) -- (2-|4) (3-|1) -- (3-|4) (4-|1) -- (4-|4) ; 
    \draw (2-|2) -- (5-|2) (2-|3) -- (5-|3) ; 
  \end{scope}
  \draw (2-|1) -- (2-|4) (3-|1) -- (3-|4) (4-|1) -- (4-|4) ; 
  \draw (1-|2) -- (5-|2) (1-|3) -- (5-|3) ; 
  \draw [color = white, line width = 2 pt] (1-|1) rectangle (5-|4) ;
  \end{tikzpicture}
\Body
  \circled{A} & \circled{B} & \circled{C} \\
    p$_1$ & p$_1$ & p$_1$ \\
    p$_2$ & p$_2$ & p$_2$ \\
    p$_3$ & p$_3$ & p$_3$
\end{NiceTabular}

\end{document}

您需要多次编译(因为nicematrix在后台使用 PGF/Tikz 节点)。

第二段代码的输出

答案2

是否有可能...在垂直和水平方向上相邻单元格之间留有一些白色间距?

您可以执行\arrayrulecolor{white}以实现您的排版目标。

在此处输入图片描述

\documentclass{article}
%%\usepackage[utf8x]{inputenc} % that's the default nowadays
\usepackage[english]{babel}
\usepackage[T1]{fontenc}
\usepackage{xcolor,colortbl}

\usepackage{tikz}
\usetikzlibrary{calc}
\newcommand*\circled[1]{\tikz[baseline=(char.base)]{% <---- BEWARE
   \node[shape=circle,draw,inner sep=2pt] (char) {#1};}}

\begin{document}

\begin{table}[h]
\arrayrulecolor{white}
\arrayrulewidth=2\arrayrulewidth % double the width of the rules

\centering
\begin{tabular}{c|c|c}
    \circled{A} & \circled{B} & \circled{C} \\ 
    \hline
    \cellcolor{red!25}p\textsubscript{1} & 
    \cellcolor{red!25}p\textsubscript{1} & 
    \cellcolor{red!25}p\textsubscript{1} \\ 
    \hline
    \cellcolor{red!25}p\textsubscript{2} & 
    \cellcolor{red!25}p\textsubscript{2} & 
    \cellcolor{red!25}p\textsubscript{2} \\ 
    \hline
    \cellcolor{red!25}p\textsubscript{3} & 
    \cellcolor{red!25}p\textsubscript{3} & 
    \cellcolor{red!25}p\textsubscript{3} 
\end{tabular}
\end{table}
\end{document}

答案3

像这样 ?

在此处输入图片描述

tabularay

\documentclass{article}
\usepackage{tabularray}
\usepackage{tikz}
\usetikzlibrary{calc}
\newcommand*\circled[1]{\tikz[baseline=(char.base)]{% <---- BEWARE
\node[shape=circle,draw,inner sep=2pt] (char) {#1};}}

\begin{document}
\begin{tblr}{
  colspec = { *{3}c },
  hline{2-Y} = {1pt,solid},
  vline{2-Y} = {1pt,solid},
  row{2-Z} = {cmd=\colorbox{red!25}}
  }
  \circled{A} & \circled{B} & \circled{C}\\ 
  p$_1$ & p$_1$ & p$_1$ \\
  p$_2$ & p$_2$ & p$_2$ \\
  p$_3$ & p$_3$ & p$_3$ \\
\end{tblr}
\end{document}

编辑:有了tabularray,我认为 zarko 的答案更好(如果将 p1 替换为 d1,则 \strut 和 \colorbox 不起作用)。

另一个解决方案是使用 tikz

EDIT2:为了让它看起来更漂亮一些,但与 F. Pantigny 的回答不同nicematrix,如果列的宽度不一样,您必须手动调整矩形的宽度。

在此处输入图片描述

代码:

    \documentclass{article}
    \usepackage{tabularray}
    \usepackage{tikz}
    \newcommand*\circled[1]{\tikz[baseline=(char.base)]{% <---- BEWARE
    \node[shape=circle,draw,inner sep=2pt] (char) {#1};}}

    \newcommand*\rect[1]{\tikz[baseline=(char.base)]{% <---- BEWARE
    \node[inner sep=2pt,fill=red!25,minimum width=2em] (char) {\strut$#1$};}}

    \begin{document}
    \begin{tblr}{
    colspec = { *{3}{Q[wd=2em,c]} },
    hline{2-Y} = {1pt,solid},
    vline{2-Y} = {1pt,solid},
    row{1} = {cmd=\circled},
    row{2-Z} = {cmd=\rect},
    rowsep=1pt,
    colsep=1pt
    }
    A & B & C\\ 
    p_1 & p_1  & d_1 \\
    p_2 & p_2  & d_2 \\
    p_3 & p_3  & d_3 \\
    \end{tblr}
    \end{document}

答案4

不错的@pascal 答案的一个小变化(+1):

  • 定义是用于单元格内容后面的着色框的命令
  • 表格主体处于数学模式
\documentclass[border=3.141592]{standalone}
\usepackage{tikz}
\usepackage{tabularray}
\UseTblrLibrary{amsmath}

\newcommand*\circled[1]{\tikz[baseline=(char.base)]{% <---- BEWARE
                        \node[shape=circle,draw,inner sep=2pt] (char) {#1};}}
\makeatletter   % https://tex.stackexchange.com/questions/522350/
    \newcommand{\cb}[3]{\fcolorbox{#2}{#1}{\m@th$\displaystyle#3$}}
\makeatother

\begin{document}
\begin{tblr}{hline{2-Y} = {1pt}, vline{2-Y} = {1pt},
             colspec = { *{3}{Q[c]} },
             colsep  = {2pt},
             row{2-Z}= {cmd=\cb{red!30}{red!30}}
            }
\circled{A} & \circled{B} & \circled{C}\\
  p_1 & p_1 & p_1 \\
  p_2 & p_2 & p_2 \\
  p_3 & p_3 & p_3 \\
\end{tblr}
\end{document}

在此处输入图片描述

相关内容