使用粗彩色边框突出显示表格单元格

使用粗彩色边框突出显示表格单元格

我想突出显示表格中的某些单元格。具体来说,我想将这些单元格的边框设置为彩色且粗体。这些单元格还应具有背景颜色。

这是我的表格的一个最小示例:

\documentclass{article}
\usepackage{array,colortbl,xcolor}

\begin{document} 

\begin{tabular}{p{1.4cm}p{2.55cm}p{2.55cm}p{2.55cm}p{2.55cm}}
\hline
\multicolumn{1}{c}{} & \multicolumn{2}{c}{\textbf{Football}} & \multicolumn{2}{c}{\textbf{Basketball}} \\\hline
\multicolumn{1}{c}{} & \multicolumn{1}{c}{\textbf{1. League}} & \multicolumn{1}{c}{\textbf{2. League}} & \multicolumn{1}{c}{\textbf{1. League}} & \multicolumn{1}{c}{\textbf{2. League}} \\ \hline
\textbf{Average} & 10000000000 & \cellcolor{lightgray}20000000000 & 30000000000 & 40000000000\\ \hline
\textbf{Total} & 50000000000 & 60000000000 & 70000000000 & \cellcolor{lightgray}80000000000\\ \hline

\end{tabular}
\end{document}

我尝试使用多列和 hhline 的组合来设置特定单元格的边框,但失败了 :-( (其中一个问题是单元格背景部分覆盖了边框)。

我也试过蒂克兹但无法在单元格的边框上准确地绘制,尤其是在出现换行符时。

其最佳外观如下:

在此处输入图片描述

有人可以帮我完成这个工作吗?

答案1

在此处输入图片描述

我阻止了单元格因边框而变宽,但允许其垂直增长。如果不希望这样,则需要增加一点负间距。

而不是使用更colortbl容易抓住一个框中的单元格内容(因为无论如何你都会对p列执行此操作)并使用\fcolorbox

\documentclass{article}
\usepackage{array,colortbl,xcolor}

\begin{document} 

\makeatletter
\def\highlight#1{%
\fboxrule2pt %
\hsize=\dimexpr\hsize-2\fboxrule-2\fboxsep\relax
#1%
\@endpbox\unskip\setbox0\lastbox\bgroup
\fboxrule2pt %
\fcolorbox{red}{lightgray}{\box0}\hfill}

\begin{tabular}{p{1.4cm}p{2.55cm}p{2.55cm}p{2.55cm}p{2.55cm}}
\hline
\multicolumn{1}{c}{} & \multicolumn{2}{c}{\textbf{Football}} & \multicolumn{2}{c}{\textbf{Basketball}} \\\hline
\multicolumn{1}{c}{} & \multicolumn{1}{c}{\textbf{1. League}} & \multicolumn{1}{c}{\textbf{2. League}} & \multicolumn{1}{c}{\textbf{1. League}} & \multicolumn{1}{c}{\textbf{2. League}} \\ \hline
\textbf{Average} & 10000000000 & \cellcolor{lightgray}20000000000 & 30000000000 & 40000000000\\ \hline
\textbf{Total} & 50000000000 & 60000000000 &
\highlight{70000000000}& \cellcolor{lightgray}80000000000\\ \hline

\end{tabular}
\end{document}

答案2

我使用 TikZ 和现在著名的宏提供了解决方案\tikzmark

\documentclass{article}
\usepackage{xparse}
\usepackage{tikz}
\usetikzlibrary{calc}

% to change colors
\newcommand{\fillcol}{blue!20}
\newcommand{\bordercol}{blue}

\newcommand{\setfillcolor}[1]{\renewcommand{\fillcol}{#1}}
\newcommand{\setbordercolor}[1]{\renewcommand{\bordercol}{#1}}

%% code by Andrew Stacey 
% https://tex.stackexchange.com/questions/51582/background-coloring-with-overlay-specification-in-algorithm2e-beamer-package#51582

\makeatletter
\tikzset{%
     remember picture with id/.style={%
       remember picture,
       overlay,
       save picture id=#1,
     },
     save picture id/.code={%
       \edef\pgf@temp{#1}%
       \immediate\write\pgfutil@auxout{%
         \noexpand\savepointas{\pgf@temp}{\pgfpictureid}}%
     },
     if picture id/.code args={#1#2#3}{%
       \@ifundefined{save@pt@#1}{%
         \pgfkeysalso{#3}%
       }{
         \pgfkeysalso{#2}%
       }
     }
   }

   \def\savepointas#1#2{%
  \expandafter\gdef\csname save@pt@#1\endcsname{#2}%
}

\def\tmk@labeldef#1,#2\@nil{%
  \def\tmk@label{#1}%
  \def\tmk@def{#2}%
}

\tikzdeclarecoordinatesystem{pic}{%
  \pgfutil@in@,{#1}%
  \ifpgfutil@in@%
    \tmk@labeldef#1\@nil
  \else
    \tmk@labeldef#1,(0pt,0pt)\@nil
  \fi
  \@ifundefined{save@pt@\tmk@label}{%
    \tikz@scan@one@point\pgfutil@firstofone\tmk@def
  }{%
  \pgfsys@getposition{\csname save@pt@\tmk@label\endcsname}\save@orig@pic%
  \pgfsys@getposition{\pgfpictureid}\save@this@pic%
  \pgf@process{\pgfpointorigin\save@this@pic}%
  \pgf@xa=\pgf@x
  \pgf@ya=\pgf@y
  \pgf@process{\pgfpointorigin\save@orig@pic}%
  \advance\pgf@x by -\pgf@xa
  \advance\pgf@y by -\pgf@ya
  }%
}

\NewDocumentCommand{\tikzmarkin}{m D(){0.825,-0.10} D(){-0.175,0.27}}{%
      \tikz[remember picture,overlay]
      \draw[line width=1pt,rectangle,fill=\fillcol,draw=\bordercol]
      (pic cs:#1) ++(#2) rectangle (#3)
      ;}

\newcommand\tikzmarkend[2][]{%
\tikz[remember picture with id=#2] #1;}


\begin{document} 
\setfillcolor{lightgray}
\setbordercolor{red}
\begin{tabular}{p{1.4cm}p{2.55cm}p{2.55cm}p{2.55cm}p{2.55cm}}
\hline
\multicolumn{1}{c}{} & \multicolumn{2}{c}{\textbf{Football}} & \multicolumn{2}{c}{\textbf{Basketball}} \\\hline
\multicolumn{1}{c}{} & \multicolumn{1}{c}{\textbf{1. League}} & \multicolumn{1}{c}{\textbf{2. League}} & \multicolumn{1}{c}{\textbf{1. League}} & \multicolumn{1}{c}{\textbf{2. League}} \\ \hline
\textbf{Average} & \tikzmarkin{d}(0.1,-0.1)(-0.1,0.27)10000000000\tikzmarkend{d} & \tikzmarkin{b}20000000000\tikzmarkend{b} & 30000000000 & 40000000000\\ \hline
\textbf{Total} & 50000000000 & 60000000000 & \tikzmarkin{c}70000000000\tikzmarkend{c} & \tikzmarkin{a}80000000000\tikzmarkend{a}\\ \hline

\end{tabular}
\end{document}

默认设置允许获取所谓的最佳外观,但也可以缩短突出显示框。这就是为什么\tikzmark采用的版本取自tikzmark 宏的问题和潜力:动态框自适应和用途可扩展标记。上面的代码给出了一个例子:

\tikzmarkin{d}(0.1,-0.1)(-0.1,0.27)10000000000\tikzmarkend{d}

其中(0.1,-0.1)代表end移位标记和移位(-0.1,0.27)标记in

解决方案是:

在此处输入图片描述

这种方法需要对文档进行两次编译,甚至可以得到类似这样的结果:

在此处输入图片描述

\begin{document} 
\setfillcolor{lightgray}
\setbordercolor{red}
\begin{tabular}{p{1.4cm}p{2.55cm}p{2.55cm}p{2.55cm}p{2.55cm}}
\hline
\multicolumn{1}{c}{} & \multicolumn{2}{c}{\textbf{Football}} & \multicolumn{2}{c}{\textbf{Basketball}} \\\hline
\multicolumn{1}{c}{} & \multicolumn{1}{c}{\textbf{1. League}} & \multicolumn{1}{c}{\textbf{2. League}} & \multicolumn{1}{c}{\textbf{1. League}} & \multicolumn{1}{c}{\textbf{2. League}} \\ \hline
\textbf{Average} & \tikzmarkin{d}(0.1,-0.1)(-0.1,0.27)10000000000 & \tikzmarkin{b}20000000000\tikzmarkend{b} & 30000000000 & 40000000000\\ \hline
\textbf{Total} & 50000000000\tikzmarkend{d} & 60000000000 & \tikzmarkin{c}70000000000\tikzmarkend{c} & \tikzmarkin{a}80000000000\tikzmarkend{a}\\ \hline
\end{tabular}
\end{document}

答案3

我无法运行 David Carlisle 的最后一个解决方案,也许是由于软件包发生了变化array(使用 v2.4l)。

我不熟练使用 TeX 来纠正 的定义highlight,所以我编写了一个新命令highlighti,它似乎可以得到相同的结果。

我还将列规格从更改为pw获得更好的数字对齐,并对行进行了拉伸以便为边缘留出更多空间。

\documentclass{article}
\usepackage{array,colortbl,xcolor}

\begin{document} 

%   \makeatletter
%   \def\highlight#1{%
%       \fboxrule2pt %
%       \hsize=\dimexpr\hsize-2\fboxrule-2\fboxsep\relax
%       #1%
%       \@endpbox\unskip\setbox0\lastbox\bgroup
%       \fboxrule2pt %
%       \fcolorbox{red}{lightgray}{\box0}\hfill}

\newcommand{\highlighti}[2]{%
    \setlength{\fboxrule}{2pt}%
    \fcolorbox{#1}{lightgray}{#2}}

    \renewcommand{\arraystretch}{1.7} %make room for borders
    
%\begin{tabular}{p{1.4cm}p{2.55cm}p{2.55cm}w{c}{2.55cm}p{2.55cm}}
\begin{tabular}{p{1.4cm}w{c}{2.55cm}w{c}{2.55cm}w{c}{2.55cm}w{c}{2.55cm}}   % changed for better alignment
    \hline
    \multicolumn{1}{c}{} & \multicolumn{2}{c}{\textbf{Football}} & \multicolumn{2}{c}{\textbf{Basketball}} \\
    \hline
    \multicolumn{1}{c}{} & \multicolumn{1}{c}{\textbf{1. League}} & \multicolumn{1}{c}{\textbf{2. League}} & \multicolumn{1}{c}{\textbf{1. League}} & \multicolumn{1}{c}{\textbf{2. League}} \\
     \hline
    \textbf{Average} & 10000000000 & \highlighti{white}{20000000000} & 30000000000 & \highlighti{white}{40000000000}\\ 
    \hline
    \textbf{Total} & 50000000000 & 60000000000 &
    \highlighti{red}{70000000000}
     & \highlighti{white}{80000000000}\\
     \hline
\end{tabular}

\end{document}

在此处输入图片描述

答案4

您可以使用 来实现这{NiceTabular}一点nicematrix

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

\begin{document} 

\begin{NiceTabular}{p{1.4cm}p{2.55cm}p{2.55cm}p{2.55cm}p{2.55cm}}[hlines]
& \Block{1-2}{\textbf{Football}} && \Block{1-2}{\textbf{Basketball}} \\
& \Block{}{\textbf{1. League}} & \Block{}{\textbf{2. League}} & \Block{}{\textbf{1. League}} & \Block{}{\textbf{2. League}} \\ 
\textbf{Average} & 10000000000 &\Block[l,fill=lightgray,draw=red,line-width=1.2pt]{}{20000000000} & 30000000000 & 40000000000\\ 
\textbf{Total} & 50000000000 & 60000000000 & 70000000000 &\Block[l,fill=lightgray,draw=red,line-width=1.2pt]{}{80000000000}\\ 
\end{NiceTabular}
\end{document}

第一个代码的输出

如果希望红线完全位于单元格内,也是可能的(≥ 6.24):

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

\begin{document} 

\newcommand{\MyBlock}{\Block[l,tikz={offset=0.6pt,fill=lightgray,draw=red,line width=1.2pt}]}

\begin{NiceTabular}{p{1.4cm}p{2.55cm}p{2.55cm}p{2.55cm}p{2.55cm}}[hlines,color-inside]
& \Block{1-2}{\textbf{Football}} && \Block{1-2}{\textbf{Basketball}} \\
& \Block{}{\textbf{1. League}} & \Block{}{\textbf{2. League}} & \Block{}{\textbf{1. League}} & \Block{}{\textbf{2. League}} \\ 
\textbf{Average} & 10000000000 & \MyBlock{}{20000000000} & 30000000000 & 40000000000\\ 
\textbf{Total} & 50000000000 & 60000000000 & 70000000000 &\MyBlock{}{80000000000}\\ 
\end{NiceTabular}
\end{document}

第二段代码的输出

相关内容