11

I'd like to highlight certain cells of my table. In particular, I'd like to set the border of these sells colored and thick. These cells should also have a background color.

Here's a minimal example of my table:

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

I tried to set the border of specific cells using a combination of multicolumn and hhline but failed miserably :-( (One of the problems was that the cell background partially covers the border).

I also tried tikz but failed to paint exactly on the cell's border, especially when a linebreak occurs.

Here's how it should look optimally:

enter image description here

Can someone please help me to get this working?

12

enter image description here

I have prevented the cell from becoming wider due to the border, but allowed it to grow vertically. If that isn't desired a bit more negative spacing is needed.

rather than use colortbl it is easier to grab the cell contents in a box (since you are doing that anyway for p columns) and using \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}
  • Thank you so much for this nice solution! I still have a minor request for improvement: Is it possible, that the red box takes the whole space of the cell, i.e. that it has contact with the left and right cell? In the current solution there is a small white space. – Dave Martin Aug 15 '12 at 21:13
  • I think you are just seeing the tabcolsep space between columns in a tabular if you suppress that with p{2.55cm}@{}p{2.55cm} where the @{} suppresses all inter-column material, two adjacent \highlight entries touch when I try it. – David Carlisle Aug 15 '12 at 21:25
  • How would you vertically fit the bordered fbox exactly in the cell? In your example, the bordered cells are higher than regular cells. – Jasper Habicht Feb 21 '18 at 23:35
4

I provide a solution with TikZ and the now famous \tikzmark macro.

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

The default setting allows to get the so called optimal look, but it is also possible to shorten the highlighting box. This is why the \tikzmark version adopted is taken from Issues and potentiality of the tikzmark macro: dynamic box adaptation and uses extendible markers. An example is given in the above code:

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

where (0.1,-0.1) represent the end shift marker and (-0.1,0.27) the in shift marker.

The solution leads to:

enter image description here

This approach requires the document to be compiled twice and even allows to get something like:

enter image description here

\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}
  • Looks complicated for beginners, but works like a charm. Especially the boxes across multiple cells are neat. – Klaster Sep 22 '15 at 14:25
  • @Klaster: Glad it helps. Yes, I think this solution can give a lot of flexibility. – Claudio Fiandrino Sep 22 '15 at 14:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.