makecell 导致错误对齐

makecell 导致错误对齐

我得到了以下简单的表格,我用它来makecell获得良好的间距。但第一行的多列对齐方式有点不对。它应该是左对齐的,但却居中了。对于以下行,它又正常工作了。有什么提示为什么会这样吗?我也在使用 siunitx,因为我不知道问题是否出在那里(尽管这里可能不太合理,因为只有一行)

\documentclass{article}
\usepackage{array}
\usepackage{lipsum}
\usepackage{booktabs}
\usepackage[a4paper]{geometry}
\usepackage{makecell}
\usepackage{siunitx}
\geometry{left=1cm,right=1cm,top=1cm,bottom=1cm}

\begin{document}

\begin{table}[ht]
    \setcellgapes{12pt}\makegapedcells
\centering
\begin{tabular}{
      lSSlSSS
  }
  \hline
  \multicolumn{7}{l}{This text should be left aligned} \\
  Column 1 & Column 2 & Column 3 & Column 4 & Column 5 & Column 6 & Column 7 \\
  \hline
  0.123 & 0.123  & 0.123  & 0.123 & 0.123 & 0.123 & 0.123 \\
  \hline
\end{tabular}%
\end{table}

\end{document}

答案1

由于我找不到一种方法来摆脱不需要的中心对齐,同时仍然使用包\makegapedcells中的makecell,所以这里是使用替代解决方案cellspace。 (在第二个表中,我还添加了\sisetup以便table-formatS类型列分配适当的选项。我还将类型列中的所有非数字内容括S在一组中{}

\documentclass{article}
\usepackage{array}
\usepackage{lipsum}
\usepackage{booktabs}
\usepackage[a4paper]{geometry}
\usepackage{makecell}
\usepackage{siunitx}
\geometry{left=1cm,right=1cm,top=1cm,bottom=1cm}
\usepackage[column=0]{cellspace}
\setlength{\cellspacetoplimit}{12pt}
\setlength{\cellspacebottomlimit}{\cellspacetoplimit}

\begin{document}

\begin{table}[ht]
\centering
\begin{tabular}{
      0l S S 0l S S S
  }
  \hline
  \multicolumn{7}{0l}{This text should be left aligned} \\
    Column 1 & {Column 2} & {Column 3} & Column 4 & {Column 5} & {Column 6} & {Column 7} \\
  \hline
  0.123 & 0.123  & 0.123  & 0.123 & 0.123 & 0.123 & 0.123 \\
  \hline
\end{tabular}%
\end{table}

\begin{table}[ht]
\centering
\sisetup{table-format=1.3}
\begin{tabular}{
      0l S S 0l S S S
  }
  \hline
  \multicolumn{7}{0l}{This text should be left aligned} \\
    Column 1 & {Column 2} & {Column 3} & Column 4 & {Column 5} & {Column 6} & {Column 7} \\
  \hline
  0.123 & 0.123  & 0.123  & 0.123 & 0.123 & 0.123 & 0.123 \\
  \hline
\end{tabular}%
\end{table}

\end{document}

答案2

解决方法\rlap如下makecell

\documentclass{article}
\usepackage{array}
\usepackage{lipsum}
\usepackage{booktabs}
\usepackage[a4paper]{geometry}
\usepackage{makecell}
\usepackage{siunitx}
\geometry{left=1cm,right=1cm,top=1cm,bottom=1cm}

\begin{document}

\begin{table}[ht]
    \setcellgapes{12pt}\makegapedcells
\centering
\begin{tabular}{
      lSSlSSS
  }
  \hline
  \makecell[l]{\rlap{This text should be left aligned}} \\
  Column 1 & Column 2 & Column 3 & Column 4 & Column 5 & Column 6 & Column 7 \\
  \hline
  0.123 & 0.123  & 0.123  & 0.123 & 0.123 & 0.123 & 0.123 \\
  \hline
\end{tabular}%
\end{table}

\end{document}

相关内容