Equation error in Latex table - latex

I'd like to create a table with two parameters and the corresponding equation.
Somehow Latex gives me the error:
'! Missing $ inserted.'
and I don't know what that means...
\begin{table}[H]
\centering
\caption{XXX}
\label{tab:XXX}
\begin{adjustbox}{max width=\textwidth}
\begin{tabular}{c c}
\hline\hline
\vspace{0.25cm}
Parameter & Mathematical Equation \\ \hline
\vspace{0.25cm}
Standard Deviation & \sigma=\sqrt{\frac{\sum(X-\mu)$^2$}{N}} \\
\vspace{0.25cm}
Variance & \sigma^2=\frac{\sum(X-\mu)$^2$}}{N} \\
\\ \hline
\end{tabular}
\end{adjustbox}
\end{table}

In TeX, $ signs enclose inline math code (see here). Your example already uses it around ^2. However, you also use other macros that are only valid inside a math environment, such as e.g. \sigma. Hence, you should surround all macros that require math mode with $ signs:
\begin{table}[H]
\centering
\caption{XXX}
\label{tab:XXX}
\begin{adjustbox}{max width=\textwidth}
\begin{tabular}{c c}
\hline\hline
\vspace{0.25cm}
Parameter & Mathematical Equation \\ \hline
\vspace{0.25cm}
Standard Deviation & $\sigma=\sqrt{\frac{\sum(X-\mu)^2}{N}}$ \\
\vspace{0.25cm}
Variance & $\sigma^2=\frac{\sum(X-\mu)^2}{N}$ \\
\\ \hline
\end{tabular}
\end{adjustbox}
\end{table}
If you prefer not to mix TeX and LaTeX commands, consider using \(...\) instead of $...$.

Related

Overfull \hbox and Package array Errors. Latex tabular

I am getting trouble to fit my table - tabular to the paragraph width.
This is the error:
Overfull \hbox (341.06795pt too wide) in paragraph at lines 391--411
Package array Error: Illegal pream-token (M): `c' used.
Package array Error: Illegal pream-token (4cm): `c' used
I have been trying one thousand things and nothing seems to work.
I am using this template from Overleaf, where you can find the document class.
This is my code.
\documentclass{JASSS}
\usepackage{amsmath,systeme}
\usepackage{tabularx,colortbl}
\usepackage{booktabs}
\title{XXXXX}
\reviewcopy{true}
\author[1]{}
\affil[1]{}
\email{XX}
\usepackage{natbib}
\setcitestyle{authoryear,round,aysep={}}
\begin{document}
\maketitle
\begin{abstract}
Bla bla
\end{abstract}
\section{xxxx}
\begin{table}[h!]
\centering
\begin{tabular}{ |M{4cm}||M{4cm}|M{4cm}|M{4cm}|}
% \begin{tabular}{|c|c|c|c|}
\cline{2-4}
\multicolumn{1}{c|}{} & \multicolumn{3}{c|}{Initial value parameter combinations ($t_0=1932$)} \\
\hline
Scenarios from 2020 & $\phi_{B}=\phi_{C}=0.05$ and 1932 electoral results & $\phi_{B}=0.05, \phi_{C}=0.055$ and 1932 electoral results & $\phi_{B}=\phi_{C}=0.05$ and electoral tie \\
\hline
$\phi_{D}=\phi_{E}=0.01$, $\gamma_{B}=\gamma_{C}=\gamma_{D}=\gamma_{E}=0.01$ & S0000 & S0100 & S1000 \\
\hline
\rowcolor{blue!20}
$\phi_{D}=0.015,\phi_{E}=0.01$, $\gamma_{B}=\gamma_{C}=\gamma_{D}=\gamma_{E}=0.01$ & S0001 & S0101 & S1001 \\
\hline
\rowcolor{red!20}
$\phi_{D}=0.01,\phi_{E}=0.015$, $\gamma_{B}=\gamma_{C}=\gamma_{D}=\gamma_{E}=0.01$ & S0010 & S0110 & S1010 \\
\hline
\rowcolor{yellow!20}
$\phi_{D}=0.01,\phi_{E}=0.01$, $\gamma_{B}=\gamma_{C}=\gamma_{D}=\gamma_{E}=0.015$ & S0011 & S0111 & S1011 \\
\hline
\end{tabular}
\bibliographystyle{jasss}
\bibliography{references}
\end{document}
The M column type is not defined by default. If you want to use it, you have to somehow define it. I don't know what you indent it to do, but here some dummy code:
\documentclass{JASSS}
\usepackage{array}
\usepackage{amsmath,systeme}
\usepackage{tabularx,colortbl}
\usepackage{booktabs}
\newcolumntype{M}[1]{p{#1}}
\begin{document}
\begin{table}[h!]
\centering
\begin{tabular}{ |M{4cm}||M{4cm}|M{4cm}|M{4cm}|}
% \begin{tabular}{|c|c|c|c|}
\cline{2-4}
\multicolumn{1}{c|}{} & \multicolumn{3}{c|}{Initial value parameter combinations ($t_0=1932$)} \\
\hline
Scenarios from 2020 & $\phi_{B}=\phi_{C}=0.05$ and 1932 electoral results & $\phi_{B}=0.05, \phi_{C}=0.055$ and 1932 electoral results & $\phi_{B}=\phi_{C}=0.05$ and electoral tie \\
\hline
$\phi_{D}=\phi_{E}=0.01$, $\gamma_{B}=\gamma_{C}=\gamma_{D}=\gamma_{E}=0.01$ & S0000 & S0100 & S1000 \\
\hline
\rowcolor{blue!20}
$\phi_{D}=0.015,\phi_{E}=0.01$, $\gamma_{B}=\gamma_{C}=\gamma_{D}=\gamma_{E}=0.01$ & S0001 & S0101 & S1001 \\
\hline
\rowcolor{red!20}
$\phi_{D}=0.01,\phi_{E}=0.015$, $\gamma_{B}=\gamma_{C}=\gamma_{D}=\gamma_{E}=0.01$ & S0010 & S0110 & S1010 \\
\hline
\rowcolor{yellow!20}
$\phi_{D}=0.01,\phi_{E}=0.01$, $\gamma_{B}=\gamma_{C}=\gamma_{D}=\gamma_{E}=0.015$ & S0011 & S0111 & S1011 \\
\hline
\end{tabular}
\end{table}
\end{document}

Stata - Esttab to Latex Decimal Alignment

I am trying to output a LaTex file using Stata's esttab command. I cannot get the decimals to align, nor can I get the nice formatting from the "booktabs" option to work. I also would like to increase the width of the columns. I have been tinkering with this for hours and have had no luck. Below is my Stata code and the LaTex code that I am running through Overleaf. Any help would be greatly appreciated!
Here is my Stata code:
#delimit ;
esttab r1 r2 r3 r4 using "C:\Users\user\Dropbox\Private
Code\Code\STATA\latex\table1.tex", replace
b(3) nomtitle label star(* 0.10 ** 0.05 *** 0.01)
booktabs alignment(D{.}{.}{-1})
title(Placeholder)
s(N DAY FFE FIRM_QUARTER r2_a,
fmt(%9.0fc 0 0 0 3)
label("Observations" "Day FE" "Firm FE" "Firm-Quarter FE" "Adj. R-Sq{\super 2}"))
compress nocons;
Here is the LaTex code it spits out:
\begin{table}[htbp]\centering
\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}
\caption{Placeholder}
\begin{tabular}{l*{4}{D{.}{.}{-1}}}
\toprule
&\multicolumn{1}{c}{(1)} &\multicolumn{1}{c}{(2)} &\multicolumn{1}
{c}{(3)} &\multicolumn{1}{c}{(4)} \\
\midrule
VAR1 & 0.007\sym{***}& 0.007\sym{***}& 0.006\sym{***}& 0.007\sym{***}\\
& (8.55) & (7.82) & (7.67) & (8.54) \\
\addlinespace
VAR2 & 0.132\sym{***}& 0.131\sym{***}& 0.131\sym{***}& 0.132\sym{***}\\
& (20.35) & (20.15) & (20.15) & (20.35) \\
\midrule
Observations &1,315,077 &1,315,077 &1,315,077 &1,315,077
\\
Day FE & No & Yes & Yes & No \\
Firm FE & Yes & No & Yes & No \\
Firm-Quarter FE & No & No & No & Yes \\
Adj. R-Sq{\super 2}& 0.045 & 0.027 & 0.046 & 0.044
\\
\bottomrule
\multicolumn{5}{l}{\footnotesize \textit{t} statistics in parentheses}\\
\multicolumn{5}{l}{\footnotesize \sym{*} \(p<0.10\), \sym{**} \(p<0.05\), \sym{***} \(
p<0.01\)}\\
\end{tabular}
\end{table}
Since you haven't provided the full code, I can't see what would your output be. When I add \documentclass{article} with missing packages in a preamble, I get very wide table which does not fit a page. Also, I don't know whether the code is automatically generated or created/altered by you.
Assuming the latter, I would do the following. Since you add footnotes in the table, I would enclose tabular within threeparttable, which splits the area for table into three parts: top for captions, middle for a table definition e.g. tabular, and bottom part for footnotes. It also adds a convenient macro \tnote{}.
Secondly, dcolumn causes big numbers to be pushed way too much to the left. You should let dcolumn suppress their alignment. I personally don't know how, although I assume there is a way.
I prefer siunitx for formatting numbers in tables. However siunitx needs a bit more work. It automatically parses numbers in cells and any non-number content generates errors. Therefore, non-numbers need to be enclosed within {...} which instructs siunitx to leave the content intact. It also forces numbers not to be formatted, aligned etc., which is what I did for the big numbers you have in the table. There's also option to format thousands and millions with any separator of your choice. For instance you could use one of the macros: \,, :, or \; for thin, mid or thick space, respectively instead of ,.
Below is my suggestion for your table based on siunitx and threeparttable, so you can compare it with your approach.
\documentclass{article}
\usepackage{array}
\usepackage{booktabs}
\usepackage{siunitx}
\usepackage{threeparttable}
\newcommand\super[1]{\textsuperscript{#1}}
\sisetup{
group-separator={,},
table-format=1.3,
table-align-text-after = false,
}
\begin{document}
\begin{table}[htbp]
\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}
\centering
\begin{threeparttable}
\caption{Placeholder}
\begin{tabular}{l *{4}{S}}
\toprule
& \multicolumn{1}{c}{(1)}
& \multicolumn{1}{c}{(2)}
& \multicolumn{1}{c}{(3)}
& \multicolumn{1}{c}{(4)} \\
\midrule
{VAR1} & 0.007\tnote{***} & 0.007\tnote{***} & 0.006\tnote{***} & 0.007\tnote{***} \\
& {(8.55)} & {(7.82)} & {(7.67)} & {(8.54)} \\
\addlinespace
VAR2 & 0.132\tnote{***} & 0.131\tnote{***} & 0.131\tnote{***} & 0.132\tnote{***} \\
& {(20.35)} & {(20.15)} & {(20.15)} & {(20.35)} \\
\midrule
Observations
& \multicolumn{1}{c}{\num{1315077}}
& \multicolumn{1}{c}{\num{1315077}}
& \multicolumn{1}{c}{\num{1315077}}
& \multicolumn{1}{c}{\num{1315077}} \\
Day FE & {No} & {Yes} & {Yes} & {No} \\
Firm FE & {Yes} & {No} & {Yes} & {No} \\
Firm-Quarter FE & {No} & {No} & {No} & {Yes} \\
Adj. R-Sq{\super2} & 0.045 & 0.027 & 0.046 & 0.044 \\
\bottomrule
\end{tabular}
\par\(t\) statistics in parentheses
\par\tnote{*} \(p < 0.10\),\quad\tnote{**} \(p < 0.05\),\quad\tnote{***} \(p < 0.01\)
\end{threeparttable}
\end{table}
\end{document}
EDIT. I also enclosed statistics in{...} because siunitx does not recognised brackets as part of a number.

Minipage in Latex failing to compile

I want to put a table and figure above/below one another in my article tex file as they are related. Until now it has worked in this document but suddenly it isn't working and I can't compile in Overleaf. Please help.
\begin{document}
\begin{minipage}[b]{1\textwidth}
\centering
\captionof{table}{Best Case Scenario A IC values when synthesis, desiccation (and algae collection) have been excluded from the system.}
\label{tab:BestA_synthANDdesiccation_excluded}
\begin{tabular}{#{}llcc#{}}
\toprule
IC & Unit & Total (no collection) & Total (with collection) \\ \midrule
CC & kg CO2 eq & -1.1E+03 & -7.0E+02 \\
Ecotox & CTUe & 2.1E+02 & 1.4E+04 \\
HTnc & CTUh & 6.9E-08 & 1.4E-05 \\
PM & disease inc. & 3.7E-07 & 1.0E-05 \\
RUf & MJ & 9.3E+01 & 5.6E+03 \\
RUm & kg Sb eq & 8.0E-06 & 3.6E-02 \\ \bottomrule
\end{tabular}
\end{minipage}
\begin{minipage}
\includegraphics[width=1\textwidth]{Pictures/A_best_nosynANDnodesiccationANDnocollection.png}
\captionof{figure}{Characterisation of the sub-systems in best-case scenario A when synthesis, desiccation and algae collection have been excluded from the system boundaries. Method: EF 3.0 Method (adapted) V1.00}
\label{fig:Abest_nosyn_nodesiccation}
\end{minipage}
\end{document}
The main problem is that the minipage has an mandatory argument to specify the width, so you'll need
\begin{minipage}{\linewidth}
Some other comments:
best not specify a file type for your image \includegraphics[width=1\textwidth]{Pictures/A_best_nosynANDnodesiccationANDnocollection}. If you have the same image in different formats latex will then include the one best suited for the engine you use to compile your document
Unless your minipage is in a float environment like figure or table you want to add \noindent before the minipage. Without this there is not enough space in your line to fit a minipage with \textwidth
if you want to have the numbers in your table nicely aligned, have a look at the siunitx package
\documentclass{article}
\usepackage{caption}
\usepackage{booktabs}
\usepackage{graphicx}
\begin{document}
\noindent%
\begin{minipage}[b]{1\textwidth}
\centering
\captionof{table}{Best Case Scenario A IC values when synthesis, desiccation (and algae collection) have been excluded from the system.}
\label{tab:BestA_synthANDdesiccation_excluded}
\begin{tabular}{#{}llcc#{}}
\toprule
IC & Unit & Total (no collection) & Total (with collection) \\ \midrule
CC & kg CO2 eq & -1.1E+03 & -7.0E+02 \\
Ecotox & CTUe & 2.1E+02 & 1.4E+04 \\
HTnc & CTUh & 6.9E-08 & 1.4E-05 \\
PM & disease inc. & 3.7E-07 & 1.0E-05 \\
RUf & MJ & 9.3E+01 & 5.6E+03 \\
RUm & kg Sb eq & 8.0E-06 & 3.6E-02 \\ \bottomrule
\end{tabular}
\end{minipage}
\begin{minipage}{\linewidth}
\includegraphics[width=1\textwidth]{example-image-duck}
\captionof{figure}{Characterisation of the sub-systems in best-case scenario A when synthesis, desiccation and algae collection have been excluded from the system boundaries. Method: EF 3.0 Method (adapted) V1.00}
\label{fig:Abest_nosyn_nodesiccation}
\end{minipage}
\end{document}

Table title with outreg2

I am working with the community-contributed command outreg2 to export regression estimates from a logit model to LaTeX.
Is there a way to show the table reference/label, for example, "Table 1: Some Title"?
I cannot seem to be able to do this using outreg2:
logit v1 v2 v43, or
outreg2 using "table", label cti(odds ratio) eform tex replace ctitle(Logit)
With esttab, using \label in the title option to get the desired result works:
esttab using "Table.tex",tex eform label replace title("Logit"\label{tab1})
Any suggestions?
EDIT:
Note that even when i do the following:
outreg2 using "table", label cti(odds ratio) eform tex replace ///
ctitle(Logit) title("Logit"\label{tab1})
This only produces the following:
Consider the following toy example:
webuse lbw
logit low age lwt i.race smoke ptl ht ui, or
Both esttab and outreg2 produce the same result when their title() option is used:
esttab, tex eform label replace title("Logit"\label{tab1})
outreg2 using "table", tex label cti(odds ratio) eform replace title("Logit"\label{tab1})
However, this will not work when typesetting as outreg2 produces
different markup than esttab.
The way to do what you want with outreg2 is to run the following from Stata:
outreg2 using table, tex replace label cti(odds ratio) eform
Unlike esttab, the outreg2 command does not have an option to insert markup
directly from within Stata. Thus you will have to add the markup below manually:
.
.
.
\begin{table}[htbp]\centering
\caption{Logit \label{tab1}}
.
.
.
\end{table}
.
.
.
The produced tex file should then look as follows:
\documentclass[]{article}
\setlength{\pdfpagewidth}{8.5in} \setlength{\pdfpageheight}{11in}
\begin{document}
\begin{table}[htbp]\centering
\caption{Logit \label{tab1}}
\begin{tabular}{lc} \hline
& (1) \\
VARIABLES & odds ratio \\ \hline
& \\
birthweight<2500g & \\
& \\
age of mother & 0.973 \\
& (0.0355) \\
weight at last menstrual period & 0.985** \\
& (0.00682) \\
race = 2, black & 3.535** \\
& (1.861) \\
race = 3, other & 2.368** \\
& (1.040) \\
smoked during pregnancy & 2.518** \\
& (1.009) \\
premature labor history (count) & 1.719 \\
& (0.595) \\
has history of hypertension & 6.250*** \\
& (4.322) \\
presence, uterine irritability & 2.135* \\
& (0.981) \\
Constant & 1.586 \\
& (1.910) \\
& \\
Observations & 189 \\ \hline
\multicolumn{2}{c}{ seEform in parentheses} \\
\multicolumn{2}{c}{ *** p$<$0.01, ** p$<$0.05, * p$<$0.1} \\
\end{tabular}
\end{table}
\end{document}
Leaving this here for future reference.
Actually, if you add the option tex(fragment) to outreg2, the resulting file will only contain the tabular environment. Then, you can create the table environment, with the caption that you desire, and use \input only to replace the tabular environment.

\table in \enumerate environment in latex

I want to insert a \table in \enumerate environment and I have something like this:
\begin{enumerate}
\item Item 1.
\item Item 2.
\item \begin{table}[htbp]
\textbf{\caption{Table1 Caption}}
\centering
\begin{tabular}{c c}
\hline\hline
Value 1 & Value 2\\
\hline
r1c1 & r2c2\\
r2c1 & r2c2\\
r3c1 & r3c2\\
\hline
\end{tabular}
\label{table1}
\end{table}
\item \begin{table}[htbp]
\textbf{\caption{Table 2 Caption}}
\centering
\begin{tabular}{ccc}
\hline\hline
Value 1 & Value 2 & Value 3\\
\hline
r1c1 & r1c2 & r1c3\\
r2c1 & r2c2 & r2c3\\
r3c1 & r3c2 & r3c3\\
\end{tabular}
\label{table2}
\end{table}
\item \ref{table1} and \ref{table2}
\end{enumerate}
But when I compile the latex document the \enumerate number are no where near the table. Also when I refer to label "table1" and "table2" it shows up as 3 and 4 respectively (for extra info this part is in subsection 3.3 and these are the only two tables in the whole document).
How do I use \table environment with \enumerate environment. I have seen people use just \tabular with \enumerate but I want to use \table as it gives me an easy option to define \caption and \label.
Also regarding table labeling I am assuming it has something to do with subsection number but I can't really figure it out.
I would really appreciate any help regarding this matter.
A table environment is a float. It is not meant to be included in other environments.
You can use a tabular environment like this:
\documentclass[a4paper, 11pt]{article}
\begin{document}
\begin{enumerate}
\item Item 1.
\item Item 2.
\item \begin{tabular}[t]{c c}
Value 1 & Value 2\\
\hline
r1c1 & r2c2\\
r2c1 & r2c2\\
r3c1 & r3c2\\
\hline
\end{tabular}
\item \begin{tabular}[t]{ccc}
Value 1 & Value 2 & Value 3\\
\hline
r1c1 & r1c2 & r1c3\\
r2c1 & r2c2 & r2c3\\
r3c1 & r3c2 & r3c3\\
\end{tabular}
\end{enumerate}
\end{document}
The output will look like this:
Note that the vertical alignment will look weird if you try tp put \hline on top of the table.
Edit 1: You can put a \label almost everywhere you like, but it will not refer to itself as a "Table" unless a variable is set. You'd have to delve into the bowels of LaTeX to see how that is done.
There are at least two packages (capt-of and caption) that provide a \captionof command to define a caption outside a figure or table environment.

Resources