# highlighting diagonal of a square matrix

I have a 3x3 eqnarray. Is there a way to highlight the diagonal with background color, perhaps using tikz? I was using tikz ellipse to highlight equation terms, though stretching the ellipse to the whole diagonal makes it too thin at ends; I also don't know how to rotate it without trial-and-error angle value.

-
It is recommended to avoid eqnarray, see e.g. tug.org/pracjourn/2006-4/madsen/madsen.pdf –  N.N. Sep 30 '11 at 10:59
Maybe latex-community.org/forum/viewtopic.php?f=46&t=7479#p29022 is of interest. –  N.N. Sep 30 '11 at 11:00

You can use the following but I have to admit that I did not quite get your question.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
%\usetikzlibrary{shapes} %Uncomment if you want additional shapes
\begin{document}
\begin{tikzpicture}
\matrix (m) [matrix of math nodes]{
| [draw,fill= yellow] | a=b     & b                                  &c\\
d                               &|[draw,fill= yellow]| e^2=m^2c^4 &f\\
g                               &h          &|[draw,fill= yellow]|F = \frac{d}{dt}{(m\vec{v})}\\
};
\end{tikzpicture}
\end{document}


This results in

I hope it helps.

EDIT: if you also include the shapes library, you can change the shape of the highlighting to ellipse etc.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix,shapes}
\begin{document}
\begin{tikzpicture}
\matrix (m) [matrix of math nodes,inner sep=0cm]{
| [draw,regular polygon,regular polygon sides=3,fill= yellow] | a=b&F(i\omega) = \int^{\infty}_{-\infty}f(t)e^{-i\omega t}dt &c\\
%Second row
d&|[draw,regular polygon,fill= red]| e^2=m^2c^4 &f\\
%Third row
\nabla^2(\vec{r}\times \nabla\psi)  - \frac{1}{c^2}\frac{\partial^2 (\vec{r}\times \nabla\psi)}{\partial t^2}&h&|[diamond,fill= blue!20]|F = \frac{d}{dt}{(m\vec{v})}\\
};
\end{tikzpicture}
\end{document}


which gives

By the way, sorry for the lame matrix entries.

-

It's not clear from your question whether you want a single stripe that goes over the whole diagonal, or each diagonal term to be individually highlighted. The latter, which percusse goes for, can be simplified from percusse's answer by using some code from Obstacles to simulating an amsmath matrix by a TiKZ matrix of math nodes. This replaces the amsmath matrix commands by TikZ versions. It effectively does the same as percusse's code, but hides a lot of the messy bits. One change in the implementation is that we apply the style by testing the row and column numbers, rather than specifying it on every cell. The preamble may seem a bit daunting, so I'll put the result first:

The code itself is:

\documentclass{article}
%\url{http://tex.stackexchange.com/q/26866/86}
\usepackage{tikz}
\usetikzlibrary{matrix}

\newlength\mtxrowsep   \setlength\mtxrowsep{1.5ex}
\newlength\mtxcolsep   \setlength\mtxcolsep{2\arraycolsep}

\tikzset{
ams/.style={
baseline=-.7ex,
every delimiter/.style={yshift=-1pt},
every left delimiter/.style={xshift=2pt},
every right delimiter/.style={xshift=-2pt},
every node/.style={inner sep=0pt},
},
ams matrix/.style={
inner sep=1pt,
column sep=\mtxcolsep,
row sep=\mtxrowsep,
%    ampersand replacement=\&,
matrix of math nodes,
},
bmatrix/.style={
ams,
every matrix/.style={
ams matrix,
left delimiter={[},
right delimiter={]},
}
},
Bmatrix/.style={
ams,
every matrix/.style={
ams matrix,
left delimiter={\lbrace},
right delimiter={\rbrace},
}
},
pmatrix/.style={
ams,
every matrix/.style={
ams matrix,
left delimiter={(},
right delimiter={)},
}
},
vmatrix/.style={
ams,
every matrix/.style={
ams matrix,
left delimiter={|},
right delimiter={|},
}
},
Vmatrix/.style={
ams,
every matrix/.style={
ams matrix,
left delimiter={\|},
right delimiter={\|},
}
},
highlight diagonal/.style={
nodes={highlight diagonal nodes}
},
highlight diagonal nodes/.code={
\ifnum\pgfmatrixcurrentrow=\pgfmatrixcurrentcolumn
\tikzset{fill=yellow}
\fi
}
}

\let\matamp=&

\catcode\&=13
\makeatletter
\def&{\iftikz@is@matrix
\pgfmatrixnextcell
\else
\matamp
\fi}
\makeatother

\newenvironment{tikzbmatrix}[1][]{%
\begin{tikzpicture}[bmatrix]
\matrix[#1] \bgroup}
{\\\egroup;\end{tikzpicture}}

\begin{document}
\begin{tikzbmatrix}[highlight diagonal,nodes={inner sep=2pt}]
a=b & b          & c\\
d   & e^2=m^2c^4 & f\\
g   & h          & F = \frac{d}{dt}{(m\vec{v})}
\end{tikzbmatrix}
\end{document}


You have to imagine all of that mess hidden away in a nice neat package! (Actually it's longer than necessary because it has code for every one of the AMS matrix types.)

To get the other interpretation, we need a way to draw a line underneath the text. Fortunately, there's already something that can draw underneath it: the matrix node shape. So we simply find a shape that draws a line from the top left to the bottom right. As there isn't one, we define one. (The strike out is close, but not close enough.) We want the highlighter stroke to be wide, but setting the line width for the matrix also sets it for the cells which we don't want so we have to override that. Other than that, it's straightforward.

(It's not positioned perfectly, but that might be as much to do with the elongated shape of the cells. That's something that could be corrected, but would need maybe a little tweaking on a per-case basis.)

Here's the code. Again, think of the preamble as a nice package.

\documentclass[border=10]{standalone}
%\url{http://tex.stackexchange.com/q/26866/86}
\usepackage{tikz}
\usetikzlibrary{matrix,shapes.misc}

\newlength\mtxrowsep   \setlength\mtxrowsep{1.5ex}
\newlength\mtxcolsep   \setlength\mtxcolsep{2\arraycolsep}

\makeatletter
\pgfdeclareshape{strike under}
{
\inheritsavedanchors[from=rectangle] % this is nearly a rectangle
\inheritanchorborder[from=rectangle]
\inheritanchor[from=rectangle]{north}
\inheritanchor[from=rectangle]{north west}
\inheritanchor[from=rectangle]{north east}
\inheritanchor[from=rectangle]{center}
\inheritanchor[from=rectangle]{west}
\inheritanchor[from=rectangle]{east}
\inheritanchor[from=rectangle]{mid}
\inheritanchor[from=rectangle]{mid west}
\inheritanchor[from=rectangle]{mid east}
\inheritanchor[from=rectangle]{base}
\inheritanchor[from=rectangle]{base west}
\inheritanchor[from=rectangle]{base east}
\inheritanchor[from=rectangle]{south}
\inheritanchor[from=rectangle]{south west}
\inheritanchor[from=rectangle]{south east}
\backgroundpath{
\pgfpathmoveto{\northeast}
\pgfpathmoveto{\southwest}
\southwest
\pgf@xa=\pgf@x
\northeast
\pgf@ya=\pgf@y
\pgfpathmoveto{\pgfqpoint{\pgf@xa}{\pgf@ya}}
\southwest
\pgf@ya=\pgf@y
\northeast
\pgf@xa=\pgf@x
\pgfpathlineto{\pgfqpoint{\pgf@xa}{\pgf@ya}}
}
}
\makeatother
\tikzset{
ams/.style={
baseline=-.7ex,
every delimiter/.style={yshift=-1pt},
every left delimiter/.style={xshift=2pt},
every right delimiter/.style={xshift=-2pt},
every node/.style={inner sep=0pt},
},
ams matrix/.style={
inner sep=1pt,
column sep=\mtxcolsep,
row sep=\mtxrowsep,
%    ampersand replacement=\&,
matrix of math nodes,
},
bmatrix/.style={
ams,
every matrix/.style={
ams matrix,
left delimiter={[},
right delimiter={]},
}
},
Bmatrix/.style={
ams,
every matrix/.style={
ams matrix,
left delimiter={\lbrace},
right delimiter={\rbrace},
}
},
pmatrix/.style={
ams,
every matrix/.style={
ams matrix,
left delimiter={(},
right delimiter={)},
}
},
vmatrix/.style={
ams,
every matrix/.style={
ams matrix,
left delimiter={|},
right delimiter={|},
}
},
Vmatrix/.style={
ams,
every matrix/.style={
ams matrix,
left delimiter={\|},
right delimiter={\|},
}
},
highlight diagonal/.style={
nodes={highlight diagonal nodes}
},
highlight diagonal nodes/.code={
\ifnum\pgfmatrixcurrentrow=\pgfmatrixcurrentcolumn
\tikzset{fill=yellow}
\fi
},
highlighter width/.initial={5mm}
}

\let\matamp=&

\catcode\&=13
\makeatletter
\def&{\iftikz@is@matrix
\pgfmatrixnextcell
\else
\matamp
\fi}

\makeatother

\newenvironment{tikzbmatrix}[1][]{%
\begin{tikzpicture}[bmatrix]
\matrix[#1] \bgroup}
{\\\egroup;\end{tikzpicture}}

\begin{document}
\begin{tikzbmatrix}[draw=yellow,strike under,line width=1cm,nodes={thin}]
a=b & b          & c\\
d   & e^2=m^2c^4 & f\\
g   & h          & F = \frac{d}{dt}{(m\vec{v})}
\end{tikzbmatrix}
\end{document}

-
Wow, you have saved a huge amount of time and effort of mine, by giving away that preamble. Thanks a lot.Looking forward to have this on CTAN ;) –  percusse Sep 30 '11 at 13:06
@percusse: If you like it and want to see it on CTAN, you should add it to the list: meta.tex.stackexchange.com/q/1220/86 –  Loop Space Sep 30 '11 at 13:08
I'm on it. Thanks again –  percusse Sep 30 '11 at 13:10