siunitx 的 table-column-width 根据内容而变化

siunitx 的 table-column-width 根据内容而变化

我试图将一张有 3 列的表格变成 2 列。

当我测试一些选项时,我注意到table-column-width选项siunitx不会产生相同的宽度,这取决于内容。

为什么?

比较以下两个表中两个子表的尺寸(参见红色矩形中突出显示的内容)。

(欢迎提出任何改进表格的建议。示例中的垂直规则只是为了说明问题。)

\documentclass{book}
\usepackage[a4paper]{geometry}
\geometry{tmargin=3cm,bmargin=3.5cm,lmargin=4cm,rmargin=3cm,
    marginparwidth=70pt}

\usepackage{array}
\renewcommand{\arraystretch}{1.2}
\usepackage{booktabs}
\usepackage[flushleft]{threeparttable}
\usepackage{makecell}
\newcommand{\tabitem}{--~~}%{~~\llap{--}~~} % command for itemizing in tables
\usepackage[font=small,labelfont=bf,labelsep=period,format=hang]{caption}
\captionsetup[table]{position=above, aboveskip=2pt}

\usepackage{siunitx}

\newlength{\mylen}
\setlength{\mylen}{2.5em}
\newcolumntype{A}[1]{S[table-format =#1]}
\newcolumntype{B}[2]{S[table-format =#1,table-column-width =#2]}
\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}

\newcommand{\baselii}{\mbox{Basel~II}} 

\usepackage{environ}
\newcommand{\tabfs}{\small}
\NewEnviron{tabnote}[1][para]{%
    \begin{tablenotes}[#1]    
        \footnotesize \BODY
    \end{tablenotes} 
}
\NewEnviron{tabella}{%
    \begin{table}[!tbh]
        \centering\tabfs
        \begin{threeparttable}
            \BODY
        \end{threeparttable}
    \end{table}
}

\raggedbottom

\usepackage{showframe}

\begin{document}
    \begin{tabella}
        \caption{Weights in the Standardized Approach of \baselii\label{tab:withB}}
        \begin{tabular}{l*2{A{2}}*3{B{3}{\mylen}}*2{A{3}}}
            \toprule 
            Category of claims & \multicolumn{6}{c}{Weights (\%) depending on
                ratings\tnote{a}}\\
            \cmidrule{2-7} 
             & \multicolumn{1}{c}{\makecell{AAA \\to AA$-$}} & \multicolumn{1}{c}{\makecell{A$+$ \\to A$-$}} & \multicolumn{1}{c}{\makecell{BBB$+$ \\to BBB$-$}} & \multicolumn{1}{c}{\makecell{BB$+$ \\to B$-$}} & \multicolumn{1}{c}{\makecell{Below \\B$-$}} & \multicolumn{1}{c}{Unrated}\\
            \midrule
            Sovereigns & 0 & 20 & 50 & 100 & 150 & 100\\\midrule%[1ex] 
            Non-central government PSEs: & \multicolumn{1}{c}{} & \multicolumn{1}{c}{} & \multicolumn{1}{c}{} & \multicolumn{1}{c}{} & \multicolumn{1}{c}{} & \multicolumn{1}{c}{}\\
            \tabitem option 1 & 20 & 50 & 100 & 100 & 150 & 100\\
            \tabitem option 2 & 20 & 50 & 50 & 100 & 150 & 50\\\midrule%[1ex] 
            MDBs & 20 & 50 & 50 & 100 & 150 & 50\\\midrule%[1ex] 
            Banks: & \multicolumn{1}{c}{} & \multicolumn{1}{c}{} & \multicolumn{1}{c}{} & \multicolumn{1}{c}{} & \multicolumn{1}{c}{} & \multicolumn{1}{c}{} \\
            \tabitem option 1 & 20 & 50 & 100 & 100 & 150 & 100\\
            \tabitem option 2 & 20 & 50 & 50 & 100 & 150 & 50\\
            \tabitem option 2 - short-term claims & 20 & 20 & 20 & 50 & 150 & 20\\
            \midrule 
             & \multicolumn{1}{c}{\makecell{AAA \\to AA$-$}} & \multicolumn{1}{c}{\makecell{A$+$ \\to A$-$}} &
             \multicolumn{3}{@{}c@{}}{\begin{tabular}{@{\hspace{1.34\tabcolsep}}*2{B{3}{1.5\mylen+.33\tabcolsep}}}
                    \multicolumn{1}{|B{3}{1.5\mylen+.33\tabcolsep}|}{\makecell{BBB$+$ \\to BB$-$}} &  
                     \multicolumn{1}{B{3}{1.5\mylen+.33\tabcolsep}|}{\makecell{Below \\BB$-$}}\\ 
                \end{tabular}}
                         & \multicolumn{1}{c}{Unrated} \\
            \cmidrule{2-7}
            Corporate & 20 & 50 & \multicolumn{3}{@{}c@{}}{\begin{tabular}{|@{\hspace{1.34\tabcolsep}}*2{B{3}{1.5\mylen+.33\tabcolsep}|}} 100 & 150\\\end{tabular}} & 100\\
            \bottomrule
        \end{tabular}
        \begin{tabnote}
            \item[a]{The vertical rules are only for illustrative purpose.}
        \end{tabnote}
    \end{tabella}
The sub-\verb|tabular|s in Table \ref{tab:withB} are built with the \verb|table-column-width| option of \verb|siunitx|: why does the with 
of the \verb|tabular| with the headers differ from the one with the numbers?  

With an ordinary \verb|p{...}| it doesn't happen, compare Table \ref{tab:withC}.
    \begin{tabella}
        \caption{Weights in the Standardized Approach of \baselii\label{tab:withC}}
        \begin{tabular}{l*2{A{2}}*3{B{3}{\mylen}}*2{A{3}}}
            \toprule 
            Category of claims & \multicolumn{6}{c}{Weights (\%) depending on
                ratings\tnote{a}}\\
            \cmidrule{2-7} 
             & \multicolumn{1}{c}{\makecell{AAA \\to AA$-$}} & \multicolumn{1}{c}{\makecell{A$+$ \\to A$-$}} & \multicolumn{1}{c}{\makecell{BBB$+$ \\to BBB$-$}} & \multicolumn{1}{c}{\makecell{BB$+$ \\to B$-$}} & \multicolumn{1}{c}{\makecell{Below \\B$-$}} & \multicolumn{1}{c}{Unrated}\\
            \midrule
            Sovereigns & 0 & 20 & 50 & 100 & 150 & 100\\\midrule%[1ex] 
            Non-central government PSEs: & \multicolumn{1}{c}{} & \multicolumn{1}{c}{} & \multicolumn{1}{c}{} & \multicolumn{1}{c}{} & \multicolumn{1}{c}{} & \multicolumn{1}{c}{}\\
            \tabitem option 1 & 20 & 50 & 100 & 100 & 150 & 100\\
            \tabitem option 2 & 20 & 50 & 50 & 100 & 150 & 50\\\midrule%[1ex] 
            MDBs & 20 & 50 & 50 & 100 & 150 & 50\\\midrule%[1ex] 
            Banks: & \multicolumn{1}{c}{} & \multicolumn{1}{c}{} & \multicolumn{1}{c}{} & \multicolumn{1}{c}{} & \multicolumn{1}{c}{} & \multicolumn{1}{c}{} \\
            \tabitem option 1 & 20 & 50 & 100 & 100 & 150 & 100\\
            \tabitem option 2 & 20 & 50 & 50 & 100 & 150 & 50\\
            \tabitem option 2 - short-term claims & 20 & 20 & 20 & 50 & 150 & 20\\
            \midrule 
            & \multicolumn{1}{c}{\makecell{AAA \\to AA$-$}} & \multicolumn{1}{c}{\makecell{A$+$ \\to A$-$}} &
            \multicolumn{3}{@{}c@{}}{\begin{tabular}{|@{\hspace{1.34\tabcolsep}}*2{C{1.5\mylen}@{\hspace{1.33\tabcolsep}}|}}
                    \makecell{BBB$+$ \\to BB$-$} &  
                    \makecell{Below \\BB$-$}\\ 
            \end{tabular}}
            & \multicolumn{1}{c}{Unrated} \\
            \cmidrule{2-7}
            Corporate & 20 & 50 & \multicolumn{3}{@{}c@{}}{\begin{tabular}{|@{\hspace{1.34\tabcolsep}}*2{C{1.5\mylen}@{\hspace{1.33\tabcolsep}}|}} 100 & 150\\\end{tabular}} & 100\\
            \bottomrule
        \end{tabular}
        \begin{tabnote}
            \item[a]{The vertical rules are only for illustrative purpose.}
        \end{tabnote}
    \end{tabella}
\end{document}

在此处输入图片描述

答案1

我想你会喜欢得到这样的东西:

在此处输入图片描述

我只考虑第一个表。提出的解决方案与您的表之间的区别是:

  • 增加了尺寸\mylen(参见我在问题下方的评论)
  • 删除的是所有\multicolumn{1}{c}{...}覆盖基本列定义的内容,即:确定table-column-width其内容的自然宽度
  • \multicolumn{3}{c}{...}用于嵌套表格的in在其开头和结尾处\tabcolsep被省略。@{}
  • 重新计算嵌套表中的列宽(现在等于跨度列的宽度)

\documentclass{book}
\usepackage[a4paper]{geometry}
\geometry{tmargin=3cm,bmargin=3.5cm,lmargin=4cm,rmargin=3cm,
    marginparwidth=70pt}

\usepackage{array}
\renewcommand{\arraystretch}{1.2}
\usepackage{booktabs}
\usepackage[flushleft]{threeparttable}
\usepackage{makecell}
\newcommand{\tabitem}{--~~}%{~~\llap{--}~~} % command for itemizing in tables
\usepackage[font=small,labelfont=bf,labelsep=period,format=hang]{caption}
\captionsetup[table]{position=above, aboveskip=2pt}

\usepackage{siunitx}

\newlength{\mylen}
\setlength{\mylen}{4em} % <--- increased
\newcolumntype{A}[1]{S[table-format =#1]}
\newcolumntype{B}[2]{S[table-format =#1,table-column-width =#2]}
\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}

\newcommand{\baselii}{\mbox{Basel~II}}

\usepackage{environ}
\newcommand{\tabfs}{\small}
\NewEnviron{tabnote}[1][para]{%
    \begin{tablenotes}[#1]
        \footnotesize \BODY
    \end{tablenotes}
}
\NewEnviron{tabella}{%
    \begin{table}[!tbh]
        \centering\tabfs
        \begin{threeparttable}
            \BODY
        \end{threeparttable}
    \end{table}
}

\raggedbottom
\usepackage{showframe}

\begin{document}
    \begin{tabella}
        \caption{Weights in the Standardized Approach of \baselii\label{tab:withB}}
    \setlength\tabcolsep{4pt}
\begin{tabular}{l*2{A{2}}|*3{B{3}{\mylen}|}*2{A{3}}}
    \toprule
    Category of claims & \multicolumn{6}{c}{Weights (\%) depending on
        ratings\tnote{a}}\\
    \cmidrule{2-7}
& {\makecell{AAA \\to AA$-$}}   & {\makecell{A$+$ \\to A$-$}} & {\makecell{BBB$+$ \\to BBB$-$}}
& {\makecell{BB$+$ \\to B$-$}}  & {\makecell{Below \\B$-$}}   & {Unrated}\\
    \midrule
    Sovereigns & 0 & 20 & 50 & 100 & 150 & 100\\\midrule%[1ex]
    Non-central government PSEs: & \multicolumn{1}{c}{} & \multicolumn{1}{c}{} & \multicolumn{1}{c}{} & \multicolumn{1}{c}{} & \multicolumn{1}{c}{} & \multicolumn{1}{c}{}\\
    \tabitem option 1 & 20 & 50 & 100 & 100 & 150 & 100\\
    \tabitem option 2 & 20 & 50 & 50 & 100 & 150 & 50\\\midrule%[1ex]
    MDBs & 20 & 50 & 50 & 100 & 150 & 50\\\midrule%[1ex]
    Banks: & \multicolumn{1}{c}{} & \multicolumn{1}{c}{} & \multicolumn{1}{c}{} & \multicolumn{1}{c}{} & \multicolumn{1}{c}{} & \multicolumn{1}{c}{} \\
    \tabitem option 1 & 20 & 50 & 100 & 100 & 150 & 100\\
    \tabitem option 2 & 20 & 50 & 50 & 100 & 150 & 50\\
    \tabitem option 2 - short-term claims & 20 & 20 & 20 & 50 & 150 & 20\\
    \midrule
    & {\makecell{AAA \\to AA$-$}} & {\makecell{A$+$ \\to A$-$}}
                        & \multicolumn{3}{@{} c @{}}{  % <---
                          \begin{tabular}{@{}|*2{B{3}{1.5\mylen+\tabcolsep+3\arrayrulewidth}|}  % <---
                                          @{}}
                          {\makecell{BBB$+$ \\to BB$-$}}
                                & {\makecell{Below \\BB$-$}}
                        \end{tabular}}      & {Unrated} \\
    \cmidrule{2-7}
    Corporate & 20 & 50 & \multicolumn{3}{@{} c @{}}{  % <---
                          \begin{tabular}{@{}|*2{B{3}{1.5\mylen+\tabcolsep+3\arrayrulewidth}|}  % <---
                                          @{}}
                          100   & 150
                          \end{tabular}}    & 100   \\
    \bottomrule
\end{tabular}
\begin{tabnote}
    \item[a]{The vertical rules are only for illustrative purpose.}
\end{tabnote}
\end{tabella}
\end{document}

答案2

我不确定我是否理解了这个问题。但为了简化问题:

\documentclass{book}

\usepackage{array}
\usepackage{siunitx}

\begin{document}

\begin{tabular}{|S[table-column-width=1cm]|} 
abc
\end{tabular}
\begin{tabular}{|S[table-column-width=1cm]|}
\multicolumn{1}{l}{XXXXXXXXXXXXXXXXXXXX}\\
abc
\end{tabular}

\begin{tabular}{|p{1cm}|}
abc
\end{tabular}
\begin{tabular}{|p{1cm}|}
\multicolumn{1}{l}{XXXXXXXXXXXXXXXXXXXX}\\
abc
\end{tabular}

\end{document}

这表明 \multicolumn 覆盖了 的宽度table-column-width。因此,您的示例首先应删除所有 \multicolumn 以避免它们干扰。

相关内容