7

With the following codes

\documentclass[12pt,a4paper]{report}
\usepackage{amsfonts,amsmath,amssymb,graphicx,amsthm,xcolor,tikz}
\usetikzlibrary{calc,matrix}
    \begin{document}

\begin{tikzpicture}[ strip/.style = { draw=#1,%color line width=1em, opacity=0.2, shorten <=-2mm,shorten >=-2mm, }, ] \matrix (mtrx) [matrix of math nodes, column sep=1em, nodes={text height=1ex,text width=2ex}] { |[red]|+ & |[red]|+ & \color{red}+\color{blue}- & |[blue]|- & |[blue]|- \[3.3mm,between origins] a_1 & b_1 & c_1 & a_1 & a_2 \ a_2 & b_2 & c_2 & a_2 & b_2 \ a_3 & b_3 & c_3 & a_3 & b_3 \ }; \draw[thick] (mtrx-2-1.north) -| (mtrx-4-1.south west) -- (mtrx-4-1.south); \draw[thick] (mtrx-2-3.north) -| (mtrx-4-3.south east) -- (mtrx-4-3.south); \path[draw,strip=blue] (mtrx-4-1.center) edge (mtrx-2-3.center) (mtrx-4-2.center) edge (mtrx-2-4.center) (mtrx-4-3.center) -- (mtrx-2-5.center); \path[draw,strip=red] (mtrx-2-1.center) edge (mtrx-4-3.center) (mtrx-2-2.center) edge (mtrx-4-4.center) (mtrx-2-3.center) -- (mtrx-4-5.center); \end{tikzpicture} \end{document}

I get

enter image description here

Howerver I want to replace [] by || and chage the place of the minus -. Also I want to add them in equations as the picture shows

enter image description here

I tried with \begin{align*} .. & = \\ &= \end{align*} but it does not work.

Student
  • 1,134
  • 2
  • 9
  • 27
  • I never teach Sarrus' rule to my students. It's practical, that's true, but students have the bad propensity to generalize and to apply the rule also for bigger matrices. I don't find any real advantage in teaching it. – egreg Jun 27 '21 at 15:16
  • @egreg Thanks Professor for your advise. But I must teach this rule to my students since it is in official program – Student Jun 27 '21 at 15:20
  • At an exam one may have to compute a determinant; how this is done cannot be the examination purpose. Does the program also specifies what foot the students have to put in the classroom first? ;-) Do we really want to present mathematics as a set of rules? – egreg Jun 27 '21 at 15:34

1 Answers1

7

The brackets are drawn manually, so you just need to modify (simplify) how they are drawn. E.g. \draw[thick] (mtrx-4-1.south west) -- (mtrx-2-1.north west); -- a single line from the bottom left corner of one cell, to the top left of another cell.

To move the minus signs, literally just move the minus signs in the code to a new row at the end of the matrix.

Note also the addition of the baseline key, as specified in use of tikzpicture matrix in align or gather environment, do get the proper vertical alignment.

Finally, the align* environment clashes with a TikZ matrix because for both, the & character has special meaning. To fix that, you can tell TikZ to use something else as the column separator with ampersand replacement. The common thing to use is \&, but you could use other macros as well. See that in the example below I added ampersand replacement=\& to the \matrix options, and I used \& inside the matrix, for example a_1 \& b_1 \& c_1 \& a_1 \& a_2 \\

enter image description here

\documentclass[12pt,a4paper]{report}
\usepackage{amsfonts,amsmath,amssymb,amsthm,tikz}
\usetikzlibrary{calc,matrix}

\begin{document} \begin{align} D &= \begin{tikzpicture}[ baseline=-\the\dimexpr\fontdimen22\textfont2\relax, strip/.style = { draw=#1,%color line width=1em, opacity=0.2, shorten <=-2mm,shorten >=-2mm, }, ] \matrix (mtrx) [matrix of math nodes, column sep=1em, nodes={text height=1ex,text width=2ex}, ampersand replacement=&amp;] { |[red]|+ &amp; |[red]|+ &amp; |[red]| + \[3.3mm,between origins] a_1 &amp; b_1 &amp; c_1 &amp; a_1 &amp; a_2 \ a_2 &amp; b_2 &amp; c_2 &amp; a_2 &amp; b_2 \ a_3 &amp; b_3 &amp; c_3 &amp; a_3 &amp; b_3 \ |[blue]|- &amp; |[blue]|- &amp; |[blue]|- \ }; \draw[thick] (mtrx-4-1.south west) -- (mtrx-2-1.north west); \draw[thick] (mtrx-4-3.south east) -- (mtrx-2-3.north east); \path[draw,strip=blue] (mtrx-4-1.center) edge (mtrx-2-3.center) (mtrx-4-2.center) edge (mtrx-2-4.center) (mtrx-4-3.center) -- (mtrx-2-5.center); \path[draw,strip=red] (mtrx-2-1.center) edge (mtrx-4-3.center) (mtrx-2-2.center) edge (mtrx-4-4.center) (mtrx-2-3.center) -- (mtrx-4-5.center); \end{tikzpicture} \ &= a+b+c\dots \end{align} \end{document}

Torbjørn T.
  • 206,688
  • Just baseline by itself is "wrong". You need to calculate it using egreg's formula. – Henri Menke Jun 26 '21 at 18:26
  • @HenriMenke Thanks! I kind of just added it, and forgot to actually check what it should be ... Will edit. – Torbjørn T. Jun 26 '21 at 18:29
  • @TorbjørnT. Dear Professor. Thanks a lot for your answer. Please see my related question and I will be greatful if you help me. Thanks a lot https://tex.stackexchange.com/questions/602794/equations-including-tikzpicture-ii – Student Jun 26 '21 at 21:43
  • @Student (I'm not a professor.) Might take a look tomorrow, but there are several other people who can answer as well. – Torbjørn T. Jun 26 '21 at 21:46