0

Can anyone help me to do this on LaTeX?ilustration

1
  • 4
    Welcome to TeX.SX! On this site, a question should typically revolve around an abstract issue (e.g. "How do I get a double horizontal line in a table?") rather than a concrete application (e.g. "How do I make this table?"). Questions that look like "Please do this complicated thing for me" tend to get closed because they are either "off topic", "too broad", or "unclear". Please try to make your question clear and simple by giving a minimal working example (MWE): you'll stand a greater chance of getting help. Apr 23, 2020 at 7:19

1 Answer 1

5

You can generate the whole graph from an input array.

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{fit,shapes.geometric,matrix,positioning}
\begin{document}
\begin{tikzpicture}[font=\sffamily]
 % everything is computed from the input array
 \def\marray{{1,0,0,0,0},{0,0,1,0,0},{0,0,0,1,0},{0,1,0,0,1},{0,1,0,0,0}}
 % left scope
 \begin{scope}[local bounding box=L]
  \matrix[matrix of math nodes,column sep=4em,row sep=0.6em] (matL){
   h_{\the\pgfmatrixcurrentrow} & v_{\the\pgfmatrixcurrentrow} \\
   h_{\the\pgfmatrixcurrentrow} & v_{\the\pgfmatrixcurrentrow} \\
   h_{\the\pgfmatrixcurrentrow} & v_{\the\pgfmatrixcurrentrow} \\
   h_{\the\pgfmatrixcurrentrow} & v_{\the\pgfmatrixcurrentrow} \\
   h_{\the\pgfmatrixcurrentrow} & v_{\the\pgfmatrixcurrentrow} \\
  };
  \node[fit=(matL-1-1) (matL-5-1),ellipse,draw,inner ysep=-1em,
     label=above:$H$] (h){};
  \node[fit=(matL-1-2) (matL-5-2),ellipse,draw,inner ysep=-1em,
     label=above:$V$] (v){};
  \foreach \X in {1,...,5} 
  {\foreach \Y in {1,...,5} 
  {\pgfmathtruncatemacro{\itest}{{\marray}[\X-1][\Y-1]}
  \ifnum\itest=1
   \draw[shorten <=-2pt,shorten >=-1pt] (matL-\X-1) -- (matL-\Y-2);
  \fi}} 
 \end{scope}
 % right scope
 \begin{scope}[local bounding box=R] 
  \matrix[right=12em of matL,yshift=-1em,
    matrix of math nodes,nodes in empty cells,inner sep=0pt,
    column sep=-\pgflinewidth/2,row sep=-1.5\pgflinewidth,
    cells={nodes={draw,minimum width=2em,minimum height=1.5em,outer sep=0pt,
    execute at begin node={%
    \ifnum\pgfmatrixcurrentcolumn=6
     \ifnum\pgfmatrixcurrentrow<6
      h_{\the\pgfmatrixcurrentrow}
     \fi
    \else
     \ifnum\pgfmatrixcurrentrow=6
      \ifnum\pgfmatrixcurrentcolumn<6
       v_{\the\pgfmatrixcurrentcolumn}
      \fi
     \else
      \pgfmathtruncatemacro{\itest}{{\marray}[\pgfmatrixcurrentrow-1][\pgfmatrixcurrentcolumn-1]}%
      \itest
     \fi 
    \fi
    }}},
    column 6/.style={nodes={draw=none}},
    row 6/.style={nodes={draw=none}}] (mat) {
     &  &  &  &  & \\
     &  &  &  &  & \\
     &  &  &  &  & \\
     &  &  &  &  & \\
     &  &  &  &  & \\
     &  &  &  &  & \\
  };
  \path[nodes={draw,minimum height=2em,outer sep=0pt}] 
     (mat-1-1.north west) node[anchor=south east] (Fh) {Features $H$}
  (mat-1-5.north east) node[rotate=-90,anchor=south east] (Fv) {Features $V$};
  \draw (Fv.south west) -| (Fh.north east) (Fh.south west) |- (mat-5-1.south west);
 \end{scope} 
 %
 \path[nodes={text height=1.2em}] (current bounding box.south) coordinate (aux)
  (aux-|L) node[below]{(a)} (aux-|R) node[below]{(b)};
\end{tikzpicture}
\end{document}

enter image description here

If you change the entries of

\def\marray{{1,0,0,0,0},{0,0,1,0,0},{0,0,0,1,0},{0,1,0,0,1},{0,1,0,0,0}}

the diagram will adjust itself.

One can also create a version that allows arbitrary n by n arrays of 0 and 1 entries.

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{fit,shapes.geometric,positioning}
\makeatletter
\pgfmathdeclarefunction{Dim}{1}{%
\begingroup%
\pgfutil@tempcnta0%
\@for\pgfutil@tempa:=#1\do{\advance\pgfutil@tempcnta1}%
\edef\pgfmathresult{\the\pgfutil@tempcnta}%
\pgfmathsmuggle\pgfmathresult\endgroup%
}
\makeatother
\begin{document}
\begin{tikzpicture}[font=\sffamily,box/.style={minimum width=2em,minimum
height=1.5em,draw}]
 % everything is computed from the input array
 \edef\marray{{1,0,0,0,0,0},%
 {0,0,1,0,0,0},%
 {0,0,0,1,0,0},%
 {0,1,0,0,1,0},%
 {0,1,0,0,0,1},%
 {0,0,1,0,0,0}}
 \pgfmathtruncatemacro{\mydim}{Dim("\marray")}
 % left scope
 \begin{scope}[local bounding box=L]
  \path foreach \X in {1,...,\mydim}
  {(0,{(0.5+0.5*\mydim-\X)*1.5em}) node (h-\X){$h_{\X}$}
   (2,{(0.5+0.5*\mydim-\X)*1.5em}) node (v-\X){$v_{\X}$}
  };
  \node[fit=(h-1) (h-\mydim),ellipse,draw,inner ysep=-1em,
     label=above:$H$] (h){};
  \node[fit=(v-1) (v-\mydim),ellipse,draw,inner ysep=-1em,
     label=above:$V$] (v){};
  \foreach \X in {1,...,\mydim} 
  {\foreach \Y in {1,...,\mydim} 
  {\pgfmathtruncatemacro{\itest}{{\marray}[\X-1][\Y-1]}
  \ifnum\itest=1
   \draw[shorten <=-2pt,shorten >=-1pt] (h-\X) -- (v-\Y);
  \fi}} 
 \end{scope}
 % right scope
 \begin{scope}[local bounding box=R] 
  \path ([xshift=2em+\mydim*2em,yshift=1.5em]v-1.south) coordinate (m-0-1);
  \foreach \X in {1,...,\mydim} 
  {\foreach \Y in {1,...,\mydim} 
  {\pgfmathtruncatemacro{\itest}{{\marray}[\X-1][\Y-1]}
  \ifnum\X=1
   \node[box,anchor=north] (m-\Y-\X) 
   at ([yshift=\pgflinewidth]m-\the\numexpr\Y-1\relax-\X.south){$\itest$};
  \else 
   \node[box,anchor=west] (m-\Y-\X) 
   at ([xshift=-\pgflinewidth]m-\Y-\the\numexpr\X-1\relax.east){$\itest$};
  \fi}} 
  \foreach \X in {1,...,\mydim} 
  {\node[minimum height=1.5em,anchor=north] at (m-\mydim-\X.south){$v_{\X}$};
   \node[minimum height=1.5em,anchor=west] at (m-\X-\mydim.east){$h_{\X}$};
  }
  \path[nodes={draw,minimum height=2em,outer sep=0pt}] 
     (m-1-1.north west) node[anchor=south east] (Fh) {Features $H$}
  (m-1-\mydim.north east) node[rotate=-90,anchor=south east] (Fv) {Features $V$};
  \draw (Fv.south west) -| (Fh.north east) (Fh.south west) |- (m-\mydim-1.south west);
 \end{scope} 
 %
 \path[nodes={text height=1.2em}] (current bounding box.south) coordinate (aux)
  (aux-|L) node[below]{(a)} (aux-|R) node[below]{(b)};
\end{tikzpicture}
\end{document}

enter image description here

Of course, one could also allow other entries as long as it is clear what is to be done with those (double line, different color, whatever).

Alternatively you can type the entries by hand.

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{fit,shapes.geometric,matrix}
\begin{document}
\begin{tikzpicture}[font=\sffamily]
 \matrix[matrix of math nodes,column sep=4em,row sep=1em] (matL){
  h_{\the\pgfmatrixcurrentrow} & v_{\the\pgfmatrixcurrentrow} \\
  h_{\the\pgfmatrixcurrentrow} & v_{\the\pgfmatrixcurrentrow} \\
  h_{\the\pgfmatrixcurrentrow} & v_{\the\pgfmatrixcurrentrow} \\
  h_{\the\pgfmatrixcurrentrow} & v_{\the\pgfmatrixcurrentrow} \\
  h_{\the\pgfmatrixcurrentrow} & v_{\the\pgfmatrixcurrentrow} \\
 };
 \node[fit=(matL-1-1) (matL-5-1),ellipse,draw,inner ysep=-1em,
    label=above:$H$] (h){};
 \node[fit=(matL-1-2) (matL-5-2),ellipse,draw,inner ysep=-1em,
    label=above:$V$] (v){};
 \draw (matL-1-1) -- (matL-1-2) (matL-2-1) -- (matL-3-2)
  (matL-3-1) -- (matL-4-2) (matL-4-1) -- (matL-5-2)
  (matL-4-1) -- (matL-2-2.south west) (matL-5-1) -- (matL-2-2.south west);  
\end{tikzpicture}
%
\begin{tikzpicture}[font=\sffamily]
 \matrix[matrix of math nodes,nodes in empty cells,inner sep=0pt,
    column sep=-\pgflinewidth/2,row sep=-1.5\pgflinewidth,
    cells={nodes={draw,minimum width=2em,minimum height=1.5em,outer sep=0pt}},
    column 6/.style={nodes={draw=none}},
    row 6/.style={nodes={draw=none}}] (mat) {
    1 & 0 & 0 & 0 & 0 & h_{\the\pgfmatrixcurrentrow}\\
    0 & 0 & 1 & 0 & 0 & h_{\the\pgfmatrixcurrentrow}\\
    0 & 0 & 0 & 1 & 0 & h_{\the\pgfmatrixcurrentrow}\\
    0 & 1 & 0 & 0 & 1 & h_{\the\pgfmatrixcurrentrow}\\
    0 & 1 & 0 & 0 & 0 & h_{\the\pgfmatrixcurrentrow}\\
    v_{\the\pgfmatrixcurrentcolumn}  & v_{\the\pgfmatrixcurrentcolumn} & 
        v_{\the\pgfmatrixcurrentcolumn}  &  v_{\the\pgfmatrixcurrentcolumn} & 
        v_{\the\pgfmatrixcurrentcolumn}  & \\
 };
 \path[nodes={draw,minimum height=2em,outer sep=0pt}] 
    (mat-1-1.north west) node[anchor=south east] (Fh) {Features $h$}
 (mat-1-5.north east) node[rotate=-90,anchor=south east] (Fv) {Features $v$};
 \draw (Fv.south west) -| (Fh.north east) (Fh.south west) |- (mat-5-1.south west);
\end{tikzpicture}
\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .