4

I am trying to draw what's on the picture:

enter image description here

How can I draw the ellipses?

mwe:

\documentclass{article}
\usepackage{mathtools}
\usepackage{blkarray, bigstrut}

\begin{document}
\begin{table}
    \begin{tabular}{ccc}
          &  & V1  & V2 & V3  \\ \hline
        U1& 6 &    2 &   2    \\ \hline
        U2& 8 &     3&    5  \\ \hline
        U3& 4 &    2 &    6 \\
        \hline
    \end{tabular}
\end{table}

= 

\begin{table}[]
\begin{tabular}{ccc}
 & Comedy & Tragedy  \\
2 &  &  \\ \hline
3 &  &  \\ \hline
2 &  & \hline
\end{tabular}
\end{table}

\times

\begin{table}
\begin{tabular}{cccc}
 & V1 &V2  &V3  \\ \hline
 Comedy & 3 & 1 &1  \\ \hline
 Tragedy & 1 & 0 &-2 \hline
\end{tabular}
\end{table}
\end{document}
NaveganTeX
  • 2,630

2 Answers2

4

With use of the nicematrix package, designed as unnumbered equation:

enter image description here

\documentclass{article}
\usepackage{mathtools}
\usepackage{nicematrix,tikz}
\usetikzlibrary{fit,shapes.geometric}
\tikzset{highlight/.style={ellipse, draw=red, semithick, inner sep=1pt}}

\begin{document} [ \begin{NiceMatrix}[hvlines] & V1 & V2 & V3 \ U1 & 6 & 2 & 2 \ U2 & 8 & 3 & 5 \ U3 & 4 & 2 & 6 \ \end{NiceMatrix} \quad=\quad \begin{NiceMatrix}[hvlines] & \text{Comedy} & \text{Tragedy}\
U1 & 6 & 2 \
U2 & 8 & -1 \
U3 & 4 & -2 \
\CodeAfter \tikz \node [highlight, fit=(4-1) (4-3), label=below:User Features Vector] {} ; \end{NiceMatrix} \quad\times\quad \begin{NiceMatrix}[hvlines] & V1 & V2 & V3 \
\text{Comedy} & 3 & 1 & 1 \
\text{Tragedy} & 1 & 0 & -2 \
\CodeAfter \tikz \node [highlight, fit=(1-2) (3-2), label=below:Item Features Vector] {}; \end{NiceMatrix} ] \end{document}

Note: for the showed result you need to compile MWE at least twice!

Addendum:*

  • From your comments follows, that you like to have such solution in documents using standalone document class. Unfortunately this document class not support well images or writing equations which are based on remember picture and overlay options (as far as i know).
  • On the other hand, it is not clear what benefits are expected to have one equation (combined with tikz drawings in form of nicematrix or for example with tikzmark nodes). To include it as pdf or png file which you can simple scale?
  • Reason, that you can include in other environments, for example in float, is not convincing. this can be done directly. For example in figure float:
\documentclass{article}
\usepackage{geometry}
\usepackage{mathtools}
\usepackage{nicematrix,tikz}
\usetikzlibrary{fit,shapes.geometric}
\tikzset{highlight/.style={ellipse, draw=red, semithick, inner sep=1pt}}

\usepackage{lipsum} \begin{document} \lipsum[1] \begin{figure}[htb] [ \begin{NiceMatrix}[hvlines] & V1 & V2 & V3 \ U1 & 6 & 2 & 2 \ U2 & 8 & 3 & 5 \ U3 & 4 & 2 & 6 \ \end{NiceMatrix} \quad=\quad \begin{NiceMatrix}[hvlines] & \text{Comedy} & \text{Tragedy}\
U1 & 6 & 2 \
U2 & 8 & -1 \
U3 & 4 & -2 \
\CodeAfter \tikz \node [highlight, fit=(4-1) (4-3), label=below:User Features Vector] {} ; \end{NiceMatrix} \quad\times\quad \begin{NiceMatrix}[hvlines] & V1 & V2 & V3 \
\text{Comedy} & 3 & 1 & 1 \
\text{Tragedy} & 1 & 0 & -2 \
\CodeAfter \tikz \node [highlight, fit=(1-2) (3-2), label=below:Item Features Vector] {} ; \end{NiceMatrix} ] \caption{For some reason this equation is present as figure} \label{fig:nicematrix} \end{figure} \lipsum[2] \end{document}

which gives:

enter image description here

  • In the case, that the equation is to big (as it is now, it is wider than default text width defined by article document class), you can use for it smaller fonts or macro \medmath defined in the nccmath package:
\documentclass{article}
\usepackage{geometry}
\usepackage{mathtools, nccmath}
\usepackage{nicematrix,tikz}
\usetikzlibrary{fit,
                shapes.geometric}
\tikzset{highlight/.style={ellipse, draw=red, semithick, inner sep=1pt}}

\usepackage{lipsum} \begin{document} \lipsum[1] \begin{equation}\medmath{ \begin{NiceMatrix}[hvlines] & V1 & V2 & V3 \ U1 & 6 & 2 & 2 \ U2 & 8 & 3 & 5 \ U3 & 4 & 2 & 6 \ \end{NiceMatrix} \quad=\quad \begin{NiceMatrix}[hvlines] & \text{Comedy} & \text{Tragedy}\
U1 & 6 & 2 \
U2 & 8 & -1 \
U3 & 4 & -2 \
\CodeAfter \tikz \node [highlight, fit=(4-1) (4-3), label=below:User Features Vector] {} ; \end{NiceMatrix} \quad\times\quad \begin{NiceMatrix}[hvlines] & V1 & V2 & V3 \
\text{Comedy} & 3 & 1 & 1 \
\text{Tragedy} & 1 & 0 & -2 \
\CodeAfter \tikz \node [highlight, fit=(1-2) (3-2), label=below:Item Features Vector] {} ; \end{NiceMatrix} } \medskip \end{equation} \lipsum[2] \end{document}

enter image description here

  • If you for some reason like to retype this equation meny times in document, you can store equation code in file and simple call it with for example

       \input{equations/nicematrix-1}
    

    where nicematrix-1 is name of the file with equation code stored in equations sub folder in your document folder.

  • if there is still some reason to have this equation as standalone document, than might be solution to draw it with plain tikz using matrix library. Code for such image can be "slightly" more complicated (see second mine answer).

F. Pantigny
  • 40,250
Zarko
  • 296,517
  • 2
    Zarko is an astronomical genius –  Jun 23 '19 at 03:19
  • 1
    @Delan, I doubt in this (regardless that astronomy is one of my hobbies) ... :-). I'm pretty sure that during day you will receive more, maybe better answers. – Zarko Jun 23 '19 at 03:24
  • @Zarko How can I make it a standalone document? – NaveganTeX Jun 23 '19 at 06:13
  • 1
    @naveganTeX, simple replace \documentclass{article} with \documentclass[varwidth]{standalone} doesn't work as expected (what probably you found out yourself) . It cut-off labels defined in nodes highlight as well part of left and right side of equation. How to solve this we should ask the package author, so the best what can be done now is ask new question with this problem. – Zarko Jun 23 '19 at 08:47
  • 1
    @naveganTeX ,the closest approximation is use of the preview package, for example \usepackage[active,tightpage]{preview} \PreviewEnvironment{minipage} \setlength\PreviewBorder{2pt} and than insert equation in minipage as: \begin{minipage}{1.1\textwidth} \vspace*{-0.8\baselineskip} <... equation ...>\vspace*{0pt} \end{minipage} and than use result as pdf file. – Zarko Jun 23 '19 at 09:04
  • Thank you. Also I could try by reducing the space between cells or something like that – NaveganTeX Jun 23 '19 at 09:07
  • 1
    @naveganTeX, I don't know why you like to have equation in standalone version. The best is include into your document as it is. Anyway, I would be ask new question about this problem. It might already exist some clever workaround how to solve this. Now I will go away and haven't time for searching site for solution or ask new question. – Zarko Jun 23 '19 at 09:11
  • Because in when I include into the document it asks me for an environment. I need to give it a \caption and label. And I do not know which environment to use. – NaveganTeX Jun 23 '19 at 09:16
  • @naveganTeX, see addendum to my answer. For eventual further help i will have spare time late in evening again. – Zarko Jun 23 '19 at 15:03
  • @Zarko Always my compliments for your work. It is very nice ...nicematrix :-) – Sebastiano Jun 23 '19 at 15:07
1

Your equation as tikz picture. Used is standalone document class as you request in your comments:

\documentclass[tikz, margin=1pt]{standalone}
\usetikzlibrary{fit,
                matrix,
                positioning,
                shapes.geometric}
\tikzset{
highlight/.style args={#1/#2}{ellipse, draw=red, semithick, 
                              inner xsep=#1, inner ysep=#2},
 mymatrix/.style={matrix of nodes,
                  nodes in empty cells,
                  nodes={minimum height=3ex, minimum width=2em, 
                         inner sep=2pt, outer sep=0pt, anchor=center,
                         draw},
                  column sep=-\pgflinewidth,
                  row sep=-\pgflinewidth
                  }
        }

\begin{document}
    \begin{tikzpicture}[node distance=0pt]
\matrix (m1) [mymatrix] 
{
    & V1    & V2    & V3    \\
U1  & 6     & 2     & 2     \\
U2  & 8     & 3     & 5     \\
U3  & 4     & 2     & 6     \\
};
\node (times) [right=of m1] {$\times$};
\matrix (m2) [mymatrix, 
              column 2/.style={nodes={minimum width=4em}},
              column 3/.style={nodes={minimum width=4em}},
              right=of times]
{
    & Comedy    & Tragedy   \\
U1  & 6         & 2         \\
U2  & 8         & $-1$      \\
U3  & 4         & $-2$      \\
};
\node (equal) [right=of m2] {$=$};
\matrix (m3) [mymatrix,
              column 1/.style={nodes={minimum width=4em}},
              right=of equal]
{
        & V1    & V2    & V3    \\  
Comedy  & 3     & 1     & 1     \\  
Tragedy & 1     & 0     & $-2$  \\  
};
\node[highlight=-9pt/0pt, fit=(m2-4-1) (m2-4-3),
      label=below:User Features Vector] {};  
\node[highlight=0pt/-3pt, fit=(m3-1-2) (m3-3-2),
      label=below:Item Features Vector] {};
\end{tikzpicture}
\end{document}

enter image description here

or if you delete line line nodes in empty cells, in mymatrix style definition:

enter image description here

Zarko
  • 296,517
  • It worked!!! Thank you so much for taking the time, Zarko. Someone said earlier that you’re an astronomical genius and I agree! :) – NaveganTeX Jun 23 '19 at 18:00
  • How can I make the tables (matrices) look bold? Something related to \textbf – NaveganTeX Jun 24 '19 at 06:04
  • 1
    To definition of nodes add \text=\bfseries. After this you probably need to increase cells minimum width. – Zarko Jun 24 '19 at 06:44