# Array cell highlighting tikz

I'm beginning to write up a PhD thesis and am pretty new to LaTeX. I'm trying to explain a numerical method I'm using and in doing this I wish to highlight sections of arrays to aid explanation. The entries in the arrays I am highlighting vary in size i.e. they could be integers or longer terms of several characters in length.

The first option I have found is using the \cellcolor command which gives a block color over the array element. I found this doesn't give the best presentation however (I know this sounds fussy but I'm sure a better presented thesis comes across better). Anyway, the second option I found was through some examples found on here mainly by defining nodes and creating a rectangle with customisable options etc. The examples look nice and I'd like to use something similar for my work but, and here it comes, all of the examples work well for simple arrays, i.e. arrays full of integers, but not so well for the arrays I am using.

You will notice from the code I have uploaded that the rectangle fitting fits directly to the text within the array cell, this causes the two highlighted regions to be misaligned due to the node placement/fitting of each highlight. The \cellcolor command however, fits around the entire 'cell' not the text within the cell.

I want to highlight a boxed region around the array cells not the text.

This will allow any sized array cells to be highlighted without the need for complicated fitting around odd sized array cells.

Finally, I am very new to LaTeX and even 'newer' to drawing in LaTeX. I apologise if I have not explained properly etc, please let me know and I will do my best to edit my response/question.

\documentclass[a4paper,10pt,english,table]{article}

%Document normal packages
\usepackage{hyperref}
\usepackage{amsmath,amssymb}
\usepackage{graphicx}
\usepackage{subfigure}
\usepackage{epstopdf}
\usepackage[parfill]{parskip}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{babel}
\usepackage{xcolor}

% Tikz drawing settings====================
\usepackage{tikz}
\usetikzlibrary{calc,fit}
\tikzset{%
highlight1/.style={rectangle,rounded corners,color=red!,fill=red!15,draw,fill opacity=0.5,thick,inner sep=0pt}
}
\tikzset{%
highlight2/.style={rectangle,rounded corners,color=green!,fill=green!15,draw,fill opacity=0.5,thick,inner sep=0pt}
}
\newcommand{\tikzmark}[2]{\tikz[overlay,remember picture,baseline=(#1.base)] \node (#1) {#2};}
%
\newcommand{\RHighlight}[1][submatrix]{%
\tikz[overlay,remember picture]{
\node[highlight1,fit=(left.north west) (right.south east)] (#1) {};}
}
\newcommand{\GHighlight}[1][submatrix]{%
\tikz[overlay,remember picture]{
\node[highlight2,fit=(left.north west) (right.south east)] (#1) {};}
}
% Tikz drawing settings====================

\begin{document}

Using 'cellcolor' command, note the array cells are highlighted and the two highlighted regions are nicely aligned.

$$\renewcommand{\arraystretch}{1.5} A_{L}=\left[\begin{array}{cccc} \cellcolor{green}-T^{1}_{11} & \cellcolor{green}0 & 0 & -T^{1}_{12} \\ \cellcolor{green}-T^{2}_{12} & \cellcolor{green}-T^{2}_{11} & 0 & 0 \\ \cellcolor{red}0 & \cellcolor{red}-T^{3}_{12} & \cellcolor{red}T^{3}_{11} & \cellcolor{red}0 \\ \cellcolor{red}0 & \cellcolor{red}0 & \cellcolor{red}0 & \cellcolor{red}0 \\ \end{array}\right] \left[\begin{array}{c} \phi_{A} \\ \phi_{B} \\ \phi_{C} \\ \phi_{D} \end{array}\right] \label{eq:ALphif}$$

Using an example taken from the website, note the mis-alignment and errors of highlighted regions crossing the brackets

$$\renewcommand{\arraystretch}{1.5} A_{L}=\left[\begin{array}{cccc} \tikzmark{left}{-T^{1}_{11}} & 0 & 0 & -T^{1}_{12} \\ -T^{2}_{12} & \tikzmark{right}{-T^{2}_{11}} & 0 & 0 \GHighlight \\ \tikzmark{left}{0} & -T^{3}_{12} & T^{3}_{11} & 0 \\ 0 & 0 & 0 & \tikzmark{right}{0} \RHighlight\qquad \\ \end{array}\right] \left[\begin{array}{c} \phi_{A} \\ \phi_{B} \\ \phi_{C} \\ \phi_{D} \end{array}\right] \label{eq:ALphif}$$

$$\renewcommand{\arraystretch}{1.5} B_{L}= \left[\begin{array}{ccc} \tikzmark{left}{(T^{1}_{11}+T^{1}_{12})} & 0 & 0 \\ 0 & (T^{2}_{11}+T^{2}_{12}) & \tikzmark{right}{0} \GHighlight \\ \tikzmark{left}{0} & 0 & (T^{3}_{11}+T^{3}_{12}) \\ 0 & 0 & \tikzmark{right}{0} \RHighlight\qquad \\ \end{array}\right] \left[\begin{array}{c} \phi_{1} \\ \phi_{2} \\ \phi_{3} \end{array}\right] \label{eq:BLphii}$$

$$\renewcommand{\arraystretch}{1.5} A_{R}= \left[\begin{array}{cccc} \tikzmark{left}{T^{2}_{22}} & T^{2}_{21} & 0 & 0 \\ 0 & \tikzmark{right}{T^{3}_{22}} & T^{3}_{21} & 0 \GHighlight \\ \tikzmark{left}{0} & 0 & 0 & 0 \\ T^{1}_{21} & 0 & 0 & \tikzmark{right}{T^{1}_{22}} \RHighlight\qquad \\ \end{array}\right] \left[\begin{array}{c} \phi_{A} \\ \phi_{B} \\ \phi_{C} \\ \phi_{D} \end{array}\right] \label{eq:ARphif}$$

$$\renewcommand{\arraystretch}{1.5} B_{R}= \left[\begin{array}{ccc} \tikzmark{left}{0} & -(T^{2}_{21}+T^{2}_{22}) & 0 \\ 0 & 0 & \tikzmark{right}{-(T^{3}_{21}+T^{3}_{22})} \GHighlight \\ \tikzmark{left}{0} & 0 & 0 \\ -(T^{1}_{21}+T^{1}_{22}) & 0 & \tikzmark{right}{0} \RHighlight\qquad \\ \end{array}\right] \left[\begin{array}{c} \phi_{1} \\ \phi_{2} \\ \phi_{3} \end{array}\right] \label{eq:BRphii}$$
\end{document}


-
Welcome to TeX.sx! –  lockstep Nov 19 '12 at 12:06

This is the best I can think: use a tikz matrix to create a matrix of math nodes (which you can include inside a math environment and delimit with brackets if you want), and then use the implicit naming of nodes to refer to individual cells of the matrix, as for example: m-1-1.north east to refer to the north east corner of the first element.

In order to avoid alignment problems, you have to ensure that all the nodes of that matrix have the same dimensions, by giving a minimum width and minimum height option. I'm not very satisfied with this solution, because it requires you to know the dimensions of the larger cell. However, appropiate values are not difficult to find by trial and error.

After some tries, my code is the following:

\documentclass{article}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{inputenc}
\usepackage{xcolor}
\usepackage{tikz}
\begin{document}
\thispagestyle{empty}
\usetikzlibrary{matrix}
\usetikzlibrary{calc,fit}
\tikzset{%
highlight1/.style={rectangle,rounded corners,color=red!,fill=red!15,draw,fill opacity=0.5,thick,inner sep=0pt}
}
\tikzset{%
highlight2/.style={rectangle,rounded corners,color=green!,fill=green!15,draw,fill opacity=0.5,thick,inner sep=0pt}
}
$$\renewcommand{\arraystretch}{1.5} A_{L}= \begin{tikzpicture}[baseline=(m.center)] \matrix (m) [matrix of math nodes, left delimiter={[}, right delimiter={]}, row sep=1mm, nodes={minimum width=3em, minimum height=1.6em}] { -T^{1}_{11} & 0 & 0 & -T^{1}_{12} \\ -T^{2}_{12} & -T^{2}_{11} & 0 & 0 \\ 0 & -T^{3}_{12} & |(r)| T^{3}_{11} & 0 \\ 0 & 0 & 0 & 0 \\ }; \node[highlight2, fit=(m-1-1.north west) (m-2-2.south east)] {}; \node[highlight1, fit=(m-3-1.north west) (m-4-4.south east)] {}; \end{tikzpicture} \left[\begin{array}{c} \phi_{A} \\ \phi_{B} \\ \phi_{C} \\ \phi_{D} \end{array}\right] \label{eq:ALphif}$$

$$\renewcommand{\arraystretch}{1.5} B_{L}= \begin{tikzpicture}[baseline=(m.center)] \matrix (m) [matrix of math nodes, left delimiter={[}, right delimiter={]}, row sep=1mm, nodes={minimum width=5.5em, minimum height=1.6em}] { (T^{1}_{11}+T^{1}_{12}) & 0 & 0 \\ 0 & (T^{2}_{11}+T^{2}_{12}) & 0 \\ 0 & 0 & (T^{3}_{11}+T^{3}_{12}) \\ 0 & 0 & 0 \\ }; \node[highlight2, fit=(m-1-1.north west) (m-2-2.south east)] {}; \node[highlight1, fit=(m-3-1.north west) (m-4-3.south east)] {}; \end{tikzpicture} \left[\begin{array}{c} \phi_{1} \\ \phi_{2} \\ \phi_{3} \end{array}\right] \label{eq:BLphii}$$

$$\renewcommand{\arraystretch}{1.5} A_{R}= \begin{tikzpicture}[baseline=(m.center)] \matrix (m) [matrix of math nodes, left delimiter={[}, right delimiter={]}, row sep=1mm, nodes={minimum width=2.5em, minimum height=1.6em}] { T^{2}_{22} & T^{2}_{21} & 0 & 0 \\ 0 & T^{3}_{22} & T^{3}_{21} & 0 \\ 0 & 0 & 0 & 0 \\ T^{1}_{21} & 0 & 0 & T^{1}_{22}\\ }; \node[highlight2, fit=(m-1-1.north west) (m-2-2.south east)] {}; \node[highlight1, fit=(m-3-1.north west) (m-4-4.south east)] {}; \end{tikzpicture} \left[\begin{array}{c} \phi_{A} \\ \phi_{B} \\ \phi_{C} \\ \phi_{D} \end{array}\right] \label{eq:ARphif}$$

$$\renewcommand{\arraystretch}{1.5} B_{R}= \begin{tikzpicture}[baseline=(m.center)] \matrix (m) [matrix of math nodes, left delimiter={[}, right delimiter={]}, row sep=1mm, nodes={minimum width=6.5em, minimum height=1.6em}] { 0 & -(T^{2}_{21}+T^{2}_{22}) & 0 \\ 0 & 0 & -(T^{3}_{21}+T^{3}_{22})\\ 0 & 0 & 0 \\ -(T^{1}_{21}+T^{1}_{22}) & 0 & 0 \\ }; \node[highlight2, fit=(m-1-1.north west) (m-2-3.south east)] {}; \node[highlight1, fit=(m-3-1.north west) (m-4-3.south east)] {}; \end{tikzpicture} \left[\begin{array}{c} \phi_{1} \\ \phi_{2} \\ \phi_{3} \end{array}\right] \label{eq:BRphii}$$
\end{document}


Which produces the following output:

-

Another solution could be the use of the hf-tikz package: it is based on the \tikzmark macro, but with extensible markers, that is the extreme of the area to be highlighted could be defined by the user and, thus, extended. That solution has been used in Issues and potentiality of the tikzmark macro: dynamic box adaptation. The advantages of this approach is that you don't have to rely on internal TikZ matrix, so you don't need to change the code, but simply plug the marker and then extend it. The extension, to be honest, could be seen and a cons of the approach: find the right coordinates perhaps is not immediate, so you should proceed doing some trials, but after all, when one is familiar just two or three trials are necessary. Notice also, in the second example, that once the right values have been determined they could be repeated (see the height as reference).

The code:

\documentclass[a4paper,10pt,english,table]{article}

%Document normal packages
\usepackage{amsmath,amssymb}
\usepackage{graphicx}
\usepackage{subfigure}
\usepackage{epstopdf}
\usepackage[parfill]{parskip}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{babel}
\usepackage{xcolor}

% Tikz drawing settings====================
\usepackage[customcolors]{hf-tikz}
\newcommand{\usegreen}{
\hfsetbordercolor{green!50!black}
\hfsetfillcolor{green!20}
}
\newcommand{\usered}{
\hfsetbordercolor{red!50!black}
\hfsetfillcolor{red!20}
}
% Tikz drawing settings====================

\usepackage{hyperref} % hyperref should be loaded at end preamble

\begin{document}

Using 'cellcolor' command, note the array cells are highlighted and the two highlighted regions are nicely aligned.

$$\renewcommand{\arraystretch}{1.5} A_{L}=\left[\begin{array}{cccc} \cellcolor{green}-T^{1}_{11} & \cellcolor{green}0 & 0 & -T^{1}_{12} \\ \cellcolor{green}-T^{2}_{12} & \cellcolor{green}-T^{2}_{11} & 0 & 0 \\ \cellcolor{red}0 & \cellcolor{red}-T^{3}_{12} & \cellcolor{red}T^{3}_{11} & \cellcolor{red}0 \\ \cellcolor{red}0 & \cellcolor{red}0 & \cellcolor{red}0 & \cellcolor{red}0 \\ \end{array}\right] \left[\begin{array}{c} \phi_{A} \\ \phi_{B} \\ \phi_{C} \\ \phi_{D} \end{array}\right] \label{eq:ALphif}$$

Using an example taken from the website, note the mis-alignment and errors of highlighted regions crossing the brackets

$$\renewcommand{\arraystretch}{1.5} A_{L}=\left[\begin{array}{cccc} \usegreen\tikzmarkin{a}-T^{1}_{11} & 0 & 0 & -T^{1}_{12} \\ -T^{2}_{12} & -T^{2}_{11}\tikzmarkend{a} & 0 & 0 \\ 0 & \usered\tikzmarkin{b}-T^{3}_{12} & T^{3}_{11} & 0 \\ 0 & 0 & 0 & 0 \tikzmarkend{b}\\ \end{array}\right] \left[\begin{array}{c} \phi_{A} \\ \phi_{B} \\ \phi_{C} \\ \phi_{D} \end{array}\right] \label{eq:ALphif}$$

$$\renewcommand{\arraystretch}{1.5} B_{L}= \left[\begin{array}{ccc} \usegreen\tikzmarkin{c} (T^{1}_{11}+T^{1}_{12}) & 0 & 0 \\ 0 & (T^{2}_{11}+T^{2}_{12})\tikzmarkend{c} & 0 \\ \usered\tikzmarkin{d}(0.85,-0.2)(-0.1,0.35) 0 & 0 & (T^{3}_{11}+T^{3}_{12}) \\ 0 & 0 & 0\tikzmarkend{d} \\ \end{array}\right] \left[\begin{array}{c} \phi_{1} \\ \phi_{2} \\ \phi_{3} \end{array}\right] \label{eq:BLphii}$$

$$\renewcommand{\arraystretch}{1.5} A_{R}= \left[\begin{array}{cccc} \usegreen\tikzmarkin{e} T^{2}_{22} & T^{2}_{21} & 0 & 0 \\ 0 & T^{3}_{22}\tikzmarkend{e} & T^{3}_{21} & 0 \\ \usered\tikzmarkin{f}(0.1,-0.2)(-0.25,0.35) 0 & 0 & 0 & 0 \\ T^{1}_{21} & 0 & 0 & T^{1}_{22} \tikzmarkend{f}\\ \end{array}\right] \left[\begin{array}{c} \phi_{A} \\ \phi_{B} \\ \phi_{C} \\ \phi_{D} \end{array}\right] \label{eq:ARphif}$$

$$\renewcommand{\arraystretch}{1.5} B_{R}= \left[\begin{array}{ccc} \usegreen\tikzmarkin{g} 0 & -(T^{2}_{21}+T^{2}_{22}) & 0 \\ 0 & 0 & -(T^{3}_{21}+T^{3}_{22}) \tikzmarkend{g} \\ \usered\tikzmarkin{h}(0.15,-0.2)(-0.95,0.35) 0 & 0 & 0 \\ -(T^{1}_{21}+T^{1}_{22}) & 0 & 0 \tikzmarkend{h} \\ \end{array}\right] \left[\begin{array}{c} \phi_{1} \\ \phi_{2} \\ \phi_{3} \end{array}\right] \label{eq:BRphii}$$
\end{document}


The result:

Of course, you could customize the highlighting to reproduce exactly the result of JLDiaz: just extend the areas as you need. Another example:

\documentclass[a4paper,10pt,english,table]{article}

%Document normal packages
\usepackage{amsmath,amssymb}
\usepackage{graphicx}
\usepackage{subfigure}
\usepackage{epstopdf}
\usepackage[parfill]{parskip}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{babel}
\usepackage{xcolor}

% Tikz drawing settings====================
\usepackage[customcolors]{hf-tikz}

% command to typeset the highlighted area in green
\newcommand{\usegreen}{
\hfsetbordercolor{green!75!black}
\hfsetfillcolor{green!10}
}
% command to typeset the highlighted area in red
\newcommand{\usered}{
\hfsetbordercolor{red!75!black}
\hfsetfillcolor{red!10}
}
% Tikz drawing settings====================

\usepackage{hyperref}
\begin{document}

Using 'cellcolor' command, note the array cells are highlighted and the two highlighted regions are nicely aligned.

$$\renewcommand{\arraystretch}{1.5} A_{L}=\left[\begin{array}{cccc} \cellcolor{green}-T^{1}_{11} & \cellcolor{green}0 & 0 & -T^{1}_{12} \\ \cellcolor{green}-T^{2}_{12} & \cellcolor{green}-T^{2}_{11} & 0 & 0 \\ \cellcolor{red}0 & \cellcolor{red}-T^{3}_{12} & \cellcolor{red}T^{3}_{11} & \cellcolor{red}0 \\ \cellcolor{red}0 & \cellcolor{red}0 & \cellcolor{red}0 & \cellcolor{red}0 \\ \end{array}\right] \left[\begin{array}{c} \phi_{A} \\ \phi_{B} \\ \phi_{C} \\ \phi_{D} \end{array}\right] \label{eq:ALphifcellcol}$$

Using the \textsf{hf-tikz} package the mis-alignment and errors of highlighted regions crossing the brackets could be avoided by properly selecting/extended the highlighted area.

$$\renewcommand{\arraystretch}{1.5} A_{L}=\left[\begin{array}{cccc} \usegreen\tikzmarkin{a}-T^{1}_{11} & 0 & 0 & -T^{1}_{12} \\ -T^{2}_{12} & -T^{2}_{11}\tikzmarkend{a} & 0 & 0 \\ 0 & \usered\tikzmarkin{b}-T^{3}_{12} & T^{3}_{11} & 0 \\ 0 & 0 & 0 & 0 \tikzmarkend{b}\\ \end{array}\right] \left[\begin{array}{c} \phi_{A} \\ \phi_{B} \\ \phi_{C} \\ \phi_{D} \end{array}\right] \label{eq:ALphif}$$

$$\renewcommand{\arraystretch}{1.5} B_{L}= \left[\begin{array}{ccc} \usegreen\tikzmarkin{c} (T^{1}_{11}+T^{1}_{12}) & 0 & 0 \\ 0 & (T^{2}_{11}+T^{2}_{12})\tikzmarkend{c} & 0 \\ \usered\tikzmarkin{d}(0.85,-0.2)(-0.85,0.375) 0 & 0 & (T^{3}_{11}+T^{3}_{12}) \\ 0 & 0 & 0\tikzmarkend{d} \\ \end{array}\right] \left[\begin{array}{c} \phi_{1} \\ \phi_{2} \\ \phi_{3} \end{array}\right] \label{eq:BLphii}$$

$$\renewcommand{\arraystretch}{1.5} A_{R}= \left[\begin{array}{cccc} \usegreen\tikzmarkin{e} T^{2}_{22} & T^{2}_{21} & 0 & 0 \\ 0 & T^{3}_{22}\tikzmarkend{e} & T^{3}_{21} & 0 \\ \usered\tikzmarkin{f}(0.1,-0.2)(-0.25,0.375) 0 & 0 & 0 & 0 \\ T^{1}_{21} & 0 & 0 & T^{1}_{22} \tikzmarkend{f}\\ \end{array}\right] \left[\begin{array}{c} \phi_{A} \\ \phi_{B} \\ \phi_{C} \\ \phi_{D} \end{array}\right] \label{eq:ARphif}$$

$$\renewcommand{\arraystretch}{1.5} B_{R}= \left[\begin{array}{ccc} \usegreen\tikzmarkin{g}(0.15,-0.2)(-0.95,0.375) 0 & -(T^{2}_{21}+T^{2}_{22}) & 0 \\ 0 & 0 & -(T^{3}_{21}+T^{3}_{22}) \tikzmarkend{g} \\ \usered\tikzmarkin{h}(1.05,-0.2)(-0.95,0.375) 0 & 0 & 0 \\ -(T^{1}_{21}+T^{1}_{22}) & 0 & 0 \tikzmarkend{h} \\ \end{array}\right] \left[\begin{array}{c} \phi_{1} \\ \phi_{2} \\ \phi_{3} \end{array}\right] \label{eq:BRphii}$$
\end{document}


The result:

-