LaTeX 中的“tabularx”表格宽度显示不正常

LaTeX 中的“tabularx”表格宽度显示不正常

我想将表格的宽度设为文本宽度。所以我这样做了:

\documentclass{article}
\usepackage{tabularx}
\usepackage{threeparttable}
\usepackage{siunitx}
\usepackage{booktabs}
\usepackage{multirow}
\begin{document}
\begin{table}[h]
  \caption{2018 water quality table}
  \label{2018 water quality table}
  \sisetup{table-alignment-mode = format,
  table-number-alignment = center,
  }
  \begin{threeparttable}
  \begin{tabularx}{\textwidth}{@{\extracolsep{\fill}}
      c
      S[table-format = 2.1]
        S[table-format = 2.1]
        S[table-format = 2.1]
          S[table-format = 1.1]
          S[table-format = 2]
            c
            @{\extracolsep{\fill}}}
      \toprule
        \multirow{2}{*}{number} & \multicolumn{5}{c}{\num{2018}Year~\num{0}\num{7}Month~\num{0}\num{9}Day~to~\num{2018}Year~\num{0}\num{7}Month~\num{13}day  water quality} & \multirow{2}{*}{Werther} \\ \cmidrule{2-6}
                            & $\mathrm{COD}_\mathrm{cr}$\tnote{1}  & $\mathrm{BOD}_\mathrm{5}$\tnote{2}  & NN &TP\tnote{3} &{SS}\tnote{4}\\
      \midrule
        1                   & 73        & 22.3      & 21.4      & 3.7      & 52     & sun              \\
        2                   & 69        & 18.9      & 11        & 1.3      & 28     & sun                 \\
        3                   & 80        & 29.1      & 10.8      & 1.3      & 37     & sun                \\
        4                   & 78        & 26        & 10.8      & 1.4      & 30     & sun                 \\
        5                   & 72        & 19.4      & 10.4      & 1        & 33     & sun              \\
       average                & 74.4      & 23.1      & 12.8      & 1.7      & 36 
          &                 \\
    \bottomrule       
  \end{tabularx}
  \begin{tablenotes}
    \footnotesize
    \item[1] S1
    \item[2] S2
    \item[3] S3
    \item[4] S4
  \end{tablenotes}
  \end{threeparttable}
\end{table}
\end{document}

效果是: 在此处输入图片描述

可以看到第六列太宽了,这个tabularx包可以把每列分布的很好,但是为什么这里不行呢?

我还有另一张在水平分布方面表现良好的表:

\begin{table}[h]
  \centering
  \caption{test1}
  \label{tab:test2}
  \begin{tabularx}{\textwidth}{@{\extracolsep{\fill}}lcccccc@       {\extracolsep{\fill}}}
        \toprule
         & $\mathrm{COD}_\mathrm{cr}$  & $\mathrm{BOD}_\mathrm{5}$   & NN\tnote{1}     & TP     & SS     & pH       \\ \midrule
         test4    & \num{\le 80}    & \num{\le 13}     & \num{\le 25}    & \num{\le 2.0}   & \num{\le 40}    & \num{6}--\num{9}  \\
        test5 & \num{\le 50}   & \num{\le 5}     & \num{\le 10}   & \num{\le 0.5}   & \num{\le 10}   & \num{6}--\num{9} \\
        test6     & \SI{37.5}{\%}& \SI{61.5}{\%} & \SI{60.0}{\%} & \SI{75.0}{\%} & \SI{75.0}{\%} &          \\ \bottomrule
  \end{tabularx}
\end{table}

在此处输入图片描述

答案1

这两个表的可能正确代码之一是:

\documentclass{article}
\usepackage{siunitx}
\usepackage{tabularx, threeparttable}
\usepackage{booktabs, multirow}

\begin{document}
First table:
    \begin{table}[ht]
\sisetup{table-format=2.1,
         table-column-width=3.8em}
\begin{threeparttable}
\caption{2018 water quality table}
\label{2018 water quality table}
    \begin{tabularx}{\textwidth}{
      c SSS
        S[table-format = 1.1]
        S[table-format = 2]
    X
                                }
    \toprule
\multirow{2}{*}{number} 
    &   \multicolumn{5}{c}{water qualityin perios 2018.07.o9 to 2018.07.13} 
                        &   \multirow{2}{*}{Werther}\\ 
    \cmidrule{2-6}
    &   {$\mathrm{COD}_\mathrm{cr}$\tnote{1}}  
        &   {$\mathrm{BOD}_\mathrm{5}$\tnote{2}}  
            &   {NN}
                &   {TP\tnote{3}} 
                    &   {SS\tnote{4}}
                        &                           \\
      \midrule
1   & 73    & 22.3  & 21.4  & 3.7   & 52    & sun   \\
2   & 69    & 18.9  & 11    & 1.3   & 28    & sun   \\
3   & 80    & 29.1  & 10.8  & 1.3   & 37    & sun   \\
4   & 78    & 26    & 10.8  & 1.4   & 30    & sun   \\
5   & 72    & 19.4  & 10.4  & 1     & 33    & sun   \\
average
    & 74.4  & 23.1  & 12.8  & 1.7   & 36    &       \\
    \bottomrule
      \end{tabularx}\footnotesize
      \begin{tablenotes}
\item[1] S1
\item[2] S2
\item[3] S3
\item[4] S4
    \end{tablenotes}
\end{threeparttable}
    \end{table}
    
Second table
    \begin{table}[ht]
\caption{test1}
\label{tab:test2}
    \begin{tabularx}{\textwidth}{l*{6}{>{\centering\arraybackslash}X}}       
    \toprule
    &   $\mathrm{COD}_\mathrm{cr}$  
        &   $\mathrm{BOD}_\mathrm{5}$   
            &   NN\tnote{1}     
                &   TP
                    &   SS
                        & pH       \\ \midrule
test4   & \num{\le 80}  & \num{\le 13}  & \num{\le 25}  
        & \num{\le 2.0} & \num{\le 40}  & \numrange{6}{9}   \\
test5   & \num{\le 50}  & \num{\le 5}   & \num{\le 10}   
        & \num{\le 0.5} & \num{\le 10}  & \numrange{6}{9}   \\
test6   & \qty{37.5}{\%}    & \qty{61.5}{\%}    & \qty{60.0}{\%} 
        & \qty{75.0}{\%}    & \qty{75.0}{\%}    &           \\ 
    \bottomrule
    \end{tabularx}
    \end{table}
\end{document}

笔记: 正如您的问题中所写,第二个表格是偶然起作用的。

在此处输入图片描述

编辑: 更简单的代码可以更好地控制(无需手动调整)列宽,从而启用该tabularray包:

\documentclass{article}
\usepackage{tabularray}
\UseTblrLibrary{booktabs, siunitx}

\begin{document}
First table:
    \begin{table}[ht]
\begin{talltblr}[
caption = {2018 water quality table},
  label = {2018 water quality table},
note{1} = {S1},
note{2} = {S2},
note{3} = {S3},
note{4} = {S4}
                ]{colspec={X[c]
                      *{3}{X[c, si={table-format=2.1}]}
                           X[c, si={table-format=1.1}]
                           X[c, si={table-format=2.0}]
                           X[c]}
                 }
    \toprule
\SetCell[r=2]{c}    number   
    &   \SetCell[c=5]{c}
        {{{water quality in periode 2018.07.09 to 2018.07.13}}} 
        &   &   &   &   &   \SetCell[r=2]{c}    {{{Wether}}}    
                                                    \\ 
    \cmidrule{2-6}
    &   {{{$\mathrm{COD}_\mathrm{cr}$}}}\TblrNote{1}  
        &   {{{$\mathrm{BOD}_\mathrm{5}$}}}\TblrNote{2}  
            &   {{{NN}}}
                &   {{{TP}}}\TblrNote{3}
                    &   {{{SS}}}\TblrNote{5}
                        &                           \\
      \midrule
1   & 73    & 22.3  & 21.4  & 3.7   & 52    & sun   \\
2   & 69    & 18.9  & 11    & 1.3   & 28    & sun   \\
3   & 80    & 29.1  & 10.8  & 1.3   & 37    & sun   \\
4   & 78    & 26    & 10.8  & 1.4   & 30    & sun   \\
5   & 72    & 19.4  & 10.4  & 1     & 33    & sun   \\
average
    & 74.4  & 23.1  & 12.8  & 1.7   & 36    &       \\
    \bottomrule
      \end{talltblr}
    \end{table}
    
Second table
    \begin{table}[ht]
\caption{test1}
\label{tab:test2}
    \begin{tblr}{colspec={l *{6}{X[c]}}}
    \toprule
    &   $\mathrm{COD}_\mathrm{cr}$  
        &   $\mathrm{BOD}_\mathrm{5}$   
            &   NN    
                &   TP
                    &   SS
                        & pH       \\ \midrule
test4   & \num{\le 80}      & \num{\le 13}      & \num{\le 25}  
        & \num{\le 2.0}     & \num{\le 40}      & \numrange{6}{9}   \\
test5   & \num{\le 50}      & \num{\le 5}       & \num{\le 10}   
        & \num{\le 0.5}     & \num{\le 10}      & \numrange{6}{9}   \\
test6   & \qty{37.5}{\%}    & \qty{61.5}{\%}    & \qty{60.0}{\%} 
        & \qty{75.0}{\%}    & \qty{75.0}{\%}    &           \\ 
    \bottomrule
    \end{tblr}
    \end{table}
\end{document}

在此处输入图片描述

附录: 关于您的评论:

  • 部分你说得对:如果不更改talltblr设置,列“SS”的宽度可以变得更宽。在这种情况下,必须将以下内容添加到表格选项中hspan=minimal
    \begin{table}[ht]
\begin{talltblr}[
caption = {2918 water quality},
  label = {2018 water quality table},
note{1} = {S1},
note{2} = {S2},
note{3} = {S3},
note{4} = {S4}
                ]{colspec={X[c]
                      *{3}{X[c, si={table-format=2.1}]}
                           X[c, si={table-format=1.1}]
                           X[c, si={table-format=2.0}]
                           X[c]},
                 hspan=minimal  % <---
                 }
    \toprule
\SetCell[r=2]{c}    number
    &   \SetCell[c=5]{c}
        {{{water quality in periode 2018.07.09 to 2018.07.13\\ test by gjlksjlfd}}}
        &   &   &   &   &   \SetCell[r=2]{c}    {{{Wether}}}    \\
    \cmidrule{2-6}
    &   {{{$\mathrm{COD}_\mathrm{cr}$}}}\TblrNote{1}  
        &   {{{$\mathrm{BOD}_\mathrm{5}$}}}\TblrNote{2}  
            &   {{{NN}}}
                &   {{{TP}}}\TblrNote{3}
                    &   {{{SS}}}\TblrNote{5}
                        &  {{{Wether}}}             \\
      \midrule
% table body
    \bottomrule
    \end{talltblr}
    \end{table}

在此处输入图片描述

  • 但是,我会重新考虑,如果表格中的第一行确实有必要的话。例如,可以重新措辞此信息并将其移至标题。通过从 @Mico 的回答 (+1) 中窃取时间段和表格注释的符号,并添加caption用于在第二个表格上方很好地放置标题的包,MWE 可以是:
\documentclass{article}
\usepackage[skip=1ex]{caption}
\usepackage{tabularray}
\UseTblrLibrary{booktabs, siunitx}

\begin{document}
First table:
    \begin{table}[ht]
\begin{talltblr}[
caption = {MacAuthor test of water quality in period 7--13 July, 2018},
  label = {2018 water quality table},
note{ } = {\textsuperscript{1} S1,
           \textsuperscript{2} S2,
           \textsuperscript{3} S3,
           \textsuperscript{4} S4}
                ]{colspec={X[c]
                      *{3}{X[c, si={table-format=2.1}]}
                           X[c, si={table-format=1.1}]
                           X[c, si={table-format=2.0}]
                           X[c]}
                 }
    \toprule
number
    &   {{{$\mathrm{COD}_\mathrm{cr}$}}}\TblrNote{1}
        &   {{{$\mathrm{BOD}_\mathrm{5}$}}}\TblrNote{2}
            &   {{{NN}}}
                &   {{{TP}}}\TblrNote{3}
                    &   {{{SS}}}\TblrNote{5}
                        &  {{{Wether}}}             \\
      \midrule
1   & 73    & 22.3  & 21.4  & 3.7   & 52    & sun   \\
2   & 69    & 18.9  & 11    & 1.3   & 28    & sun   \\
3   & 80    & 29.1  & 10.8  & 1.3   & 37    & sun   \\
4   & 78    & 26    & 10.8  & 1.4   & 30    & sun   \\
5   & 72    & 19.4  & 10.4  & 1     & 33    & sun   \\
average
    & 74.4  & 23.1  & 12.8  & 1.7   & 36    &       \\
    \bottomrule
      \end{talltblr}
    \end{table}
    
Second table
    \begin{table}[ht]
\caption{test1}
\label{tab:test2}
    \begin{tblr}{colspec={l *{6}{X[c]}}}
    \toprule
    &   $\mathrm{COD}_\mathrm{cr}$  
        &   $\mathrm{BOD}_\mathrm{5}$   
            &   NN    
                &   TP
                    &   SS
                        & pH       \\ \midrule
test4   & \num{\le 80}      & \num{\le 13}      & \num{\le 25}  
        & \num{\le 2.0}     & \num{\le 40}      & \numrange{6}{9}   \\
test5   & \num{\le 50}      & \num{\le 5}       & \num{\le 10}   
        & \num{\le 0.5}     & \num{\le 10}      & \numrange{6}{9}   \\
test6   & \qty{37.5}{\%}    & \qty{61.5}{\%}    & \qty{60.0}{\%} 
        & \qty{75.0}{\%}    & \qty{75.0}{\%}    &           \\ 
    \bottomrule
    \end{tblr}
    \end{table}
\end{document}

在此处输入图片描述

答案2

由于所有单元格都不需要自动换行,因此我将使用tabular*环境,不是一个tabularx环境。

我也会以不同的方式构造材料,以便将“期间:2018 年 7 月 7 日至 13 日”这一行放在表格内部而不是标题中,因为它似乎不属于标题。

哦,使用threeparttable环境的时候,请将\caption\label指令里面环境threeparttable。(环境的三个正式部分threeparttable是标题、表格环境和tablenotes环境。)

我注释掉了该\sisetup指令,因为它似乎没有做任何有用的事情 —— 至少对于手头的表格来说没有。

在此处输入图片描述

\documentclass{article}
\usepackage[flushleft,para]{threeparttable} % 'para' option is optional
\usepackage{siunitx}  % for 'S' column type
\usepackage{booktabs}

\begin{document}
\begin{table}[ht]
%  \sisetup{table-alignment-mode = format,
%  table-number-alignment = center,
%  }
  \setlength\tabcolsep{0pt} % let LaTeX figure out intercolumn whitespace
  \begin{threeparttable}
  \caption{2018 water quality table}
  \label{tab:2018_water_quality}
 
  \begin{tabular*}{\textwidth}{ @{\extracolsep{\fill}}
      c
      *{3}{S[table-format = 2.1]}
      S[table-format = 1.1]
      S[table-format = 2]
      c }
  \toprule
  Number 
  & {$\mathrm{COD}_\mathrm{cr}$\tnote{1}}  
  & {$\mathrm{BOD}_\mathrm{5}$\tnote{2}}  
  & {NN} 
  & {TP\tnote{3}} 
  & {{SS}\tnote{4}}
  & {Weather} \\
  \midrule
  \multicolumn{7}{l}{Period: 7--13 July, 2018} \\[0.5ex]
  1           & 73   & 22.3 & 21.4 & 3.7 & 52     & sun \\
  2           & 69   & 18.9 & 11   & 1.3 & 28     & sun \\
  3           & 80   & 29.1 & 10.8 & 1.3 & 37     & sun \\
  4           & 78   & 26   & 10.8 & 1.4 & 30     & sun \\
  5           & 72   & 19.4 & 10.4 & 1   & 33     & sun \\[0.5ex]
  average     & 74.4 & 23.1 & 12.8 & 1.7 & 36     & \\
  \bottomrule       
  \end{tabular*}
  
  \smallskip
  \begin{tablenotes}
    \footnotesize
    \item[1] S1
    \item[2] S2
    \item[3] S3
    \item[4] S4
  \end{tablenotes}
  \end{threeparttable}
\end{table}
\end{document}

相关内容