Indentation issues with highlighting cell using tikzmarkin

I am trying to eliminate offset/indentation issues when using tikzmarkin to highlight regions of a table.

\documentclass{beamer}

\RequirePackage{booktabs,colortbl,caption,tabularx,chngcntr, tikz, comment, subfig,xcolor}

\usepackage{lmodern}
\usepackage[beamer,customcolors]{hf-tikz}

\tikzset{hl/.style={
set fill color=red!80!black!40,
set border color=red!80!black,
},
}

\begin{document}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}
\frametitle{Correlations}
\vspace{-2em}
\begin{tabular*}{1.00\textwidth}{@{\hskip\tabcolsep\extracolsep\fill}l*{6}r}
& $\alpha$ & $p_L$ & $\kappa$ & $\sigma$ & $\delta$ & $\lambda$ \\
[1.5pt]  \hline
\tikzmarkin<3>[hl]{c1}$\alpha$  \tikzmarkend{c1}                                & \tikzmarkin<1>[hl]{a1} -0.08 \tikzmarkend{a1}   & 0.05    & -0.18    & 0.18     & -0.06    & -0.18       \\ [2pt]
$~p_L$                                   & -0.07    & 0.03    & -0.08    & 0.02     & 0.09     & -0.02       \\ [2pt]
$\varepsilon$                             & -0.31    & 0.05    & -0.33    & \tikzmarkin<2>[hl]{b1} 0.59     & 0.04     & -0.30       \\ [2pt]
$\mu$                                     & -0.33    & 0.10    & -0.39    & 0.57 \tikzmarkend{b1}     & 0.07     & -0.40       \\ [2pt]
$\frac{\mu}{\varepsilon}$               & 0.23     & -0.06   & 0.47     & -0.39    & 0.24     & 0.49        \\ [2pt]
\end{tabular*}
\end{frame}
\end{document}


For instance, the contents of the first highlighted cell (-0.08) is shifted to the left. Similarly, the bottom cell contents of the second highlighted cells are shifted left. When elements in the first column are highlighted, the contents are shifted right however. Is there a fix for this using the options of tikz? Thanks.

Remove the space before and after the cell content you want to highlight i.e:

Change this:

& \tikzmarkin<1>[hl]{a1} -0.08 \tikzmarkend{a1} &

to this:

&\tikzmarkin<1>[hl]{a1}-0.08\tikzmarkend{a1}&

Complete MWE of the solution:

\documentclass{beamer}

\RequirePackage{booktabs,colortbl,caption,tabularx,chngcntr, tikz, comment, subfig,xcolor}

\usepackage{lmodern}
\usepackage[beamer,customcolors]{hf-tikz}

\tikzset{hl/.style={
set fill color=red!80!black!40,
set border color=red!80!black,
},
}

\begin{document}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}
\frametitle{Correlations}
\vspace{-2em}
\begin{tabular*}{1.00\textwidth}{@{\hskip\tabcolsep\extracolsep\fill}l*{6}r}
& $\alpha$ & $p_L$ & $\kappa$ & $\sigma$ & $\delta$ & $\lambda$ \\[1.5pt] \hline
\tikzmarkin<3>[hl]{c1}$\alpha$\tikzmarkend{c1}& \tikzmarkin<1>[hl]{a1}-0.08\tikzmarkend{a1} & 0.05 & -0.18 & 0.18 & -0.06 & -0.18 \\[2pt]
$~p_L$ & -0.07 & 0.03 & -0.08 & 0.02  & 0.09  & -0.02 \\ [2pt]
$\varepsilon$ & -0.31 & 0.05  & -0.33 & \tikzmarkin<2>[hl]{b1}0.59 & 0.04 & -0.30 \\[2pt]
$\mu$ & -0.33 & 0.10 & -0.39 & 0.57\tikzmarkend{b1} & 0.07 & -0.40 \\[2pt]
$\frac{\mu}{\varepsilon}$ & 0.23  & -0.06 & 0.47  & -0.39 & 0.24 & 0.49 \\[2pt]
\end{tabular*}
\end{frame}
\end{document}


Since, at least implicitly, you are using tikz I suggest using a matrix of (math) nodes -- see section 57.1 of the tikz manual.

This way you input your matrix inside a \matrix {...} command in the usual way and then apply any tikz formatting that you want "on top" of the table. To get this to work you need to add a fragile command to the beamer and to highlight an entry in the matrix you add |[hl]| to the corresponding cell. This results in:

Here is the full code:

\documentclass{beamer}

\RequirePackage{booktabs,colortbl,caption,tabularx,chngcntr, tikz, comment, subfig,xcolor}

\usetikzlibrary{matrix}
\usepackage{lmodern}
\usepackage[beamer,customcolors]{hf-tikz}

\tikzset{hl/.style={
fill=red!80!black!40,
draw=red!80!black,
rounded corners
}
}

\begin{document}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{Correlations}
\begin{tikzpicture}
\matrix (M)[matrix of math nodes,nodes in empty cells,
row sep=2mm,column sep=2mm,
nodes={anchor=east, minimum width=2em, minimum height=2em}
]{
& \alpha&  p_L & \kappa & \sigma & \delta  & \lambda \\
\alpha     & |[hl]|-0.08 & 0.05 & -0.18  & 0.18   & -0.06   & -0.18   \\
~p_L      & -0.07 & 0.03 & -0.08  & 0.02   & 0.09    & -0.02   \\
\varepsilon& -0.31 & 0.05 & -0.33  & 0.59   & 0.04    & -0.30   \\
\mu        & -0.33 & 0.10 & -0.39  & 0.57   &|[hl]| 0.07    & -0.40   \\
\frac{\mu}{\varepsilon}& 0.23&-0.06& 0.47&-0.39& 0.24  & 0.49  \\
};
\draw[thick,blue](M-1-7.south east)--(M-1-1.south west);
\end{tikzpicture}
\end{frame}

\end{document}


A few remarks:

• I have arbitrarily set row sep=2mm,column sep=2mm for the row and column spacing. Change to suit your tastes
• The nodes in empty cells is only necessary because there is no (1,1) entry in the matrix and I use this entry to draw the blue \hrule under the table header
• As I have used a matrix of math nodes every entry is typeset in math-mode. There is a similar matrix of nodes where the entries are not in math-mode
• the anchor=east in the nodes={...} specification aligns every column to the "east"
• rather than using \frametitle{...} I prefer adding the frame title as an argument to the frame environment
• you might want to add something like inner sep=1mm to the hl style
• Thanks a lot for your answer! It works perfectly fine and the additional remarks are good advices (even if I disagree with the second-to-last point :) It is hard to decide whom to award the bounty, both answers are good, just different. Would you be terrible devastated if I would give the bounty to @OleAnders because he has less reputation points? – user36296 Nov 12 '18 at 15:17
• @samcarter No, not at all: they are your points to award as you think best:) – Andrew Nov 12 '18 at 20:03