Why is LaTeX missing $ when I have no mathematical equation and how do I center the table?

The main issue is that the \pm macro must occur in math mode. To minimize the number of $ symbols needed to enter and exit math mode, I suggest you employ versions of the l, r, and c column types which are in math mode automatically. See the code below for a way to do just that.

In addition, I would restructure the table to provided separate columns for the counts and percentages.

And, don't use \begin{center} and \end{center} in a table environment. Instead, use a single \centering instruction.

Finally, I would omit most horizontal lines or, at the very least, replace them with a bit of vertical whitespace. And, do load the booktabs package and use \toprule, \midrule, and \bottomrule for the three remaining horizontal lines.

enter image description here

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}

\usepackage{multirow}
\usepackage{array,booktabs} % <-- new

% automatic math-mode column types:
\newcolumntype{L}{>{$}l<{$}}  % left-aligned
\newcolumntype{C}{>{$}c<{$}}  % centered
\newcolumntype{R}{>{$}r<{$}}  % right-aligned

% two handy shortcut macros:
\newcommand\mcC[1]{\multicolumn{2}{C}{#1}}
\newcommand\mcR[1]{\multicolumn{2}{R}{#1}}

\begin{document}

\begin{table}[!ptbh]
\caption{An informative table}
\smallskip

\centering
\begin{tabular}{@{}l *{2}{R @{\hspace{1mm}} R} R @{}}
\toprule
&\mcC{$Female$} & \mcC{$Male$}  & \\ 
&\mcC{(N=287)}  & \mcC{(N=570)} & \multirow{-2}{*}{$p$}\\ 
\midrule
Age          & \mcR{68.7 \pm 10.7} & \mcR{60.6 \pm 11.2} & <0.001\\ 
 \addlinespace
CardiogenicShock   &&&&& 0.136\\ 
 \hspace{5mm} No       &275 & (95.8\%) &530 & (93.0\%) &\\ 
 \hspace{5mm} Yes      & 12 & ( 4.2\%) & 40 &  (7.0\%) &\\ 
 \addlinespace
Entry        &&&&&0.035\\ 
 \hspace{5mm} Femoral  &119 & (41.5\%) &193 & (33.9\%) &\\ 
 \hspace{5mm} Radial   &168 & (58.5\%) &377 & (66.1\%) &\\
 \addlinespace
DX           &&&&&0.012\\ 
 \hspace{5mm} NSTEMI   &50 & (17.4\%) &103 & (18.1\%) &\\ 
 \hspace{5mm} STEMI    &84 & (29.3\%) &220 & (38.6\%) &\\ 
 \hspace{5mm} Unstable Angina&153 & (53.3\%) &247 & (43.3\%) &\\ 
 \addlinespace
EF           & \mcR{56.3 \pm 10.1} & \mcR{55.6 \pm  9.4} &0.387\\ 
 \addlinespace
Height       & \mcR{153.8\pm  6.2} & \mcR{167.9\pm  6.1}&< 0.001\\
 \addlinespace
Weight       & \mcR{57.2 \pm  9.3} & \mcR{68.7 \pm 10.3} &< 0.001\\ 
 \addlinespace
BMI          & \mcR{24.2 \pm  3.6} & \mcR{24.3 \pm  3.2} &0.611\\ 
 \addlinespace
Obesity      &&&&&0.580\\ 
 \hspace{5mm} No       &194 & (67.6\%) &373 & (65.4\%) &\\ 
 \hspace{5mm} Yes      & 93 & (32.4\%) &197 & (34.6\%) &\\ 
 \addlinespace
TC           & \mcR{188.9\pm 51.1} & \mcR{183.3\pm 45.9} &0.124\\ 
 \addlinespace
LDLC         & \mcR{117.8\pm 41.2} & \mcR{116.0\pm 41.1} &0.561\\ 
 \addlinespace
HDLC         & \mcR{39.0 \pm 11.5} & \mcR{37.8 \pm 10.9} &0.145\\
 \addlinespace
TG           & \mcR{119.9\pm 76.2} & \mcR{127.9\pm 97.3} &0.195\\ 
 \addlinespace
DM           &&&&&0.077\\ 
 \hspace{5mm} No        &173 & (60.3\%) &380 & (66.7\%) &\\ 
 \hspace{5mm} Yes       &114 & (39.7\%) &190 & (33.3\%) &\\ 
 \addlinespace
HBP          &&&&&< 0.001\\ 
 \hspace{5mm} No        & 83 & (28.9\%) &273 & (47.9\%) &\\ 
 \hspace{5mm} Yes       &204 & (71.1\%) &297 & (52.1\%) &\\ 
 \addlinespace
Smoking      &&&&&< 0.001\\ 
 \hspace{5mm} Ex-smoker & 49 & (17.1\%) &155 & (27.2\%) &\\ 
 \hspace{5mm} Never     &209 & (72.8\%) &123 & (21.6\%) &\\ 
 \hspace{5mm} Smoker    & 29 & (10.1\%) &292 & (51.2\%) &\\ 
\bottomrule
\end{tabular}
\end{table}
\end{document}

\pm is a math command so the first error you get is about that and recovers by adding $ so your input is equivalent to $\pm then you get a second error at the end of the cell that a second $ to close math mode is missing. use $68.7 \pm 10.7$ and similarly use math for all the numeric entries whether or not they have \pm so they are all set in the same font.


One solution is to use the command \ensuremath. This command will put the argument into maths mode if it was not already in maths mode. It will also correctly put it back to normal text mode. If you are in math mode already, TeX leaves the argument in math mode.

You could use a macro definition to put something into maths mode if you were not already.

For your use (\pm in either math or text mode) you could define a macro \ppmm say:

  \newcommand{\ppmm}{\ensuremath{\pm}}

You could then use \ppmm in text mode or $\ppmm$ math mode without syntax errors.

Macros with \ensuremath are useful in lots of situations like this. For instance to describe a 12-bit ADC in TeX, I use $\mathcal{ADC}_{12}$ but sometimes I might want to use this when already in maths mode. By making the macro employ an \ensuremath wrapper, it can be safely used in both modes:

  \newcommand{\adctw}{\ensuremath{\mathcal{ADC}_{12}}

Then in the document you can call the macro whether in math mode, ${\adctw}$, or text mode, \adctw, and it works without syntax errors.

Tags:

Tables