如何将小数点处的数字对齐?{siunitx} 和 {dcolumn} 在我的代码中不起作用

如何将小数点处的数字对齐?{siunitx} 和 {dcolumn} 在我的代码中不起作用

我希望数字在小数点处垂直对齐。我尝试使用 {siunitx} 和 {dcolumn} 包,但它们无法生成我想要的表格。

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

这是我正在谈论的表格:

在此处输入图片描述

答案1

我建议您使用一个tabularx环境,其中 5 个主列中的 4 个指定了类型S(由siunitx包提供)。为了确保表格具有整体宽度\textwidth,对标题单元格使用了居中版本的X列类型。为了减少标题中的冗余,我在下面的代码中对其进行了一些重新组织。我还建议您通过删除所有垂直条和大多数水平条来使表格看起来更“开放”。对于剩余的几条水平线,请使用包的宏booktabs。最后,不要在表格的标题中塞入太多信息。表格图例中应包含对代表什么的解释N/A,而不是标题。

在此处输入图片描述

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

答案2

这里有两个解决方案siunitx,一个有,一个没有垂直规则,一个有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} 

在此处输入图片描述 在此处输入图片描述

相关内容