[Tex/LaTex] tikz matrix background color for unevenly sized cells

backgroundsmatricestikz-pgf

I've adapted the example below from elsewhere on this forum. What I'd like is to have the background fill to come out as a rectangle covering the contents of all cells. What is the easiest way to accomplish that automatically in tikz, i.e. in such a way that the code works regardless of the cell contents? (I know how to fudge things manually using minimum width, \vphantom and the like, but I'm looking for something automatic in tikz.)

Thanks!

\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{arrows,matrix,positioning}
\begin{document}
    \begin{tikzpicture}
        \matrix [matrix of math nodes,left delimiter=(,right delimiter=)] (m)
        {
            8 &8 & \dfrac{1}{5} &6 \\
            \text{three} &\dfrac{8}{3} &5 &7 \\
            4 &8 &9 &5 \\
        };  
        \draw[fill=pink,opacity=0.2] (m-1-1.north west) -- (m-1-3.north east) -- (m-2-3.south east) -- (m-2-1.south west) -- (m-1-1.north west);
     \end{tikzpicture}
\end{document}

enter image description here

Best Answer

Instead to draw line arround nodes try to use rectangle shape, which fit this nodes:

\documentclass{article}
    \usepackage{amsmath}
    \usepackage{tikz}
    \usetikzlibrary{backgrounds,fit,matrix,positioning}
%%%% se-prewiew-tikz
\usepackage[active,floats,tightpage]{preview}
\PreviewEnvironment{tikzpicture}% <---------------------------------
    \setlength\PreviewBorder{1em}

\begin{document}
    \begin{tikzpicture}
\matrix [matrix of math nodes,left delimiter=(,right delimiter=)] (m)
{
            8   &   8               &   \dfrac{1}{5}    &   6   \\
 \text{three}   &   \dfrac{8}{3}    &           5       &   7   \\
            4   &   8               &           9       &   5   \\
};
\scoped[on background layer]
    \node[fill=pink!50,inner xsep=0mm, yshift=-1mm,
          fit=(m-1-1) (m-1-3) (m-2-3) (m-2-1) (m-1-1)]   {};
     \end{tikzpicture}
\end{document}

enter image description here