13

I am new to LaTeX. However, I don't know why this code is not working:

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

\usepackage[english]{babel}

\usepackage[usenames, dvipsnames]{color}
\usepackage{multirow}

\title{r}
\author{omar113057 }
\date{September 2018}

\begin{document}

\maketitle

\section{Introduction}

\definecolor{black}{rgb}{0.0, 0.0, 0.0}
\begin{table}[!hbtp]
\begin{center}
\begin{normalsize}
\color{black}
\begin{tabular}{llcrr}
\hline
&Female&Male&\\
&(N=287)&(N=570)&\multirow{-2}{*}{p}\\
\hline
age                & 68.7 \pm 10.7 & 60.6 \pm 11.2 & < 0.001\\
\hline
cardiogenicShock   &&& 0.136\\
 \hspace{0.5cm} No             &275 (95.8\%)&530 (93.0\%)&\\
 \hspace{0.5cm} Yes            &12 ( 4.2\%)&40 ( 7.0\%)&\\
 \hline
entry              &&&0.035\\
 \hspace{0.5cm} Femoral        &119 (41.5\%)&193 (33.9\%)&\\
 \hspace{0.5cm} Radial         &168 (58.5\%)&377 (66.1\%)&\\
 \hline
Dx                 &&&0.012\\
 \hspace{0.5cm} NSTEMI         &50 (17.4\%)&103 (18.1\%)&\\
 \hspace{0.5cm} STEMI          &84 (29.3\%)&220 (38.6\%)&\\
 \hspace{0.5cm} Unstable Angina&153 (53.3\%)&247 (43.3\%)&\\
 \hline
EF                 &56.3 \pm 10.1&55.6 \pm  9.4&0.387\\
\hline
height             &153.8 \pm  6.2&167.9 \pm  6.1&< 0.001\\
\hline
weight             &57.2 \pm  9.3&68.7 \pm 10.3&< 0.001\\
\hline
BMI                &24.2 \pm  3.6&24.3 \pm  3.2&0.611\\
\hline
obesity            &&&0.580\\
\hline
 \hspace{0.5cm} No             &194 (67.6\%)&373 (65.4\%)&\\
     \hline
 \hspace{0.5cm} Yes            &93 (32.4\%)&197 (34.6\%)&\\
TC                 &188.9 \pm 51.1&183.3 \pm 45.9&0.124\\
\hline
LDLC               &117.8 \pm 41.2&116.0 \pm 41.1&0.561\\
\hline
HDLC               &39.0 \pm 11.5&37.8 \pm 10.9&0.145\\
\hline
TG                 &119.9 \pm 76.2&127.9 \pm 97.3&0.195\\
\hline
DM                 &&&0.077\\
 \hspace{0.5cm} No             &173 (60.3\%)&380 (66.7\%)&\\
 \hspace{0.5cm} Yes            &114 (39.7\%)&190 (33.3\%)&\\
HBP                &&&< 0.001\\
 \hspace{0.5cm} No             &83 (28.9\%)&273 (47.9\%)&\\
 \hspace{0.5cm} Yes            &204 (71.1\%)&297 (52.1\%)&\\
 \hline
smoking            &&&< 0.001\\
 \hspace{0.5cm} Ex-smoker      &49 (17.1\%)&155 (27.2\%)&\\
 \hspace{0.5cm} Never          &209 (72.8\%)&123 (21.6\%)&\\
 \hspace{0.5cm} Smoker         &29 (10.1\%)&292 (51.2\%)&\\
\hline
\end{tabular}
\end{normalsize}
\end{center}
\end{table}
\color{black}
\end{document}

This code is generated from an R package called ztable. I fixed a lot of errors in this code however, and this $ sign that the log keeps throwing is very strange to me as I don't see any pattern here.

Also I want to make a pretty centered and well spaced table. How can I achieve that?

  • 9
    Hi, welcome. As a first step, note that \pm works only in math mode. – Torbjørn T. Sep 30 '18 at 9:46
  • I actually have replaced +- symbol that was generated by ztable package as it was not rendered at any point. @TorbjørnT. – Omar113 Sep 30 '18 at 10:17
23

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}
  • 1
    thank you. one last enquiry. i got this symbol (±) initially before I replaced it with \pm. I was not rendered and showed error. Do you know how to render it without adding \pm? @DavidCarlisle – Omar113 Sep 30 '18 at 10:31
  • 5
    @Omar113 - I take it that you're using pdfLaTeX. To typeset the Unicode symbol ± directly, i.e., without first changing it over to \pm, you need to use a unicode-aware typesetting engine (such as XeLaTeX and LuaLaTeX) as well as a font that includes this symbol. – Mico Sep 30 '18 at 10:34
18

\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.

3

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.

  • 1
    good point. edited my post – Robin Oct 1 '18 at 10:50
  • I've taken the liberty of adding backticks around TeX and LaTeX expressions, to them stand out as computer code. Feel free to revert. – Mico Oct 1 '18 at 11:14
  • 1
    did not know this site did that! makes it look better thanks. – Robin Oct 1 '18 at 11:16
  • 2
    A related approach would be to \let\oldpm=\pm and then \renewcommand{\pm}{\ensuremath{\oldpm}}. Then the table would work as it stands. And if you did this within the tabular, scoping would prevent \pm from being redefined elsewhere. – Teepeemm Oct 1 '18 at 14:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.