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:
This approach requires the document to be compiled twice and even allows to get something like:
\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}