5

I have a matrix and need to highlight some elements in the matrix as in the example. Exemple Thanks

\documentclass{beamer}

\usepackage{tikz}
\usetikzlibrary{calc,arrows,automata,positioning} 

\usepackage{blkarray}

\begin{document}

\begin{frame}{Example}

\begin{figure}[H]
\centering

\begin{tabular}{c c}

    \begin{minipage}{.5\linewidth}
       \scalebox{0.5}{
       $
\renewcommand\arraystretch{1.3}
\begin{blockarray}{ccccccc}
 & 1 & 2 & 3 & 4 & 5 & 6  \\
\begin{block}{@{}c\{[ccccc]c}
1 & 0 & 1 & 0 & 0 & 0 & 0  \\
2 & 0 & 0 & 1 & 0 & 1 & 0  \\
3 & 0 & 0 & 0 & 1 & 0 & 0  \\
4 & 0 & 0 & 0 & 0 & 0 & 0  \\
5 & 1 & 0 & 0 & 0 & 0 & 1  \\
6 & 0 & 1 & 0 & 0 & 0 & 0  \\
\end{block}
s & 1 & 2 & 2 & 2 & 2 \\
\end{blockarray}
$
      }
    \end{minipage}


    \begin{minipage}{.5\linewidth}
        \scalebox{0.5}{
       $
\renewcommand\arraystretch{1.3}
\begin{blockarray}{ccccccc}
 & 1 & 2 & 3 & 4 & 5 & 6  \\
\begin{block}{@{}c\{[ccccc]c}
1 & 0 & 1 & 0 & 0 & 0 & 0  \\
2 & 0 & 0 & 1 & 0 & 1 & 0  \\
3 & 0 & 0 & 0 & 1 & 0 & 0  \\
4 & 0 & 0 & 0 & 0 & 0 & 0  \\
5 & 1 & 0 & 0 & 0 & 0 & 1  \\
6 & 0 & 1 & 0 & 0 & 0 & 0  \\
\end{block}
s & 1 & 2 & 2 & 2 & 2 \\
\end{blockarray}
$
      }
    \end{minipage}

\end{tabular}

\caption{Example}

\end{figure}

\end{frame}

\end{document}
  • Beamer has the \alert command, are you aware of that? –  Jun 14 '18 at 16:08
  • see https://tex.stackexchange.com/questions/69713/ and http://tex.stackexchange.com/questions/257043/, your question is very related to this two. – Zarko Jun 14 '18 at 16:23

2 Answers2

7

To expand the applications of the following framework used to answer TikZ : Drawing a Triangular Block Matrix, an option using the tikz matrix and fit library, through matrix coordinate names to highlight using a simple style of line; whatever the kind of document you are working on, you can include it using the graphicx package, with all the advantages that it has, as I explain in Scaling different components of tikzpicture together. creating a separate tex document with the standalone document class.

RESULT: enter image description here

MWE:

\documentclass[tikz,border=14pt]{standalone}
\usepackage{tikz}  
\usetikzlibrary{matrix,shapes,decorations.pathreplacing,fit,backgrounds}

\begin{document}
    \begin{tikzpicture}[
        %Global config
        >=latex,
        line width=1pt,
        %Styles
        Brace/.style={
            decorate,
            decoration={
                brace,
                raise=-7pt
            }
        },
        Matrix/.style={
            matrix of nodes,
            text height=2.5ex,
            text depth=0.75ex,
            text width=3.25ex,
            align=center,
            %left delimiter=\{,
            %right delimiter=\},
            column sep=5pt,
            row sep=5pt,
            %nodes={draw=black!10}, % Uncoment to see the square nodes.
            nodes in empty cells,
        },
        DA/.style={
            fill,
            opacity=0.2,
            rounded corners,
            inner sep=-3pt,
            line width=1pt,
        },
        DL/.style={
            left delimiter=[,
            right delimiter=],
            inner sep=-2pt,
        },
        DG/.style={
            line cap= round,
            line width =15pt,
            opacity=0.2,
        }
    ]

    \matrix[Matrix] at (0,0) (M){ % Matrix contents  
       & 1 & 2 & 3 & 4 & 5 & 6 \\
    1 & 0 & 1 & 0 & 0 & 0 & 0\\
    2 & 0 & 0 & 1 & 0 & 1 & 0\\
    3 & 0 & 0 & 0 & 1 & 0 & 0\\
    4 & 0 & 0 & 0 & 0 & 0 & 0\\
    5 & 1 & 0 & 0 & 0 & 0 & 1\\
    6 & 0 & 1 & 0 & 0 & 0 & 0\\
    S & 1 & 2 & 2 & 2 & 2 &   \\
    };

    \matrix[Matrix] at (10,0) (M1){ % Matrix contents  
       & 1 & 2 & 3 & 4 & 5 & 6 \\
    1 & 0 & 1 & 0 & 0 & 0 & 0\\
    2 & 0 & 0 & 1 & 0 & 1 & 0\\
    3 & 0 & 0 & 0 & 1 & 0 & 0\\
    4 & 0 & 0 & 0 & 0 & 0 & 0\\
    5 & 1 & 0 & 0 & 0 & 0 & 1\\
    6 & 0 & 1 & 0 & 0 & 0 & 0\\
    S & 1 & 2 & 2 & 2 & 2 &   \\
    };
    \begin{scope}[on background layer] 
        %FOR MATRIX M
        %To delimit internal braces
        \node[DL,fit=(M-2-2)(M-7-6)](subM-1){};
        %To delimit internal area groups
        \node[DA,blue,fit=(M-3-2)(M-5-4)](subM-2){};
        \node[DA,green,fit=(M-6-4)(M-7-6)](subM-3){};
        % For line sectors
        \draw[DG,red](M-2-2.center) --(M-6-6.center);
        \draw[DG,orange](M-1-7.center) --(M-7-7.center);
        \draw[DG,black](M-8-1.center) --(M-8-6.center); 

        %FOR MATRIX M1      
        %To delimit internal braces
        \node[DL,fit=(M1-1-1)(M1-8-7)](subM1-1){};
        \node[DL,fit=(M1-7-2)(M1-7-3)](subM1-2){};
        %To delimit internal area groups
        \node[DA,red,fit=(M1-3-1)(M1-5-4)](subM1-3){};
        \node[DA,green,fit=(M1-6-4)(M1-7-6)](subM1-4){};
        % For line sectors
        \draw[DG,red](M1-2-2.center) --(M1-6-6.center);
        \draw[DG,orange](M1-1-7.center) --(M1-7-7.center);
        \draw[DG,black](M1-8-1.center) --(M1-8-6.center);
        % You can connect Sub groups.
        \draw[->,line width=5pt,red,opacity=0.2] (subM-2) -- (subM1-3) ;    
        \draw[->,line width=5pt,red,opacity=0.2] (subM1-3.south) -| (subM1-2) ;         
    \end{scope}

    \end{tikzpicture}
\end{document}


UPDATE: Integration in Beamer document class.

ISSUES: MAtrix command ampersand has incompatibilities, solution taken from tikz matrix undefined control sequence, using tikz config option ampersand replacement=\&.

MWE:

% PROCESADOR ARARA V3.0
% arara: pdflatex: {synctex: yes, action: nonstopmode}

\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{matrix,shapes,decorations.pathreplacing,fit,backgrounds}

\begin{document}

    \begin{frame}
    \frametitle{Example}
    \centering
    \begin{figure}[H]
        \tikzset{
            %Global config
            >=latex,
            font=\small,
            line width=1pt,
            %Styles
            Brace/.style={
                decorate,
                decoration={
                    brace,
                    raise=-7pt
                }
            },
            Matrix/.style={
                matrix of nodes,
                text height=8pt,
                text depth=0pt,
                text width=8pt,
                align=center,
                %left delimiter=\{,
                %right delimiter=\},
                column sep=0pt,
                row sep=0pt,
                %nodes={draw=black!10}, % Uncoment to see the square nodes.
                nodes in empty cells,
            },
            DA/.style={
                fill,
                opacity=0.2,
                rounded corners,
                inner sep=-3pt,
                line width=1pt,
            },
            DL/.style={
                left delimiter=[,
                right delimiter=],
                inner sep=-2pt,
            },
            DG/.style={
                line cap= round,
                line width =15pt,
                opacity=0.2,
            }
        }
        \begin{tikzpicture}[ampersand replacement=\&]

        \matrix[Matrix] at (0,0) (M){ % Matrix contents  
            \& 1 \& 2 \& 3 \& 4 \& 5 \& 6 \\
            1 \& 0 \& 1 \& 0 \& 0 \& 0 \& 0\\
            2 \& 0 \& 0 \& 1 \& 0 \& 1 \& 0\\
            3 \& 0 \& 0 \& 0 \& 1 \& 0 \& 0\\
            4 \& 0 \& 0 \& 0 \& 0 \& 0 \& 0\\
            5 \& 1 \& 0 \& 0 \& 0 \& 0 \& 1\\
            6 \& 0 \& 1 \& 0 \& 0 \& 0 \& 0\\
            S \& 1 \& 2 \& 2 \& 2 \& 2 \&   \\
        };

        \matrix[Matrix] at (5,0) (M1){ % Matrix contents  
            \& 1 \& 2 \& 3 \& 4 \& 5 \& 6 \\
            1 \& 0 \& 1 \& 0 \& 0 \& 0 \& 0\\
            2 \& 0 \& 0 \& 1 \& 0 \& 1 \& 0\\
            3 \& 0 \& 0 \& 0 \& 1 \& 0 \& 0\\
            4 \& 0 \& 0 \& 0 \& 0 \& 0 \& 0\\
            5 \& 1 \& 0 \& 0 \& 0 \& 0 \& 1\\
            6 \& 0 \& 1 \& 0 \& 0 \& 0 \& 0\\
            S \& 1 \& 2 \& 2 \& 2 \& 2 \&   \\
        };
        \begin{scope}[on background layer,transform canvas={scale=.5}] 
        %FOR MATRIX M
        %To delimit internal braces
        \node[DL,fit=(M-2-2)(M-7-6)](subM-1){};
        %To delimit internal area groups
        \node[DA,blue,fit=(M-3-2)(M-5-4)](subM-2){};
        \node[DA,green,fit=(M-6-4)(M-7-6)](subM-3){};
        % For line sectors
        \draw[DG,red](M-2-2.center) --(M-6-6.center);
        \draw[DG,orange](M-1-7.center) --(M-7-7.center);
        \draw[DG,black](M-8-1.center) --(M-8-6.center); 

        %FOR MATRIX M1      
        %To delimit internal braces
        \node[DL,fit=(M1-1-1)(M1-8-7)](subM1-1){};
        \node[DL,fit=(M1-7-2)(M1-7-3)](subM1-2){};
        %To delimit internal area groups
        \node[DA,red,fit=(M1-3-1)(M1-5-4)](subM1-3){};
        \node[DA,green,fit=(M1-6-4)(M1-7-6)](subM1-4){};
        % For line sectors
        \draw[DG,red](M1-2-2.center) --(M1-6-6.center);
        \draw[DG,orange](M1-1-7.center) --(M1-7-7.center);
        \draw[DG,black](M1-8-1.center) --(M1-8-6.center);
        % You can connect Sub groups.
        \draw[->,line width=3pt,red,opacity=0.2] (subM-2) -- (subM1-3) ;    
        \draw[->,line width=3pt,red,opacity=0.2] (subM1-3.south) -| (subM1-2) ;         
        \end{scope}

        \end{tikzpicture}\\
        \caption{Example}  
    \end{figure}
\end{frame}
\end{document}

RESULT:

enter image description here

J Leon V.
  • 11,533
  • 16
  • 47
1

One possibility is to set nodes and then do the highlighting with overlays.

\documentclass{beamer}

\usepackage{tikz}
\usetikzlibrary{calc,arrows,automata,positioning,fit} 

\newcommand{\tikznode}[2]{\relax
\ifmmode%
  \tikz[remember picture,baseline=(#1.base),inner sep=0pt] \node (#1) {$%\tikznodestyle 
  #2$};
\else
  \tikz[remember picture,baseline=(#1.base),inner sep=0pt] \node (#1) {#2};%
\fi}

\usepackage{blkarray}

\begin{document}

\begin{frame}{Example}

\begin{figure}[H]
\centering

\begin{columns}
\begin{column}{.5\linewidth}
       \scalebox{0.5}{
       $
\renewcommand\arraystretch{1.3}
\begin{blockarray}{ccccccc}
 & 1 & 2 & 3 & 4 & 5 & \tikznode{1}{6}  \\
\begin{block}{@{}c\{[ccccc]c}
1 & 0 & 1 & 0 & 0 & 0 & 0  \\
2 & 0 & 0 & 1 & 0 & 1 & 0  \\
3 & 0 & 0 & 0 & 1 & 0 & 0  \\
4 & 0 & 0 & 0 & 0 & 0 & 0  \\
5 & 1 & 0 & 0 & 0 & 0 & 1  \\
6 & 0 & 1 & 0 & 0 & 0  & \tikznode{2}{0} \\
\end{block}
\tikznode{s}{s} & 1 & 2 & 2 & 2 & \tikznode{3}{2} \\
\end{blockarray}
$
\tikz[overlay,remember picture]{
\node[fill=blue,opacity=0.3,fit=(1) (2)]{}; 
\node[fill=blue,opacity=0.3,fit=(s) (3)]{}; }
      }
    \end{column}
    \begin{column}{.5\linewidth}
        \scalebox{0.5}{
       $
\renewcommand\arraystretch{1.3}
\begin{blockarray}{ccccccc}
 & 1 & 2 & 3 & 4 & 5 & \tikznode{5}{6}  \\
\begin{block}{@{}c\{[ccccc]c}
1 & \tikznode{A}{0} & 1 & 0 & 0 & 0 & 0  \\
2 & 0 & 0 & 1 & 0 & 1 & 0  \\
3 & 0 & 0 & 0 & 1 & 0 & 0  \\
4 & 0 & 0 & 0 & 0 & 0 & 0  \\
5 & 1 & 0 & 0 & 0 & \tikznode{B}{0}  & 1  \\
6 & 0 & 1 & 0 & 0 & 0 & \tikznode{6}{0}  \\
\end{block}
\tikznode{7}{s} & 1 & 2 & 2 & 2 & \tikznode{8}{2} \\
\end{blockarray}
$
\tikz[overlay,remember picture]{
\node[fill=green,opacity=0.3,fit=(5) (6)]{}; 
\node[fill=green,opacity=0.3,fit=(7) (8)]{}; 
\path let \p1=(A), \p2=(B), \n1={atan2(\y2-\y1,\x2-\x1)} in
node[fill=red,opacity=0.3,rotate fit=\n1,fit=(A) (B)]{}; }
      }
    \end{column}
\end{columns}

\caption{Example}

\end{figure}

\end{frame}

\end{document}

enter image description here