# How to do nested highlighting of part of a row in an array?

I'm trying to highlight part of a row in an array, where some parts are "double" highlighted so it appears darker.

From a previous question, I tried the following:

\usepackage{tikz}
\usetikzlibrary{fit}
\tikzset{%
highlight/.style={rectangle,rounded corners,fill=blue!15,draw,fill opacity=0.5,
inner sep=0pt}
}
\newcommand{\tikzmark}[2]{\tikz[overlay,remember picture,baseline=(#1.base)]
\node (#1) {#2};}
%
\newcommand{\Highlight}[1][submatrix]{%
\tikz[overlay,remember picture]{
\node[highlight,fit=(left.north west) (right.south east)] (#1) {};}
}
\begin{document}
$\begin{array}{ *{5}{c} } a &0 &0 &0 &0 \\ b & c &0 &0 &0 \\ d & e &f &0 &0 \\ \tikzmark{left}{\tikzmark{left}{\tikzmark{left}{\tikzmark{right}{$g$}}\Highlight[first]}} & \tikzmark{right}{$h$} \Highlight[second]&\tikzmark{right}{$i$} \Highlight[third]&j&0 \\ k & l & m & n &o \end{array}$
\end{document}


The goal here is to have g highlighted 3 times, h highlighted 2 times, and i highlighted 1 time, with all the highlights overlapping and carrying across columns. What I end up with is an array that looks like this:

Obviously this has lots of issues, including a large gap before the last row and that the highlights don't center around the values. Also, if it is easier to use a matrix or something similar, it doesn't need to be an array.

Your solution will not quite work because the nodes with the same names will overwrite each other. Also your construction looks very complicated. This is a little easier:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{fit}
\tikzset{%
highlight/.style={
rectangle,
rounded corners,
fill=blue!15,
draw,fill opacity=0.5,
inner sep=0pt,
}
}
\newcommand{\tikzmark}[2]{%
\tikz[overlay,remember picture,baseline=(#1.base)]\node (#1) {#2};%
}
%
\newcommand{\Highlight}[1]{%
\tikz[overlay,remember picture] \node[highlight,fit=#1] {};%
}
\begin{document}

$\begin{array}{ *{5}{c} } a & 0 & 0 & 0 & 0 \\ b & c & 0 & 0 & 0 \\ d & e & f & 0 & 0 \\ \tikzmark{left}{g} & \tikzmark{first}{h} & \tikzmark{second}{i} & j & 0 \\ k & l & m & n & o \\ \end{array} \Highlight{(left)} \Highlight{(left)(first)} \Highlight{(left)(second)}$
\end{document}


Or much better, use a TikZ matrix and put the highlight in the background.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{backgrounds,matrix}
\begin{document}

$\begin{tikzpicture}[ highlight/.style={ rounded corners, fill=blue!15, fill opacity=0.5, }] \matrix[matrix of math nodes,execute at begin cell=\mathstrut] (m) { a & 0 & 0 & 0 & 0 \\ b & c & 0 & 0 & 0 \\ d & e & f & 0 & 0 \\ g & h & i & j & 0 \\ k & l & m & n & o \\ }; \begin{scope}[on background layer] \filldraw[highlight] (m-4-1.north west) rectangle (m-4-1.south east); \filldraw[highlight] (m-4-1.north west) rectangle (m-4-2.south east); \filldraw[highlight] (m-4-1.north west) rectangle (m-4-3.south east); \end{scope} \end{tikzpicture}$

\end{document}


• Thanks so much. This is exactly what I was looking for. Do you know how to change the highlighting so the black border around it is gone? Mar 28 '17 at 3:58
• Add draw=none to the highlight style. Mar 28 '17 at 4:27