3

I have a problem with the rowcolor of the tabularx. I have a column with dimensions where I want all the 'x' chars centered.

I thought of two solutions. One with the c@{ x } definition in the table columns. (i know this cant be filled with rowcolor, but is it exactly what I wanted.

The other one is to define for each x a column.. but then the spacing is not nice.. Is there an option to giva de c@{ x } a background color for each row?

\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}

The result is

enter image description here

5
  • To highlight code-blocks, either indent them by four spaces or use the {} on the gui. You can highlight code in your post using back-ticks. – user30471 Jan 17 '20 at 13:38
  • What do you try to achieve exactly? It is not clear to me. Could you explain a bit more? – Bernard Jan 17 '20 at 13:41
  • I've added some more information with screenshots in a post bellow. Hopefully, this is clearer – ErikB Jan 17 '20 at 13:54
  • Just another question: the x actually means times (×)? – Bernard Jan 17 '20 at 14:38
  • No the x is just a divider for a dimension. (lika a squar of 10inch x 10inch) – ErikB Jan 20 '20 at 9:25
1

One possible solution is to use separate columns for the x, and manipulate spacing using \tabcolsep. I find the spacing adequate, but the problem is different numbers of figures behind the decimal point:

Example 1 – tabcolsep

enter image description here

\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}

Example 2 – dcolumn and tabcolsep

enter image description here

A better solution is to set the the three columns with decimal point as D-columns, using the package dcolumn. I have also set the two \times-columns as D-columns, giving correct mathematical symbol and nice spacing.

I have fine tuned the column spacing using \tabolsep and reduced the width of the multicolumn in the heading to 41 mm.

\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}

Example 3 – tabularx, dcolumns and \tabcolsep

Still not satisfied? Set the two last rows as right aligned X-columns. Better spacing and fewer overfull hboxes.

enter image description here

\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}
5
  • tex hero!!! Tnx a lot. – ErikB Jan 20 '20 at 8:18
  • Is it also possible to left allign the last column of the dimensions? i read the dcolumn pdf link but the -1 doesnt work for the last dimension. – ErikB Jan 20 '20 at 9:13
  • align to the left i mean :) so the numbers in the last column of the dimensions are next to the x on the left. When i add a empty column after the last dimension, the -1 works.. so i have a work arround – ErikB Jan 20 '20 at 9:28
  • @ErikB I am not sure if I understand what you are after. Since the last column in the dimension all have the same number of digits, it is left aligned, even if it centred at the full stop. If you want the last column closer to the x-signe, you can cheat and use D{.}{.}{1.1}. In my MWE, D{.}{.}{-1} works, ut give the same result as D{.}{.}{2.1}, which is as documented at page 1. – Sveinung Jan 20 '20 at 10:32
  • @ErikB The following header gives a very tight fit: \begin{tabularx}{\linewidth}{ L{26mm} D{.}{.}{-1} D{x}{\times}{0.0} D{.}{.}{2.2} D{x}{\times}{0.0} D{.}{.}{1.1} C{23mm} R R } – Sveinung Jan 20 '20 at 10:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.