6

I have a matrix which has certain properties. To illustrate these properties, I would like to generate the following image using Tikz:

enter image description here

Additionally, I would like the following things on the image:

  1. The upper triangle should be shaded light blue
  2. The lower triangle should be shaded light red
  3. The red and blue lines should be dotted exactly where there are dots in the zeros along the horizontal and vertical directions.

I am new to using Tikz and any help with this would be greatly appreciated!

13

For fun: that is easy with pstricks:

\documentclass[11pt,x11names]{standalone}
\usepackage{pst-node}
\usepackage{auto-pst-pdf}
\usepackage{amsmath}
\usepackage{sansmath}

\begin{document}

 \mathversion{sans}\setlength\fboxsep{0pt}
\begin{postscript}
    $\begin{pmatrix}
        0 & 0 & 0 & 0 & \dots & 0 \\ 0 & \pnode[0,1.5ex]{A} & & &\pnode[0,1.5ex]{Dh} \\0\\0\\\vdots &\pnode{Dv} \\0 & & & & & \pnode{B}
    \end{pmatrix}$
    \psset{arrowinset=0, linewidth=1.5pt, linearc=0.2pt, nodesep = 6pt,, arrows =c- }
    \psline{<->}(A)(B)
    \psset{linecolor=RoyalBlue2, fillcolor=RoyalBlue1!15!, arrows =c-}
    \ncangle[angleB=90, linestyle = none, fillstyle=solid,]{A}{B}
    \ncline[nodesepB=9.5pt]{A}{Dh}
    \ncangle[angleA=90, nodesepB=7.5pt]{B}{Dh}
    \rput(Dh){\color{RoyalBlue2}\dots}
    \psset{angleA=-90, angleB=180, linecolor=Red1!60!, fillcolor=Red1!10!, arrows =-c}
    \ncangle[linestyle=none, fillstyle=solid]{A}{B}
    \ncangle[nodesepA=3pt,]{Dv}{B}
    \ncline[nodesepA=12pt]{Dv}{A}
    \rput[B](Dv){\color{Red1!60!}\vdots}
\end{postscript}

\end{document} 

enter image description here

  • I am finding it hard to compile this. What is the package auto-pst-pdf used for since I think that is the source of the error. If I run the file with XeLaTeX, it gives the image but in black and white rather than in colour, but if I then run the file in LaTeX, the picture vanishes. What should be done? – Sid Sep 8 '15 at 18:28
  • auto-pst-pdf allows pstricks code to be compiled with pdflatex. Although pdf format is a subset of postcript it lacks a computing engine so pstricks code cannot be used directly with pdflatex. You have to launch pdflatex with the --enable-write18 switch if you're under MiKTeX, or --shell-escape with TeX Live or MacTeX. For XeLaTeX, normally it does not require the auto-pst-pdf package for pstricks code. I'll test what happens with XeLaTeX. – Bernard Sep 8 '15 at 18:45
  • @Sid: I'm sorry, there were two typos when I copied the code. It's corrected now. It should work with pdflatex as well as xelatex. With xelatex, you don't have to load auto-pst-pdf, but then you must replace the postscript environment with pspicture. – Bernard Sep 8 '15 at 18:59
  • Thanks again for your help. When I try to compile the new code, I still cannot seem to generate the image using LaTeX. I am using TexShop for Mac. – Sid Sep 8 '15 at 19:48
  • Strange… Could you post (a link to) the .log file? – Bernard Sep 8 '15 at 19:51
7

With TikZ is easy to:

\documentclass[border=3mm,
               tikz,
               prewiev]{standalone}
\usetikzlibrary{matrix}
\begin{document}
    \begin{tikzpicture}
\matrix[matrix of math nodes,
        left delimiter=(,
        right delimiter=),
        nodes in empty cells] (m)
{
0       & 0 & 0 & 0 & \dots & 0 \\
0   & \vphantom{0}  
            &   &   &       &   \\
0       &   &   &   &       &   \\
0       &   &   &   &       &   \\
\vdots  &   &   &   &       &   \\
0       &   &   &   &       &   \\
};
\fill[blue!30] (m-2-2.north west) -| (m-6-6.south east);
\fill[ red!30] (m-2-2.north west) |- (m-6-6.south east);
\draw[line width=3mm,white]  (m-2-2.north west) -- (m-6-6.south east);
\draw[thick,stealth-stealth] (m-2-2.north west) -- (m-6-6.south east);

    \end{tikzpicture}
\end{document}

enter image description here

Edit: Gonzalo Medina remind me on the forgotten dots, Sorry for this. Now I add them in the first row and column and also appearance of diagonal line with use white background line.

Edit (2): The matrix will be centered in text, if you enclose with \begin{center} ... \end{center} or put in some other appropriate environment, for example \begin{figure}\centering\begin{tikzpicture} ... \end{tikzpicture}\end{figure}, etc.

For rounded shadow fill you need to add option rounded corners. Se new MWE below.

\documentclass{article}
    \usepackage{tikz}
\usetikzlibrary{matrix}

\usepackage[active,tightpage]{preview}% just for show only picture
\PreviewEnvironment{center}%
\setlength\PreviewBorder{1em}


\begin{document}
    \begin{center}
    \begin{tikzpicture}
\matrix[matrix of math nodes,
        left delimiter=(,
        right delimiter=),
        nodes in empty cells] (m)
{
0       & 0 & 0 & 0 & \dots & 0 \\
0   & \vphantom{0}
            &   &   &       &   \\
0       &   &   &   &       &   \\
0       &   &   &   &       &   \\
\vdots  &   &   &   &       &   \\
0       &   &   &   &       &   \\
};
\fill[rounded corners,blue!30] (m-2-2.north west) -| (m-6-6.south east);
\fill[rounded corners, red!30] (m-2-2.north west) |- (m-6-6.south east);
\draw[line width=3mm,white]  (m-2-2.north west) -- (m-6-6.south east);
\draw[thick,stealth-stealth] (m-2-2.north west) -- (m-6-6.south east);
    \end{tikzpicture}
    \end{center}
\end{document}

enter image description here

  • The dots in the first row and columns and the dotted lines are missing. – Gonzalo Medina Sep 8 '15 at 13:01
  • @GonzaloMedina, yes, ho have right! Well, one zero can in row and column should be replaced by dots/vdots or introduced new row nd column. I will add them. – Zarko Sep 8 '15 at 14:47
  • @GonzaloMedina, the image is what I needed. However how could the triangles be made to have rounded edges? Also, is it possible to have white strips running horizontally exactly where the \vdots are and vertical white strips exactly where the \ldots are. This will make it very clear that the matrix is arbitrarily very large and will make a nice illustration. Thank you both very much for your help!!! – Sid Sep 8 '15 at 16:15
  • @Zarko When I try to add the code into my file, it is not centred. I use the \begin{center} and \end{center} within the \begin{tikzpicture} \end{tikzpicture} commands but it does not work. Any ideas? – Sid Sep 8 '15 at 16:19
  • @Sid, I just generate desired picture. I have any clue, where it will be used. If you like to have centered in text, than you need to ad begin{center} ... \end{center} outside of picture environment. Se recet edit of my answer. – Zarko Sep 8 '15 at 16:37

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.