4

I am trying to get this to work in Latex, this is the output I am looking for:

Desired output

This is the code I have so far, but I'm not getting what I want:

\documentclass{article}
\usepackage{amsmath,mathrsfs}
\usepackage[usenames, dvipsnames,svgnames]{xcolor}
\usepackage{tikz}
\usetikzlibrary{intersections, decorations.pathreplacing,shapes,shapes.geometric,hobby,patterns,babel,matrix,calc}
\DeclareMathOperator{\Mat}{Mat}
\begin{document}


\begin{tikzpicture}[
decoration = {
brace,amplitude=5pt},
baseline = (current bounding box.west),
strip/.style = {
draw=MediumPurple,%color
line width=1.1em, opacity=0.4,
line cap=round ,% only if you like them ...
},
stripp/.style = {
draw=LightSkyBlue,%color
line width=1.8em, opacity=0.4,
line cap=round ,% only if you like them ...
},
]
\matrix (mtrxx)  [matrix of math nodes,left delimiter=(,right delimiter=),
column sep=.5em,   
nodes={text height=2ex,text width=1.5ex}]
{
1 & 2 \\
3 & 4 \\
5 & 6 \\
};

\begin{scope}[xshift = 6cm]
\matrix (mtrxz)  [matrix of math nodes,left delimiter=(,right delimiter=),
column sep=.5em,
nodes={text height=2ex,text width=1.5ex}]
{
-1 & 2 & 0 & 3 \\
1 & 0 & 4 & -1 \\
};
\end{scope}

\begin{scope}[yshift=-2cm]
\matrix {mtrxi}  [matrix of math nodes,left delimiter=(,right delimiter=),
column sep=.5em,
nodes={text height=2ex,text width=1.5ex}]
{
$3 \times 2$ & & & & & & & & & $2 \times 4$ \\
};
\end{scope}

\node[color=black] at ([shift={(-12mm,-3mm)}]mtrxx-2-1.north) {$A =$};
\node[color=black] at ([shift={(-12mm,0mm)}]mtrxz-2-1.north) {$B =$};

\end{tikzpicture} \\

\end{document}

Any help would be greatly appreciated.

Sephya
  • 473
  • 3
  • 13
  • Hi ! I am not a scientist at all, but is tikz really needed ? Can't you do this with the usual math mode ? Just a naive question. – sztruks Oct 09 '18 at 17:53
  • I have tried, but I had trouble getting it, unless you have a way to do it. – Sephya Oct 09 '18 at 17:55
  • Unfortunately I don't, being an historian. But using tikz looks overkill to me. I suggest adding the math-mode tag to your question. – sztruks Oct 09 '18 at 17:58
  • @Sephya: Can you provide a better title to your question? Doing "this" is very generic... be more descriptive. – Werner Oct 09 '18 at 18:13

1 Answers1

5

Here is a proposal in the matrices are produced in the boring standard way and then annotated with TikZ. To this end, \tikznode is employed, which will be superseded by \tikzmarknode in the hopefully not so far future, when the tikzmark library will be extended by this command.

\documentclass{article}
\usepackage{amsmath,mathrsfs}
\usepackage[usenames, dvipsnames,svgnames]{xcolor}
\usepackage{tikz}
\usetikzlibrary{positioning}
\newcommand{\tikznode}[2]{% from https://tex.stackexchange.com/a/402466/121799
\ifmmode%
\tikz[remember picture,baseline=(#1.base),inner sep=0pt] \node (#1) {$#2$};%
\else
\tikz[remember picture,baseline=(#1.base),inner sep=0pt] \node (#1) {#2};%
\fi}
\DeclareMathOperator{\Mat}{Mat}
\begin{document}
\begin{align*}
A=&\begin{pmatrix}
1 & 2 \\
3 & 4 \\
\tikznode{5}{5} & \tikznode{6}{6} \\
\end{pmatrix}
&
B=&
\begin{pmatrix}
-1 & 2 & 0 & 3 \\
\tikznode{1}{1} & 0 & 4 & \tikznode{m1}{-1} \\
\end{pmatrix} \\[1cm]
\begin{tikzpicture}[overlay,remember picture]
 \draw[latex-latex] ([yshift=-3pt]5.south) -- ([yshift=-3pt]6.south)
  node[midway,below](dimA){$3\times2$};
 \draw[latex-latex] ([yshift=-3pt]1.south) -- ([yshift=-3pt]m1.south)
  node[midway,below](dimB){$2\times4$};
 \draw[latex-latex] ([xshift=-6pt]dimA.south east) -- ++(0,-0.3) -|
  ([xshift=6pt]dimB.south west);
\end{tikzpicture}
=&\begin{pmatrix}
 \bullet & \bullet & \bullet & \bullet \\
 \bullet & \bullet & \bullet & \bullet \\
 \bullet & \bullet & \bullet & \bullet \\
 \bullet & \bullet & \tikznode{24}{24} & \bullet \\
\end{pmatrix}
&&%\begin{array}{r@{}l}
\tikznode{AB}{$(AB)_{33}$}%&
=\begin{pmatrix} \tikznode{5p}{5} & \tikznode{6p}{6}
\end{pmatrix} \begin{pmatrix} 0\\\tikznode{4p}{4}\end{pmatrix} %\\
%&
=6\cdot0+6\cdot4=\tikznode{24p}{24}
%\end{array}
\begin{tikzpicture}[overlay,remember picture]
\draw[latex-latex] (24) to[out=-30,in=-150] (AB);
\path (5p.south) -- (6p.south) coordinate[midway] (aux);
\draw[latex-] (4p.south) to[out=-90,in=90] ++ (0.5,-1) node[below right](B3){3rd column of $B$};
\draw[latex-] (aux) to[out=-90,in=90] ++ (-0.5,-1) node[below left](A3){3rd row of $A$};
\end{tikzpicture} \\[0.5cm]
\end{align*}
Some more text.
\end{document}

enter image description here

  • 1
    Every time I click through an answer by you I get a gentle kick towards updating tikzmark! It will happen soon, I promise! – Andrew Stacey Oct 09 '18 at 19:45
  • 1
    @marmot Yes, I know! The hold-up (apart from general busyness) is that I want to merge it with savenodes (https://github.com/loopspace/savenodes) as I think they'd work well together. – Andrew Stacey Oct 09 '18 at 19:52
  • @LoopSpace Yes, I guess there is a huge demand for that. And thanks for doing all this. (There is something that sort of bothers me when writing these answers: I always have to add vertical space by hand such that the overlay pics to not run over the subsequent text. The only reason for Some more text. here is that I wanted to check that this does not happen. In principle, I think, it should be possible to compute and automatically insert this space. Have you ever thought about this?) –  Oct 09 '18 at 19:56
  • 1
    One of the issues there is that the overlaid stuff is viewed by TeX as being anchored to where it is specified (so there's a zero-size box somewhere), so giving it some size is tricky as you want its actual size not its TeX-size (which would include that zero-size box). I guess you could stick a \tikzmark in each overlaid bit and then add enough space that you're below all those marks. That could be automated, I guess. – Andrew Stacey Oct 09 '18 at 20:01