3
\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}

The above code creates the following table. enter image description here

Is it possible to make the cell coloring not merge with the neighboring cells such that the text in the cell has the colored background but there is still some white spacing between the neighboring cells in both vertical and horizontal directions?

0

4 Answers 4

5

With {NiceTabular} of 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}

You need several compilations (because nicematrix uses PGF/Tikz nodes under the hood).

Output of the above code

3

Is it possible ... to [have] some white spacing between the neighboring cells in both vertical and horizontal directions?

You could execute \arrayrulecolor{white} to achieve your typesetting objective.

enter image description here

\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}
2
  • Thanks, this is better. But is it possible to have the white color on the sides of the red color patch in the cell but not lose the black lines between the cells?
    – user25957
    Jan 5 at 2:40
  • @user25957 - In you initial posting, you mentioned "some white spacing between the neighboring cells", but didn't mention anything about keeping the black lines as well. Sorry for not picking up on that.
    – Mico
    Jan 5 at 4:29
3

Like this ?

enter image description here

With 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}

EDIT : with tabularray, i think the answer of zarko is better (if you replace p1 by d1, \strut with \colorbox don't work).

Another solution with tikz

EDIT2: To make it look a little nicer, but unlike F. Pantigny's answer with nicematrix, you have to adjust the width of the rectangles manually, if the columns are not the same width.

enter image description here

THE CODE:

    \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}
0
2

A small variation of nice @pascal answer (+1):

  • defined is command for coloring box behind cells content
  • table body is in math mode
\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}

enter image description here

2
  • your answer is better, if you replace p1 by d1, we can easily catch up with \strut in \fcolorbox{#2}{#1}{\strut\m@th$\displaystyle#3$}
    – pascal974
    Jan 7 at 10:35
  • @pascal974, both solution has drawback that size of cell contents should not differ. otherwise result is awful. This means, that we still need to digging for better solution ;-).
    – Zarko
    Jan 7 at 13:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .