4

Is it possible to create a matrix like the following draft with LaTeX?

enter image description here

I have found this Tikzample, but I don't know how to get the dots on the diagonal.

edit: This is how far I got: enter image description here

$$
\begin{pmatrix}
1 &  &   &    &  &      &              &                & &              \\
  &  &   &    &  &      &              &                & &              \\
  &  & 1 &    &  &      &              &                & &              \\
  &  &   & -1 &  &      &              &                & &              \\
  &  &   &    &  &      &              &                & &              \\
  &  &   &    &  & - 1  &              &                & &              \\
  &  &   &    &  &      & cos \omega_1 & -sin \omega_1  & &              \\
  &  &   &    &  &      & sin \omega_1 &  cos \omega_1  & &              \\
  &  &   &    &  &      &              &                & &              \\
  &  &   &    &  &      &              &                & & cos \omega_2 \\
  &  &   &    &  &      &              &                & & sin \omega_2 \\
\end{pmatrix}
$$  

The lime part is what I would like to have, but wasn't able to do with LaTeX.

Martin Thoma
  • 18,799
  • 1
    Can you provide your code without the dots? – percusse Jun 20 '12 at 16:19
  • Possible duplicate of http://tex.stackexchange.com/questions/47765/how-can-i-add-some-enhancement-to-the-matrix which is duplicate of http://tex.stackexchange.com/questions/40028/highlight-elements-in-the-matrix – Ichibann Jun 20 '12 at 17:18
  • If you write \sin and \cos -- note the backslash characters -- you'll get upright roman lettering for the sine and cosine functions, rather than the differently-spaced math italics that result if you type sin and cos without the backslash characters. – Mico Jun 20 '12 at 17:22
  • @Ichibann: I don't think the new question is a duplicate -- the older question you reference doesn't have the blank spaces off the (block) diagonal of the matrix that are required in the new question. – Mico Jun 20 '12 at 17:23
  • @Mico, but blank spaces are the easiest part assuming you already have those borders. – Ichibann Jun 20 '12 at 17:47
  • @Ichibann -- I don't think we should make too many assumptions about what's easy for other LaTeX users and what's not... – Mico Jun 20 '12 at 17:50
  • @Mico, you right. I meant that OP already managed to make blank spaces. – Ichibann Jun 20 '12 at 18:18
  • @Ichibann : I didn't know \ddots. This was the part where I had no clue how to solve it. Additionally, I think the boxes are also not that easy to create (although I had an example). – Martin Thoma Jun 20 '12 at 18:22
  • @Mico : Thanks for your answer :-) It was really helpful => Upvote + Accept, although I think the one of Altermundus looks much nicer. – Martin Thoma Jun 20 '12 at 18:22

3 Answers3

6

If you need the vertical and horizontal lines around the matrix blocks, the following solution (which also doesn't require TikZ) may be of interest. (I've updated the example to reflect the update in your question, which appears to require an extra \ddots row/column.)

Note that I use the array environment rather than the matrix (or pmatrix) environment as it's necessary to right-align the contents of some columns.

\documentclass{article}
\usepackage{amsmath,array}
\renewcommand\arraycolsep{4pt} % default value: 6pt
% short-hand commands for multicolumn entries with vertical bar
% on left and right hand sides, respectively
\newcommand{\mcl}[1]{\multicolumn{1}{|r}{#1}}
\newcommand{\mcr}[1]{\multicolumn{1}{r|}{#1}}
\begin{document}
\begin{equation*}
\left( \,\begin{array}{rcr rcr rr c rr}
\cline{1-3}
 \mcl{1} &        & \mcr{}  \\
 \mcl{}  & \ddots & \mcr{}  \\
 \mcl{}  &        & \mcr{1} \\
\cline{1-6}
 & & & \mcl{-1} &        & \mcr{}  \\
 & & & \mcl{}   & \ddots & \mcr{}  \\
 & & & \mcl{}   &        & \mcr{-1}\\
\cline{4-8}
 & & & & & & \mcl{\cos \omega} & \mcr{-\sin \omega}\\
 & & & & & & \mcl{\sin \omega} & \mcr{ \cos \omega}\\
\cline{7-8} 
 & & & & & & & & \ddots\\
\cline{10-11}
 & & & & & & & & & \mcl{\cos \omega} & \mcr{-\sin \omega}\\
 & & & & & & & & & \mcl{\sin \omega} & \mcr{ \cos \omega}\\
\cline{10-11}
\end{array}\,\right)
\end{equation*}
\end{document}

enter image description here

Addendum: Using the \boxed macro, the code that sets up this matrix can be simplified considerably:

\documentclass{article}
\usepackage{amsmath,array}
\renewcommand\arraycolsep{4pt} % default value: 6pt
\begin{document}
\begin{equation*}
\left( \,
\begin{array}{r@{}r@{}r r r}  % @{} is used twice to suppress intercolumn whitespace
  \boxed{ \begin{array}{rrr}              % First block
    1 \\
    & \ddots\\
    & & 1 \\
  \end{array} } \\
  & \boxed{ \begin{array}{rrr}            % Second block
      -1 \\
      & \ddots\\
      & & -1\\
   \end{array} } \\
  & & \boxed{ \begin{array}{rr}           % Third block
        \cos \omega & -\sin \omega\\
        \sin \omega &  \cos \omega\\
      \end{array} } \\
  & & & \ddots\\                          % Fourth "block" -- not boxed
  & & & & \boxed{ \begin{array}{rr}       % Fifth block
            \cos \omega & -\sin \omega\\
            \sin \omega &  \cos \omega\\
          \end{array} } \\
\end{array}\,\right)
\end{equation*}
\end{document} 

enter image description here

Mico
  • 506,678
3

If you use the amsmath package you can do it without tikz:

\documentclass{article}

\usepackage{amsmath}

\begin{document}

\[\begin{pmatrix}1&&&&&&&&&\\
        &\ddots&&&&&&&&\\
        &&1&&&&&&&\\
        &&&-1&&&&&&\\
        &&&&\ddots&&&&&\\
        &&&&&-1&&&&\\
        &&&&&&\cos w&-\sin w&&\\
        &&&&&&\sin w&\cos w&&\\
        &&&&&&&&\cos w&-\sin w\\
        &&&&&&&&\sin w&\cos w\\\end{pmatrix}
\]

or with those boxes, if you need them:

    \[\begin{pmatrix}
            \boxed{\begin{matrix}
                1&&\\
                &\ddots&\\
                &&1
            \end{matrix}}&&&\\
            &\boxed{\begin{matrix}
                -1&&\\
                &\ddots&\\
                &&-1
            \end{matrix}}&&\\
            &&\boxed{\begin{matrix}
                \cos w&-\sin w\\
                \sin w&\cos w
            \end{matrix}}&\\
            &&&\boxed{\begin{matrix}
                \cos w&-\sin w\\
                \sin w&\cos w
            \end{matrix}}
    \end{pmatrix}
\]
\end{document}
David Carlisle
  • 757,742
bodo
  • 6,228
3

You can use the excellent code from Stefan : (personally I prefer tikz's code )

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

\usetikzlibrary{fit,matrix}
\tikzset{%
  highlight/.style={rectangle,rounded corners,fill=red!15,draw,fill opacity=0.5,thick,inner sep=0pt}
}
\newcommand{\tikzmark}[2]{\tikz[overlay,remember picture,baseline=(#1.base)] \node (#1) {#2};}
%
\newcommand{\Highlight}[1][submatrix]{%
    \tikz[overlay,remember picture]{
    \node[highlight,fit=(left.north west) (right.south east)] (#1) {};}
}
\begin{document}​  
\[
  M = \left(\begin{array}{*5{c}}
    \tikzmark{left}{1} & 2 & 3 & 4 & 5\\
    6 & \ddots & 8 & 9 & 10 \\
    11 & 12 & \ddots\tikzmark{right}{} & 14 & 15 \\
    16 & 17 & 18 & \ddots & 20 \\
    0 & 0 & 0 & 0 & 1
  \end{array}\right)
  \Highlight[first]
\]

\end{document}​ 

enter image description here

Something more complex :

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

\usetikzlibrary{fit,matrix}
\tikzset{%
  highlight/.style={rectangle,fill=#1!15,draw,fill opacity=0.5,thick,inner sep=1pt}
}
\newcommand{\tikzmark}[2]{\tikz[overlay,remember picture,baseline=(#1.base)] \node (#1) {#2};}

\newcommand{\Highlight}[3]{%
    \tikz[overlay,remember picture]{
    \node[highlight=#3,fit=(#1.north west) (#2.south east)]  {};}
}
\begin{document}​  

\begin{equation*}
  \begin{pmatrix}
\tikzmark{1}{c}  &  -a              &  0    & \dots  & \dots  & \dots                &  0 \\
              -b & \tikzmark{2}{c}  & -a    & \ddots &        &                      & \vdots \\
               0 &  -b              & \tikzmark{3}{c}    & \ddots & \ddots  &        & \vdots \\
          \vdots & \ddots           & \ddots & \ddots & \ddots & \ddots              & \vdots \\
          \vdots &                  & \ddots & \ddots &   c    &  -a                 &  0 \\
          \vdots &                  &        & \ddots &  -b    &    \tikzmark{4}{c}  & -a \\
               0 & \dots            & \dots  & \dots  &   0    &  -b                 &  c
  \end{pmatrix}
\end{equation*} 
  \Highlight{1}{2}{blue} 
  \Highlight{3}{4}{red}  

\end{document}​

enter image description here

Alain Matthes
  • 95,075