1

I want to have my numbers aligned vertically at decimal point. I tried to use {siunitx} and {dcolumn} packages, but they did not work to generate the table that I want to.

\begin{table}
 \centering
 \captionsetup{justification=centering,margin=1.5cm}
\begin{tabular}{|p{1.2cm}|p{2cm}|p{2cm}|p{2cm}|p{2cm}|}
  \hline
 Angle            & Time (sec)&      cost function with DC (Nm$)^2$s & cost   function with GPOPS-II (Nm$)^2$s & cost function with Shooting (Nm$)^2$s \\ \hline
       & 0.5           & 44.22  & 42.25     &  42.99 \\ \cline{2-5}
$45^{\circ}$   & 1       & 12.68    & 11.29     & 11.04\\ \cline{2-5}
                       & 2       & 7.14     & 11.29     & N/A \\ \cline{2-5}
                    & 4          &  4.41    & 11.28     & N/A\\ \hline
      & 0.5           & 192.71  & 184.70    &  188.66 \\ \cline{2-5}
$90^{\circ}$   &  1             & 41.33     & 37.59     &  37.21 \\ \cline{2-5}
      & 2              & 23.47  & 36.95     &  N/A \\ \cline{2-5}
              & 4                  & 14.43  & 36.96     &  N/A \\ \hline
       & 0.5           & 1002.90    & 962.44       &  992.26 \\ \cline{2-5}
$180^{\circ}$ & 1        & 157.81   & 148.12    &  N/A  \\ \cline{2-5}
                    & 2      & 72.51    & 66.13 &  N/A  \\ \cline{2-5}
                    & 4          &  32.05   & 66.85     &  N/A  \\ \hline
      & 0.5           & 4118.20     & N/A   &  4231.74  \\ \cline{2-5}
$360^{\circ}$ &  1             & 650.87 & 651.16    &  658.81  \\ \cline{2-5}
      & 2              & 182.65 & 182.73    &  N/A  \\ \cline{2-5}
              & 4                  & 109.12 & 134.11       &  N/A  \\ \hline
 \end{tabular}
 \newline\newline
 \caption[Cost function values from DC, GPOPS-II , and Shooting]{Cost function values from DC, GPOPS-II , and Shooting. N/A indicates that the method was not able to solve the problem}
 \label{Table1}
 \end{table}

This is the table I am talking about:

enter image description here

Mico
  • 506,678
Milad
  • 147
  • 1
    How do you expect the alignment methods of the dcolumn and siunitx packages to work if you don't use their dedicated column types? – Mico Jul 31 '16 at 19:26
  • @Mico I took them out of my code, because I was not able to generate this table using them! That's the question! I used this command: \begin{tabular}{|l| *{5}{S[table-format=-1.3}}. It did not work out at all! – Milad Jul 31 '16 at 19:28
  • @Mico do you have any ideas? – Milad Jul 31 '16 at 19:47

2 Answers2

2

I suggest you use a tabularx environment, with 4 of the 5 main columns assigned type S (provided by the siunitx package). To assure that the table has overall width \textwidth, used a centered version of the X column type for the header cells. To cut down on redundancy in the header, I've reorganized it a bit in the code below. I would also like to recommend that you give the table a more "open" look by getting rid of all vertical bars and most horizontal bars. For the few remaining horizontal lines, use the macros of the booktabs package. Finally, don't pack too much information into the table's caption. Explanations of what N/A stands for belong in the table's legend, not its caption.

enter image description here

\documentclass{article}
\usepackage{caption,tabularx,booktabs,siunitx}
\newcolumntype{C}{>{\centering\arraybackslash}X}
\newcommand{\mc}[1]{\multicolumn{1}{@{}C@{}}{#1}}
\begin{document}

\begin{table}
\begin{tabularx}{\textwidth}{@{} l
  S[table-format=1.1] *{3}{S[table-format=4.2]} @{}}
\toprule
 Angle & \mc{Time} & \multicolumn{3}{c}{Cost function with}\\
\cmidrule{3-5}
 && \mc{DC} & \mc{GPOPS-II} & \mc{Shooting} \\ 
 & {(sec)} & {(Nm)\textsuperscript{2}s} 
           & {(Nm)\textsuperscript{2}s} 
           & {(Nm)\textsuperscript{2}s}  \\
\midrule
$45^{\circ}$ & 0.5 & 44.22  &  42.25 &  42.99 \\ 
             & 1   & 12.68  &  11.29 &  11.04 \\ 
             & 2   &  7.14  &  11.29 &  {N/A} \\ 
             & 4   &  4.41  &  11.28 &  {N/A} \\ \addlinespace
$90^{\circ}$ & 0.5 & 192.71 & 184.70 & 188.66 \\ 
             & 1   &  41.33 &  37.59 &  37.21 \\ 
             & 2   &  23.47 &  36.95 &  {N/A} \\ 
             & 4   &  14.43 &  36.96 &  {N/A} \\ \addlinespace
$180^{\circ}$& 0.5 &1002.90 & 962.44 & 992.26 \\ 
             & 1   & 157.81 & 148.12 &  {N/A}  \\ 
             & 2   &  72.51 &  66.13 &  {N/A}  \\ 
             & 4   &  32.05 &  66.85 &  {N/A}  \\ \addlinespace
$360^{\circ}$& 0.5 &4118.20 &  {N/A} &4231.74  \\ 
             & 1   & 650.87 & 651.16 & 658.81  \\ 
             & 2   & 182.65 & 182.73 &  {N/A}  \\ 
             & 4   & 109.12 & 134.11 &  {N/A}  \\ 
\bottomrule\addlinespace
\multicolumn{5}{@{}l@{}}{N/A indicates that the method was not able to solve the problem}\\
\end{tabularx}

\caption{Cost function values from DC, GPOPS-II, and Shooting}
\label{Table1}
\end{table}
\end{document} 
Mico
  • 506,678
1

Here are two solutions with siunitx, one without vertical rules and booktabs:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{array, tabularx, caption, threeparttable,booktabs}
\usepackage{siunitx , multirow, makecell}
\setcellgapes{3pt}
\let\oldmakecell\makecell
\renewcommand\makecell[2][cc]{\renewcommand\cellalign{#1}\oldmakecell{#2}}
\usepackage{lipsum}

\begin{document}

\begin{table}
  \centering
  \captionsetup{justification=centering,margin=1.5cm}
  \sisetup{table-number-alignment=center}
  \setlength\extrarowheight{3pt}
  \setcellgapes[b]{5pt}\makegapedcells
  \begin{threeparttable}
    \begin{tabular}{|c|S[table-format=1.1]|S[table-format=4.2]|S|S|}
      \hline
      Angle                                & {Time (s)} & {\Gape[3pt][0pt]{\makecell{cost function with &        &         \\ DC (\si{Nm²})}}} & {\makecell{cost function with\\ GPOPS-II (\si{Nm²})}} & {\makecell{cost function with\\ Shooting (\si{Nm²})} } \\[-4pt] \hline
      \multirowcell{4}{ \SI{45}{\degree}}  & 0.5        & 44.22                                         & 42.25  & 42.99   \\ \cline{2-5}

                                           & 1          & 12.68                                         & 11.29  & 11.04   \\ \cline{2-5}
                                           & 2          & 7.14                                          & 11.29  & {N/A}   \\ \cline{2-5}
                                           & 4          & 4.41                                          & 11.28  & {N/A}   \\%
      \hline
      \multirowcell{4}{ \SI{90}{\degree}}  & 0.5        & 192.71                                        & 184.70 & 188.66  \\ \cline{2-5}
                                           & 1          & 41.33                                         & 37.59  & 37.21   \\ \cline{2-5}
                                           & 2          & 23.47                                         & 36.95  & { N/A } \\ \cline{2-5}
                                           & 4          & 14.43                                         & 36.96  & {N/A}   \\ \hline
      \multirowcell{4}{ \SI{180}{\degree}} & 0.5        & 1002.90                                       & 962.44 & 992.26  \\ \cline{2-5}
                                           & 1          & 157.81                                        & 148.12 & {N/A}   \\ \cline{2-5}
                                           & 2          & 72.51                                         & 66.13  & {N/A}   \\ \cline{2-5}
                                           & 4          & 32.05                                         & 66.85  & {N/A}   \\ \hline
      \multirowcell{4}{ \SI{360}{\degree}} & 0.5        & 4118.20                                       & {N/A}  & 4231.74 \\ \cline{2-5}
                                           & 1          & 650.87                                        & 651.16 & 658.81  \\ \cline{2-5}
                                           & 2          & 182.65                                        & 182.73 & {N/A}   \\ \cline{2-5}
                                           & 4          & 109.12                                        & 134.11 & {N/A}   \\ \hline
    \end{tabular}
    \begin{tablenotes}[flushleft]\footnotesize\smallskip
      \item[]N/A indicates that the method was not able to solve the problem
    \end{tablenotes}
    \caption[Cost function values from DC, GPOPS-II , and Shooting]{Cost function values from DC, GPOPS-II , and Shooting.}
    \label{Table1}
  \end{threeparttable}
\end{table}



\begin{table}
  \centering
  \captionsetup{justification=centering,margin=1.5cm}
  \sisetup{table-number-alignment=center}
  \setlength\extrarowheight{3pt}
  \setcellgapes[b]{3pt}\makegapedcells
  \begin{threeparttable}
    \begin{tabular}{cS[table-format=1.1]S[table-format=4.2]SS}
      \toprule
      Angle & {Time (s)} & {\makecell{cost function with\\ DC (\si{Nm²})}} & {\makecell{cost function with\\ GPOPS-II (\si{Nm²})}} %
      & {\makecell{cost function with\\ Shooting (\si{Nm²})} } \\[-3pt]
      \midrule[\heavyrulewidth]
      \multirowcell{4}{ \SI{45}{\degree}}  & 0.5 & 44.22   & 42.25  & 42.99   \\
                                           & 1   & 12.68   & 11.29  & 11.04   \\
                                           & 2   & 7.14    & 11.29  & {N/A}   \\
                                           & 4   & 4.41    & 11.28  & {N/A}   \\%
      \cmidrule(lr){1-5}
      \multirowcell{4}{ \SI{90}{\degree}}  & 0.5 & 192.71  & 184.70 & 188.66  \\
                                           & 1   & 41.33   & 37.59  & 37.21   \\
                                           & 2   & 23.47   & 36.95  & { N/A } \\
                                           & 4   & 14.43   & 36.96  & {N/A}   \\
      \cmidrule(lr){1-5}
      \multirowcell{4}{ \SI{180}{\degree}} & 0.5 & 1002.90 & 962.44 & 992.26  \\
                                           & 1   & 157.81  & 148.12 & {N/A}   \\
                                           & 2   & 72.51   & 66.13  & {N/A}   \\
                                           & 4   & 32.05   & 66.85  & {N/A}   \\
      \cmidrule(lr){1-5}
      \multirowcell{4}{ \SI{360}{\degree}} & 0.5 & 4118.20 & {N/A}  & 4231.74 \\
                                           & 1   & 650.87  & 651.16 & 658.81  \\
                                           & 2   & 182.65  & 182.73 & {N/A}   \\
                                           & 4   & 109.12  & 134.11 & {N/A}   \\
      \bottomrule
    \end{tabular}
    \begin{tablenotes}[flushleft]\footnotesize\smallskip
      \item[]N/A indicates that the method was not able to solve the problem
    \end{tablenotes}
    \caption[Cost function values from DC, GPOPS-II , and Shooting]{Cost function values from DC, GPOPS-II , and Shooting.}
    \label{Table1}
  \end{threeparttable}
\end{table}
\end{document} 

enter image description here enter image description here

Bernard
  • 271,350