# Center labels in a matrix and fix row color

Hi. I need to center the phrase "Active user", meaning, I'd like to put it in the middle and not to the right nor the left. One more thing, I need to color those two cells that are in white right now. I do not know why, but they should be in gray.

CODE

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{matrix}
\usepackage{lmodern}
\begin{document}
\begin{tikzpicture}[font=\footnotesize,b/.style={text=blue,font=\bfseries}]
\matrix(m)[matrix of nodes,
nodes={minimum size=5ex,anchor=center,draw},
row 1/.style={nodes={draw=none,fill=none}},
column 5/.style={nodes={fill=white}},
column 6/.style={nodes={fill=gray!20}},
column 7/.style={nodes={fill=gray!20}},
row 4/.style={nodes={fill=none}},
column 1/.style={nodes={draw=none,fill=none}},
row sep=-\pgflinewidth,
column sep={-\pgflinewidth},
nodes in empty cells,
]{
& $i_{1}$& $i_{2}$ & $i_{3}$ & \dots &$i_{k}$    & $i_{a}$  & \dots &$i_{n}$ \\
$u_{1}$ & &  &   &  &  |[b]| c    &  |[b]| ?  &  &  \\
$u_{2}$ & &  &  &  & |[b]| c & |[b]| c  &   &\\
|[fill=none]|\dots & &   &   &  &   &  &  &    \\
$u_{j}$ &  &  &  &  &  &  & &   \\
$u_{a}$ & &  &  &  & |[b]| c &|[b]| c  & &  \\
\dots& &  &  &  &  &  & &  \\
$u_{6}$ & &  &  &  & |[b]| c & |[b]| c & &  \\
};
%   \draw[-latex](m-6-9.east)--+(1,0)node[above right]{}|-(m-5-9.east);

\node[left]at(m-6-1.west){Active user};
%   \draw[latex-] (m-1-6) -- ++ (-1,1) node[above left,align=left]
%   {Item preference score is\\ predicted for active user};
\draw[latex-] (m-1-7) -- ++ (0,1) node[above right,align=right]
{Active item};

\draw[-latex]([xshift=-1pt]m-8-7.south east)--++(0,-1) -|
([xshift=2pt]m-8-6.south west)
node[pos=0.25,below]{Item similarity}
node[pos=0.25,above,font=\tiny]{$\mathrm{sim}(i_{k},i_{a})$};
\end{tikzpicture}
\end{document}


• Change node[above right,align=right] with node[above] ... You could also add a shift=(0.0.2) or something in these options (inside the "[]" like node[above,shift={(0,0.2)}]) – koleygr Jul 9 '19 at 16:11
• It worked! Thank you – NaveganTeX Jul 9 '19 at 16:13
• Welcome ... happy TeXing! I will turn that into an answer if don't find a simmilar dupplicate – koleygr Jul 9 '19 at 16:14
• Yes ... forgot that... What about removing the row 4/.style={nodes={fill=none}}, ? – koleygr Jul 9 '19 at 16:16
• SuPer cool. Turn it into an answer so that I can accept it! :) – NaveganTeX Jul 9 '19 at 16:17

For the centering problem you have the code:

\draw[latex-] (m-1-7) -- ++ (0,1) node[above right,align=right]
{Active item};


That should be:

\draw[latex-] (m-1-7) -- ++ (0,1) node[above]
{Active item};


(A node[above,shift={(0,-0.1)}] could also be useful for negative or positive vertical shift)

For the coloring problem it is caused from the line:

row 4/.style={nodes={fill=none}},


that is read from the compiler after reading the column (gray) colors and overrides them. As far as I can see this line is not really usefull, so, just remove it.

This matrix being called (m) it is enough to anchor the text to the west of (m) like this \node[left]at(m.west){Active user}; Your matrix having 8 lines, the text is placed pile between the fourth and fifth columns.

Unless you want it in the center of the last 7 lines?

As for the grey colouring, it is enough to remove the white colouring from line 4.

%    row 4/.style={nodes={fill=none}},


Although you didn't ask to center the text Active item and as @koleygr gave a way to do it, I give another one that uses the positioning library. It consists in positioning the text above and using the edge operation to draw the arrow between the two nodes.

\node[above= 5mm of m-1-7] {Active item}edge[->](m-1-7);


\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{matrix}
\usepackage{lmodern}
\begin{document}
\begin{tikzpicture}[font=\footnotesize,b/.style={text=blue,font=\bfseries}]
\matrix(m)[matrix of nodes,
nodes={minimum size=5ex,anchor=center,draw},
row 1/.style={nodes={draw=none,fill=none}},
column 5/.style={nodes={fill=white}},
column 6/.style={nodes={fill=gray!20}},
column 7/.append style={nodes={fill=gray!20}},
%    row 4/.style={nodes={fill=none}},
column 1/.style={nodes={draw=none,fill=none}},
row sep=-\pgflinewidth,
column sep={-\pgflinewidth},
nodes in empty cells,
]{
& $i_{1}$& $i_{2}$ & $i_{3}$ & \dots &$i_{k}$    & $i_{a}$  & \dots &$i_{n}$ \\
$u_{1}$ & &  &   &  &  |[b]| c    &  |[b]| ?  &  &  \\
$u_{2}$ & &  &  &  & |[b]| c & |[b]| c  &   &\\
|[fill=none]|\dots & &   &   &  &   &  &  &    \\
$u_{j}$ &  &  &  &  &  &  & &   \\
$u_{a}$ & &  &  &  & |[b]| c &|[b]| c  & &  \\
\dots& &  &  &  &  &  & &  \\
$u_{6}$ & &  &  &  & |[b]| c & |[b]| c & &  \\
};
%   \draw[-latex](m-6-9.east)--+(1,0)node[above right]{}|-(m-5-9.east);

%\node[left]at(m.west){Active user};
\node[above= 5mm of m-1-7] {Active item}edge[->](m-1-7);
%   \draw[latex-] (m-1-6) -- ++ (-1,1) node[above left,align=left]
%   {Item preference score is\\ predicted for active user};
\draw[latex-] (m-1-7) -- ++ (0,1) node[above right,align=right]
{Active item};

\draw[-latex]([xshift=-1pt]m-8-7.south east)--++(0,-1) -|
([xshift=2pt]m-8-6.south west)
node[pos=0.25,below]{Item similarity}
node[pos=0.25,above,font=\tiny]{$\mathrm{sim}(i_{k},i_{a})$};
\end{tikzpicture}
\end{document}