3

I have two questions:

  1. How can I add more horizontal spacing between the matrices?
  2. Furthermore, is there a way to add some text above the matrices?

I added a picture with the desired ouput.

Here's a MWE to obtain the matrices on the left:

\documentclass[12pt]{article}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{matrix,chains,positioning,decorations.pathreplacing,arrows}
\begin{document}

\[
\begin{matrix}
\begin{bmatrix}
 \begin{tikzpicture}
\matrix [matrix of math nodes] (m)
{
   0 & 0 & 0 & 0 & 0 & 0 & 0 \\
   0 & 6 & 3 & 5 & 2 & 1 & 0 \\
   0 & 2 & 1 & 7 & 8 & 4 & 0 \\
   0 & 1 & 8 & 4 & 1 & 7 & 0 \\
   0 & 6 & 3 & 5 & 2 & 1 & 0 \\
   0 & 2 & 1 & 7 & 8 & 4 & 0 \\
   0 & 0 & 0 & 0 & 0 & 0 & 0 \\
   };  
   \draw[color=red] (m-1-1.north west) -- (m-1-3.north east) -- (m-3-3.south east) -- (m-3-1.south west) -- (m-1-1.north west);
    \end{tikzpicture}
\end{bmatrix}\\
%\end{bmatrix}\\
\\
\begin{bmatrix}
 \begin{tikzpicture}
\matrix [matrix of math nodes] (m)
{
   0 & 0 & 0 & 0 & 0 & 0 & 0 \\
   0 & 1 & 3 & 1 & 0 & 2 & 0 \\
   0 & 9 & 3 & 4 & 2 & 6 & 0 \\
   0 & 1 & 2 & 8 & 9 & 2 & 0 \\
   0 & 5 & 6 & 4 & 5 & 1 & 0 \\
   0 & 7 & 5 & 3 & 3 & 7 & 0 \\
   0 & 0 & 0 & 0 & 0 & 0 & 0 \\
   };  
   \draw[color=green] (m-1-1.north west) -- (m-1-3.north east) -- (m-3-3.south east) -- (m-3-1.south west) -- (m-1-1.north west);
    \end{tikzpicture}
\end{bmatrix}\\ 
\\
\begin{bmatrix}
 \begin{tikzpicture}
\matrix [matrix of math nodes] (m)
{
   0 & 0 & 0 & 0 & 0 & 0 & 0 \\
   0 & 3 & 1 & 8 & 1 & 9 & 0 \\
   0 & 4 & 5 & 2 & 1 & 8 & 0 \\
   0 & 7 & 6 & 3 & 1 & 1 & 0 \\
   0 & 7 & 4 & 8 & 2 & 5 & 0 \\
   0 & 8 & 4 & 8 & 8 & 3 & 0 \\
   0 & 0 & 0 & 0 & 0 & 0 & 0 \\
   };  
   \draw[color=blue] (m-1-1.north west) -- (m-1-3.north east) -- (m-3-3.south east) -- (m-3-1.south west) -- (m-1-1.north west);
    \end{tikzpicture}
\end{bmatrix}
\end{matrix}
%
\begin{matrix}
\begin{bmatrix}
 \begin{tikzpicture}
\matrix [matrix of math nodes] (m)
{
0 & 1 & 0 \\
0 & 1 & 1 \\
1 & 1 & 1 \\
   };  
   \draw[color=red] (m-1-1.north west) -- (m-1-3.north east) -- (m-3-3.south east) -- (m-3-1.south west) -- (m-1-1.north west);
    \end{tikzpicture}
\end{bmatrix}\\ 
\\
\begin{bmatrix}
 \begin{tikzpicture}
\matrix [matrix of math nodes] (m)
{
0 & 1 & 0 \\
1 & 0 & 1 \\
1 & 0 & 0 \\
   };  
   \draw[color=green] (m-1-1.north west) -- (m-1-3.north east) -- (m-3-3.south east) -- (m-3-1.south west) -- (m-1-1.north west);
    \end{tikzpicture}
\end{bmatrix}\\ 
\\
\begin{bmatrix}
 \begin{tikzpicture}
\matrix [matrix of math nodes] (m)
{
1 & 0 & 1 \\
1 & 1 & 0 \\
1 & 1 & 0 \\
   };  
   \draw[color=blue] (m-1-1.north west) -- (m-1-3.north east) -- (m-3-3.south east) -- (m-3-1.south west) -- (m-1-1.north west);
    \end{tikzpicture}
\end{bmatrix}
\end{matrix}
=
\begin{matrix}
\begin{bmatrix}
 \begin{tikzpicture}
\matrix [matrix of math nodes] (m)
{
1 & 1 & 1 \\
0 & 1 & 1 \\
1 & 0 & 0 \\
   };  
   \draw[color=yellow] (m-1-1.north west) -- (m-1-1.north east) -- (m-1-1.south east) -- (m-1-1.south west) -- (m-1-1.north west);
    \end{tikzpicture}
\end{bmatrix}\\ 
\end{matrix}
\label{eq:convex}
\]
\end{document}

matrix

  • 3
    While code snippets are useful in explanations, it is always best to compose a fully compilable MWE that illustrates the problem including the \documentclass and the appropriate packages so that those trying to help don't have to recreate it. This is especially important for tikz as there are numerous libraries. – Peter Grill Jul 22 '16 at 0:00
  • You're right! I edited the code. – apples-oranges Jul 22 '16 at 0:12
  • 1
    you have in your example \begin{matrix} immediately following \end{matrix}, so of course there is no horizontal space. insert \qquad or \hspace{<dimen>} to specify as much space as you want. this is an elementary principle governing spaces in tex math. – barbara beeton Jul 22 '16 at 1:56
1

You can put your matrices in an array environment, that way you can easily put title on top of your columns. As for the extra space, you can then set the desired length using arraycolsep.

Edit: as egreg stated in the comments, \arraycolsep affects the placement of pmatrix, so to set extra space between the matrices, use \begin{array{@{}c@{\hspace{20pt}}c@{\hspace{20pt}}c@{\hspace{20pt}}c@{}} instead.

\documentclass[12pt]{article}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{matrix,chains,positioning,decorations.pathreplacing,arrows}
\setlength{\arraycolsep}{20pt}

\begin{document}

\[
    \begin{array{@{}c@{\hspace{20pt}}c@{\hspace{20pt}}c@{\hspace{20pt}}c@{}}
    Title 1 & Title 2 & & Title 3\\
    \begin{matrix}
        \begin{bmatrix}
            \begin{tikzpicture}
                \matrix [matrix of math nodes] (m)
                    {
                        0 & 0 & 0 & 0 & 0 & 0 & 0 \\
                    0 & 6 & 3 & 5 & 2 & 1 & 0 \\
                    0 & 2 & 1 & 7 & 8 & 4 & 0 \\
                    0 & 1 & 8 & 4 & 1 & 7 & 0 \\
                    0 & 6 & 3 & 5 & 2 & 1 & 0 \\
                    0 & 2 & 1 & 7 & 8 & 4 & 0 \\
                    0 & 0 & 0 & 0 & 0 & 0 & 0 \\
                };  
                    \draw[color=red] (m-1-1.north west) -- (m-1-3.north east) -- (m-3-3.south east) -- (m-3-1.south west) -- (m-1-1.north west);
            \end{tikzpicture}
        \end{bmatrix}\\
        %\end{bmatrix}\\
        \\
        \begin{bmatrix}
            \begin{tikzpicture}
                \matrix [matrix of math nodes] (m)
                {
                    0 & 0 & 0 & 0 & 0 & 0 & 0 \\
                0 & 1 & 3 & 1 & 0 & 2 & 0 \\
                0 & 9 & 3 & 4 & 2 & 6 & 0 \\
                0 & 1 & 2 & 8 & 9 & 2 & 0 \\
                0 & 5 & 6 & 4 & 5 & 1 & 0 \\
                0 & 7 & 5 & 3 & 3 & 7 & 0 \\
                0 & 0 & 0 & 0 & 0 & 0 & 0 \\
            };  
                \draw[color=green] (m-1-1.north west) -- (m-1-3.north east) -- (m-3-3.south east) -- (m-3-1.south west) -- (m-1-1.north west);
            \end{tikzpicture}
        \end{bmatrix}\\ 
        \\
        \begin{bmatrix}
            \begin{tikzpicture}
                \matrix [matrix of math nodes] (m)
                {
                    0 & 0 & 0 & 0 & 0 & 0 & 0 \\
                0 & 3 & 1 & 8 & 1 & 9 & 0 \\
                0 & 4 & 5 & 2 & 1 & 8 & 0 \\
                0 & 7 & 6 & 3 & 1 & 1 & 0 \\
                0 & 7 & 4 & 8 & 2 & 5 & 0 \\
                0 & 8 & 4 & 8 & 8 & 3 & 0 \\
                0 & 0 & 0 & 0 & 0 & 0 & 0 \\
            };  
                \draw[color=blue] (m-1-1.north west) -- (m-1-3.north east) -- (m-3-3.south east) -- (m-3-1.south west) -- (m-1-1.north west);
            \end{tikzpicture}
        \end{bmatrix}
\end{matrix}
&
\begin{matrix}
\begin{bmatrix}
    \begin{tikzpicture}
        \matrix [matrix of math nodes] (m)
        {
            0 & 1 & 0 \\
        0 & 1 & 1 \\
        1 & 1 & 1 \\
    };  
        \draw[color=red] (m-1-1.north west) -- (m-1-3.north east) -- (m-3-3.south east) -- (m-3-1.south west) -- (m-1-1.north west);
    \end{tikzpicture}
\end{bmatrix}\\ 
\\
\begin{bmatrix}
    \begin{tikzpicture}
        \matrix [matrix of math nodes] (m)
        {
            0 & 1 & 0 \\
        1 & 0 & 1 \\
        1 & 0 & 0 \\
    };  
        \draw[color=green] (m-1-1.north west) -- (m-1-3.north east) -- (m-3-3.south east) -- (m-3-1.south west) -- (m-1-1.north west);
    \end{tikzpicture}
\end{bmatrix}\\ 
\\
\begin{bmatrix}
    \begin{tikzpicture}
        \matrix [matrix of math nodes] (m)
        {
            1 & 0 & 1 \\
        1 & 1 & 0 \\
        1 & 1 & 0 \\
    };  
        \draw[color=blue] (m-1-1.north west) -- (m-1-3.north east) -- (m-3-3.south east) -- (m-3-1.south west) -- (m-1-1.north west);
    \end{tikzpicture}
\end{bmatrix}
\end{matrix}
& = &
\begin{matrix}
\begin{bmatrix}
    \begin{tikzpicture}
        \matrix [matrix of math nodes] (m)
        {
            1 & 1 & 1 \\
        0 & 1 & 1 \\
        1 & 0 & 0 \\
    };  
        \draw[color=yellow] (m-1-1.north west) -- (m-1-1.north east) -- (m-1-1.south east) -- (m-1-1.south west) -- (m-1-1.north west);
    \end{tikzpicture}
\end{bmatrix}\\ 
\end{matrix}
\label{eq:convex}
\end{array}
\]
\end{document}

Gives:

matrices with title and space

  • Does setting \arraycolsep affect the spacing for pmatrix as well? – Werner Jul 22 '16 at 5:18
  • I don't think so, but I'm no expert of the internals of the amsmath, sorry. – Lukhio Jul 22 '16 at 6:38
  • @Werner Of course it does – egreg Jul 22 '16 at 8:27
  • Instead of setting \arraycolsep, wouldn't it be easier to use \begin{array}{@{}c@{\hspace{20pt}}c@{\hspace{20pt}}c@{\hspace{20pt}}c@{}}? – egreg Jul 22 '16 at 9:18
  • 1
    @egreg it seems to be working yes, I will update my answer then. @apples-oranges this is because you're in math mode. You can use \text{}to print text with spaces. – Lukhio Jul 23 '16 at 13:00

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.