[Tex/LaTex] Missing $ inserted for matrices

matricesmissing

I'm relatively new to Latex, but why is latex giving me a missing $ inserted error? I looked at other posts, but they have not fixed my problem. Maybe I'm confused about when to use the inline math mode for equations, \[ and \], so another way of phrasing my question would be, "am I getting a missing $ error because my matrices aren't equations, or is it some other reason?" For an example of what I'm saying, here is this matrix

\[\begin{bmatrix}
    a & b \\
    c & d
\end{bmatrix}\] 

I already tried the code above, and while the example above doesn't give me any errors when I copy and paste the

\[\begin{bmatrix}

and

\end{bmatrix}\] 

for my matrices, it didn't work.

Here is the code for the matrices I'm using.

$R_x (\theta)=$
    \[\begin{bmatrix}
    $1$ & $0$ & $0$ \\
    $0$ & $cos(\theta)$ & $-sin(\theta)$ \\
    $0$ & $sin(\theta)$ & $cos(\theta)$
\end{bmatrix}\] 

$R_y (\theta)=$
    \[\begin{bmatrix}
    $cos(\theta)$ & $0$ & $sin(\theta)$ \\
    $0$ & $1$ & $0$ \\
    $-sin(\theta)$ & $0$ & $cos(\theta)$
\end{bmatrix}\] 

$R_z (\theta)=$
    \[\begin{bmatrix}
    $cos(\theta)$ & $-sin(\theta)$  & $0$ \\
    $sin(\theta)$ & $cos(\theta)$ & $0$ \\
    $0$ & $0$ & $1$
\end{bmatrix}\] 

Here is my full preamble, sorry!

\documentclass[12pt]{article}

\usepackage{graphicx}
\usepackage{caption}
\usepackage{subcaption}
\usepackage{siunitx}
\usepackage{textcomp}
\usepackage[margin=1in]{geometry}
\usepackage{amsfonts, amsmath, amssymb}
\usepackage[none]{hyphenat}
\usepackage{fancyhdr}
\usepackage{graphicx}
\usepackage{float}
\usepackage[nottoc, notlot, notlof]{tocbibind}

\pagestyle{fancy}
\fancyhead{}
\fancyfoot{}
\fancyhead[R]{\slshape \MakeUppercase}
\fancyhead[L]{\slshape}
\fancyfoot[C]{\thepage}
\setlength{\headheight}{15pt}
%\renewcommand{\headrulewidth}{0pt}
%\renewcommand{\footrulewidth}{0pt}

%\parindent 0ex
%\setlength{\parindent}{4em}
%\setlength{\parskip}{1em}
\renewcommand{\baselinestretch}{1.5}


\begin{document}

\section{sample section}


$R_x (\theta)=$
    \[\begin{bmatrix}
    $1$ & $0$ & $0$ \\
    $0$ & $cos(\theta)$ & $-sin(\theta)$ \\
    $0$ & $sin(\theta)$ & $cos(\theta)$
\end{bmatrix}\] 

$R_y (\theta)=$
    \[\begin{bmatrix}
    $cos(\theta)$ & $0$ & $sin(\theta)$ \\
    $0$ & $1$ & $0$ \\
    $-sin(\theta)$ & $0$ & $cos(\theta)$
\end{bmatrix}\] 

$R_z (\theta)=$
    \[\begin{bmatrix}
    $cos(\theta)$ & $-sin(\theta)$  & $0$ \\
    $sin(\theta)$ & $cos(\theta)$ & $0$ \\
    $0$ & $0$ & $1$
\end{bmatrix}\] 


\subsection{sample subsection}

\end{document}

I know it's a bit over excessive, but I'm writing a paper.

Anyway, any help would be appreciated, thanks again!

P.S.S. Just thinking about how to ask questions in the future, and for anyone interested, what I should've asked about is:

\documentclass[12pt]{article}
\usepackage{amsfonts, amsmath, amssymb}


$R_x (\theta)=$
    \[\begin{bmatrix}
    $1$ & $0$ & $0$ \\
    $0$ & $cos(\theta)$ & $-sin(\theta)$ \\
    $0$ & $sin(\theta)$ & $cos(\theta)$
\end{bmatrix}\] 

And as more of a theoretical add-on to the question, how did you know that the important package was amsmath? Or that the error was nested equations? Was it just experience? Thanks! Once more, just trying to revise mistakes, because that's what coding's all about, right? Thanks!

Best Answer

top example work as expected, in all other you make mistakes: nested equations in equation:

$R_z (\theta)=$
    \[\begin{bmatrix}
    $cos(\theta)$ & $-sin(\theta)$  & $0$ \\
    $sin(\theta)$ & $cos(\theta)$ & $0$ \\
    $0$ & $0$ & $1$
\end{bmatrix}\] 

are forbidden. correct way is:

\documentclass{article}
\usepackage{amsmath}

\begin{document}
\[    % <-- start math environment
R_z (\theta)=
\begin{bmatrix}
    \cos(\theta) & -\sin(\theta)  & 0 \\
    \sin(\theta) &  \cos(\theta)  & 0 \\
    0            & 0             & 1
\end{bmatrix}
\]    % <-- end of math environment
\end{document}

which gives expected rezult:

enter image description here

note: for cos and sin you should use predefined math operators \cos and \sin which write them correctly with upright fonts (and on this way distinguish operators from variables).

as novice, i suggest you to read some introductory text about math settings by latex. for example LaTeX/Mathematics and more advanced use LaTeX/Advanced Mathematics (beside this two text you can find by googling many others :-))

Related Question