# How to highlight elements of a matrix in a formula? [duplicate]

I currently have problems with hihglighting elements inside a matrix. I've red a lot of questions here, my problem is that I have a matrix formula, {N}=[AB]{E} where I would like to highlight parts inside the [AB] matrix. I've uploaded a picture to give you an idea. My code so far:

\documentclass[11pt,twoside]{report}
\usepackage{amsmath} %helps with mathematics
\usepackage{amssymb} %helps with mathematical symbols
\numberwithin{equation}{section} %numbers the equation like (2.1)
\begin{document}
$$\begin{Bmatrix} N_x \\ N_y \\ N_{xy} \\ M_x \\ M_y \\ M_{xy} \end{Bmatrix} = \begin{bmatrix} A_{11} & A_{12} & A_{16} & B_{11} & B_{12} & B_{16} \\ A_{21} & A_{22} & A_{26} & B_{21} & B_{22} & B_{26} \\ A_{61} & A_{62} & A_{66} & B_{61} & B_{62} & B_{66} \\ B_{11} & B_{12} & B_{16} & D_{11} & D_{12} & D_{16} \\ B_{21} & B_{22} & B_{26} & D_{21} & D_{22} & D_{26} \\ B_{61} & B_{62} & B_{66} & D_{61} & D_{62} & D_{66} \\ \end{bmatrix} \begin{Bmatrix} \epsilon_x \\ \epsilon_y \\ \epsilon_{xy} \\ \kappa_x \\ \kappa_y \\ \kappa_{xy} \end{Bmatrix}$$
\end{document}


If I try to work with tikzpictures I face the following problems: - not able to arrange the matrices as meant. They will be placed below eachother - with tikzpictures the equation number will be not aligned half the height of the equation Preferably I would like to have a filled area behind the to be highlighted part of the matrix.

Can someone help me?

I will propose two solutions. The first one highlights the given elements by simply change the font colour, whilst the second will highlight elements by changing the background colour.

## Font Colour

This is probably the easiest. You can just use the xcolor to change the colour of the font. Due to the way matrices are constructed, the colour only applies to that particle element.

Because quite a few elements are being highlighted, I defined shortcuts \r and \b for \color{red} and \color{blue} respectively. Since I do not want to override these functions globally, I define them right after and the original \r and \b functions will be restored after

\documentclass[11pt,twoside]{report}
\usepackage{amsmath} %helps with mathematics
\usepackage{amssymb} %helps with mathematical symbols
\numberwithin{equation}{section} %numbers the equation like (2.1)
\usepackage{xcolor}
\begin{document}
$$\def\r{\color{red}}\def\b{\color{blue}} \begin{Bmatrix} N_x \\ N_y \\ N_{xy} \\ M_x \\ M_y \\ M_{xy} \end{Bmatrix} = \begin{bmatrix} A_{11} & A_{12} & \r A_{16} & \b B_{11} & \b B_{12} & \b B_{16} \\ A_{21} & A_{22} & \r A_{26} & \b B_{21} & \b B_{22} & \b B_{26} \\ \r A_{61} & \r A_{62} & A_{66} & \b B_{61} & \b B_{62} & \b B_{66} \\ \b B_{11} & \b B_{12} & \b B_{16} & D_{11} & D_{12} & \r D_{16} \\ \b B_{21} & \b B_{22} & \b B_{26} & D_{21} & D_{22} & \r D_{26} \\ \b B_{61} & \b B_{62} & \b B_{66} & \r D_{61} & \r D_{62} & D_{66} \\ \end{bmatrix} \begin{Bmatrix} \epsilon_x \\ \epsilon_y \\ \epsilon_{xy} \\ \kappa_x \\ \kappa_y \\ \kappa_{xy} \end{Bmatrix}$$
\end{document}


## Background Colour

To highlight the background of matrix elements, I used the tikz which has a matrix library which is quite powerful when combined with everything else that TikZ offers.

The key here is that I don't want awkward gaps in between elements which are avoid by setting minimum width to an appropriate value. Since all nodes have the same text height and depth (that is, distance above and below the baseline), the height and depth of the nodes need not be adjusted but if you do need to adjust them, you'll have to set text height and text depth in the same way that minimum width is set.

Also, in the case where one column is a lot wider than the rest, it is possible to adjust make the above adjustments apply to only that column with column <number>/.style={} (and of course, the same goes for rows mutatis mutandis).

In the end though, despite the above precautions, there is still a minuscule white gap between each matrix element which is avoided by setting row sep and column sep to a small negative distance.

Lastly, since we are using TikZ matrices in combination with the usual maths matrices, I adjust the inner sep so that the elements come closer together so the matrices are of similar dimensions. By default, the TikZ matrix is a bit bigger than the usual maths matrices.

\documentclass[11pt,twoside]{report}
\usepackage{amsmath} %helps with mathematics
\usepackage{amssymb} %helps with mathematical symbols
\numberwithin{equation}{section} %numbers the equation like (2.1)
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
$$\begin{Bmatrix} N_x \\ N_y \\ N_{xy} \\ M_x \\ M_y \\ M_{xy} \end{Bmatrix} = \tikz [baseline=(m.center)] \matrix (m) [ matrix of math nodes, left delimiter={[}, right delimiter={]}, column sep=-0.1pt, row sep=-0.1pt, nodes={minimum width=2.4em, inner sep=2pt}, r/.style={fill=red!20}, b/.style={fill=blue!20} ] { A_{11} & A_{12} & |[r]| A_{16} & |[b]| B_{11} & |[b]| B_{12} & |[b]| B_{16} \\ A_{21} & A_{22} & |[r]| A_{26} & |[b]| B_{21} & |[b]| B_{22} & |[b]| B_{26} \\ |[r]| A_{61} & |[r]| A_{62} & A_{66} & |[b]| B_{61} & |[b]| B_{62} & |[b]| B_{66} \\ |[b]| B_{11} & |[b]| B_{12} & |[b]| B_{16} & D_{11} & |[r]| D_{12} & |[r]| D_{16} \\ |[b]| B_{21} & |[b]| B_{22} & |[b]| B_{26} & |[r]| D_{21} & D_{22} & D_{26} \\ |[b]| B_{61} & |[b]| B_{62} & |[b]| B_{66} & |[r]| D_{61} & D_{62} & D_{66} \\ }; \begin{Bmatrix} \epsilon_x \\ \epsilon_y \\ \epsilon_{xy} \\ \kappa_x \\ \kappa_y \\ \kappa_{xy} \end{Bmatrix}$$
\end{document}