15

Is there a way to draw a continuous zigzag diagonal better than this one ?

enter image description here

my code :

\documentclass[]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix}
\usepackage{fullpage,amsmath}
\usepackage{times}
\begin{document}

\begin{tikzpicture}[baseline=(A.center)]
  \tikzset{BarreStyle/.style =   {opacity=.3,line width=3 mm,line cap=round,color=#1}}


\matrix(A) [matrix of math nodes, left delimiter  = {[}, right delimiter ={]}] 
{ M_{11} &|[color = blue]|  M_{12} & M_{13} &|[color = magenta]| M_{14} & M_{15} &|[color = red]| M_{16} \\
  |[color = blue]|M_{21} & M_{22} & |[color = magenta]|M_{23} & M_{24} & |[color = red]|M_{25} & M_{26} \\
  M_{31} & |[color = magenta]|M_{32} & M_{33} &|[color = red]| M_{34} & M_{35} &|[color = orange]| M_{36} \\
  |[color = magenta]| M_{41} & M_{42} &|[color = red]| M_{43} & M_{44} &|[color = orange]| M_{45} & M_{46} \\
  M_{51} & |[color = red]|M_{52} & M_{53} & |[color = orange]|M_{54} & M_{55} &|[color = olive]| M_{56} \\
 |[color = red]|M_{61} & M_{62} &|[color = orange]| M_{63} & M_{64} &|[color = olive]| M_{65} & M_{66} \\
};

 \draw [BarreStyle=red] (A-1-1.west) to (A-1-2.east);
 \draw [BarreStyle=red] (A-1-2.north) to (A-2-2.south);
 \draw [BarreStyle=red] (A-2-2.west) to (A-2-3.east);
 \draw [BarreStyle=red] (A-2-3.north) to (A-3-3.south);
 \draw [BarreStyle=red] (A-3-3.west) to (A-3-4.east);
 \draw [BarreStyle=red] (A-3-4.north) to (A-4-4.south);
 \draw [BarreStyle=red] (A-4-4.west) to (A-4-5.east);
 \draw [BarreStyle=red] (A-4-5.north) to (A-5-5.south);
 \draw [BarreStyle=red] (A-5-5.west) to (A-5-6.east);
 \draw [BarreStyle=red] (A-5-6.north) to (A-6-6.south);

\end{tikzpicture}

\end{document}
21

Possibly...

\documentclass[]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix}
\usepackage{fullpage,amsmath}
\usepackage{times}
\begin{document}

\begin{tikzpicture}[baseline=(A.center)]
  \tikzset{BarreStyle/.style =   {opacity=.3,line width=3 mm,line cap=round,color=#1}}


\matrix(A) [matrix of math nodes, left delimiter  = {[}, right delimiter ={]}] 
{ M_{11} &|[color = blue]|  M_{12} & M_{13} &|[color = magenta]| M_{14} & M_{15} &|[color = red]| M_{16} \\
  |[color = blue]|M_{21} & M_{22} & |[color = magenta]|M_{23} & M_{24} & |[color = red]|M_{25} & M_{26} \\
  M_{31} & |[color = magenta]|M_{32} & M_{33} &|[color = red]| M_{34} & M_{35} &|[color = orange]| M_{36} \\
  |[color = magenta]| M_{41} & M_{42} &|[color = red]| M_{43} & M_{44} &|[color = orange]| M_{45} & M_{46} \\
  M_{51} & |[color = red]|M_{52} & M_{53} & |[color = orange]|M_{54} & M_{55} &|[color = olive]| M_{56} \\
 |[color = red]|M_{61} & M_{62} &|[color = orange]| M_{63} & M_{64} &|[color = olive]| M_{65} & M_{66} \\
};

 \path [fill=red, fill opacity=0.25,rounded corners=0.125cm]
  (A-1-1.north west) \foreach \i in {1,...,6}{ |- (A-\i-\i.south east) } 
  \foreach \i in {5,...,1} { |- (A-\i-\i.north east) } -- cycle;

\end{tikzpicture}

\end{document}

enter image description here

1
  • This answer is nice too, powerful with the loop, thank you
    – Thomas
    Dec 17 '13 at 11:10
13

What about this?

Result

Code (only the changed parts):

\tikzset{BarreStyle/.style = {
    opacity=.3,line width=6 mm,
    line cap=round, line join=round, 
    color=#1, shorten >= -0.1ex, shorten <= -1ex}
}

[Same matrix definition]

\draw [BarreStyle=red] (A-1-1.center) to (A-1-2.center)
                                      to (A-2-2.center)
                                      to (A-2-3.center)
                                      to (A-3-3.center)
                                      to (A-3-4.center)
                                      to (A-4-4.center)
                                      to (A-4-5.center)
                                      to (A-5-5.center)
                                      to (A-5-6.center)
                                      to (A-6-6.center);
1
  • That's perfect. I forgot the .center command ...
    – Thomas
    Dec 17 '13 at 10:42
2

With {pNiceMatrix} of nicematrix.

\documentclass{article}
\usepackage{nicematrix,tikz}

\begin{document}

\renewcommand{\arraystretch}{1.4}
\setlength{\arraycolsep}{8pt}

$\begin{bNiceMatrix}[margin]
\CodeBefore
    \tikz \fill [red!10,rounded corners]
       (1-|1) |- (2-|2) |- (3-|3) |- (4-|4) |- (5-|5) |- (6-|6) |- (7-|7) 
       |- (5-|6) |- (4-|5) |- (3-|4) |- (2-|3) |- (1-|2) -- cycle ;
\Body
    M_{11} &\color{blue}  M_{12} & M_{13} &\color{magenta} M_{14} & M_{15} &\color{red} M_{16} \\
    \color{blue}M_{21} & M_{22} & \color{magenta}M_{23} & M_{24} & \color{red}M_{25} & M_{26} \\
    M_{31} & \color{magenta}M_{32} & M_{33} &\color{red} M_{34} & M_{35} &\color{orange} M_{36} \\
    \color{magenta} M_{41} & M_{42} &\color{red} M_{43} & M_{44} &\color{orange} M_{45} & M_{46} \\
    M_{51} & \color{red}M_{52} & M_{53} & \color{orange}M_{54} & M_{55} &\color{olive} M_{56} \\
   \color{red}M_{61} & M_{62} &\color{orange} M_{63} & M_{64} &\color{olive} M_{65} & M_{66} 
\end{bNiceMatrix}$

\end{document}

You need several compilations (because nicematrix uses PGF/Tikz nodes under the hood).

Output of the above code

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.