NiceTabular:列之间的空间太大

NiceTabular:列之间的空间太大

我想保留我绘制的这个表格,但第一列和第二列之间的间距应该减小,这样最后一列就不会太挤。您还有其他改进建议吗?


% page setup 
\documentclass[a4paper, 11pt]{article}
\usepackage[margin=2cm]{geometry}

% color
\usepackage[dvipsnames, table,x11names]{xcolor}
\definecolor{mintbg}{rgb}{.63,.79,.95}
\colorlet{lightmintbg}{mintbg!40}

% graphics
\usepackage{array,multirow,graphicx} 
\usepackage[
singlelinecheck=false 
]{caption}

\newenvironment{conditions}
    {\noindent\tabularx{\textwidth}{>{$}l<{$} @{${}={}$} X}}
    {\endtabularx}

% units 
\usepackage{siunitx} %  comprehensive (SI) units package


% tables
\usepackage{ragged2e} 
\usepackage{booktabs, tabularx}
\usepackage{makecell}
\usepackage{nicematrix,tikz}


\begin{document}

\begin{table}
 \caption{TheCaption.}
\scriptsize
\renewcommand{\arraystretch}{1.4}
\begin{NiceTabular*}{\textwidth}{@{}>{\scriptsize}c*{4}{@{\extracolsep{\fill}}l}@{}}[colortbl-like]
\toprule 
\textbf{\makecell[l]{Time step}}& \textbf{\makecell[l]{Time [\si{\minute}]}} & \textbf{\makecell[l]{Assay A}} & \textbf{\makecell[l]{Assay B}} & \textbf{\makecell[l]{Assay C}} \\
\midrule
\rowcolor{lightmintbg}\Block{3-1}{0}    & 0  & $+$ Enzymes &    - & - \\
\rowcolor{lightmintbg}                  & 1  &-            &    - & -  \\
\rowcolor{lightmintbg}                  & 2  &-            &    - & $+$ Enzymes \\


\bottomrule
\end{NiceTabular*}
\end{table}


\end{document}

图片

附有解决方法(有错误!!!)

\documentclass[a4paper, 11pt]{article}
\usepackage[margin=2cm]{geometry}

% color
\usepackage[dvipsnames, table,x11names]{xcolor}
\definecolor{mintbg}{rgb}{.63,.79,.95}
\colorlet{lightmintbg}{mintbg!40}

% graphics
\usepackage{tikz}
\usepackage{graphicx}
\usepackage[skip=1ex, singlelinecheck=false]{caption}

%\newenvironment{conditions}
%    {\noindent\tabularx{\textwidth}{>{$}l<{$} @{${}={}$} X}}
%    {\endtabularx}

% units
\usepackage{siunitx} %  comprehensive (SI) units package

% tables
\usepackage{ragged2e}
\usepackage{array, booktabs, makecell, multirow, tabularx}
\renewcommand\theadfont{\bfseries}
\renewcommand\theadgape{}
\usepackage{nicematrix}

    \begin{table}
    \caption{The Caption.}
    \NiceMatrixOptions{cell-space-limits = 2pt}
\begin{NiceTabular*}{\textwidth}{@{} 
                                w{c}{\dimexpr0.11\linewidth-1\tabcolsep}
                                w{c}{\dimexpr0.11\linewidth-2\tabcolsep}
                           *{2}{W{l}{\dimexpr0.26\linewidth-2\tabcolsep}}
                                W{l}{\dimexpr0.26\linewidth-1\tabcolsep} 
                                 @{}}
\CodeBefore
\rectanglecolor{lightmintbg}{2-1}{4-5}
\rectanglecolor{lightmintbg}{8-7}{10-11}
\rectanglecolor{lightmintbg}{14-13}{16-17}
\rectanglecolor{lightmintbg}{20-19}{22-23}
\Body
    \toprule
\thead[l]{Time\\ step}    
    & \thead[l]{Time\\ [\si{\minute}]}
        & \thead[l]{Assay A}
            & \thead[l]{Assay B}
                & \thead[l]{Assay C}        \\
    \midrule
\Block{3-1}{0}    & 0  & $+$ Enzymes &    - & - \\
                  & 1  &-            &    - & -  \\
                  & 2  &-            &    - & $+$ Enzymes \\
\midrule
\Block{3-1}{I}   & 3   & 1 A   &    - & - \\
                  & 4   & -     &    - & 1 C \\
                  & 5   & -     &  1 B & - \\
\midrule
\Block{3-1}{II}  & 6   & 2 A   &    - & - \\
                  & 7   & -     &    - & 2 C \\
                  & 8   & -     &  2 B & - \\
\midrule
\Block{3-1}{III}  & 9   & 3 A   &    - & - \\
                  & 10   & -     &    - & 3 C \\
                  & 11   & -     &  3 B & - \\
\midrule
\Block{3-1}{IV}   & 12   & 4 A   &    - & - \\
                  & 13   & -     &    - & 4 C \\
                  & 14   & -     &  4 B & - \\
\midrule
\Block{3-1}{V}    & 15   & 5 A   &    - & - \\
                  & 16   & -     &    - & 5 C \\
                  & 17  & -     &  5 B & - \\
\midrule
\Block{3-1}{VI}   & 18   & 6 A   &    - & - \\
                  & 19   & -     &    - & 6 C \\
                  & 20   & -     &  6 B & - \\
\midrule
\Block{3-1}{VII}  & 21   & 7 A   &    - & - \\
                  & 22   & -     &    - & 7 C \\
                  & 23   & -     &  7 B & - \\

\bottomrule
\end{NiceTabular*}
   \label{tab:time}
\end{table}

该表应如下所示:

像这样

答案1

像这样?

在此处输入图片描述

与@Simon Dispa 类似回答(+1),您需要定义列的宽度,例如使用宏\dimexpr,其中考虑\tabcolsep每一列。

编辑:

单元格宽度是根据估算来计算的,前两列的宽度应该是多少,即大约0.11\linewidth,表格的其余宽度分配给其余的列:2 x 0.11 + 3 x 0.26 = 1。从每列的宽度中减去以下列tabcolsep:从第一列减去最后一列(因为它们由于@{}在列规范的开始和结束处使用,所以没有第一列和最后一列\tabcolsep)并从另外两列中减去。

表格中单元格块的颜色由以下公式定义:

\rectanglecolor{<color>}{<top left cell of block>}{<bottom right cell in block>}

在您的情况下,左上角的单元格位于第一列的第 2. 行:{2-1}而右下角的单元格位于第五列的第 4. 行:{4-5}

根据您编辑的问题,色块的正确代码是:

\CodeBefore
\rectanglecolor{lightmintbg}{2-1}{4-5}
\rectanglecolor{lightmintbg}{8-1}{10-5}
\rectanglecolor{lightmintbg}{14-1}{16-5}
\rectanglecolor{lightmintbg}{20-1}{22-5}
\Body

考虑到编辑问题中的表代码,产生了:

在此处输入图片描述

原始 MWE,生成表格的第一张图像:

\documentclass[a4paper, 11pt]{article}
\usepackage[margin=2cm]{geometry}

% color
\usepackage[dvipsnames, table,x11names]{xcolor}
\definecolor{mintbg}{rgb}{.63,.79,.95}
\colorlet{lightmintbg}{mintbg!40}

% graphics
\usepackage{tikz}
\usepackage{graphicx}
\usepackage[skip=1ex, singlelinecheck=false]{caption}

%\newenvironment{conditions}
%    {\noindent\tabularx{\textwidth}{>{$}l<{$} @{${}={}$} X}}
%    {\endtabularx}

% units
\usepackage{siunitx} %  comprehensive (SI) units package

% tables
\usepackage{ragged2e}
\usepackage{array, booktabs, makecell, multirow, tabularx}
\renewcommand\theadfont{\bfseries}
\renewcommand\theadgape{}
\usepackage{nicematrix}


\begin{document}
    \begin{table}
    \caption{The Caption.}
    \NiceMatrixOptions{cell-space-limits = 2pt}
\begin{NiceTabular*}{\textwidth}{@{} 
                                w{c}{\dimexpr0.11\linewidth-1\tabcolsep}
                                w{c}{\dimexpr0.11\linewidth-2\tabcolsep}
                           *{2}{W{l}{\dimexpr0.26\linewidth-2\tabcolsep}}
                                W{l}{\dimexpr0.26\linewidth-1\tabcolsep} 
                                 @{}}
\CodeBefore
\rectanglecolor{blue!15}{2-1}{4-5}
\Body
    \toprule
\thead[l]{Time\\ step}    
    & \thead[l]{Time\\ [\si{\minute}]}
        & \thead[l]{Assay A}
            & \thead[l]{Assay B}
                & \thead[l]{Assay C}        \\
    \midrule
\Block{3-1}{0}    
    & 0  & $+$ Enzymes &    - & -           \\
    & 1  &-            &    - & -           \\
    & 2  &-            &    - & $+$ Enzymes \\
    \bottomrule
\end{NiceTabular*}
    \end{table}
\end{document}

在上面的 MWE 中做了一些调整以使代码更短。

答案2

设置第一列的宽度,并在其他列之间分配剩余空间。

A

% page setup 
\documentclass[a4paper, 11pt]{article}
\usepackage[margin=2cm]{geometry}

% color
\usepackage[dvipsnames, table,x11names]{xcolor}
\definecolor{mintbg}{rgb}{.63,.79,.95}
\colorlet{lightmintbg}{mintbg!40}

% graphics
\usepackage{array,multirow,graphicx} 
\usepackage[
singlelinecheck=false 
]{caption}

\newenvironment{conditions}
{\noindent\tabularx{\textwidth}{>{$}l<{$} @{${}={}$} X}}
{\endtabularx}

% units 
\usepackage{siunitx} %  comprehensive (SI) units package


% tables
\usepackage{ragged2e} 
\usepackage{booktabs, tabularx}
\usepackage{makecell}
\usepackage{nicematrix,tikz}

\usepackage{calc}% added <<<

\begin{document}
    
    \newlength{\firstcolumn}
    \newlength{\othercolumn}
    
    \setlength{\firstcolumn}{60pt} % set the first column width
    \setlength{\othercolumn}{(\textwidth-\firstcolumn-8\tabcolsep)/4} % the width of the other columns
    

    \begin{table}
        \caption{TheCaption.}
        \scriptsize
        \renewcommand{\arraystretch}{1.4}
        \begin{NiceTabular*}{\textwidth}{@{}>{\scriptsize}wc{\firstcolumn}  *{4}{@{\extracolsep{\fill}}Wl{\othercolumn}}@{}}[colortbl-like] % changed <<<< w W from package array
            \toprule 
            \textbf{\makecell[l]{Time step}}& \textbf{\makecell[l]{Time [\si{\minute}]}} & \textbf{\makecell[l]{Assay A}} & \textbf{\makecell[l]{Assay B}} & \textbf{\makecell[l]{Assay C}} \\
            \midrule
            \rowcolor{lightmintbg}\Block{3-1}{0}    & 0  & $+$ Enzymes &    - & - \\
            \rowcolor{lightmintbg}                  & 1  &-            &    - & -  \\
            \rowcolor{lightmintbg}                  & 2  &-            &    - & $+$ Enzymes \\          
            \bottomrule
        \end{NiceTabular*}
    \end{table} 
    
\end{document}

答案3

最新版本nicematrix(2021-08-10 的 v. 6.0)提供了X样式为(并且带有与过时包中tabularx一样的权重选项)的列。tabularraytabu

此外,还有一个命令\rowlistcolors可用\CodeBefore。使用该命令,很容易要求交替排列三行蓝色行和三行白色行。

\documentclass[a4paper, 11pt]{article}
\usepackage[margin=2cm]{geometry}

\usepackage{xcolor}
\definecolor{mintbg}{rgb}{.63,.79,.95}

\usepackage{graphicx}
\usepackage[skip=1ex, singlelinecheck=false]{caption}
\usepackage{siunitx}
\sisetup{text-series-to-math}
\usepackage{nicematrix}
\usepackage{booktabs}

\begin{document}

\begin{table}
\caption{The Caption.}
\begin{NiceTabular}{@{}X[11]X[11,c]X[26]X[26]X[26]@{}}
\CodeBefore
  \rowlistcolors{2}{mintbg!40,=,=,white,=,=}[restart]
\Body
\toprule
\RowStyle[cell-space-limits=3pt]{\bfseries}
\Block{}{Time\\ step}  
    & \Block{}{Time\\ [\si{\minute}]}
        & Assay A
            & Assay B
                & Assay C        \\
    \midrule
\Block{3-1}{0}    & 0  & $+$ Enzymes &    - & - \\
                  & 1  &-            &    - & -  \\
                  & 2  &-            &    - & $+$ Enzymes \\
\midrule
\Block{3-1}{I}   & 3   & 1 A   &    - & - \\
                  & 4   & -     &    - & 1 C \\
                  & 5   & -     &  1 B & - \\
\midrule
\Block{3-1}{II}  & 6   & 2 A   &    - & - \\
                  & 7   & -     &    - & 2 C \\
                  & 8   & -     &  2 B & - \\
\midrule
\Block{3-1}{III}  & 9   & 3 A   &    - & - \\
                  & 10   & -     &    - & 3 C \\
                  & 11   & -     &  3 B & - \\
\midrule
\Block{3-1}{IV}   & 12   & 4 A   &    - & - \\
                  & 13   & -     &    - & 4 C \\
                  & 14   & -     &  4 B & - \\
\midrule
\Block{3-1}{V}    & 15   & 5 A   &    - & - \\
                  & 16   & -     &    - & 5 C \\
                  & 17  & -     &  5 B & - \\
\midrule
\Block{3-1}{VI}   & 18   & 6 A   &    - & - \\
                  & 19   & -     &    - & 6 C \\
                  & 20   & -     &  6 B & - \\
\midrule
\Block{3-1}{VII}  & 21   & 7 A   &    - & - \\
                  & 22   & -     &    - & 7 C \\
                  & 23   & -     &  7 B & - \\

\bottomrule
\end{NiceTabular}
   \label{tab:time}
\end{table}

\end{document}

您需要多次编译(因为nicematrix在后台使用 PGF/Tikz 节点)。

上述代码的输出

相关内容