5

I need to show the connection of an element of a matrix, with a column on another matrix, like this:

Sparse matrix Save on coordinate method

How i can do this?

4
  • 2
    can you post your matrix code?
    – percusse
    Mar 22, 2014 at 12:30
  • A = \left[ \begin{matrix} 1 & 0 & 0 & 2 & 0 \\ 3 & 4 & 0 & 5 & 0 \\ 6 & 0 & 7 & 8 & 9 \\ 0 & 0 & 10 & 11 & 0 \\ 0 & 0 & 0 & 0 & 12 \end{matrix} \right] @percusse
    – meysam
    Mar 22, 2014 at 12:47
  • @meysam And what about the code for the matrices on the right side? Mar 22, 2014 at 14:20
  • It's my problem too! @GonzaloMedina
    – meysam
    Mar 22, 2014 at 14:31

1 Answer 1

8

One option using \tikzmark from the tikzmark library:

\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{tikzmark}

\newcommand\BoxMatrix[2]{%
  \tikz[remember picture,overlay]
  \draw 
  ([xshift=-1pt,yshift=-2pt]{pic cs:#1}) 
  rectangle ++(#2,2.65ex);
}
\newcommand\BoxEntry[2][fill=red!30]{
  \tikz[remember picture,overlay,baseline=(O.base)]\node[draw,inner sep=2.5pt,#1] (O){#2};
}
\begin{document}

\begin{tikzpicture}[remember picture,overlay]
\draw[fill=cyan!50] 
  ([xshift=-7pt,yshift=20pt]{pic cs:topa}) 
  rectangle 
  ([xshift=13pt,yshift=-15pt]{pic cs:bottoma});
\draw[->]
  ([yshift=3pt]{pic cs:starta}) 
  to[out=80,in=90] 
  ([yshift=22pt]{pic cs:topa});  
\end{tikzpicture}
\BoxMatrix{startAAa}{3cm}
\BoxMatrix{startJRa}{3cm}
\BoxMatrix{startJBa}{3cm}

\[
A = 
\begin{bmatrix} 
1 & 0 & 0 & 2 & 0 \\ 
3 & 4 & 0 & 5 & 0 \\ 
6 & 0 & 7 & \BoxEntry{8}\tikzmark{starta} & 9 \\ 
0 & 0 & 10 & 11 & 0 \\ 
0 & 0 & 0 & 0 & 12 
\end{bmatrix} 
\begin{array}{r@{\,=\ }rrr}
AA & 
\tikzmark{startAAa}12 & \tikzmark{topa}8 & 4\\[1ex]
JR &
\tikzmark{startJRa}\phantom{1}5 & 4 & 15\\[1ex]
JB &
\tikzmark{startJBa}\phantom{1}1 & \tikzmark{bottoma}13 & 6
\end{array}
\]

\begin{tikzpicture}[remember picture,overlay]
\draw[fill=orange!50] 
  ([xshift=-7pt,yshift=20pt]{pic cs:topb}) 
  rectangle 
  ([xshift=8pt,yshift=-15pt]{pic cs:bottomb});
\draw[->]
  ([yshift=3pt]{pic cs:startb}) 
  to[out=-50,in=230] 
  ([yshift=-18pt]{pic cs:bottomb});  
\end{tikzpicture}
\BoxMatrix{startAAb}{4cm}
\BoxMatrix{startJRb}{4cm}
\BoxMatrix{startJBb}{4cm}

\vspace{1cm}

\[
A = 
\begin{bmatrix} 
1 & 0 & 0 & 2 & 0 \\ 
3 & 4 & 0 & 5 & 0 \\ 
6 & 0 & 7 & 8 & 9 \\ 
0 & 0 & 10 & 11 & 0 \\ 
0 & 0 & \BoxEntry[fill=yellow]{30}\tikzmark{startb} & 0 & 12 
\end{bmatrix} 
\begin{array}{r@{\,=\ }rrr}
AA & 
\tikzmark{startAAb}12 & 8 & \tikzmark{topb}4\\[1ex]
JR &
\tikzmark{startJRb}\phantom{1}5 & 4 & 15\\[1ex]
JB &
\tikzmark{startJBb}\phantom{1}1 & 13 & \tikzmark{bottomb}6
\end{array}
\]

\end{document}

enter image description here

7
  • 1
    @meysam Please see my updated answer. Mar 22, 2014 at 15:05
  • 1
    There are a few alignment problems: (1) The red square is not exactly centered relative to 8; (2) 5 and 1 are not exactly aligned under the 2 in 12; (3) 8 and 4 are not aligned relative to 13; (4) the spacing after = is too small; (5) the boxes to the right are not exactly aligned. Mar 22, 2014 at 15:11
  • 1
    @SvendTveskæg all of them fixed now (I hope). Mar 22, 2014 at 15:32
  • 2
    @SvendTveskæg Arghhhh! I think it's fine now in the last edit :) Mar 22, 2014 at 16:07
  • 1
    Indeed it is. I can't find any more reasons for nitpicking on your otherwise great answer. :) Mar 22, 2014 at 16:34

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.