12

I would like to have matrix like this one: enter image description here

I know how to make color frames in tikz, but I havee problems with braces. I tried this code:

\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}

\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{mathtools} %do robienia mini macierzy
%           linki w spisie tresci
\usepackage[customcolors]{hf-tikz} % do kolorowych macierzy
\usetikzlibrary{patterns}
\usetikzlibrary{decorations.pathreplacing}



\newcommand{\tikzmark}[1]{\tikz[overlay,remember picture,baseline=(#1.base)]
  \node (#1) {\strut};}

%opening
\title{}
\author{}

\begin{document}
\begin{equation}
  \left[ \begin{array}{cccccc}
    v_{1,1}  &  v_{1,2} & \tikzmark{F1p} v_{1,3}  \tikzmark{F1k} & \tikzmark{F2p} \textcolor{white}{v_{1,4}}  \tikzmark{F2k}&  &   \\
    v_{2,1}  & v_{2,2} & v_{2,3} & & &   \\
    \tikzmark{E1p} v_{3,1}  & v_{3,2} & v_{3,3} & v_{3,4}   & &   \\
     & &  v_{4,3} & v_{4,4} & v_{4,5}  & v_{4,6} \\
    \tikzmark{E1k} & & & v_{5,4} & v_{5,5} & v_{5,6}   \\
    & &  & v_{6,4} & v_{6,5} & v_{6,6} \\
  \end{array}\right]
\end{equation}



\begin{tikzpicture}[overlay, remember picture,decoration={brace,amplitude=2pt}]
\draw[decorate,thick] (F1p.north) -- (F1k.north)
      node [midway,above=5pt] {$F'$};
\draw[decorate,thick] (F2p.north) -- (F2k.north)
      node [midway,above=5pt] {$F''$};

\draw [decorate,thick,xshift=-2pt,yshift=0pt]
(E1p) -- (E1k) node [black,midway,xshift=-0.6cm] 
{\footnotesize $P_1$};
\end{tikzpicture}

\end{document}

but it gives the following result: enter image description here

I only know a bit how to make upper braces. How should I make it properly?

I tried to use the code:

\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}

\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{mathtools} %do robienia mini macierzy
%           linki w spisie tresci
\usepackage[customcolors]{hf-tikz} % do kolorowych macierzy
\usetikzlibrary{patterns}
\usetikzlibrary{matrix,decorations.pathreplacing,calc}



\newcommand{\tikzmark}[1]{\tikz[overlay,remember picture,baseline=(#1.base)]
  \node (#1) {\strut};}

%opening
\title{}
\author{}

\pgfkeys{tikz/mymatrixenv/.style={decoration=brace,every left delimiter/.style={xshift=3pt},every right delimiter/.style={xshift=-3pt}}}
\pgfkeys{tikz/mymatrix/.style={matrix of math nodes,left delimiter=[,right delimiter={]},inner sep=2pt,column sep=1em,row sep=0.5em,nodes={inner sep=0pt}}}
\pgfkeys{tikz/mymatrixbrace/.style={decorate,thick}}
\newcommand\mymatrixbraceoffseth{0.5em}
\newcommand\mymatrixbraceoffsetv{0.2em}


\newcommand*\mymatrixbraceright[4][m]{
    \draw[mymatrixbrace] ($(#1.north west)!(#1-#3-1.south west)!(#1.south west)-(\mymatrixbraceoffseth,0)$)
        -- node[left=2pt] {#4} 
        ($(#1.north west)!(#1-#2-1.north west)!(#1.south west)-(\mymatrixbraceoffseth,0)$);
}
\newcommand*\mymatrixbraceleft[4][m]{
    \draw[mymatrixbrace] ($(#1.north east)!(#1-#2-1.north east)!(#1.south east)+(\mymatrixbraceoffseth,0)$)
        -- node[right=2pt] {#4} 
        ($(#1.north east)!(#1-#3-1.south east)!(#1.south east)+(\mymatrixbraceoffseth,0)$);
}
\newcommand*\mymatrixbracetop[4][m]{
    \draw[mymatrixbrace] ($(#1.north west)!(#1-1-#2.north west)!(#1.north east)+(0,\mymatrixbraceoffsetv)$)
        -- node[above=2pt] {#4} 
        ($(#1.north west)!(#1-1-#3.north east)!(#1.north east)+(0,\mymatrixbraceoffsetv)$);
}
\newcommand*\mymatrixbracebottom[4][m]{
    \draw[mymatrixbrace] ($(#1.south west)!(#1-1-#3.south east)!(#1.south east)-(0,\mymatrixbraceoffsetv)$)
        -- node[below=2pt] {#4} 
        ($(#1.south west)!(#1-1-#2.south west)!(#1.south east)-(0,\mymatrixbraceoffsetv)$);
}

\begin{document}



\begin{equation}
\mathbf{X} = 
\begin{tikzpicture}[baseline=0cm,mymatrixenv]
    \matrix [mymatrix,inner sep=4pt,row sep=1em] (m)  
    {
    v_{1,1}  &  v_{1,2} & \tikzmark{F1p} v_{1,3}  \tikzmark{F1k} & \tikzmark{F2p} \textcolor{white}{v_{1,4}}  \tikzmark{F2k}&  &   \\
    v_{2,1}  & v_{2,2} & v_{2,3} & & &   \\
    \tikzmark{E1p} v_{3,1}  & v_{3,2} & v_{3,3} & v_{3,4}   & &   \\
     & &  v_{4,3} & v_{4,4} & v_{4,5}  & v_{4,6} \\
    \tikzmark{E1k} & & & v_{5,4} & v_{5,5} & v_{5,6}   \\
    & &  & v_{6,4} & v_{6,5} & v_{6,6} \\
    };

    % Braces     
    \mymatrixbraceright{1}{3}{$B'$}
    \mymatrixbraceright{4}{6}{$B''$}
%    \mymatrixbracetop{1}{3}{$C'$}
%    \mymatrixbracetop{4}{6}{$C''$}
%    \mymatrixbracebottom{3}{3}{$F'$}
%    \mymatrixbracebottom{4}{4}{$F''$}
\end{tikzpicture}
\end{equation}



\end{document}

but it gives the error: "package pgf error: no shape named m-4-1 is known \mymatrixbraceright{4}{6}{$B''$}"

works much better, after changing corresponding line to:

\pgfkeys{tikz/mymatrix/.style={matrix of math nodes, nodes in empty cells, left delimiter=[,right delimiter={]},inner sep=2pt,column sep=1em,row sep=0.5em,nodes={inner sep=0pt}}}

but still there is a gap. How to delete the gap or make it smaller? enter image description here

  • Could this answer help? It would require to change your matrix to a Tikz matrix, but that might also help with adding the colored areas. – Max Jul 27 '18 at 9:42
  • thanks, but I still have problems. After implementation of the answer I obtain errors with drawing brace 4-6. I mean: \mymatrixbraceright{4}{6}{$B''$} , gives error no shape named m-4-1 – Filip Parker Jul 27 '18 at 11:03
  • If you could update your question to include what you have achieved so far, I can try something :) – Max Jul 27 '18 at 11:05
  • You only have to add the nodes in empty cells key to your matrix and it should work. – Max Jul 27 '18 at 11:39
  • thanks! now it's working, but not perfect. I obtained [link] (i.imgur.com/Ob7F8FQ.png). Why the second bracket is shorter than the upper and there is so big gap between them? – Filip Parker Jul 27 '18 at 12:06
6

An (maybe simpler) alternative to @Milo's excellent answer, I've given all nodes a minimum size.

enter image description here

I also changed the \mymatrixbrace<side> commands slightly so you don't need the calc library anymore.

\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}

\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{mathtools} %do robienia mini macierzy
%           linki w spisie tresci
\usepackage[customcolors]{hf-tikz} % do kolorowych macierzy
\usetikzlibrary{patterns}
\usetikzlibrary{matrix,decorations.pathreplacing}

%opening
\title{}
\author{}

\pgfkeys{tikz/mymatrixenv/.style={decoration={brace},every left delimiter/.style={xshift=8pt},every right delimiter/.style={xshift=-8pt}}}
\pgfkeys{tikz/mymatrix/.style={matrix of math nodes,nodes in empty cells,left delimiter={[},right delimiter={]},inner sep=1pt,outer sep=1.5pt,column sep=2pt,row sep=2pt,nodes={minimum width=20pt,minimum height=10pt,anchor=center,inner sep=0pt,outer sep=0pt}}}
\pgfkeys{tikz/mymatrixbrace/.style={decorate,thick}}

\newcommand*\mymatrixbraceright[4][m]{
    \draw[mymatrixbrace] (#1.west|-#1-#3-1.south west) -- node[left=2pt] {#4} (#1.west|-#1-#2-1.north west);
}
\newcommand*\mymatrixbraceleft[4][m]{
    \draw[mymatrixbrace] (#1.east|-#1-#2-1.north east) -- node[right=2pt] {#4} (#1.east|-#1-#2-1.south east);
}
\newcommand*\mymatrixbracetop[4][m]{
    \draw[mymatrixbrace] (#1.north-|#1-1-#2.north west) -- node[above=2pt] {#4} (#1.north-|#1-1-#3.north east);
}
\newcommand*\mymatrixbracebottom[4][m]{
    \draw[mymatrixbrace] (#1.south-|#1-1-#2.north east) -- node[below=2pt] {#4} (#1.south-|#1-1-#3.north west);
}

\begin{document}

\begin{equation}
    \mathbf{X} = 
    \begin{tikzpicture}[baseline={-0.5ex},mymatrixenv]
        \matrix [mymatrix,inner sep=4pt] (m)  
        {
        v_{1,1} & v_{1,2} & v_{1,3} &         &         &         \\
        v_{2,1} & v_{2,2} & v_{2,3} &         &         &         \\
        v_{3,1} & v_{3,2} & v_{3,3} & v_{3,4} &         &         \\
                &         & v_{4,3} & v_{4,4} & v_{4,5} & v_{4,6} \\
                &         &         & v_{5,4} & v_{5,5} & v_{5,6} \\
                &         &         & v_{6,4} & v_{6,5} & v_{6,6} \\
        };

        % Braces     
        \mymatrixbraceright{1}{3}{$B'$}
        \mymatrixbraceright{4}{6}{$B''$}
        \mymatrixbracetop{1}{3}{$C'$}
        \mymatrixbracetop{4}{6}{$C''$}
        \mymatrixbracebottom{3}{3}{$F'$}
        \mymatrixbracebottom{4}{4}{$F''$}
        \mymatrixbraceleft{3}{3}{$E'$}
        \mymatrixbraceleft{4}{4}{$E''$}
    \end{tikzpicture}
\end{equation}



\end{document}
11

UPDATE

Here is one way to do it. The steps are laid out in my original answer below. Not claiming this is the best way, one could optimise the steps certainly, but it works for this particular matrix.

Seeing as you need to draw coloured boxes around certain subsets of this matrix anyway, you can use the fit library for that and subsequently use those coordinates to help guide where your braces go.

enter image description here

\documentclass[margin=0.5cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix,decorations.pathreplacing,calc,fit}

\pgfkeys{tikz/mymatrixenv/.style={decoration=brace,every left delimiter/.style={xshift=4.7pt},every right delimiter/.style={xshift=-4.7pt}}}
\pgfkeys{tikz/mymatrix/.style={matrix of math nodes, nodes in empty cells, left delimiter=[,right delimiter={]},inner sep=2pt,column sep=1em,row sep=0.5em,nodes={inner sep=0pt}}}
\pgfkeys{tikz/mymatrixbrace/.style={decorate,thick}}

% The hack required for foreach loops in fit. Code from https://tex.stackexchange.com/questions/4751/fitting-a-list-of-points-with-tikz-and-its-foreach?noredirect=1&lq=1
\makeatletter
\def\tikz@lib@fit@scan{%
  \pgfutil@ifnextchar\pgf@stop{\pgfutil@gobble}{%
    \pgfutil@ifnextchar\foreach{\tikz@lib@fit@scan@handle@foreach}{%
      \tikz@scan@one@point\tikz@lib@fit@scan@handle}}}
\def\tikz@lib@fit@scan@handle@foreach\foreach#1in#2#3{%
  \foreach #1 in {#2}
  {\tikz@scan@one@point\tikz@lib@fit@scan@handle@foreach@#3}
  \tikz@lib@fit@scan}
\def\tikz@lib@fit@scan@handle@foreach@#1{%
  \iftikz@shapeborder
    \tikz@lib@fit@adjust{%
      \pgfpointanchor{\tikz@shapeborder@name}{west}}%
    \tikz@lib@fit@adjust{%
      \pgfpointanchor{\tikz@shapeborder@name}{east}}%
    \tikz@lib@fit@adjust{%
      \pgfpointanchor{\tikz@shapeborder@name}{north}}%
    \tikz@lib@fit@adjust{%
      \pgfpointanchor{\tikz@shapeborder@name}{south}}%
  \else
    \tikz@lib@fit@adjust{#1}%
  \fi
  \global\pgf@xa=\pgf@xa
  \global\pgf@ya=\pgf@ya
  \global\pgf@xb=\pgf@xb
  \global\pgf@yb=\pgf@yb}
\makeatletter

\begin{document}


\begin{tikzpicture}[baseline=0cm,mymatrixenv]
    \matrix [mymatrix,outer ysep=0.7pt,inner sep=4pt,row sep=1em] (m)  
    {
    v_{1,1}  &  v_{1,2} &  v_{1,3}  & &  &   \\
    v_{2,1}  & v_{2,2} & v_{2,3} & & &   \\
   v_{3,1}  & v_{3,2} & v_{3,3} & v_{3,4}   & &   \\
     & &  v_{4,3} & v_{4,4} & v_{4,5}  & v_{4,6} \\
    & & & v_{5,4} & v_{5,5} & v_{5,6}   \\
    & &  & v_{6,4} & v_{6,5} & v_{6,6} \\
    };

% Colours
\definecolor{brightpurple}{HTML}{C151EF}

% Fitting - note to get a \foreach loop in these fits requires the hack from https://tex.stackexchange.com/questions/4751/fitting-a-list-of-points-with-tikz-and-its-foreach
\node [fit= \foreach \X in {1,...,3}{(m-\X-1)}
            \foreach \X in {1,...,3}{(m-\X-2)}
            \foreach \X in {1,...,3}{(m-\X-3)}]
            [draw=green, thick,inner sep=2.6pt] (fit-a) {};

\node [fit= \foreach \X in {4,...,6}{(m-\X-4)}
            \foreach \X in {4,...,6}{(m-\X-5)}
            \foreach \X in {4,...,6}{(m-\X-6)}]
            [draw=cyan, thick,inner sep=2.6pt] (fit-b) {};

\node [fit= \foreach \X in {1,...,4}{(m-\X-3)}] [draw=brightpurple,fill=brightpurple,fill opacity=0.1, thick,inner sep=1.8pt] (fit-purple-a) {};
\node [fit= \foreach \X in {3,...,6}{(m-\X-4)}] [draw=brightpurple,fill=brightpurple,fill opacity=0.1, thick,inner sep=1.8pt]  (fit-purple-b) {};       
\node [fit= \foreach \X in {1,...,4}{(m-3-\X)}] [draw=brown,fill=brown,fill opacity=0.1, thick,inner sep=1pt] (fit-brown-a) {};   \node [fit= \foreach \X in {3,...,6}{(m-4-\X)}] [draw=brown,fill=brown,fill opacity=0.1, thick,inner sep=1pt] (fit-brown-b) {}; 
\node [fit = (m-3-3) (m-3-4) (m-4-3) (m-4-4)] [draw=orange,fill=orange,fill opacity=0.1, thick,inner sep=2.6pt] {};

% FINDING VERTICAL MIDPOINT
 \node [fit= \foreach \X in {1,...,3}{
            (m-\X-1)}] (fit-one)  {}; 
 \node [fit= \foreach \X in {4,...,6}{
            (m-\X-6)}] (fit-two)  {}; 
\path (fit-one.south) -- (fit-two.north) coordinate[midway] (X);

% FINDING HORIZONTAL MIDPOINT
 \node [fit= \foreach \X in {1,...,3}{
            (m-1-\X)}] (fit-one)  {}; 
 \node [fit= \foreach \X in {4,...,6}{
            (m-6-\X)}] (fit-two)  {}; 
\path (fit-one.east) -- (fit-two.west) coordinate[midway] (Y);

\newcommand\mymatrixbraceoffseth{0.3em}
\newcommand\mymatrixbraceoffsetv{0.3em}

% LHS BRACES
\draw [mymatrixbrace] ($(m.north west)!(fit-a.south)!(m.south west)-(\mymatrixbraceoffseth,0)$)   -- node[left=2pt] {$B'$}  ($(m.north west)!(fit-a.north)!(m.south west)-(\mymatrixbraceoffseth,0)$);
\draw [mymatrixbrace] ($(m.north west)!(fit-b.south)!(m.south west)-(\mymatrixbraceoffseth,0)$) -- node[left=2pt] {$B''$} ($(m.north west)!(fit-b.north)!(m.south west)-(\mymatrixbraceoffseth,0)$);

% RHS BRACES
\draw [mymatrixbrace]  ($(m.north east)!([yshift=-0.02cm]X)!(m.south east)+(\mymatrixbraceoffseth,0)$)   -- node[right=2pt] {$E''$} ($(m.north east)!(fit-brown-b.south)!(m.south east)+(\mymatrixbraceoffseth,0)$);
 \draw [mymatrixbrace]  ($(m.north east)!(fit-brown-a.north)!(m.south east)+(\mymatrixbraceoffseth,0)$) --node[right=2pt] {$E'$}   ($(m.north east)! ([yshift=+0.02cm]X)!(m.south east)+(\mymatrixbraceoffseth,0)$);

% TOP BRACES       
\draw[mymatrixbrace] ($(m.north west)!([xshift=0.05cm]Y)!(m.north east)+(0,\mymatrixbraceoffsetv)$) -- node[above=2pt] {$C''$}  ($(m.north west)!(fit-b.east)!(m.north east)+(0,\mymatrixbraceoffsetv)$);
\draw[mymatrixbrace] ($(m.north west)!(fit-a.west)!(m.north east)+(0,\mymatrixbraceoffsetv)$)-- node[above=2pt] {$C'$}   ($(m.north west)!([xshift=-0.05cm]Y)!(m.north east)+(0,\mymatrixbraceoffsetv)$);

% BOTTOM BRACES      
\draw[mymatrixbrace] ($(m.south west)!([xshift=-0.05cm]Y)!(m.south east)-(0,\mymatrixbraceoffsetv)$)-- node[below=2pt] {$F'$}  ($(m.south west)!(fit-purple-a.west)!(m.south east)-(0,\mymatrixbraceoffsetv)$);   
\draw[mymatrixbrace] ($(m.south west)!(fit-purple-b.east)!(m.south east)-(0,\mymatrixbraceoffsetv)$) -- node[below=2pt] {$F''$} ($(m.south west)!([xshift=0.05cm]Y)!(m.south east)-(0,\mymatrixbraceoffsetv)$);   

\end{tikzpicture}
\end{document}

ORIGINAL ANSWER

Here is a proof of concept to show how you can remove the gap between the two braces. I've drawn boxes around all the nodes to illustrate where the boundaries lie. The reason why in your example above the lower bracket B'' was shorter than the upper bracket B' was because it was only measuring the coordinates from cells in the first column. (It's just a limitation of the custom command \mymatrixbraceright as its been defined in your MWE). But see how in the first column cell (6,1) has a smaller bounding box than say cell (6,6)?

The method I've used here is to perform a fit around cells (1,1), (2,1) and (3,1) and perform a fit around cells (4,6), (5,6) and (6,6). Using these two fits I can find the midway point between them. This takes inspiration from Automatically find which nodes are closest, to aid drawing lines within a TikZ matrix.

Using this midpoint I can then manually draw the braces starting from the top of (1,1) to (X). And from (X) to the bottom of (6,6).

enter image description here

\documentclass[margin=0.5cm]{standalone}

\usepackage{mathtools}
\usepackage{tikz}
\usetikzlibrary{matrix,decorations.pathreplacing,calc,fit}

\pgfkeys{tikz/mymatrixenv/.style={decoration=brace,every left delimiter/.style={xshift=3pt},every right delimiter/.style={xshift=-3pt}}}
\pgfkeys{tikz/mymatrix/.style={matrix of math nodes, nodes in empty cells, left delimiter=[,right delimiter={]},inner sep=2pt,column sep=1em,row sep=0.5em,nodes={inner sep=0pt}}}
\pgfkeys{tikz/mymatrixbrace/.style={decorate,thick}}
\newcommand\mymatrixbraceoffseth{0.5em}
\newcommand\mymatrixbraceoffsetv{0em}


% CODE from https://tex.stackexchange.com/questions/4751/fitting-a-list-of-points-with-tikz-and-its-foreach?noredirect=1&lq=1
\makeatletter
\def\tikz@lib@fit@scan{%
  \pgfutil@ifnextchar\pgf@stop{\pgfutil@gobble}{%
    \pgfutil@ifnextchar\foreach{\tikz@lib@fit@scan@handle@foreach}{%
      \tikz@scan@one@point\tikz@lib@fit@scan@handle}}}
\def\tikz@lib@fit@scan@handle@foreach\foreach#1in#2#3{%
  \foreach #1 in {#2}
  {\tikz@scan@one@point\tikz@lib@fit@scan@handle@foreach@#3}
  \tikz@lib@fit@scan}
\def\tikz@lib@fit@scan@handle@foreach@#1{%
  \iftikz@shapeborder
    \tikz@lib@fit@adjust{%
      \pgfpointanchor{\tikz@shapeborder@name}{west}}%
    \tikz@lib@fit@adjust{%
      \pgfpointanchor{\tikz@shapeborder@name}{east}}%
    \tikz@lib@fit@adjust{%
      \pgfpointanchor{\tikz@shapeborder@name}{north}}%
    \tikz@lib@fit@adjust{%
      \pgfpointanchor{\tikz@shapeborder@name}{south}}%
  \else
    \tikz@lib@fit@adjust{#1}%
  \fi
  \global\pgf@xa=\pgf@xa
  \global\pgf@ya=\pgf@ya
  \global\pgf@xb=\pgf@xb
  \global\pgf@yb=\pgf@yb}
\makeatletter


\begin{document}


\begin{tikzpicture}[baseline=0cm,mymatrixenv]
    \matrix [mymatrix,inner sep=4pt,row sep=1em,draw,nodes={draw}] (m)  
    {
    v_{1,1}  &  v_{1,2} &  v_{1,3}  & &  &   \\
    v_{2,1}  & v_{2,2} & v_{2,3} & & &   \\
   v_{3,1}  & v_{3,2} & v_{3,3} & v_{3,4}   & &   \\
     & &  v_{4,3} & v_{4,4} & v_{4,5}  & v_{4,6} \\
    & & & v_{5,4} & v_{5,5} & v_{5,6}   \\
    & &  & v_{6,4} & v_{6,5} & v_{6,6} \\
    };

 \node [fit= \foreach \X in {1,...,3}{
            (m-\X-1)}] (fit-a) [draw] {}; 
 \node [fit= \foreach \X in {4,...,6}{
            (m-\X-6)}] (fit-b) [draw] {}; 
\path (fit-a.south) -- (fit-b.north) coordinate[midway] (X);

\draw [mymatrixbrace] ($(m.north west)!(X)!(m.south west)-(\mymatrixbraceoffseth,0)$)
        -- node[left=2pt] {$B'$} 
        ($(m.north west)!(m-1-1.north west)!(m.south west)-(\mymatrixbraceoffseth,0)$);

\draw [mymatrixbrace] ($(m.north west)!(m-6-6.south)!(m.south west)-(\mymatrixbraceoffseth,0)$)
        -- node[left=2pt] {$B'$} 
        ($(m.north west)!(X)!(m.south west)-(\mymatrixbraceoffseth,0)$);

\end{tikzpicture}

\end{document}

To get something closer to the image you posted. I suggest adding shifting the y coordinate of (X) slightly. Here I've used yshift=0.05cm on the upper brace and yshift=-0.05cm on the lower brace. Also, to make the braces reach the extremities of the matrix bracket, instead of drawing them from the north boundary of (1,1) and the south boundary of (6,6), I've changed it so they are drawn from m.north and from m.south. I've also added outer ysep=0.5pt to increase the length of the square brackets slightly. These numbers can be played with as you see fit.

enter image description here

\documentclass[margin=0.5cm]{standalone}

\usepackage{mathtools}
\usepackage{tikz}
\usetikzlibrary{matrix,decorations.pathreplacing,calc,fit}

\pgfkeys{tikz/mymatrixenv/.style={decoration=brace,every left delimiter/.style={xshift=3pt},every right delimiter/.style={xshift=-3pt}}}
\pgfkeys{tikz/mymatrix/.style={matrix of math nodes, nodes in empty cells, left delimiter=[,right delimiter={]},inner sep=2pt,column sep=1em,row sep=0.5em,nodes={inner sep=0pt}}}
\pgfkeys{tikz/mymatrixbrace/.style={decorate,thick}}
\newcommand\mymatrixbraceoffseth{0.5em}
\newcommand\mymatrixbraceoffsetv{0em}


% CODE from https://tex.stackexchange.com/questions/4751/fitting-a-list-of-points-with-tikz-and-its-foreach?noredirect=1&lq=1
\makeatletter
\def\tikz@lib@fit@scan{%
  \pgfutil@ifnextchar\pgf@stop{\pgfutil@gobble}{%
    \pgfutil@ifnextchar\foreach{\tikz@lib@fit@scan@handle@foreach}{%
      \tikz@scan@one@point\tikz@lib@fit@scan@handle}}}
\def\tikz@lib@fit@scan@handle@foreach\foreach#1in#2#3{%
  \foreach #1 in {#2}
  {\tikz@scan@one@point\tikz@lib@fit@scan@handle@foreach@#3}
  \tikz@lib@fit@scan}
\def\tikz@lib@fit@scan@handle@foreach@#1{%
  \iftikz@shapeborder
    \tikz@lib@fit@adjust{%
      \pgfpointanchor{\tikz@shapeborder@name}{west}}%
    \tikz@lib@fit@adjust{%
      \pgfpointanchor{\tikz@shapeborder@name}{east}}%
    \tikz@lib@fit@adjust{%
      \pgfpointanchor{\tikz@shapeborder@name}{north}}%
    \tikz@lib@fit@adjust{%
      \pgfpointanchor{\tikz@shapeborder@name}{south}}%
  \else
    \tikz@lib@fit@adjust{#1}%
  \fi
  \global\pgf@xa=\pgf@xa
  \global\pgf@ya=\pgf@ya
  \global\pgf@xb=\pgf@xb
  \global\pgf@yb=\pgf@yb}
\makeatletter


\begin{document}


\begin{tikzpicture}[baseline=0cm,mymatrixenv]
    \matrix [mymatrix,outer ysep=0.5pt,inner sep=4pt,row sep=1em] (m)  
    {
    v_{1,1}  &  v_{1,2} &  v_{1,3}  & &  &   \\
    v_{2,1}  & v_{2,2} & v_{2,3} & & &   \\
   v_{3,1}  & v_{3,2} & v_{3,3} & v_{3,4}   & &   \\
     & &  v_{4,3} & v_{4,4} & v_{4,5}  & v_{4,6} \\
    & & & v_{5,4} & v_{5,5} & v_{5,6}   \\
    & &  & v_{6,4} & v_{6,5} & v_{6,6} \\
    };

 \node [fit= \foreach \X in {1,...,3}{
            (m-\X-1)}] (fit-a)  {}; 
 \node [fit= \foreach \X in {4,...,6}{
            (m-\X-6)}] (fit-b)  {}; 
\path (fit-a.south) -- (fit-b.north) coordinate[midway] (X);

\draw [mymatrixbrace] ($(m.north west)!([yshift=0.05cm]X)!(m.south west)-(\mymatrixbraceoffseth,0)$)
        -- node[left=2pt] {$B'$} 
        ($(m.north west)!(m.north west)!(m.south west)-(\mymatrixbraceoffseth,0)$);

\draw [mymatrixbrace] ($(m.north west)!(m.south)!(m.south west)-(\mymatrixbraceoffseth,0)$)
        -- node[left=2pt] {$B''$} 
        ($(m.north west)!([yshift=-0.05cm]X)!(m.south west)-(\mymatrixbraceoffseth,0)$);

\end{tikzpicture}

\end{document}
  • 3
    +1 from a marmot ;-) – marmot Jul 27 '18 at 15:56
  • 1
    +1 for everybody for the patience and time that everyone has dedicated. – Sebastiano Jul 27 '18 at 21:01
9

An option including some tricks for the patterns, using fit to highlight elements and get the coordinates for the external braces; I use an Auxiliar node to find the intersectións between highlighted boxes to draw the braces, also to draw the brackets for the matrix and control the separations with inne xsep, and inner ysep; finally I modify the left and right delimiter to increase the line thickness.

RESULT in article class:

enter image description here

MWE in article class:

% arara: pdflatex: {synctex: yes, action: nonstopmode}
\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{lipsum}
\usepackage{mathtools} %do robienia mini macierzy
%           linki w spisie tresci
\usepackage[customcolors]{hf-tikz} % do kolorowych macierzy
\definecolor{mygreen}{HTML}{21EE7D}
\definecolor{mypurple}{HTML}{BB1BFF}
\usetikzlibrary{matrix,fit,backgrounds,patterns,decorations.pathreplacing}

%opening
\title{}
\author{}
%Create a new patern for firefox and adobe reader from https://tex.stackexchange.com/a/219808/154390
\pgfdeclarepatternformonly{north east lines b}{\pgfqpoint{0pt}{0pt}}{\pgfqpoint{20pt}{20pt}}{\pgfqpoint{20pt}{20pt}}%
{
    \pgfsetlinewidth{6pt}
    %Principal line
    \pgfpathmoveto{\pgfqpoint{0pt}{0pt}}
    \pgfpathlineto{\pgfqpoint{20pt}{20pt}}
    %Complement line north east
    \pgfpathmoveto{\pgfqpoint{16pt}{-4pt}}
    \pgfpathlineto{\pgfqpoint{24pt}{4pt}}
    %Complement line south west
    \pgfpathmoveto{\pgfqpoint{-4pt}{16pt}}
    \pgfpathlineto{\pgfqpoint{4pt}{24pt}}
    \pgfusepath{stroke}
}
\begin{document}
\lipsum[1-2]
\begin{equation}
    \mathbf{X} = 
    \begin{tikzpicture}[
        %Global config
        baseline=0cm,
        >=latex,
        line width=1pt,
        %Styles
        Brace/.style={
            decorate,
            decoration={
                brace,
                amplitude=2pt,
                raise=-7pt
            }
        },
        Brackets/.style={
            left delimiter={[},
            right delimiter={]}
        },
        every left delimiter/.style={
                xshift=2pt,
                xscale=1.5,
                transform shape
        },
        every right delimiter/.style={
                xshift=-2pt,
                xscale=1.5,
                transform shape
        },
        Matrix/.style={
            matrix of math nodes,
            text height=1.5ex,
            text depth=0.5ex,
            text width=4ex,
            align=center,
            column sep=7pt,
            row sep=7pt,
            nodes in empty cells,
        },
        HLbox/.style={
            rectangle,
            draw,
            line width=1.5pt,
            fill,
            fill opacity=0.1,   
        },
        HLPbox/.style={
            thick,
            pattern=north east lines b,         
        }
        ]

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

        %Hightlight elements in background layer
        \begin{scope}[on background layer] 
        \node[HLPbox,pattern color=orange!50,draw=orange,inner sep=2pt,fit=(M1-3-3)(M1-4-4)](F){};
        \node[HLbox,mygreen,inner sep=1pt,fit=(M1-1-1)(M1-3-3)](C1){};
        \node[HLbox,cyan,inner sep=1pt,fit=(M1-4-4)(M1-6-6)](C2){};
        \node[HLbox,brown,inner sep=-3pt,fit=(M1-3-1)(M1-3-4)](E1){};
        \node[HLbox,brown,inner sep=-3pt,fit=(M1-4-3)(M1-4-6)](E2){};
        \node[HLbox,mypurple,inner sep= -1pt,fit=(M1-1-3)(M1-4-3)](F1){};
        \node[HLbox,mypurple,inner sep= -1pt,fit=(M1-3-4)(M1-6-4)](F2){};
        \end{scope}

        %Delimiters
        \node[Brackets,inner xsep=-6pt,inner ysep=0.5pt,fit=(M1)](BM1){};
        % Auxiliar to separate external braces
        \node[inner xsep=10pt,inner ysep=10pt,fit=(M1)](AUX){};

        % Drawing the braces.
        %Above
        \draw[Brace] (AUX.90 -| C1.180) -- (AUX.90 -| C1.0) node[midway,above]{$C'$};
        \draw[Brace] (AUX.90 -| C2.180) -- (AUX.90 -| C2.0) node[midway,above]{$C''$};
        %below
        \draw[Brace] (AUX.270 -| F1.0) -- (AUX.270 -| F1.180) node[midway,below]{$F'$};
        \draw[Brace] (AUX.270 -| F2.0) -- (AUX.270 -| F2.180) node[midway,below]{$F''$};
        %Left
        \draw[Brace] (AUX.180 |- C1.270) -- (AUX.180 |- C1.90) node[midway,left]{$B'$};
        \draw[Brace] (AUX.180 |- C2.270) -- (AUX.180 |- C2.90) node[midway,left]{$B''$};
        %Right
        \draw[Brace] (AUX.0 |- E1.90) -- (AUX.0 |- E1.270) node[midway,right]{$E'$};
        \draw[Brace] (AUX.0 |- E2.90) -- (AUX.0 |- E2.270) node[midway,right]{$E''$};
    \end{tikzpicture}
\end{equation}

\lipsum[3]

\end{document}

RESULT in standalone class: enter image description here MWE in standalone class:

\documentclass[tikz,border=14pt]{standalone}
\definecolor{mygreen}{HTML}{21EE7D}
\definecolor{mypurple}{HTML}{BB1BFF}
\usetikzlibrary{matrix,fit,backgrounds,patterns,decorations.pathreplacing}

%Create a new patern for firefox and adobe reader from https://tex.stackexchange.com/a/219808/154390
\pgfdeclarepatternformonly{north east lines b}{\pgfqpoint{0pt}{0pt}}{\pgfqpoint{20pt}{20pt}}{\pgfqpoint{20pt}{20pt}}%
{
  \pgfsetlinewidth{6pt}
  %Principal line
  \pgfpathmoveto{\pgfqpoint{0pt}{0pt}}
  \pgfpathlineto{\pgfqpoint{20pt}{20pt}}
  %Complement line north east
  \pgfpathmoveto{\pgfqpoint{16pt}{-4pt}}
  \pgfpathlineto{\pgfqpoint{24pt}{4pt}}
  %Complement line south west
  \pgfpathmoveto{\pgfqpoint{-4pt}{16pt}}
  \pgfpathlineto{\pgfqpoint{4pt}{24pt}}
  \pgfusepath{stroke}
}
\begin{document}
    \begin{tikzpicture}[
        %Global config
        baseline=0cm,
        >=latex,
        line width=1pt,
        %Styles
        Brace/.style={
            decorate,
            decoration={
                brace,
                amplitude=2pt,
                raise=-7pt
            }
        },
        Brackets/.style={
            left delimiter={[},
            right delimiter={]}
        },
        every left delimiter/.style={
                xshift=2.5pt,
                xscale=1.5,
                transform shape
            },
            every right delimiter/.style={
                xshift=-2.5pt,
                xscale=1.5,
                transform shape
            },
        Matrix/.style={
            matrix of math nodes,
            text height=1.5ex,
            text depth=0.5ex,
            text width=4ex,
            align=center,
            column sep=7pt,
            row sep=7pt,
            nodes in empty cells,
        },
        HLbox/.style={
            rectangle,
            draw,
            line width=1.5pt,
            fill,
            fill opacity=0.1,   
        },
        HLPbox/.style={
            thick,
            pattern=north east lines b,         
        }
    ]

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

    %Hightlight elements in background layer
    \begin{scope}[on background layer] 
    \node[HLPbox,pattern color=orange!50,draw=orange,inner sep=2pt,fit=(M1-3-3)(M1-4-4)](F){};
    \node[HLbox,mygreen,inner sep=1pt,fit=(M1-1-1)(M1-3-3)](C1){};
    \node[HLbox,cyan,inner sep=1pt,fit=(M1-4-4)(M1-6-6)](C2){};
    \node[HLbox,brown,inner sep=-3pt,fit=(M1-3-1)(M1-3-4)](E1){};
    \node[HLbox,brown,inner sep=-3pt,fit=(M1-4-3)(M1-4-6)](E2){};
    \node[HLbox,mypurple,inner sep= -1pt,fit=(M1-1-3)(M1-4-3)](F1){};
    \node[HLbox,mypurple,inner sep= -1pt,fit=(M1-3-4)(M1-6-4)](F2){};
    \end{scope}

    %Delimiters
    \node[Brackets,inner xsep=-6pt,inner ysep=0.5pt,fit=(M1)](BM1){};
    % Auxiliar to separate external braces
    \node[inner xsep=10pt,inner ysep=10pt,fit=(M1)](AUX){};

    % Drawing the braces.
    %Above
    \draw[Brace] (AUX.90 -| C1.180) -- (AUX.90 -| C1.0) node[midway,above]{$C'$};
    \draw[Brace] (AUX.90 -| C2.180) -- (AUX.90 -| C2.0) node[midway,above]{$C''$};
    %below
    \draw[Brace] (AUX.270 -| F1.0) -- (AUX.270 -| F1.180) node[midway,below]{$F'$};
    \draw[Brace] (AUX.270 -| F2.0) -- (AUX.270 -| F2.180) node[midway,below]{$F''$};
    %Left
    \draw[Brace] (AUX.180 |- C1.270) -- (AUX.180 |- C1.90) node[midway,left]{$B'$};
    \draw[Brace] (AUX.180 |- C2.270) -- (AUX.180 |- C2.90) node[midway,left]{$B''$};
    %Right
    \draw[Brace] (AUX.0 |- E1.90) -- (AUX.0 |- E1.270) node[midway,right]{$E'$};
    \draw[Brace] (AUX.0 |- E2.90) -- (AUX.0 |- E2.270) node[midway,right]{$E''$};

    \end{tikzpicture}
\end{document}
  • +1 A very nice answer! – Milo Jul 27 '18 at 18:12
  • +1 I really should learn about the fit library:) – Andrew Jul 29 '18 at 13:33
  • +1 for this as well as two other recent great answers ... maybe you could reconsider your attitude towards nesting tikzpictures? ;-) – marmot Jul 29 '18 at 17:12
3

Here is a solution using a matrix of nodes. The main difficulty is in placing the braces across multiple rows or columns. For this I use some customised trickery and define two pics, called leftbrace and overbrace, that place the brace given the row and column indices and the label.

Here's the result:

enter image description here

and here is the code:

\documentclass[svgnames]{article}
\usepackage{tikz}
\usetikzlibrary{matrix,patterns,calc}

\pgfdeclarelayer{bg}    % declare background layer

\newdimen\leftpt
\newdimen\righpt
\tikzset{%
  pics/overbrace/.style args={#1,#2,#3}{%
    % a pic for putting an underbrace under certain columns
    % #1 = left column
    % #2 = right column index
    % #3 = label text
    code = {
       \pgfextractx\leftpt{\pgfpointanchor{M-1-#1}{north west}}  % x-coords of columns
       \pgfextractx\righpt{\pgfpointanchor{M-1-#2}{north east}}
       % draw the delimiter with the correct width
       \node[rectangle, above delimiter={\{}, minimum width=\the\dimexpr\righpt-\leftpt+1pt,
             label={[yshift=2mm]above:$\scriptscriptstyle #3$}]
             at ($ (M-1-#1)!0.5!(M-1-#2)+(0,0.2) $){};
       }
   },
   pics/leftbrace/.style args={#1,#2,#3}{%
   % a pic for putting a brace to the left of certain rows
   % #1 = top row index
   % #2 = bottom row index
   % #3 = label text
   code = {
       \pgfextracty\leftpt{\pgfpointanchor{M-#1-1}{north west}}  % y-coords of columns
       \pgfextracty\righpt{\pgfpointanchor{M-#2-1}{south west}}
       % draw the delimiter with the correct width
       \node[rectangle, left delimiter={\{}, minimum height=\dimexpr\leftpt-\righpt+1pt,
             label={[xshift=-2mm]left:$\scriptscriptstyle #3$}]
             at ($ (M-#1-1)!0.5!(M-#2-1)+(-0.6,0) $){};
       }
   },
}

\begin{document}

\begin{center}
  \pgfdeclarelayer{purple}
  \pgfdeclarelayer{brown}
  \pgfdeclarelayer{orange}
  \pgfsetlayers{purple,brown,orange,main}  % set the order of the layers (main is the standard layer)
  \begin{tikzpicture}[draw/.append style={thick}]
    \matrix (M)[matrix of math nodes, nodes in empty cells,
                left delimiter={[},
                right delimiter={]},
    ]{
         v_{1,1} & v_{1,2} & v_{1,3} &         &         &   \\
         v_{2,1} & v_{2,2} & v_{2,3} &         &         &   \\
         v_{3,1} & v_{3,2} & v_{3,3} & v_{3,4} &         &   \\
                 &         & v_{4,3} & v_{4,4} & v_{4,5} & v_{4,6} \\
                 &         &         & v_{5,4} & v_{5,5} & v_{5,6}   \\
                 &         &         & v_{6,4} & v_{6,5} & v_{6,6} \\
     };
     \pic{leftbrace={1,3,B'}};
     \pic{leftbrace={4,6,B''}};
     \pic{overbrace={1,3,C'}};
     \pic{overbrace={4,6,C''}};
     \node[minimum height=2mm, right delimiter={\}},
           label={[xshift=1mm,yshift=0.6mm]right:$\scriptscriptstyle E'$}] at ($ (M-3-6)+(0.5,0) $){};
     \node[minimum height=2mm, right delimiter={\}},
           label={[xshift=1mm,yshift=0.6mm]right:$\scriptscriptstyle E''$}] at ($ (M-4-6)+(0.5,0) $){};
     \node[minimum width=6mm, below delimiter={\}},
           label={[yshift=-1mm]below:$\scriptscriptstyle F'$}] at ($ (M-6-3)+(0,-0.2) $){};
     \node[minimum width=6mm, below delimiter={\}},
           label={[yshift=-1mm]below:$\scriptscriptstyle F''$}] at ($ (M-6-4)+(0,-0.2) $){};
     \draw[ForestGreen] (M-1-1.north west) rectangle (M-3-3.south east);
     \draw[cyan] (M-4-4.north west) rectangle (M-6-6.south east);
     \begin{pgfonlayer}{purple}    % select the background layer
       \draw[BlueViolet,fill=LightPink,opacity=0.5]
          ($ (0.04,-0.04)+(M-1-3.north west) $) rectangle ($ (-0.04,0.04)+(M-4-3.south east) $);
       \draw[BlueViolet,fill=LightPink,opacity=0.5]
          ($ (0.04,-0.04)+(M-3-4.north west) $) rectangle ($ (-0.04,0.04)+(M-6-4.south east) $);
     \end{pgfonlayer}
     \begin{pgfonlayer}{brown}    % select the background layer
       \draw[FireBrick,fill=Moccasin,opacity=0.5]
         ($ (0.08,-0.08)+(M-3-1.north west) $) rectangle ($ (-0.08,0.08)+(M-3-4.south east) $);
       \draw[FireBrick,fill=Moccasin,opacity=0.5]
         ($ (0.08,-0.08)+(M-4-3.north west) $) rectangle ($ (-0.08,0.08)+(M-4-6.south east) $);
     \end{pgfonlayer}
     \begin{pgfonlayer}{orange}    % select the background layer
       \draw[orange, pattern=north east lines, pattern color=orange, opacity=0.5]
         ($ (0.04,-0.04)+(M-3-3.north west) $) rectangle ($ (-0.04,0.04)+(M-4-4.south east) $);
     \end{pgfonlayer}
  \end{tikzpicture}
\end{center}

\end{document}

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.