Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I saw an old question on how to obtain arrows inside of matrices using arrows. And thanks to Altermundus I was able to hack together the file below:

\documentclass[12pt]{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\usepackage{fullpage,amsmath}
\begin{document}

\begin{align*}
\begin{array}{|rrr|rr}
i &  j & k & i &  j\\
3 & -2 & 2 & 3 & -2\\
3 &  2 & 1 & 3 &  2
\end{array}
\end{align*}

\begin{figure}[!htbp]
\tikzset{node style ge/.style={circle}}
\centering
\begin{tikzpicture}[baseline=(A.center)]
  \tikzset{BarreStyle/.style =   {opacity=.4,line width=3 mm,line cap=round,color=#1}}
  \tikzset{SignePlus/.style  =   {above left,,opacity=0.8,circle,fill=#1!50}}
  \tikzset{SigneMoins/.style =   {above right,,opacity=0.8,circle,fill=#1!50}}
% les matrices
\matrix (A) [matrix of math nodes, nodes = {node style ge},,column sep=0 mm] 
{      i &  j & k & i &  j \\
       3 & -2 & 2 & 3 & -2 \\
       3 &  2 & 1 & 3 &  2 \\
};

 \draw [BarreStyle=red] (A-1-1.north west) node[SignePlus=red] {$+$} to (A-3-3.south east) ;
 \draw [BarreStyle=red] (A-1-2.north west) node[SignePlus=red] {$+$} to (A-3-4.south east) ;
 \draw [BarreStyle=red] (A-1-3.north west) node[SignePlus=red] {$+$} to (A-3-5.south east) ;
 \draw [BarreStyle=blue]  (A-1-3.north east) node[SigneMoins=blue] {$-$} to (A-3-1.south west);
 \draw [BarreStyle=blue]  (A-1-4.north east) node[SigneMoins=blue] {$-$} to (A-3-2.south west);
 \draw [BarreStyle=blue]  (A-1-5.north east) node[SigneMoins=blue] {$-$} to (A-3-3.south west);
 \draw (A-1-1.north west) -- (A-3-1.south west); \draw (A-1-3.north east) -- (A-3-3.south east);
\end{tikzpicture}
\end{figure}

\end{document}

CrossProduct

Now I am having a few bunks to iron out, and some help would be much obliged =)

  • Is there any method to get the absolute signs, eg the brackets | | around the matrices to be horizontal? Right now they are slanted, due to the different size of a_{11} and $_{13}.

  • The blue and red lines are also not quite parallel, any fix here?

I was thinking of wrapping this code in a new environment, or something. But I guess that will have to do for a later question. I sort of need to make a few of these, so I would preffer not a quick and dirty fix that works only in this case. My entries are never bigger than 3 digits.

share|improve this question
4  
The best is never teach the "Sarrus' rule". If one learns it, good for him/her. But when a student will apply the rule to 4x4 matrices, the fault will be only on the teacher. Developing the determinant over the first row is just as easy. –  egreg Jun 12 '12 at 12:53
    
I try to show several methods. I must say I prefer cofactor expansion. But also one an never get enough practice in tikz! =) –  N3buchadnezzar Jun 12 '12 at 12:57

2 Answers 2

up vote 5 down vote accepted

Here are some modifications that can (hopefully) improve the result:

  1. I declared a fixed width for the text in the matrix nodes.
  2. I used the let syntax to draw the vertical rules.
  3. I reduced the inner sep value for the nodes containing the signs.
  4. I defined a \checkers command with a mandatory argument (the name of the matrix) to draw the design. In fact, a seven argument command might be used for the whole construct.

Here's the code

\documentclass[12pt]{article}
\usepackage{tikz}
\usetikzlibrary{calc,matrix}
\usepackage{fullpage,amsmath}

\newcommand\checkers[1]{%
 \draw [BarreStyle=red] (#1-1-1.120) node[SignePlus=red] {$+$} to (#1-3-3.320) ;
 \draw [BarreStyle=red] (#1-1-2.120) node[SignePlus=red] {$+$} to (#1-3-4.320) ;
 \draw [BarreStyle=red] (#1-1-3.120) node[SignePlus=red] {$+$} to (#1-3-5.320) ;
 \draw [BarreStyle=blue]  (#1-1-3.35) node[SigneMoins=blue] {$-$} to (#1-3-1.230);
 \draw [BarreStyle=blue]  (#1-1-4.35) node[SigneMoins=blue] {$-$} to (#1-3-2.230);
 \draw [BarreStyle=blue]  (#1-1-5.35) node[SigneMoins=blue] {$-$} to (#1-3-3.230);
 \draw let \p1 = (#1-3-3.south east), \p2 = ( $ (#1-1-3.north east)!.5!(#1-1-4.north) $ ) in (\p2) -- (\x2,\y1);
 \draw let \p1 = (#1-1-1.north west), \p2 = (#1-3-1.south west) in (\p2) -- (\x2,\y1);
}
\tikzset{BarreStyle/.style =   {opacity=.4,line width=4mm,line cap=round,color=#1}}
\tikzset{SignePlus/.style  =   {above left,,opacity=0.8,circle,inner sep=1pt,fill=#1!50}}
\tikzset{SigneMoins/.style =   {above right,,opacity=0.8,circle,inner sep=1pt,fill=#1!50}}

\begin{document}

\begin{figure}
\tikzset{node style ge/.style={circle}}
\centering
\begin{tikzpicture}[baseline=(A.center)]
\matrix (A) [matrix of math nodes, nodes = {node style ge},column sep=0 mm,nodes={text width=1em,align=right}] 
{      i &  j & k & i &  j \\
       3 & -2 & 2 & 3 & -2 \\
       31 &  2 & 61 & 3 &  2 \\
};
\checkers{A}
\end{tikzpicture}\\[1em]
\begin{tikzpicture}[baseline=(B.center)]
\matrix (B) [matrix of math nodes, nodes = {node style ge},column sep=0 mm,nodes={text width=1em,align=right}] 
{      i &  j & k & i &  j \\
       13 & -2 & 25 & 13 & -2 \\
       3 &  -18 & 1 & 3 &  2 \\
};
\checkers{B}
\end{tikzpicture}
\end{figure}

\end{document}

enter image description here

If this will be always used to indicate cross-products, then a command with seven arguments (the six coordinates and the matrix name) can be defined; something along these lines:

\documentclass[12pt]{article}
\usepackage{tikz}
\usetikzlibrary{calc,matrix}
\usepackage{fullpage,amsmath}

\newcommand\checkers[7]{%
\begin{tikzpicture}[baseline=(#1.center)]
\tikzset{BarreStyle/.style =   {opacity=.4,line width=4mm,line cap=round,color=##1}}
\tikzset{SignePlus/.style  =   {above left,opacity=0.8,circle,inner sep=1pt,fill=##1!50}}
\tikzset{SigneMoins/.style =   {above right,opacity=0.8,circle,inner sep=1pt,fill=##1!50}}
\tikzset{node style ge/.style={circle}}

\matrix (#1) [matrix of math nodes, nodes = {node style ge},column sep=0 mm,nodes={text width=1.8em,align=right,font=\footnotesize},ampersand replacement = \&] 
{      i \&  j \& k \& i \&  j \\
       #2 \& #3 \& #4 \& #2 \& #3 \\
       #5 \&  #6 \& #7 \& #5 \&  #6 \\
};
\draw [BarreStyle=red] (#1-1-1.120) node[SignePlus=red] {$+$} to (#1-3-3.320) ;
\draw [BarreStyle=red] (#1-1-2.120) node[SignePlus=red] {$+$} to (#1-3-4.320) ;
\draw [BarreStyle=red] (#1-1-3.120) node[SignePlus=red] {$+$} to (#1-3-5.320) ;
\draw [BarreStyle=blue]  (#1-1-3.35) node[SigneMoins=blue] {$-$} to (#1-3-1.230);
\draw [BarreStyle=blue]  (#1-1-4.35) node[SigneMoins=blue] {$-$} to (#1-3-2.230);
\draw [BarreStyle=blue]  (#1-1-5.35) node[SigneMoins=blue] {$-$} to (#1-3-3.230);
\draw let \p1 = (#1-3-3.south east), \p2 = ( $ (#1-1-3.north east)!.5!(#1-1-4.north) $ ) in (\p2) -- (\x2,\y1);
\draw let \p1 = (#1-1-1.north west), \p2 = (#1-3-1.south west) in (\p2) -- (\x2,\y1);\end{tikzpicture}
}

\begin{document}

\begin{figure}
\centering
\checkers{A}{3}{-2}{2}{31}{2}{61}\\[1em]
\checkers{B}{13}{-2}{25}{3}{-18}{1}
\end{figure}

\end{document}
share|improve this answer
    
Really great! Is the only reason to post to images to show that it works with double digit numbers? =) I tried seeing if there are any differences, and I can not spot any, –  N3buchadnezzar Jun 12 '12 at 14:22
    
@N3buchadnezzar yes; I only wanted to show that it behaves relatively well in more than one case ;-) –  Gonzalo Medina Jun 12 '12 at 14:30
    
@N3buchadnezzar I've updated my answer with a command to simplify the code. –  Gonzalo Medina Jun 12 '12 at 16:47

Just another solution using fit, rotate fit and pgfonlayer...

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc,matrix,backgrounds,fit}
\usepackage{amsmath}

\newcommand\checkers[1]{
  \begin{pgfonlayer}{background}
    \tikzset{
      bar style/.style={inner ysep=-.1em,inner xsep=1em,rounded corners=.8em},
      fillop/.style={fill=##1,fill opacity=.3},
      sign/.style={circle,inner sep=.2em,fillop=##1,text opacity=1},
    }
    \foreach \posa in {1,2,3}{
      \pgfmathsetmacro{\posb}{\posa + 2}
      \node[bar style=red,rotate fit=-45,
      fit=(#1-1-\posa)(#1-3-\posb),fillop=red,
      label={[sign=red]west:$+$}]{};
      \node[bar style=red,rotate fit=45,
      fit=(#1-1-\posb)(#1-3-\posa),fillop=blue,
      label={[sign=blue]east:$-$}]{};
    }
    \draw let \p1=($(#1-1-3)!.5!(#1-1-4)$) in
    (\p1 |- #1.north) -- (\p1 |- #1.south);
    \draw (#1.north west) -- (#1.south west);
  \end{pgfonlayer}
}

\begin{document}
\begin{tikzpicture}
  \matrix (A)
  [matrix of math nodes,inner sep=0pt,
  nodes={minimum size=2.5em,inner sep=0pt}] 
  {
    i  & j  & k  & i & j  \\
    3  & -2 & 2  & 3 & -2 \\
    61 & 2  & 61 & 3 & 2  \\
  };
  \checkers{A}
\end{tikzpicture}

{\footnotesize%
  \begin{tikzpicture}
    \matrix (B)
    [matrix of math nodes,inner sep=0pt,
    nodes={minimum size=2.5em,inner sep=0pt}] 
    {
      i  & j   & k  & i  & j  \\
      13 & -2  & 125 & 13 & -2 \\
      3  & -18 & 1  & 3  & 2  \\
    };
    \checkers{B}
  \end{tikzpicture}%
}
\end{document}

Here is results with different font sizes:

enter image description here

share|improve this answer
    
Nice solution! Unfortunately I'm out of votes for today. –  Gonzalo Medina Jun 12 '12 at 20:17

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.