5

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!

1
  • 1
    welcome to tex.se! with adding standard preamble (for my use) i cant reproduce your problem (for top example, other are completely wrong set). so, please extend your code snippet to full, small document beginning with \documentclass{...} and end with \end{document}
    – Zarko
    Commented Jan 7, 2018 at 9:26

2 Answers 2

7

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 :-))

2
  • Thanks a lot! unfortunately, I can't give you the answer thing for a few minutes, because I am using my alternate stack exchange account. Anyway, thanks again, that really clarifies the error! I think I had some other error, so I tried to put all the items in the matrix in math mode, but then I was mistified when it would give me mistakes no matter what! I'll remember: I can't nest equations inside equations. Got it. Cool!
    – user463013
    Commented Jan 7, 2018 at 9:38
  • @user463013, see added note to my answer. happy tex-ing!
    – Zarko
    Commented Jan 7, 2018 at 9:45
1

Just eliminate all those $'s from the matrices. For instance, the first matrix should just be:

\[\begin{bmatrix}
1 & 0 & 0 \\
0 & cos(\theta) & -sin(\theta) \\
0 & sin(\theta) & cos(\theta)

Or, better still:

\[R_x (\theta)=\begin{bmatrix}
1 & 0 & 0 \\
0 & cos(\theta) & -sin(\theta) \\
0 & sin(\theta) & cos(\theta)
2
  • Yep, that's precisely the mistake I made. Thanks for weighing in! I've got this paper to finish in...I'd rather not think about the deadline, but it really helped to learn about this! It really is fun to learn about LaTex, and it's really helpful in understanding how people write about mathematics in a professional manner! All I can really say is that Stack Exchange is a professional help site. Again, thanks!
    – user463013
    Commented Jan 7, 2018 at 9:43
  • better \sin and \cos. etc. these shouldn't be entered as if they are representing the multiplication of three variables, which is what will happen if the letters are entered individually. Commented Jan 7, 2018 at 15:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .