8

I'm trying to draw the shadows and also the dots between the two matrices:

enter image description here

\documentclass{article}
\usepackage{amsmath}
\begin{document}


    \centering
        \begin{tabular}{|c|c|c|c|c|c|c|c|}
        \hline
        5   & 1 &  3&  &2  & 1 & &  \\ \hline
        4   & 2 &  &  3&  & 4 & 3 & 1 \\ \hline
        1   & 3 &  &  &  & 2 & 5 & \\ \hline
        2   & 5 &  1& 2 &  & & & 1 \\ \hline
        0   & 4 &  &  & 5 &3  &1 & \\ \hline
        & 2 &  &  4&  &4 & 1& 2 \\ \hline

        \hline
    \end{tabular}
    \begin{tabular}{|c|c|c|c|c|c|c|c|}
        \hline
     1  & 1 &  1&  &1  & 1 & &  \\ \hline
     0  & 1 &  &  &  & 0 & 0 & 1 \\ \hline
    0   &  &  &  &  & 0 & 0 & \\ \hline
    1   &  &  1& 1 &  & & & 1 \\ \hline
    0   & 1 &  &  & 1 &1  &1 & \\ \hline
        & 1 &  &  &  & & &  \\ \hline

        \hline
    \end{tabular}



\end{document}

enter image description here

  • I'm curious: which solution did you choose? – Keks Dose Jul 16 '19 at 9:28
  • I wouldn’t play that card! All I can tell you is that all the solutions are fantastic! – NaveganTeX Jul 16 '19 at 15:17
11

Shadows can be cast (a) by groundhogs, (b) with shadows, and (c) with shadows.blur. I use shadows.blur here because you seem not to be asking for a duck-shaped shadow. The styles for the dividing lines and the frame are from here. I made the cells quadratic in the update.

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{matrix,calc,positioning,fit,backgrounds,shadows.blur}
\makeatletter
\long\def\ifnodedefined#1#2#3{%
    \@ifundefined{pgf@sh@ns@#1}{#3}{#2}%
}
\tikzset{matrix vlines/.style={execute at end matrix={
\foreach \XX in {1,...,\the\pgf@matrix@numberofcolumns}
{\xdef\FitList{}
\foreach \YY in {1,...,\the\pgfmatrixcurrentrow}
{\ifnodedefined{\tikzmatrixname-\YY-\XX}{\xdef\FitList{\FitList (\tikzmatrixname-\YY-\XX)}}{}
}
\node[fit=\FitList,draw=none,fill=none,inner sep=0pt,draw=none] (\tikzmatrixname-col-\XX) {};
}
\foreach \XX in {2,...,\the\pgf@matrix@numberofcolumns}
{\draw[#1] ($(\tikzmatrixname-col-\XX.west)!0.5!(\tikzmatrixname-col-\the\numexpr\XX-1\relax.east)$)
coordinate (aux) (aux|-\tikzmatrixname.north)
 --  (aux|-\tikzmatrixname.south);
}
}},matrix hlines/.style={execute at end matrix={
\foreach \YY in {1,...,\the\pgfmatrixcurrentrow}
{\xdef\FitList{}
\foreach \XX in {1,...,\the\pgf@matrix@numberofcolumns}
{\ifnodedefined{\tikzmatrixname-\YY-\XX}{\xdef\FitList{\FitList (\tikzmatrixname-\YY-\XX)}}{}
}
\node[fit=\FitList,draw=none,fill=none,inner sep=0pt,draw=none] (\tikzmatrixname-row-\YY) {};
}
\foreach \XX in {2,...,\the\pgfmatrixcurrentrow}
{\draw[#1] ($(\tikzmatrixname-row-\XX)!0.5!(\tikzmatrixname-row-\the\numexpr\XX-1\relax)$)
coordinate (aux) (aux-|\tikzmatrixname.west)
 --  (aux-|\tikzmatrixname.east);
}
}},
matrix dividers/.style={matrix vlines=#1,matrix hlines=#1},
matrix frame/.style={execute at end matrix={
\draw[#1] (\tikz@fig@name.south west) rectangle (\tikz@fig@name.north east);
}}}
\makeatother
\begin{document}
\begin{tikzpicture}[font=\sffamily\itshape,node distance=0.3em,
s-matrix/.style={matrix dividers,matrix frame,matrix of math nodes,
nodes in empty cells,fill=white,blur shadow,inner sep=0pt,
cells={text height=0.8em,text depth=0.2em,minimum width=1.8em,minimum height=1.8em}}]
\matrix[s-matrix] (mat1) {
        5   & 1 &  3&  &2  & 1 & &  \\ 
        4   & 2 &  &  3&  & 4 & 3 & 1 \\ 
        1   & 3 &  &  &  & 2 & 5 & \\ 
        2   & 5 &  1& 2 &  & & & 1 \\ 
        0   & 4 &  &  & 5 &3  &1 & \\ 
        & 2 &  &  4&  &4 & 1& 2 \\ 
};
\node[right=of mat1] (users) {users};
\matrix[s-matrix,right=of users] (mat2) {
     1  & 1 &  1&  &1  & 1 & &  \\ 
     0  & 1 &  &  &  & 0 & 0 & 1 \\ 
    0   &  &  &  &  & 0 & 0 & \\ 
    1   &  &  1& 1 &  & & & 1 \\ 
    0   & 1 &  &  & 1 &1  &1 & \\ 
        & 1 &  &  &  & & &  \\ 
};
\foreach \X in {1,2}
{\node[above=of mat\X] (items-\X) {items};
\node at (mat\X-1-1|-items-\X) {i$_1$};
\node at (mat\X-1-8|-items-\X) {i$_8$};}
\foreach \X in {1,6}
{\draw[dotted] (mat1-\X-2-|mat1.east) -- (mat2-\X-2-|mat2.west)
 node[midway,fill=white,inner sep=0.5pt]{u$_\X$};}
\node[below=1em of mat1,font=\sffamily\bfseries]  {Explicit ratings};
\node[below=1em of mat2,font=\sffamily\bfseries]  {Implicit ratings};
\end{tikzpicture}
\end{document}

enter image description here

| improve this answer | |
  • 2
    Excellent! One small observation, how can we make the size of the cells in both matrices are the same? If you take a look at the screen shot, it does not matter if there is or there isn't numbers inside each cell, each one is a perfect square. – NaveganTeX Jul 6 '19 at 8:37
  • @NaveganTeX I made them quadratic in the update. – user121799 Jul 6 '19 at 8:53
  • Much better with the last edit. Soft and nicer! – NaveganTeX Jul 6 '19 at 8:59
6

Less elegant than marmot's solution but maybe also interesting:

enter image description here

\documentclass{article}
\usepackage{amsmath,blkarray}
\usepackage{tikz}
\usetikzlibrary{shadows, shapes, positioning, matrix}
\makeatletter % from https://tex.stackexchange.com/a/129322/121799
\tikzset{no shadows/.code=\let\tikz@preactions\pgfutil@empty}
\makeatother
\begin{document}

   \begin{tikzpicture}[
    every node/.style = {draw, 
                         fill=white, 
                         drop shadow, 
                         anchor=center,
                         text height=2ex,
                         text depth=0.25ex},
    ]

    \matrix (leftmatrix) [no shadows,
                          draw=none, 
                          matrix of nodes, 
                          nodes in empty cells, 
                          row sep =-\pgflinewidth, 
                          column sep = -\pgflinewidth, 
                          minimum width=0.5cm, 
                          minimum height=0.5cm, 
                          ]
                          {
    5  & 1 & 3 &   & 2 & 1 &   &   \\  
    4  & 2 &   & 3 &   & 4 & 3 & 1 \\  
    1  & 3 &   &   &   & 2 & 5 &   \\  
    2  & 5 & 1 & 2 &   &   & 1 &   \\  
    0  & 4 &   &   & 5 & 3 & 1 &   \\  
       & 2 &   & 4 &   & 4 & 1 & 2 \\  
    };

    \node[right=0.25 cm of leftmatrix, draw=none, no shadows, font=\itshape] (users) {users};

    \matrix (rightmatrix) [no shadows,
                          draw=none, 
                          matrix of nodes, 
                          nodes in empty cells, 
                          row sep =-\pgflinewidth, 
                          column sep = -\pgflinewidth, 
                          minimum width=0.5cm, 
                          minimum height=0.5cm,
                          right= 0.25cm of users]
                          {
    1  & 1 &  1 &   & 1 & 1 &   &   \\ 
    0  & 1 &    &   &   & 0 & 0 & 1 \\ 
    0  &   &    &   &   & 0 & 0 &   \\ 
    1  &   &  1 & 1 &   &   &   & 1 \\ 
    0  & 1 &    &   & 1 & 1 & 1 &   \\ 
       & 1 &    &   &   &   &   &   \\ 
    };
    \node[above= 0.1cm of leftmatrix, draw=none, no shadows, font=\itshape] (itemsleft) {items};
    \node[above= 0.1cm of rightmatrix, draw=none, no shadows, font=\itshape] (itemsright) {items};
    \node[below= 0.25cm of leftmatrix, draw=none, no shadows, font=\bfseries] (captionleft) {Explicit Rating};
    \node[below= 0.25cm of rightmatrix, draw=none, no shadows, font=\bfseries] (captionright) {Implicit Rating};
    \node[draw=none, no shadows] at (leftmatrix-1-1|-itemsleft) {$i_1$};
    \node[draw=none, no shadows] at (leftmatrix-1-8|-itemsleft) {$i_8$};
    \node[draw=none, no shadows] at (rightmatrix-1-1|-itemsleft) {$i_1$};
    \node[draw=none, no shadows] at (rightmatrix-1-8|-itemsleft) {$i_8$};
    \draw[dotted] (leftmatrix-1-2-|leftmatrix.east) -- (rightmatrix-1-2-|rightmatrix.west)
 node[midway,fill=white,inner sep=0.5pt, draw=none, no shadows]{$u_1$};
    \draw[dotted] (leftmatrix-6-2-|leftmatrix.east) -- (rightmatrix-6-2-|rightmatrix.west)
 node[midway,fill=white,inner sep=0.5pt, draw=none, no shadows]{$u_6$};

    \end{tikzpicture}

\end{document}

Or without no shadows.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shadows, shapes, positioning, matrix}
\begin{document}

   \begin{tikzpicture}[
    every node/.style = {draw, 
                         fill=white, 
                         anchor=center,
                         text height=2ex,
                         text depth=0.25ex},
    ]

    \matrix (leftmatrix) [fill=white,drop shadow,inner sep=0pt,
                          draw=none, 
                          matrix of nodes, 
                          nodes in empty cells, 
                          row sep =-\pgflinewidth, 
                          column sep = -\pgflinewidth, 
                          cells={minimum width=0.5cm, 
                          minimum height=0.5cm}
                          ]
                          {
    5  & 1 & 3 &   & 2 & 1 &   &   \\  
    4  & 2 &   & 3 &   & 4 & 3 & 1 \\  
    1  & 3 &   &   &   & 2 & 5 &   \\  
    2  & 5 & 1 & 2 &   &   & 1 &   \\  
    0  & 4 &   &   & 5 & 3 & 1 &   \\  
       & 2 &   & 4 &   & 4 & 1 & 2 \\  
    };

    \node[right=0.25 cm of leftmatrix, draw=none, font=\itshape] (users) {users};

    \matrix (rightmatrix) [fill=white,drop shadow,inner sep=0pt,
                          draw=none, 
                          matrix of nodes, 
                          nodes in empty cells, 
                          row sep =-\pgflinewidth, 
                          column sep = -\pgflinewidth, 
                          cells={minimum width=0.5cm, 
                          minimum height=0.5cm},
                          right= 0.25cm of users]
                          {
    1  & 1 &  1 &   & 1 & 1 &   &   \\ 
    0  & 1 &    &   &   & 0 & 0 & 1 \\ 
    0  &   &    &   &   & 0 & 0 &   \\ 
    1  &   &  1 & 1 &   &   &   & 1 \\ 
    0  & 1 &    &   & 1 & 1 & 1 &   \\ 
       & 1 &    &   &   &   &   &   \\ 
    };
    \node[above= 0.1cm of leftmatrix, draw=none, font=\itshape] (itemsleft) {items};
    \node[above= 0.1cm of rightmatrix, draw=none, font=\itshape] (itemsright) {items};
    \node[below= 0.25cm of leftmatrix, draw=none, font=\bfseries] (captionleft) {Explicit Rating};
    \node[below= 0.25cm of rightmatrix, draw=none, font=\bfseries] (captionright) {Implicit Rating};
    \node[draw=none] at (leftmatrix-1-1|-itemsleft) {$i_1$};
    \node[draw=none] at (leftmatrix-1-8|-itemsleft) {$i_8$};
    \node[draw=none] at (rightmatrix-1-1|-itemsleft) {$i_1$};
    \node[draw=none] at (rightmatrix-1-8|-itemsleft) {$i_8$};
    \draw[dotted] (leftmatrix-1-2-|leftmatrix.east) -- (rightmatrix-1-2-|rightmatrix.west)
 node[midway,fill=white,inner sep=0.5pt, draw=none]{$u_1$};
    \draw[dotted] (leftmatrix-6-2-|leftmatrix.east) -- (rightmatrix-6-2-|rightmatrix.west)
 node[midway,fill=white,inner sep=0.5pt, draw=none]{$u_6$};

    \end{tikzpicture}
\end{document}
| improve this answer | |
  • It is, without a doubt, a very interesting solution. Thank you! – NaveganTeX Jul 6 '19 at 8:58
  • 1
    @marmot: Thanks for the edit. You are right that adding the shadow where needed is definitely a lot easier than removing it where not needed. Somehow this idea did not come to my mind. – leandriis Jul 6 '19 at 9:42
2

If you are not inclined to learn Tikz, then just make a tabular. It doesn't take much code, the most sophisticated thing was the »shadow«.

\documentclass{article}
% \usepackage{amsmath}
\usepackage{array}
\usepackage{booktabs, colortbl}
\begin{document}

\renewcommand{\arraystretch}{1.3}\sffamily{}

    \begin{tabular}{*{18}{|c} |c|}
      \multicolumn{3}{l}{$i_1$} & \multicolumn{2}{c}{\raisebox{2ex}{items}} &
     \multicolumn{3}{r}{$i_8$} &   \multicolumn{3}{c}{}  &
     \multicolumn{3}{l}{$i_8$} & \multicolumn{2}{c}{\raisebox{2ex}{items}} &
                 \multicolumn{3}{r}{$i_8$}\\\cmidrule{1-8} \cmidrule{12-19}
     5 & 1 &  3&  &2  & 1 & &   & \multicolumn{3}{c|}{\dots $u_1$ \dots} &  5   & 1 &  3&  &2  & 1 & &\\\cmidrule{1-8} \cmidrule{12-19} 
     4 & 2 &  &  3&  & 4 & 3 & 1  & \multicolumn{3}{c|}{} &  4   & 2 &  &  3&  & 4 & 3 & 1 \\\cmidrule{1-8} \cmidrule{12-19}  
     1 & 3 &  &  &  & 2 & 5 &  & \multicolumn{3}{c|}{} & 1   & 3 &  &  &  & 2 & 5 &\\\cmidrule{1-8} \cmidrule{12-19}  
     2 & 5 &  1& 2 &  & & & 1  & \multicolumn{3}{c|}{users} & 2   & 5 &  1& 2 &  & & & 1\\\cmidrule{1-8} \cmidrule{12-19}  
     0 & 4 &  &  & 5 &3  &1 &  & \multicolumn{3}{c|}{} &  0   & 4 &  &  & 5 &3  &1 & \\\cmidrule{1-8} \cmidrule{12-19}  
       & 2 &  &  4&  &4 & 1& 2  & \multicolumn{3}{c|}{\dots $u_6$ \dots} &  & 2 & &  4&  &4 & 1& 2\\\cmidrule{1-8}
      \cmidrule{12-19}\addlinespace[-4pt]
      \arrayrulecolor[gray]{0.5}
      \cmidrule[2pt](l{0.2ex}r{-.2ex}){1-8}
      \cmidrule[2pt](l{0.2ex}r{-.2ex}){12-19}\addlinespace[2ex]
      \multicolumn{8}{c}{\textbf{Explicit ratings}}& \multicolumn{3}{c}{} & \multicolumn{8}{c}{\textbf{Implicit ratings}}\\
    \end{tabular}


\end{document}

Result:

pic of tabular


EDIT

As suggested, with \clines, instead of \cmidrules. We need the latter for the shadow (as far as I know you can't trim \clines).

And I took the opportunity to streamline the layout, code of version 2:

\documentclass{article}
% \usepackage{amsmath}
\usepackage{array}
\usepackage{booktabs, colortbl, ragged2e}
\begin{document}

\renewcommand{\arraystretch}{1.8}\sffamily{}

    \begin{tabular}{*{18}{|>{\Centering}p{1em}} |>{\Centering}p{1em}|}
      \multicolumn{3}{l}{$i_1$} & \multicolumn{2}{c}{\raisebox{2ex}{items}} &
     \multicolumn{3}{r}{$i_8$} &   \multicolumn{3}{c}{}  &
     \multicolumn{3}{l}{$i_8$} & \multicolumn{2}{c}{\raisebox{2ex}{items}} &
                 \multicolumn{3}{r}{$i_8$}\\\cline{1-8} \cline{12-19}
     5 & 1 &  3&  &2  & 1 & &   & \multicolumn{3}{c|}{\dots $u_1$ \dots} &  5   & 1 &  3&  &2  & 1 & &\\\cline{1-8} \cline{12-19} 
     4 & 2 &  &  3&  & 4 & 3 & 1  & \multicolumn{3}{c|}{} &  4   & 2 &  &  3&  & 4 & 3 & 1 \\\cline{1-8} \cline{12-19}  
     1 & 3 &  &  &  & 2 & 5 &  & \multicolumn{3}{c|}{} & 1   & 3 &  &  &  & 2 & 5 &\\\cline{1-8} \cline{12-19}  
     2 & 5 &  1& 2 &  & & & 1  & \multicolumn{3}{c|}{users} & 2   & 5 &  1& 2 &  & & & 1\\\cline{1-8} \cline{12-19}  
     0 & 4 &  &  & 5 &3  &1 &  & \multicolumn{3}{c|}{} &  0   & 4 &  &  & 5 &3  &1 & \\\cline{1-8} \cline{12-19}  
       & 2 &  &  4&  &4 & 1& 2  & \multicolumn{3}{c|}{\dots $u_6$ \dots} &  & 2 & &  4&  &4 & 1& 2\\\cline{1-8}
      \cline{12-19}\addlinespace[-1pt]
      \arrayrulecolor[gray]{0.4}
      \cmidrule[3pt](l{0.2ex}r{-.2ex}){1-8}
      \cmidrule[3pt](l{0.2ex}r{-.2ex}){12-19}\addlinespace[1ex]
      \multicolumn{8}{c}{\textbf{Explicit ratings}}& \multicolumn{3}{c}{} & \multicolumn{8}{c}{\textbf{Implicit ratings}}\\
    \end{tabular}
\end{document}

And the picture:

pic of tabular v2

| improve this answer | |
  • The idea is good but I wouldn't use booktabs with vertical lines. – CarLaTeX Jul 12 '19 at 12:40
  • @CarLaTeX Yes, but I needed \cmidrules for the empty gap between the two parts. – Keks Dose Jul 12 '19 at 12:41
  • Couldn't you use \clines? – CarLaTeX Jul 12 '19 at 12:43
  • @CarLaTeX I had forgotten that \clines exist. Thank you, I amended my answer! – Keks Dose Jul 12 '19 at 13:05
  • It happens, lol! – CarLaTeX Jul 12 '19 at 13:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.