TabularX - 带有 rowcolor - 列之间不需要的间隙

TabularX - 带有 rowcolor - 列之间不需要的间隙

我对 tabularx 的 rowcolor 有疑问。我有一列,其中的尺寸是我想要让所有“x”字符居中。

我想到了两个解决方案。一个是在表格列中使用 c@{ x } 定义。(我知道这不能用 rowcolor 填充,但这正是我想要的。

另一个是为每个 xa 列定义..但间距不太好..是否有选项可以为每行赋予 c@{ x } 背景颜色?

\documentclass[11pt]{article}
\usepackage[usenames]{color} %used for font color
\usepackage{amssymb} %maths
\usepackage{amsmath} %maths
\usepackage[utf8]{inputenc} %useful to type directly diacritic characters
\usepackage{multirow}
\usepackage{array}
\usepackage{tabularx}
\usepackage{colortbl}
\usepackage{color}
\definecolor{tabelzwart}{cmyk}{.0,.0,.0,1}
\definecolor{tabelgrijs}{cmyk}{.0,.0,.0,.15}
\definecolor{Akzent}{rgb}{0.6627, 0.63529, 0.55294} %akzentfarbe
\newcolumntype{L}[1]{>{\raggedright\arraybackslash}p{#1}}
\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}
\newcolumntype{R}[1]{>{\raggedleft\arraybackslash}p{#1}}
\newcommand\colourpadding[1]{\addlinespace[-1pt]\arrayrulecolor{#1}\midrule[6pt]\arrayrulecolor{black}}
\begin{document}

\begin{tabular}{
                    L{26mm}
                    r@{\extracolsep{\fill} \ x \ }
                    c@{ \ x \ }
                    l
                    C{23mm}
                    C{19mm}
                    C{19mm}
                 }
\rowcolor{tabelzwart}
  \textcolor{white}{Article \newline code} &
  \multicolumn{3}{p{45mm}}{\cellcolor{tabelzwart}\textcolor{white}{Dimension}} &
  \textcolor{white}{QA} &
  \textcolor{white}{Per bag} &
  \textcolor{white}{Per Box} \\
\rowcolor{tabelgrijs}80100 & 17.5 & 19.1   & 17.5  & {G} & 10 & 100\\
\rowcolor{white}80100 & 17.5 & 19.1   & 17.5  & {G} & 10 & 100\\
\rowcolor{tabelgrijs}22333 & 11.8 & 12.7   & 29.3  & {KWW} & 10 & 100\\
\rowcolor{white}70100 & 326.632  &  5.6   & 35.9  & {G K} & 190 & 100\\
\rowcolor{tabelgrijs}70100 & 36.6  &  5.6   & 35.9  & {G K} & 190 & 100\\
\rowcolor{white}70100 & 6.6  &  53.63   & 35.9  & {G / K} & 9 & 10\\
\rowcolor{tabelgrijs}70100 & 6.6  &  53.6   & 35.9  & {G} & 190 & 100\\
\rowcolor{white}70100 & 6.6  &  5.6   & 35.9  & {G} & 1903 & 100\\
\rowcolor{tabelgrijs}70100 & 6.6  &  53.6   & 35.9  & {G} & 190 & 100\\
\end{tabular}
\\
\\
\\
{x} in other columns
\\
\\
\\
\begin{tabular}{
                    L{26mm}
                    r
                    |C{5mm}
                    c
                    |C{5mm}
                    l
                    C{23mm}
                    C{19mm}
                    C{19mm}
                 }
  {Article \newline number} &
  1 &
  2 &
  3 &
  4 &
  5 &  
  {QA} &
  {bag} &
  {Box} \\
80100 & 17.5 & {x} & 19.1 & {x}  & 17.5  & {G} & 10 & 100\\
\end{tabular}
\end{document}

结果是

在此处输入图片描述

答案1

一种可能的解决方案是使用单独的列来表示x,并使用 来控制间距\tabcolsep。我发现间距足够了,但问题是小数点后面的数字不同:

示例 1 – tabcolsep

在此处输入图片描述

\documentclass[11pt]{article}
\usepackage[usenames]{color} %used for font color
\usepackage{amssymb} %maths
\usepackage{amsmath} %maths
\usepackage[utf8]{inputenc} %useful to type directly diacritic characters
\usepackage{multirow}
\usepackage{array}
\usepackage{tabularx, }
\usepackage[table]{xcolor}
\definecolor{tabelzwart}{cmyk}{.0,.0,.0,1}
\definecolor{tabelgrijs}{cmyk}{.0,.0,.0,.15}
\definecolor{Akzent}{rgb}{0.6627, 0.63529, 0.55294} %akzentfarbe
\newcolumntype{L}[1]{>{\raggedright\arraybackslash}p{#1}}
\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}
\newcolumntype{R}[1]{>{\raggedleft\arraybackslash}p{#1}}
\newcommand\colourpadding[1]{\addlinespace[-1pt]\arrayrulecolor{#1}\midrule[6pt]\arrayrulecolor{black}}

\begin{document}
\centering
\setlength{\tabcolsep}{0.25em}
\begin{tabular}{
                    L{26mm}
                    r
                    c
                    c
                    c
                    l
                    C{23mm}
                    C{19mm}
                    C{19mm}
                 }
\rowcolor{tabelzwart}
  \textcolor{white}{Article \newline code} &
  \multicolumn{5}{p{50mm}}{\cellcolor{tabelzwart}\textcolor{white}{Dimension}} &
  \textcolor{white}{QA}      &
  \textcolor{white}{Per bag} &
  \textcolor{white}{Per Box} \\
\rowcolor{tabelgrijs} 80100 & 17.5    & x & 19.1  & x & 17.5  & {G}    & 10  & 100\\
\rowcolor{white}      80100 & 17.5    & x & 19.1  & x & 17.5  & {G}    & 10  & 100\\
\rowcolor{tabelgrijs} 22333 & 11.8    & x & 12.7  & x & 29.3  & {KWW}  & 10  & 100\\
\rowcolor{white}      70100 & 326.632 & x &  5.6  & x & 35.9  & {G K}  & 190 & 100\\
\rowcolor{tabelgrijs} 70100 & 36.6    & x &  5.6  & x & 35.9 & {G K}   & 190 & 100\\
\rowcolor{white}      70100 & 6.6     & x & 53.63 & x & 35.9 & {G / K} & 9   & 10\\
\rowcolor{tabelgrijs} 70100 & 6.6     & x & 53.6  & x & 35.9 & {G}     & 190 & 100\\
\rowcolor{white}      70100 & 6.6     & x & 5.6   & x & 35.9 & {G}     & 1903 & 100\\
\rowcolor{tabelgrijs} 70100 & 6.6     & x & 53.6  & x & 35.9 & {G}     & 190 & 100\\
\end{tabular}
\end{document}

示例 2 –tabcolsep

在此处输入图片描述

D更好的解决方案是使用包将带有小数点的三列设置为-columns。我还将两\times列设置为 D 列,以提供正确的数学符号和良好的间距。

我已经对列间距进行了微调,并将标题\tabolsep的宽度减小到 41 毫米。multicolumn

\documentclass[11pt]{article}
\usepackage[usenames]{color} %used for font color
\usepackage{amssymb} %maths
\usepackage{amsmath} %maths
\usepackage[utf8]{inputenc} %useful to type directly diacritic characters
\usepackage{multirow}
\usepackage{array}
\usepackage{tabularx, dcolumn}
\usepackage[table]{xcolor}
\definecolor{tabelzwart}{cmyk}{.0,.0,.0,1}
\definecolor{tabelgrijs}{cmyk}{.0,.0,.0,.15}
\definecolor{Akzent}{rgb}{0.6627, 0.63529, 0.55294} %akzentfarbe
\newcolumntype{L}[1]{>{\raggedright\arraybackslash}p{#1}}
\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}
\newcolumntype{R}[1]{>{\raggedleft\arraybackslash}p{#1}}
\newcommand\colourpadding[1]{\addlinespace[-1pt]\arrayrulecolor{#1}\midrule[6pt]\arrayrulecolor{black}}

\begin{document}
\centering
\setlength{\tabcolsep}{0.1em}
\begin{tabular}{
                    L{26mm}
                    D{.}{.}{3.3}
                    D{x}{\times}{1}
                    D{.}{.}{2.2}
                    D{x}{\times}{1}
                    D{.}{.}{2.1}
                    C{23mm}
                    C{19mm}
                    C{19mm}
                 }
\rowcolor{tabelzwart}
  \textcolor{white}{Article \newline code} &
  \multicolumn{5}{p{41mm}}{\cellcolor{tabelzwart}\textcolor{white}{Dimension}} &
  \textcolor{white}{QA}      &
  \textcolor{white}{Per bag} &
  \textcolor{white}{Per Box} \\
\rowcolor{tabelgrijs} 80100 & 17.5    & x & 19.1  & x & 17.5 & {G}    & 10 & 100\\
\rowcolor{white}      80100 & 17.5    & x & 19.1  & x & 17.5 & {G}    & 10   & 100\\
\rowcolor{tabelgrijs} 22333 & 11.8    & x & 12.7  & x & 29.3 & {KWW}   & 10  & 100\\
\rowcolor{white}      70100 & 326.632 & x &  5.6  & x & 35.9 & {G K}   & 190 & 100\\
\rowcolor{tabelgrijs} 70100 & 36.6    & x &  5.6  & x & 35.9 & {G K}   & 190 & 100\\
\rowcolor{white}      70100 & 6.6     & x & 53.63 & x & 35.9 & {G / K} &   9 & 10\\
\rowcolor{tabelgrijs} 70100 & 6.6     & x & 53.6  & x & 35.9 & {G}     & 190 & 100\\
\rowcolor{white}      70100 & 6.6     & x & 5.6   & x & 35.9 & {G}     & 1903 & 100\\
\rowcolor{tabelgrijs} 70100 & 6.6     & x & 53.6 &x  & 35.9  & {G}     & 190  & 100\\
\end{tabular}
\end{document}

示例 3表格型\tabcolsep

还不满意?将最后两行设置为右对齐的 X 列。间距更好,溢出更少hboxes

在此处输入图片描述

\documentclass[11pt]{article}

\usepackage[utf8]{inputenc} %useful to type directly diacritic characters
\usepackage{array}
\usepackage{tabularx, dcolumn}
\usepackage[table, usenames]{xcolor}  % use xcolor with option instead of color
\definecolor{tabelzwart}{cmyk}{.0,.0,.0,1}
\definecolor{tabelgrijs}{cmyk}{.0,.0,.0,.15}
\definecolor{Akzent}{rgb}{0.6627, 0.63529, 0.55294} %akzentfarbe
\newcolumntype{L}[1]{>{\raggedright\arraybackslash}p{#1}}
\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}
\newcolumntype{R}{>{\raggedleft\arraybackslash}X}
\newcommand\colourpadding[1]{\addlinespace[-1pt]\arrayrulecolor{#1}\midrule[6pt]\arrayrulecolor{black}}

\begin{document}
\centering
\setlength{\tabcolsep}{0.1em}
\begin{tabularx}{\linewidth}{
                    L{26mm}
                    D{.}{.}{3.3}
                    D{x}{\times}{1}
                    D{.}{.}{2.2}
                    D{x}{\times}{1}
                    D{.}{.}{2.1}
                    C{23mm}
                    R
                    R
                 }
\rowcolor{tabelzwart}
  \textcolor{white}{Article\linebreak code} &
  \multicolumn{5}{p{41mm}}{\cellcolor{tabelzwart}\textcolor{white}{~~\linebreak Dimension}} &
  \textcolor{white}{~~\linebreak QA} &
  \textcolor{white}{Per\linebreak bag} &
  \textcolor{white}{Per\linebreak Box} \\
\rowcolor{tabelgrijs} 80100 & 17.5    & x & 19.1  & x & 17.5 & {G}    & 10 & 100\\
\rowcolor{white}      80100 & 17.5    & x & 19.1  & x & 17.5 & {G}    & 10   & 100\\
\rowcolor{tabelgrijs} 22333 & 11.8    & x & 12.7  & x & 29.3 & {KWW}   & 10  & 100\\
\rowcolor{white}      70100 & 326.632 & x &  5.6  & x & 35.9 & {G K}   & 190 & 100\\
\rowcolor{tabelgrijs} 70100 & 36.6    & x &  5.6  & x & 35.9 & {G K}   & 190 & 100\\
\rowcolor{white}      70100 & 6.6     & x & 53.63 & x & 35.9 & {G / K} &   9 & 10\\
\rowcolor{tabelgrijs} 70100 & 6.6     & x & 53.6  & x & 35.9 & {G}     & 190 & 100\\
\rowcolor{white}      70100 & 6.6     & x & 5.6   & x & 35.9 & {G}     & 1903 & 100\\
\rowcolor{tabelgrijs} 70100 & 6.6     & x & 53.6 &x  & 35.9  & {G}     & 190  & 100\\
\end{tabularx}

\end{document}

答案2

使用(2021-08-10{NiceTabularX}nicematrix≥ 6.0),您可以直接获得预期的输出。

\documentclass[11pt]{article}
\usepackage[usenames]{color} 
\usepackage{amssymb} 
\usepackage{amsmath} 
\usepackage{nicematrix}
\usepackage{color}

\definecolor{tabelzwart}{cmyk}{.0,.0,.0,1}
\definecolor{tabelgrijs}{cmyk}{.0,.0,.0,.15}
\definecolor{Akzent}{rgb}{0.6627, 0.63529, 0.55294}

\begin{document}

\begin{NiceTabularX}{\textwidth}{X[l]r@{ \ $\times$ \ }c@{ \ $\times$ \ }lX[c]X[r]X[r]}
\CodeBefore
  \rowcolors{2}{tabelgrijs}{white}
  \rowcolor{tabelzwart}{1}
\Body
  \RowStyle[color=white]{\sffamily}
  Article code & 
  \Block{1-3}{Dimension} &&&
  QA &
  Per Bag &
  Per Box \\
80100 & 17.5 & 19.1   & 17.5  & G & 10 & 100\\
80100 & 17.5 & 19.1   & 17.5  & G & 10 & 100\\
22333 & 11.8 & 12.7   & 29.3  & KWW & 10 & 100\\
70100 & 326.632  &  5.6   & 35.9  & G K & 190 & 100\\
70100 & 36.6  &  5.6   & 35.9  & G K & 190 & 100\\
70100 & 6.6  &  53.63   & 35.9  & G / K & 9 & 10\\
70100 & 6.6  &  53.6   & 35.9  & G & 190 & 100\\
70100 & 6.6  &  5.6   & 35.9  & G & 1903 & 100\\
70100 & 6.6  &  53.6   & 35.9  & G & 190 & 100\\
\end{NiceTabularX}

\end{document}

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

上述代码的输出

相关内容