LaTeX align environment missing $ inserted - latex

I'm using an Align environment inside of a proof, and I'm getting the error "Missing $ inserted." I've commenting out all but the first line of the align makes the error go away, but I still can't figure out what the problem is. No solution I could find works. Am I missing something?
\documentclass[12pt,letterpaper]{article}
\usepackage{ifpdf, enumerate}
\usepackage{mla}
\usepackage{gb4e}
\usepackage{amsfonts}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{stmaryrd}
\newcommand{\tuple}[1]{\ensuremath{\left \langle #1 \right \rangle }}
\newcommand{\evaluation}[2][]{\ensuremath{\llbracket #2\rrbracket^{#1}}}
\begin{document}
\begin{proof}
\begin{align*}
\evaluation[\mathbb{M}]{(Mx)(D (x) \& V (x))} = 1 \iff \\
\evaluation[\mathbb{M}]{\lbrack \alpha / x \rbrack (D (x) \& V (x))} = 1 \text{for most constants \alpha} \\
\evaluation[\mathbb{M}]{(D (\alpha) \& V (\alpha))} = 1 \text{for most constants \alpha } \\
\evaluation[\mathbb{M}]{D (\alpha)} = 1 \text{and} \evaluation[\mathbb{M}]{V (\alpha)} = 1 \text{for most constants \alpha } \\
F(\alpha) \in F(D) \text{and} F(\alpha) \in F(V) \text{for most constants \alpha } \\
\intertext{Examining the definitions of the model, we see that there are only four entities that are both in $F(D)$ and $F(V)$: Alice, Bob, Colin, and David. Because this is not more than half of the total twelve entities in the universe, our naive definition of ``most'' is not satisfied. Therefore the statement is false, which implies that the truth value of the origenal proposition is not 1.}
\end{align*}
\end{proof}
\end{document}
Here is what the error message looks like.
./test.tex:24: Missing $ inserted.
<inserted text>
$
l.24 \end{align*}
?

You've missed only one tricky fact: \alpha only works in math mode, so if you replace all occurrences of
\text{for most constants \alpha} \\
(lines 18, 19, 20, 21) with
\text{for most constants }\alpha \\
then your tex will compile sweetly. Also:
\text{for most constants}\ \alpha \\
or:
\text{for most constants $\alpha$} \\
as you already did in line 22 of your code (...$F(D)$ and $F(V)$...).

This answer is not applicable to this exact case, but the given error can also pop up if the "amsmath" package is not being used. It becomes tricky because the whole code looks alright except I had forgotten to add the \usepackage{amsmath} line in the beginning of the document.

Related

Typesetting the numbered mathematical formula in latex

How to typeset the following numbered mathematical formula:
Use amsmath's cases environment:
\documentclass{article}
\usepackage{amsmath}
\begin{document}
\[
\Delta(x) = \begin{cases}
1 & \text{if $x > 0$} \\
-1 & \text{if $x < 0$} \\
0 & \text{otherwise}
\end{cases}
\]
\end{document}
Here is one way to do it:
$
\Delta(x)=
\left\{
\begin{matrix}
1 & \mathrm{if}~x>0 \\
-1 & \mathrm{if}~x<0 \\
0 & \mathrm{otherwise}
\end{matrix}
\right.
$
Which produces the equation you wanted like this:
The example LaTeX equation
This type of equation has a few key tricks in it:
The left bracket has to be declared as \{ in order for LaTeX to
know to display it since it is a special character
Also, the left brace has to be resized to match however many rows you include in your equation, which is done by the \left command preceding it. This can also be done for (, [, etc.
Since you have a \left command, it has to be accompanied by a \right}, but you don't want to display the right brace, so the \right. ends the section without displaying the right brace.
The \mathrm command turns the text inside it back to normal text (removing the math mode temporarily) so that your "if" and "otherwise" aren't italicized.
The ~ just adds a little space so that your "if" doesn't collide with the x > 0, which often happens when using mathrm
Alternatively, you can check out this post for other ways to write piecewise functions in LaTeX. I hope this helps.

Two statements next to curly brace in an equation

How can I write an equation with one curly brace ({), and on the right-hand side next to the curly, two statements in two different lines?
You can try the cases env in amsmath.
\documentclass{article}
\usepackage{amsmath}
\begin{document}
\begin{equation}
f(x)=\begin{cases}
1, & \text{if $x<0$}.\\
0, & \text{otherwise}.
\end{cases}
\end{equation}
\end{document}
That can be achieve in plain LaTeX without any specific package.
\documentclass{article}
\begin{document}
This is your only binary choices
\begin{math}
\left\{
\begin{array}{l}
0\\
1
\end{array}
\right.
\end{math}
\end{document}
This code produces something which looks what you seems to need.
The same example as in the #Tombart can be obtained with similar code.
\documentclass{article}
\begin{document}
\begin{math}
f(x)=\left\{
\begin{array}{ll}
1, & \mbox{if $x<0$}.\\
0, & \mbox{otherwise}.
\end{array}
\right.
\end{math}
\end{document}
This code produces very similar results.
Are you looking for
\begin{cases}
math text
\end{cases}
It wasn't very clear from the description. But may be this is what you are looking for http://en.wikipedia.org/wiki/Help:Displaying_a_formula#Continuation_and_cases
To answer also to the comment by #MLT, there is an alternative to the standard cases environment, not too sophisticated really, with both lines numbered. This code:
\documentclass{article}
\usepackage{amsmath}
\usepackage{cases}
\begin{document}
\begin{numcases}{f(x)=}
1, & if $x<0$\\
0, & otherwise
\end{numcases}
\end{document}
produces
Notice that here, math must be delimited by \(...\) or $...$, at least on the right of & in each line (reference).
Or this:
f(x)=\begin{cases}
0, & -\pi\leqslant x <0\\
\pi, & 0 \leqslant x \leqslant +\pi
\end{cases}
Here is a way to manually control the size of brace, if cases or \left\{ doesn't provide suitable brace size you want.
\begin{math}
\biggl\{
\begin{array}{l}
statement1\\
statement2
\end{array}
\end{math}
You can choose among \bigl\{ \Bigl\{ \biggl\{ \Biggl\{ to adjust brace size from the smallest to the largest.
Image of different braces
From left to right: cases \left\{ biggl\{ Bigl\{

How do I compile a LaTeX table exported from R?

I'm completely new to LaTeX and have the MacTeX 2009 package installed, with the intent of having tables from R output into LaTeX and formatted as PDF.
I get the following LaTeX code (below) when I run an example in R (it renders ok in R, but I´d like to use TeXshop). However, when I paste this into a TeXshop window, I get the following error:
./Untitled.tex:2: LaTeX Error: Environment table undefined
I´m sure there is something very basic I´m missing here.
% latex.default(cstats, title = title, caption = caption, rowlabel = rowlabel,
% col.just = col.just, numeric.dollar = FALSE, insert.bottom = legend,
% rowname = lab, dcolumn = dcolumn, extracolheads = extracolheads,
% extracolsize = Nsize, ...)
%
\begin{table}[!tbp]
\caption{Descriptive Statistics by treatment\label{f}}
\begin{center}
\begin{tabular}{lccc}\hline\hline
\multicolumn{1}{l}{}&\multicolumn{1}{c}{Drug}&\multicolumn{1}{c}{Placebo}&\multicolumn{1}{c}{Test Statistic}\tabularnewline
&\multicolumn{1}{c}{{\scriptsize $N=263$}}&\multicolumn{1}{c}{{\scriptsize $N=237$}}&\tabularnewline
\hline
age&{\scriptsize 46.5~}{49.9 }{\scriptsize 53.2} &{\scriptsize 46.7~}{50.0 }{\scriptsize 53.4} &$ F_{1,498}=0.1 ,~ P=0.754 ^{1} $\tabularnewline
sex~:~m&47\%~{\scriptsize~(123)}&44\%~{\scriptsize~(104)}&$ \chi^{2}_{1}=0.42 ,~ P=0.517 ^{2} $\tabularnewline
Primary~Symptoms~:~Depressed&0\%~{\scriptsize~(0)}&0\%~{\scriptsize~(0)}&$^{2}$\tabularnewline
~~~~Headache&0\%~{\scriptsize~(0)}&0\%~{\scriptsize~(0)}&$^{2}$\tabularnewline
~~~~Hangnail&0\%~{\scriptsize~(0)}&0\%~{\scriptsize~(0)}&$^{2}$\tabularnewline
~~~~Muscle~Ache&0\%~{\scriptsize~(0)}&0\%~{\scriptsize~(0)}&$^{2}$\tabularnewline
~~~~Stomach~Ache&0\%~{\scriptsize~(0)}&0\%~{\scriptsize~(0)}&$^{2}$\tabularnewline
\hline
\end{tabular}
\end{center}
\noindent
{\scriptsize $a$\ }{$b$\ }{\scriptsize $c$\ } represent the lower quartile $a$,
the median $b$, and the upper quartile $c$\ for continuous variables.\\
Numbers after percents are frequencies.\\\indent Tests used:\\
\textsuperscript{\normalfont 1}Wilcoxon test; \textsuperscript{\normalfont 2}Pearson test
\end{table}
You need some boilerplate around this to set up the document -- the LaTeXTemplate in TexShop should do.
Your \begin{table} is commented out with a preceding %
There are some missing backslashes in the legend text -- possibly these are just cut & paste artefacts?
This thrown-together version works for me, though you may need to tweak it for your purposes:
\documentclass[11pt]{article}
\begin{document}
\begin{table}[!tbp]
\caption{Descriptive Statistics by treatment\label{f}}
\begin{center}
\begin{tabular}{lccc}
\hline
\hline
\multicolumn{1}{l}{}&\multicolumn{1}{c}{Drug}&\multicolumn{1}{c}{Placebo}&\multicolumn{1}{c}{Test Statistic}
\tabularnewline
&\multicolumn{1}{c}{{\scriptsize $N=263$}}&\multicolumn{1}{c}{{\scriptsize $N=237$}}&
\tabularnewline
\hline
age&{\scriptsize 46.5~}{49.9 }{\scriptsize 53.2} &{\scriptsize 46.7~}{50.0 }{\scriptsize 53.4} &$ F_{1,498}=0.1 ,~ P=0.754 ^{1}$
\tabularnewline
sex~:~m&47\%~{\scriptsize~(123)}&44\%~{\scriptsize~(104)}&$ \chi^{2}_{1}=0.42 ,~ P=0.517 ^{2} $
\tabularnewline
Primary~Symptoms~:~Depressed&0\%~{\scriptsize~(0)}&0\%~{\scriptsize~(0)}&$^{2}$
\tabularnewline
~~~~Headache&0\%~{\scriptsize~(0)}&0\%~{\scriptsize~(0)}&$^{2}$
\tabularnewline
~~~~Hangnail&0\%~{\scriptsize~(0)}&0\%~{\scriptsize~(0)}&$^{2}$
\tabularnewline
~~~~Muscle~Ache&0\%~{\scriptsize~(0)}&0\%~{\scriptsize~(0)}&$^{2}$
\tabularnewline
~~~~Stomach~Ache&0\%~{\scriptsize~(0)}&0\%~{\scriptsize~(0)}&$^{2}$
\tabularnewline
\hline
\end{tabular}
\end{center}
\noindent {\scriptsize $a$\ }{$b$\ }{\scriptsize $c$\ } represent the lower quartile $a$, the median $b$, and the upper quartile $c$ for continuous variables.\\
Numbers after percents are frequencies.\\
\indent Tests used:\\
\textsuperscript{\normalfont 1}Wilcoxon test;
\textsuperscript{\normalfont 2}Pearson test
\end{table}
\end{document}
If you use the package stargazer (available on CRAN), you can use the argument out="filename.tex" to output the LaTeX code directly into a Tex document. That document should then be easy to compile.
It sounds like you may not have a document class defined, or you are not using a document class that defines the table environment.

Iteration in LaTeX

I would like to use some iteration control flow to simplify the following LaTeX code.
\begin{sidewaystable}
\caption{A glance of images}
\centering
\begin{tabular}{| c ||c| c| c |c| c|| c |c| c|c|c| }
\hline
\backslashbox{Theme}{Class} &\multicolumn{5}{|c|}{Class 0} & \multicolumn{5}{|c|}{Class 1} \\
\hline
\hline
1 &
\includegraphics[scale=2]{../../results/1/0_1.eps}
&\includegraphics[scale=2]{../../results/1/0_2.eps}
&\includegraphics[scale=2]{../../results/1/0_3.eps}
&\includegraphics[scale=2]{../../results/1/0_4.eps}
&\includegraphics[scale=2]{../../results/1/0_5.eps}
&\includegraphics[scale=2]{../../results/1/1_1.eps}
&\includegraphics[scale=2]{../../results/1/1_2.eps}
&\includegraphics[scale=2]{../../results/1/1_3.eps}
&\includegraphics[scale=2]{../../results/1/1_4.eps}
&\includegraphics[scale=2]{../../results/1/1_5.eps} \\
\hline
... % similarly for 2, 3, ..., 22
\hline
23 &
\includegraphics[scale=2]{../../results/23/0_1.eps}
&\includegraphics[scale=2]{../../results/23/0_2.eps}
&\includegraphics[scale=2]{../../results/23/0_3.eps}
&\includegraphics[scale=2]{../../results/23/0_4.eps}
&\includegraphics[scale=2]{../../results/23/0_5.eps}
&\includegraphics[scale=2]{../../results/23/1_1.eps}
&\includegraphics[scale=2]{../../results/23/1_2.eps}
&\includegraphics[scale=2]{../../results/23/1_3.eps}
&\includegraphics[scale=2]{../../results/23/1_4.eps}
&\includegraphics[scale=2]{../../results/23/1_5.eps} \\
\hline
\end{tabular}
\end{sidewaystable}
I learn that the forloop package provides the for loop. But I am not sure how to apply it to my case? Or other methods not by forloop?
If I also want to simply another similar case, where the only difference is that the directory does not run from 1, 2, to 23, but in some arbitrary order such as 3, 2, 6, 9,..., or even a list of strings such as dira, dirc, dird, dirb,.... How do I make the LaTeX code into loops then?
You may use pgffor package, a tool provided by pgf. The basic syntax is:
\foreach \n in {0,...,22}{do something}
Notably, this for loop is not restricted to integers, for example:
\foreach \n in {apples,burgers,cake}{Let's eat \n.\par}
Something like this will take care of the body of your tabular:
\newcounter{themenumber}
\newcounter{classnumber}
\newcounter{imagenumber}
\forloop{themenumber}{1}{\value{themenumber} < 24}{
% \hline <-- Error here
\arabic{themenumber}
\forloop{classnumber}{0}{\value{classnumber} < 2}{
\forloop{imagenumber}{1}{\value{imagenumber} < 6}{
& \includegraphics[scale=2]{
../../results/\arabic{themenumber}/\arabic{classnumber}_\arabic{imagenumber}.eps
}
}
}
\\
\hline
}
I had to comment out the first \hline because it gave me an error:
You can't use `\hrule' here except with leaders.
I'm not sure what that means; if you really cannot live without the double line, I can look into it more.
Also note that you have to use <; for example, <= 24 will not work.
As to your update: I would simply declare a command that takes the argument that you're looping over. Something like this:
\newcommand\fordir[1]{do something complex involving directory named #1}
\fordir{dira}
\fordir{dirb}
\fordir{dirc}
\dots

How can I have linebreaks in my long LaTeX equations?

My equation is very long. How do I get it to continue on the next line rather than go off the page?
If your equation does not fit on a single line, then the multline (note that that's multline without an "i", not "multiline") environment probably is what you need:
\begin{multline}
first part of the equation \\
= second part of the equation
\end{multline}
If you also need some alignment respect to the first part, you can use split:
\begin{equation}
\begin{split}
first part &= second part #1 \\
&= second part #2
\end{split}
\end{equation}
Both environments require the amsmath package.
See also aligned as pointed out in an answer below.
Not yet mentioned here, another choice is environment aligned, again from package amsmath:
\documentclass{article}
\usepackage{amsmath}
\begin{document}
\begin{equation}
\begin{aligned}
A & = B + C\\
& = D + E + F\\
& = G
\end{aligned}
\end{equation}
\end{document}
This outputs:
Without configuring your math environment to clip, you could force a new line with two backslashes in a sequence like this:
Bla Bla \\ Bla Bla in another line
The problem with this is that you will need to determine where a line is likely to end and force to always have a line break there. With equations, rather than text, I prefer this manual way.
You could also use \\* to prevent a new page from being started.
If it is inline equation, then use \allowbreak. Use it like:
$x_1,x_2,x_3,\allowbreak x_4,x_5$.
Latex will break equation in this place only if necessary.
There are a couple ways you can deal with this. First, and perhaps best, is to rework your equation so that it is not so long; it is likely unreadable if it is that long.
If it must be so, check out the AMS Short Math Guide for some ways to handle it. (on the second page)
Personally, I'd use an align environment, so that the breaking and alignment can be precisely controlled. e.g.
\begin{align*}
x&+y+\dots+\dots+x_100000000\\
&+x_100000001+\dots+\dots
\end{align*}
which would line up the first plus signs of each line... but obviously, you can set the alignments wherever you like.
I think I usually used eqnarray or something. It lets you say
\begin{eqnarray*}
x &=& blah blah blah \\
& & more blah blah blah \\
& & even more blah blah
\end{eqnarray*}
and it will be aligned by the & &... As pkaeding mentioned, it's hard to read, but when you've got an equation thats that long, it's gonna be hard to read no matter what... (The * makes it not have an equation number, IIRC)
I used the \begin{matrix}
\begin{equation}
\begin{matrix}
line_1 \\
line_2 \\
line_3
\end{matrix}
\end{equation}
multline is best to use. Instead, you can use dmath, split as well.
Here is an example:
\begin{multline}
{\text {\bf \emph {T(u)}}} ={ \alpha *}{\frac{\sum_{i=1}^{\text{\bf \emph {I(u)}}}{{\text{\bf \emph {S(u,i)}}}* {\text {\bf \emph {Cr(P(u,i))}}} * {\text {\bf \emph {TF(u,i)}}}}}{\text {\bf \emph {I(u)}}}} \\
+{ \beta *}{\frac{\sum_{i=1}^{\text{\bf \emph {$I_h$(u)}}}{{\text{\bf \emph {S(u,i)}}}* {\text {\bf \emph {Cr(P(u,i))}}} * {\text {\bf \emph {TF(u,i)}}}}}{\text {\bf \emph {$I_h$(u)}}}}
\end{multline}
This worked for me while using mathtools package.
\documentclass{article}
\usepackage{mathtools}
\begin{document}
\begin{equation}
\begin{multlined}
first term \\
second term
\end{multlined}
\end{equation}
\end{document}
Use eqnarray and \nonumber
example:
\begin{eqnarray}
sample = R(s,\pi(s),s') + \gamma V^{\pi} (s') \nonumber \\
\label{eq:temporal-difference}
V^{\pi}_{k+1}(s) = (1-\alpha)V^{\pi}(s) - \alpha[sample]
\end{eqnarray}
SIMPLE ANSWER HERE
\begin{equation}
\begin{split}
equation \\
here
\end{split}
\end{equation}
To solve this issue, I used the array environment inside the equation environment like this:
\begin{equation}
\begin{array}{r c l}
first Term&=&Second Term\\
&=&Third Term
\end{array}
\end{equation}
You do not need any extra package to do this:
\begin{equation}
\begin{gathered}
first formula\\
second formula
\end{gathered}
\end{equation}

Resources