1

I took a code and changed it. However, I can't change it correctly. The output is the top matrix and the desired matrix is in the below (green upper triangular). How can it be done right?

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix,fit}
\begin{document}
\pgfdeclarelayer{background}
\pgfsetlayers{background,main}
‎
‎‎\begin{tikzpicture}[baseline={([yshift=-1ex]current bounding box.center)}]
    \matrix[matrix of math nodes,left delimiter={[},right delimiter={]},row sep=10pt,column sep=10pt] (m) {
       1  &3 &-8‎ & 5‎\\
       2  &0 &1‎& 7‎\\
       7 &9 &1‎& 2‎\\‎
       7 &9 &1‎& 2‎\\‎‎
    };
    \begin{pgfonlayer}{background}
 \node[inner sep=3pt,fit=(m-1-1)]          (1)   {};
 \node[inner sep=3pt,‎fit=(m-1-2)‎ (m-2-3‎) ‎(m-‎3‎-‎4‎‎)‎]  (2)   {};
 \node[inner sep=3pt,fit=‎‎(m-‎4‎-‎4‎)]          (3)   {‎‎};
 \draw[rounded corners,dotted,fill=green!‎8‎0!white,inner sep=3pt,fill opacity=0.1] (1.north west) ‎-‎- (2.north east) |- (3.south west) |- (2.south west) |- (1.south west) -- cycle;
\end{‎pgfonlayer}‎
  \end{tikzpicture}‎‎
\end{document}

enter image description here

5

I do not know why I can not compile your code, it seems to me that there are rare characters in it (inputtenc Error: Unicode character ...), however I use another framework.

Here a MWE with three examples: in green using the nodes defined in the matrix; in red according to the code you sent, where you can see the error, then in blue, how can you get what you want by the code you sent method ...

RESULT: enter image description here MWE:

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

\begin{document}
    \begin{tikzpicture}[
        %Styles
        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=0pt,
            row sep=0pt,
            %nodes={draw=black!10}, % Uncoment to see the square nodes.
            nodes in empty cells,
        },
        DA/.style={
            fill=green!10,
            dotted,
            line width=0.7pt,
            rounded corners=5pt,
        }
    ]

    \matrix[Matrix] at (0,0) (M){ % Matrix contents  
    1 & 3 & -8 & 5 \\
    2 & 0 & 1 & 7 \\
    7 & 9 & 1 & 2 \\
    7 & 9 & 1 & 2 \\
    };

    \matrix[Matrix,nodes={draw=black!10}] at (4,0) (M1){ % Matrix contents  
    1 & 3 & -8 & 5 \\
    2 & 0 & 1 & 7 \\
    7 & 9 & 1 & 2 \\
    7 & 9 & 1 & 2 \\
    };
     \matrix[Matrix] at (8,0) (M2){ % Matrix contents  
    1 & 3 & -8 & 5 \\
    2 & 0 & 1 & 7 \\
    7 & 9 & 1 & 2 \\
    7 & 9 & 1 & 2 \\
    };
    \begin{scope}[on background layer] %Allows to draw in background layer.
        %FOR MATRIX M
        %To delimit area groups using matrix nodes
        \draw[DA](M-1-1.north west) 
            -| (M-4-4.south east)
            -- (M-4-4.south west)
            |- (M-3-3.south west)
            |- (M-2-2.south west)
            |- (M-1-1.south west)
            -- cycle;

        % Method for your example
        % In red some mistakes
        % Fit creates squared nodes
        %\node[styles](ID){Text};
        \node[inner sep=2pt,draw,red,fit=(M1-1-1)](fit1){};
        \node[inner sep=2pt,draw,red,fit=(M1-1-2)(M1-2-3)(M1-3-4)](fit2){};
        \node[inner sep=2pt,draw,red,fit=(M1-4-4)](fit3){};
        %If you trace the draw:
        \draw[DA,fill=red,fill opacity=0.2](fit1.north west)
            -- (fit2.north east)
            |- (fit3.south west)
            |- (fit2.south west)
            |- (fit1.south west)
            -- cycle;

    % Correcting using the method you use 
    \node[inner sep=2pt,draw,blue,fit=(M2-1-1)](n1){};
    \node[inner sep=2pt,draw,blue,fit=(M2-2-2)](n2){};
    \node[inner sep=2pt,draw,blue,fit=(M2-3-3)](n3){};  
    \node[inner sep=2pt,draw,blue,fit=(M2-4-4)](n4){};
    %If you trace the draw:
        \draw[DA,fill=blue,fill opacity=0.2](n1.north west)
            -| (n4.south east)
            |- (n4.south west)
            |- (n3.south west)
            |- (n2.south west)
            |- (n1.south west)
            -- cycle;           
    \end{scope}

    \end{tikzpicture}
\end{document}
  • @user108438 Being a long-time user yourself, it would be great, if you could accept the answers (which you find nice) for the questions that you posted. – Raaja Aug 7 '18 at 9:02

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.