[Tex/LaTex] Compilation problem with tikz diagram

compilingmatricestikz-matrixtikz-pgf

As I said, I'm writing math books for school; one of them is about matrices and I have several problems with it.

It's a big code, so I'm trying to summarize it:

  1. Problem with TikZ matrix:
    When I try to compile it something like

    \draw [<->, yl] (A-2-4.north) to [bend left] node [arrow style mul] {$+$} (B-2-4.north);
    

    using the tikz package and the following tikz libraries:

    \usepackage{tikz}
    \usetikzlibrary{decorations.pathmorphing}
    \usetikzlibrary{decorations.markings}
    \usetikzlibrary{shapes,snakes,calc,arrows}
    \usetikzlibrary{intersections,through,backgrounds}
    \usetikzlibrary{shapes.arrows,chains,positioning}
    \usetikzlibrary{matrix,decorations.pathmorphing}
    

    I get

    No shaped name A-2-4 is known. See the pgf package documentation for explanation
    
  2. And when I try to compile this (using \include{filename}):

     \chapterimage{m2summary.pdf}
     \chapter{Respostas e soluções}
     \begin{multicols}{2}
     \section*{\textcolor{ocre}{1. Matrizes}}
     \subsection*{Exercícios de fixação}
     \begin{enumerate}[label=\arabic*.]
     \tiny{
     \item $\ds A=\begin{bmatrix} 0 & -1 & -2 \\ 1 & 0 & -1 \\ 2 & 1 & 0 \end{bmatrix}$
     }
     \end{enumerate}
     \end{multicols}
    

    it shows:

    Missing $ inserted. HOW? I tried to compile with no text, actually, with no math text, and it keeps showing that $ is missing. I really don't know what to do!

Sorry for making this long post, I just tried to be as complete as I can. And I use WinEdt 8, MikTeX 2.9 that I installed this week (February 2014).

Code of TIKZ pic:

\begin{tikzpicture}
\matrix(A)[matrix of math nodes, nodes={node style ge}, left delimiter={[}, right delimiter={]}] at (0,0){
  a_{11}                         & a_{12}                         & \cdots & a_{1n}                         \\
  \node[node style sp] {a_{21}}; & \node[node style sp] {a_{22}}; & \cdots & \node[node style sp] {a_{2n}}; \\
  \vdots                         & \vdots                         & \ddots & \vdots                         \\
  a_{m1}                         & a_{m2}                         & \cdots & a_{mn}                         \\
};
\node [draw, above=10pt] at (A.north) {matriz $\color{ocre}A$};
\node [right=20pt] at (A.east) {$\color{ocre}+$};

\matrix(B)[matrix of math nodes, nodes={node style ge}, left delimiter={[}, right delimiter={]}] at (6,0){
  b_{11}                         & b_{12}                         & \cdots & b_{1n}                         \\
  \node[node style sp] {b_{21}}; & \node[node style sp] {b_{22}}; & \cdots & \node[node style sp] {b_{2n}}; \\
  \vdots                         & \vdots                         & \ddots & \vdots                         \\
  b_{m1}                         & b_{m2}                         & \cdots & b_{mn}                         \\
};
\node [draw, above=10pt] at (B.north) {matriz $\color{ocre}B$};

\matrix(C)[matrix of math nodes, nodes={node style ge}, left delimiter={[}, right delimiter={]}] at (6,-4.5){
  c_{11}                              & c_{12}                               & \cdots & c_{1n}                                \\
  \node[node style sp, ocre] {c_{21}}; & \node[node style sp, blue] {c_{22}}; & \cdots & \node[node style sp, yl] {c_{2n}}; \\
  \vdots                              & \vdots                               & \ddots & \vdots                                \\
  c_{m1}                              & c_{m2}                               & \cdots & c_{mn}                                \\
};
\node [draw, below=10pt] at (C.south) {matriz $\color{ocre}C=A+B$};

\node (a) [above=10pt] at (A-2-1.north) {};
\node (b) [above=10pt] at (C-2-1.north) {};

\draw [<->, yl] (A-2-4.north) to [bend left] node [arrow style mul] {$+$} (B-2-4.north);
\draw [->, yl] (B-2-4.south) to (C-2-4.north);

\draw [<->, blue] (A-2-2.north) to [bend left] node [arrow style mul] {$+$} (B-2-2.north);
\draw [->, blue] (B-2-2.south) to (C-2-2.north);

\draw [<->, ocre] (A-2-1.north) to [bend left] node [arrow style mul] {$+$} (B-2-1.north);
\draw [->, ocre] (B-2-1.south) to (C-2-1.north);
\end{tikzpicture}

Best Answer

(This answers only the TikZ question. For the other, I'd recommend reducing your code to a minimal working example (MWE) and asking a new question about that.)

Your problem is that you have a matrix of math nodes, but you're using \node {} in the cells. By default, when you have a matrix of (math) nodes TikZ prepends something like \node [name=A-1-2] { to the contents of the cell (and adds the closing }; after), but not when you use \node or similar in a cell. Quoting the manual:

Conceptually, this key adds \node{ at the beginning and }; at the end of each cell and sets the anchor of the node to base. Furthermore, it adds the option name option to each node, where the name is set to matrix name - row number - column number. For example, if the matrix has the name my matrix, then the node in the upper left cell will get the name my matrix-1-1.

[..]

If your cell starts with a \path command or any command that expands to \path, which includes \draw, \node, \fill and others, the \node{ startup code and the }; code are suppressed.

To be able to pass options/styles to the nodes, write e.g.

|[my node style]| content of matrix cell

I.e. add vertical bars around the brackets containing the options, and place this at the beginning of the cell.

Complete code below. You didn't add the definition of your styles, so I defined them as empty styles.

enter image description here

\documentclass[11pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix}

\tikzset{
  node style ge/.style={},
  node style sp/.style={},
  yl/.style={},
  arrow style mul/.style={},
}
\definecolor{ocre}{RGB}{204, 119, 34}
\begin{document}

\begin{tikzpicture}
\matrix(A)[matrix of math nodes, nodes={node style ge}, left delimiter={[}, right delimiter={]}] at (0,0){
  a_{11}                         & a_{12}                         & \cdots & a_{1n}                         \\
 | [node style sp]| a_{21} & |[node style sp]| a_{22} & \cdots & |[node style sp]| a_{2n} \\
  \vdots                         & \vdots                         & \ddots & \vdots                         \\
  a_{m1}                         & a_{m2}                         & \cdots & a_{mn}                         \\
};
\node [draw, above=10pt,font=\color{ocre}] at (A.north) {matriz $A$};
\node [right=20pt,font=\color{ocre}] at (A.east) {$+$};

\matrix(B)[matrix of math nodes, nodes={node style ge}, left delimiter={[}, right delimiter={]}] at (6,0){
  b_{11}                         & b_{12}                         & \cdots & b_{1n}                         \\
  |[node style sp]| b_{21} & |[node style sp]| b_{22} & \cdots & |[node style sp]| b_{2n} \\
  \vdots                         & \vdots                         & \ddots & \vdots                         \\
  b_{m1}                         & b_{m2}                         & \cdots & b_{mn}                         \\
};
\node [draw, above=10pt,font=\color{ocre}] at (B.north) {matriz $B$};

\matrix(C)[matrix of math nodes, nodes={node style ge}, left delimiter={[}, right delimiter={]}] at (6,-4.5){
  c_{11}                              & c_{12}                               & \cdots & c_{1n}                                \\
  |[node style sp,ocre]| c_{21} & |[node style sp, blue]| c_{22} & \cdots & |[node style sp, yl]| c_{2n} \\
  \vdots                              & \vdots                               & \ddots & \vdots                                \\
  c_{m1}                              & c_{m2}                               & \cdots & c_{mn}                                \\
};
\node [draw, below=10pt,font=\color{ocre}] at (C.south) {matriz $C=A+B$};

\node (a) [above=10pt] at (A-2-1.north) {};
\node (b) [above=10pt] at (C-2-1.north) {};

\draw [<->, yl] (A-2-4.north) to [bend left] node [arrow style mul] {$+$} (B-2-4.north);
\draw [->, yl] (B-2-4.south) to (C-2-4.north);

\draw [<->, blue] (A-2-2.north) to [bend left] node [arrow style mul] {$+$} (B-2-2.north);
\draw [->, blue] (B-2-2.south) to (C-2-2.north);

\draw [<->] (A-2-1.north) to [bend left] node [arrow style mul] {$+$} (B-2-1.north);
\draw [->] (B-2-1.south) to (C-2-1.north);
\end{tikzpicture}
\end{document}
Related Question