6

I am trying to put symbols above and the columns of a matrix and also to the left of its rows. The following working example is basically, what I want, except, that the symbols are within the brackets. I would like to have them outside.

Is there any way to accomplish this?

\documentclass{article}
\usepackage{pgfplots}
\usepackage{tikz}
\usetikzlibrary{matrix,shapes,positioning}

\begin{document}
    $A=$%
    \begin{tikzpicture}[baseline=(A.center)]
    \matrix (A) [matrix of math nodes, ampersand replacement=\&,
    left delimiter=(,right delimiter=)] 
    { {} \& {}  \& {}   \& {}   \& {} \\
      {} \& 1   \& 2    \& 17   \& 3 \\
      {} \& 4   \& 1    \& 24   \& 1 \\
      {} \& 0   \& 0    \& 2    \& -0.9 \\
      {} \& 0   \& 7    \& -0.95 \& 2 \\
    };

    \draw [black]  (A-1-2.west)  to (A-1-2.east);
    \draw [black,dashed]  (A-1-3.west)  to (A-1-3.east);
    \draw [black]  (A-1-4.west)--  plot[mark=square*, mark options={fill=black}] (A-1-4.center) -- (A-1-4.east);
    \draw [black]  (A-1-5.west)--  plot[mark=triangle*, mark options={fill=black}] (A-1-5.center) -- (A-1-5.east);

    \draw [black]  (A-2-1.west)  to (A-2-1.east);
    \draw [black,dashed]  (A-3-1.west)  to (A-3-1.east);
    \draw [black]  (A-4-1.west)--  plot[mark=square*, mark options={fill=black}] (A-4-1.center) -- (A-4-1.east);
    \draw [black]  (A-5-1.west)--  plot[mark=triangle*, mark options={fill=black}] (A-5-1.center) -- (A-5-1.east);
    \end{tikzpicture}%
\end{document}
3

I update one of my examples about tikz and matrix. You only need to use some nodes : here A-1-1.west and A-5-1.west and you have several possibilities to place your symbols. I used xshiftto do that for the rows but it's the same thing for columns with yshift

   \node [xshift=-1.5 cm] at  (A-1-1.west)   {$\longrightarrow$ first row}; 
   \node [xshift=-1.5 cm] at  (A-5-1.west)   {$\longrightarrow$ last row};   


\documentclass[]{article}
\usepackage[utf8]{inputenc}
\usepackage[upright]{fourier}
\usepackage{tikz}
\usetikzlibrary{matrix,arrows,decorations.pathmorphing} 
\usepackage{fullpage}          
\begin{document}

\begin{tikzpicture}[>=latex]
% unit
\newcommand{\myunit}{1.2 cm}
% the styles
\tikzset{node style sp/.style={draw,circle,minimum size=\myunit}}  
\tikzset{node style ge/.style={circle,minimum size=\myunit}}
\tikzset{arrow style mul/.style={draw,sloped,midway,fill=white}}
\tikzset{arrow style plus/.style={midway,sloped,fill=white}}
% defintion of matrices
\matrix (A) [matrix of math nodes,%
             nodes = {node style ge},%
             left delimiter  = (,%
             right delimiter = )] at (0,0)
{%
  a_{11} &\ldots & a_{1k} & \ldots & a_{1p}  \\ 
    \vdots & \ddots & \vdots & \vdots & \vdots \\
  \node[node style sp] {a_{i1}};& \ldots%
         & \node[node style sp] {a_{ik}};%
                  & \ldots%
                           & \node[node style sp] {a_{ip}}; \\ 
  \vdots & \vdots& \vdots & \ddots & \vdots  \\
  a_{n1}& \ldots & a_{nk} & \ldots & a_{np}  \\ 
}; 
\node [draw,below] at (A.south) { $A$ : \textcolor{red}{$n$ rows} $p$ columns};
\matrix (B) [matrix of math nodes,%
             nodes = {node style ge},%
             left delimiter  = (,%
             right delimiter =)] at (7*\myunit,7*\myunit)
{% 
  b_{11} &  \ldots& \node[node style sp] {b_{1j}};%
                  & \ldots & b_{1q}  \\ 
  \vdots& \ddots & \vdots & \vdots & \vdots \\
  b_{k1} &  \ldots& \node[node style sp] {b_{kj}};%
                  & \ldots & b_{kq}  \\ 
  \vdots& \vdots & \vdots & \ddots & \vdots \\
  b_{p1} &  \ldots& \node[node style sp] {b_{pj}};%
                  & \ldots & b_{pq}  \\ 
};
\node [draw,above] at (B.north) { $B$ : $p$ rows \textcolor{red}{$q$ columns}}; 
% matrice résultat
\matrix (C) [matrix of math nodes,%
             nodes = {node style ge},%
             left delimiter  = (,%
             right delimiter = )] at (7*\myunit,0)
{% 
  c_{11} & \ldots& c_{1j} & \ldots & c_{1q} \\ 
  \vdots& \ddots & \vdots & \vdots & \vdots \\
    c_{i1}& \ldots & \node[node style sp,red] {c_{ij}};%
                  & \ldots & c_{iq} \\ 
  \vdots& \vdots & \vdots & \ddots & \vdots \\
  c_{n1}& \ldots & c_{nk} & \ldots & c_{nq} \\ 
}; 
\node [draw,below] at (C.south) {$ C=A\times B$ : \textcolor{red}{$n$ rows}  \textcolor{red}{$q$ columns}};
% arrows
\draw[blue] (A-3-1.north) -- (C-3-3.north);
\draw[blue] (A-3-1.south) -- (C-3-3.south);
\draw[blue] (B-1-3.west)  -- (C-3-3.west);
\draw[blue] (B-1-3.east)  -- (C-3-3.east);
\draw[<->,red](A-3-1) to[in=180,out=90] 
      node[arrow style mul] (x) {$a_{i1}\times b_{1j}$} (B-1-3) ;
\draw[<->,red](A-3-3) to[in=180,out=90]%
      node[arrow style mul] (y) {$a_{ik}\times b_{kj}$} (B-3-3);
\draw[<->,red](A-3-5) to[in=180,out=90]%
      node[arrow style mul] (z) {$a_{ip}\times b_{pj}$} (B-5-3);
\draw[red,->] (x) to node[arrow style plus] {$+\raisebox{.5ex}{\ldots}+$} (y)%
                  to node[arrow style plus] {$+\raisebox{.5ex}{\ldots}+$} (z);
                  %
                  % to (C-3-3.north west);
\draw[->,red,decorate,decoration=zigzag] (z) -- (C-3-3.north west);
\node [xshift=-1.5 cm] at  (A-1-1.west)   {$\longrightarrow$ first row}; 
\node [xshift=-1.5 cm] at  (A-5-1.west)   {$\longrightarrow$ last row}; 
\end{tikzpicture} 
\end{document}

enter image description here

3

You can use the anchors of the matrix nodes and some maths to get the correct points outside of the brackets. You could use the following for instance:

\documentclass{article}
\usepackage{pgfplots}
\usepackage{tikz}
\usetikzlibrary{matrix,shapes,positioning,calc}
\def\hoffset{1}
\def\voffset{.5}

\begin{document}
    $A=$%
    \begin{tikzpicture}[baseline=(A.center)]
    \matrix (A) [matrix of math nodes, ampersand replacement=\&,
    left delimiter=(,right delimiter=)] 
    { 1   \& 2    \& 17   \& 3 \\
      4   \& 1    \& 24   \& 1 \\
      0   \& 0    \& 2    \& -0.9 \\
      0   \& 7    \& -0.95 \& 2 \\
    };
    \draw [black]  ($(A-1-1.west)+(-\hoffset,0)$)  to ($(A-1-1.east)+(-\hoffset,0)$);
    \draw [black,dashed]  ($(A-2-1.west)+(-\hoffset,0)$)  to ($(A-2-1.east)+(-\hoffset,0)$);
    \coordinate (A31) at ($(A-3-1.center)+(-\hoffset,0)$);
    \coordinate (A41) at ($(A-4-1.center)+(-\hoffset,0)$);
    \draw [black]  ($(A-3-1.west)+(-\hoffset,0)$)--  plot[mark=square*, mark options={fill=black}] (A31)  -- ($(A-3-1.east)+(-\hoffset,0)$);
    \draw [black]  ($(A-4-1.west)+(-\hoffset,0)$)--  plot[mark=triangle*, mark options={fill=black}] (A41) -- ($(A-4-1.east)+(-\hoffset,0)$);

    \draw [black]  ($(A-1-1.west)+(0,\voffset)$)  to ($(A-1-1.east)+(0,\voffset)$);
    \draw [black,dashed]  ($(A-1-2.west)+(.1,\voffset)$)  to ($(A-1-2.east)+(0,\voffset)$);
    \coordinate (A13) at ($(A-1-3.center)+(0,\voffset)$);
    \coordinate (A14) at ($(A-1-4.center)+(0,\voffset)$);
    \draw [black]  ($(A-1-3.west)+(0,\voffset)$)--  plot[mark=square*, mark options={fill=black}] (A13) -- ($(A-1-3.east)+(0,\voffset)$);
    \draw [black]  ($(A-1-4.west)+(0,\voffset)$)--  plot[mark=triangle*, mark options={fill=black}] (A14) -- ($(A-1-4.east)+(0,\voffset)$);
    \end{tikzpicture}%
\end{document}

You could of course define the coordinates for the positions outside of the matrix once and then simply use this. If you need this kind of thing more often that might be a little cleaner. It should be pretty straightforward to modify this code to suit your needs.

matrix with annotations

2

I wanted to learn how to use package blkarray and here you have the result. Your example just need TiKZ (which is already loaded by pgfplots) for drawing mark symbols because matrix is defined with a blockarray environment.

\documentclass{article}
\usepackage{pgfplots}
\usepackage{blkarray}

\begin{document}
\[A=
\begin{blockarray}{ccccc}
&\tikz{\draw (0,0)--++(0.5,0);}&\tikz{\draw[dashed] (0,0)--++(0.5,0);}&\tikz{\draw (0,0)--++(0.5,0) plot[mark=square*,mark options={fill=black}] (0.25,0);}&\tikz{\draw (0,0)--++(0.5,0) plot[mark=triangle*,mark options={fill=black}] (0.25,0);}\\
\begin{block}{c(cccc)}
\tikz{\draw (0,0)--++(0.5,0);}&1&2&17&3\\
\tikz{\draw[dashed] (0,0)--++(0.5,0);}&4&1&24&1\\
\tikz{\draw (0,0)--++(0.5,0) plot[mark=square*,mark options={fill=black}] (0.25,0);}&0&0&2&-0.9\\
\tikz{\draw (0,0)--++(0.5,0) plot[mark=triangle*,mark options={fill=black}] (0.25,0);}&0&7&-0.95&2\\
\end{block}
\end{blockarray}
\]
\end{document}

enter image description here

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.