6

So I'm trying to put circles around 6,8,10,18 in row 1-4 respectively, however this does give me an error warning saying "missing $ inserted"

\documentclass{article}
\usepackage{mathtools}
\setlength\parindent{0pt}
\usepackage{tikz}



\begin{document}

$   
\text{M}=\begin{pmatrix}
4 & \tikz\node[draw,circle]{6} & 4 & 5 \\ 
3 & 8 & 1 & 6 \\ 
2 & 9 & 2 & 10 \\ 
1 & 2 & 3 & 18
\end{pmatrix}
$ 

\end{document}

How can I correct this error, because positioning various $ around the command hasn't helped so far.

5 Answers5

14

With a tikz matrix you can get a better alignment:

\documentclass{article}
\usepackage{mathtools}
\setlength\parindent{0pt}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\[
\text{M}=
\begin{tikzpicture}[baseline]
\matrix[
    matrix of math nodes, 
    minimum size=16pt,
    row sep=2pt,
    column sep=2pt,
    left delimiter=(,
    right delimiter=), 
    inner xsep=0pt
    ]{
    4 & |[draw, circle]|6 & 4 & 5 \\ 
    3 & |[draw, circle]|8 & 1 & 6 \\ 
    2 & 9 & 2 & |[draw, circle]|10 \\ 
    1 & 2 & 3 & |[draw, circle]|18\\
    };
\end{tikzpicture}
\]
\end{document}

enter image description here

CarLaTeX
  • 62,716
6

The tikz code must always be ended with a semicolon.

4 & \tikz\node[draw,circle]{6}; & 4 & 5 \\ 

matrice

\documentclass{article}
\usepackage{mathtools}
\setlength\parindent{0pt}
\usepackage{tikz}

\begin{document}

$   
\text{M}=
\begin{pmatrix}
4 & \tikz\node[draw,circle]{6}; & 4 & 5 \\ 
3 & 8 & 1 & 6 \\ 
2 & 9 & 2 & 10 \\ 
1 & 2 & 3 & 18
\end{pmatrix}
$ 
\end{document}
AndréC
  • 24,137
  • @ChristianSinger It is not necessary to use mathtools, tikz has a native matrix management that simplifies syntax. – AndréC Oct 28 '18 at 14:13
2

A simple solution with an ordinary matrix and pstricks:

\documentclass{article}
\usepackage{mathtools}
\setlength\parindent{0pt}
\usepackage{pstricks}

\begin{document}

$ \psset{framesep=1pt, linewidth=0.4pt} \text{M}=\begin{pmatrix} 4 & \pscirclebox{6} & 4 & 5 \ 3 & \pscirclebox{8} & 1 & 6 \ 2 & 9 & 2 & \pscirclebox{10} \ 1 & 2 & 3 & \pscirclebox{18} \end{pmatrix}$

\end{document}

enter image description here

Bernard
  • 271,350
2

Since longer numbers rather need to be highlighted by ellipses than by circles, and multicolored circles can be useful, here is another example of how to use F. Pantigny's answer to get different colored ellipses:

 \begin{equation}
A = 
\setlength{\extrarowheight}{1mm}
\begin{bNiceMatrix}
0.0    &0.86   &-1.91  &-0.51  & 1.14\\
-1.47  & 0.0   &-0.57  &-0.33  & 0.038\\
 2.20  &-1.93  & 0.0   &-0.36  &-2.04\\
-2.42  &-2.97  & 1.78  & 0.0   &-0.59\\
-2.55  &-0.61  &-0.58  & 0.85  & 0.0
\CodeAfter
%% uses self-defined colours (see default packages/newcommands.tex)
  \tikz \draw[color = red]    (1-3) ellipse (7mm and 3mm)
                                    (3-1) ellipse (7mm and 3mm);
  \tikz \draw[color = green] (1-5) ellipse (7mm and 3mm)
                                    (5-1) ellipse (7mm and 3mm);
  \tikz \draw[color = blue]     (3-4) ellipse (7mm and 3mm)
                                    (4-3) ellipse (7mm and 3mm);
  \tikz \draw[color = black](4-5) ellipse (7mm and 3mm)
                                    (5-4) ellipse (7mm and 3mm);
\end{bNiceMatrix}
\label{matrix: competition matrix oscillating}
\end{equation}

enter image description here

J..y B..y
  • 1,348
Vahiel
  • 61
1

With nicematrix and Tikz:

\documentclass{article}
\usepackage{nicematrix,tikz}

\begin{document}

$M = \setlength{\extrarowheight}{1mm} \begin{pNiceMatrix} 4 & 6 & 4 & 5 \ 3 & 8 & 1 & 6 \ 2 & 9 & 2 & 10 \ 1 & 2 & 3 & 18 \CodeAfter \tikz \draw (1-2) circle (1.7mm) (2-2) circle (1.7mm) (3-4) circle (2.1mm) (4-4) circle (2.1mm) ; \end{pNiceMatrix}$

\end{document}

Output of the above code

F. Pantigny
  • 40,250