3

I would like to control the spacing between the siunitx columns, however, the insertion of !{\hspace{1ex}} between the third and fourth columns results in the discontinuity of the gray color in the third row,

enter image description here

while <{\hspace{1ex}} doesn't have any effect on the output.

enter image description here

\documentclass[border=5mm]{standalone}
\usepackage{tabularx,siunitx,multicol,ragged2e}
\usepackage[table]{xcolor}


\newcolumntype{Y}{>{\Centering}X}
\newcolumntype{L}{>{\RaggedRight}X}
\newcolumntype{R}{>{\RaggedLeft}X}

\begin{document}
    \setlength{\tabcolsep}{0pt}
    \rowcolors{2}{gray!20}{}
    \begin{tabularx}{1\linewidth}{
            >{\hsize=1.3\hsize}L<{\hspace{1ex}}
            >{\hsize=0.7\hsize}Y<{\hspace{1ex}}
            S[table-format=3.1, round-precision=1, table-column-width = 0.2\linewidth, table-number-alignment=right]<{\hspace{1ex}}
            s[table-unit-alignment = left, table-column-width = 0.2\linewidth]
        }
        Parameter & Symbol & \multicolumn{2}{c}{Value}\\
        Inertia  & $J_M$ & 0.6 & \kilogram\m\squared\\
        Inertia  & $J_M$ & 0.6 & \kilogram\m\squared\\
    \end{tabularx}
\end{document}
Diaa
  • 9,599
  • 1
    This is because the \hspace{1ex} is added in replacement of | to the normal \tabcolsep, and the left and right colour overhangs in each column defaults to \tabcolsep. If you really need to use the !{...} syntax, I think it might done with !{\color{gray!10}\vrule width 1ex} and playing with \hhline and arrayrulecolor. – Bernard Dec 21 '19 at 21:26
  • @Bernard Thanks for your consideration. I have an off-topic question if you don't mind: how can I have tabularx environment with different column separation? For example, in my question, I need to inject some spacing of \hspace{2ex} between the first and the second columns. It seems that <{\hspace{1ex} is not the proper way. – Diaa Dec 21 '19 at 21:41
  • 2ex between the 1st and 2nd columns, default separation between 2nd and 3rd and 1ex between 3rd and 4th? Or do I misunderstand? – Bernard Dec 21 '19 at 21:48
  • @Bernard I found out that <{\hspace{1ex}} doesn't have any effect on the output. So, I need to globally set the separation to be 0ex while injecting some space of 2ex between the first and the second columns; is this possible? – Diaa Dec 21 '19 at 21:50
  • I need to know if this will be valid for all columns separating space or only for the 1st and 2nd The solutions, if any, might differ. – Bernard Dec 21 '19 at 21:56
  • @Bernard Only the first and second columns will have this extra space. – Diaa Dec 21 '19 at 21:57
  • @Bernard For example, I need to fix this document to get rid of this ugly output. – Diaa Dec 21 '19 at 22:27
  • I've added an answer hopefully corresponding to what you want. Is it OK for you? – Bernard Dec 21 '19 at 22:40
  • If the unit is always the same, why not put it into the header of the corrensponding clumn instead of repeating it over and over? – leandriis Dec 21 '19 at 23:20
  • @leandriis It is just an MWE not my real document :) – Diaa Dec 21 '19 at 23:22

2 Answers2

5

For S type columns you can use the key table-space-text-post to enlarge them to the right instead of <{\hspace{1ex}} (which puts \hspace{1ex} after the contents of a cell):

\documentclass[border=5mm]{standalone}
\usepackage{tabularx,siunitx,multicol,ragged2e}
\usepackage[table]{xcolor}


\newcolumntype{Y}{>{\Centering}X}
\newcolumntype{L}{>{\RaggedRight}X}
\newcolumntype{R}{>{\RaggedLeft}X}

\begin{document}
    \setlength{\tabcolsep}{0pt}
    \rowcolors{2}{gray!20}{}
    \begin{tabularx}{1\linewidth}{
            >{\hsize=1.3\hsize}L<{\hspace{1ex}}
            >{\hsize=0.7\hsize}Y<{\hspace{1ex}}
            S[table-format=3.1, round-precision=1, table-column-width = 0.2\linewidth, table-number-alignment=right, table-space-text-post=\hspace{1ex}]
            s[table-unit-alignment = left, table-column-width = 0.2\linewidth]
        }
        Parameter & Symbol & \multicolumn{2}{c}{Value}\\
        Inertia  & $J_M$ & 0.6 & \kilogram\m\squared\\
        Inertia  & $J_M$ & 0.6 & \kilogram\m\squared\\
        Inertia  & $J_M$ & 0 & \kilogram\m\squared\\
    \end{tabularx}
\end{document}

enter image description here

Skillmon
  • 60,462
  • Off-topic question: how can I, for example, disable the alignment of the first number 0.6 with the other numbers and manually align it right/center/left according to the column margins? – Diaa Dec 21 '19 at 21:30
  • 1
    @Diaa you could use \multicolumn{1}{<align>}{0.6} or something like that. – Skillmon Dec 21 '19 at 22:06
3

For a simple solution, taking into account the last specifications, I would suggest for a simple solution to add a fixed width (2ex) empty column between the 1st and 2nd column. I've purposely added two (temporary) vertical rules to help visualise the width of this empty column:

\documentclass[border=5mm]{standalone}
\usepackage{tabularx, siunitx, multicol, ragged2e}
\usepackage[table]{xcolor}

\newcolumntype{Y}{>{\Centering}X}
\newcolumntype{L}{>{\RaggedRight}X}
\newcolumntype{R}{>{\RaggedLeft}X}

\begin{document}

   \rowcolors{2}{gray!20}{}
    \begin{tabularx}{\linewidth}{
            >{\hsize=1.3\hsize}L|@{}p{2ex}@{}|
            >{\hsize=0.7\hsize}Y
            S[table-format=3.1, round-precision=1, table-column-width = 0.2\linewidth, table-number-alignment=right]%
            s[table-unit-alignment = left, table-column-width = 0.2\linewidth]
        }
        Parameter & & Symbol & \multicolumn{2}{c}{Value}\\
        Inertia & & $J_M$ & 0.6 & \kilogram\m\squared\\
        Inertia & & $J_M$ & 0.6 & \kilogram\m\squared\\
    \end{tabularx}

\end{document}

enter image description here

Bernard
  • 271,350
  • Many thanks for your patience. It is exactly what I need. I am very frustrated that \hspace didn't work with me :). Thanks again – Diaa Dec 21 '19 at 22:47
  • 1
    I have another idea. I'll test it and add it as another solution if it works (the time to check for possible side effects). – Bernard Dec 21 '19 at 22:50
  • 1
    The problem is that whereas \rowcolor can take two optional arguments for the colour overhangs, it doesn't work with \rowcolors . So tere is a soltion if you do all your coloured rows individually. – Bernard Dec 21 '19 at 23:26
  • So, should this be reported to the package maintainer? – Diaa Dec 22 '19 at 09:28
  • 1
    Yes, why not? I looked at the code and didn't see how to patch the command to add these optional arguments (which only means it's too complex for me). – Bernard Dec 22 '19 at 09:57
  • I noticed something: does \rowcolors{2}{gray!20}{} mean start coloring gray!20 from the second row? If yes, why does it start from the first row? Do I miss something? O is it a valid question? – Diaa Dec 22 '19 at 23:05
  • Not quite: it means starting the colouring scheme from the 2nd tow, but even colours are in the last argument (no colour) . However, the grey colouring in the first row shouldn't happen, but I don't see why at the moment. – Bernard Dec 23 '19 at 00:35
  • I found the coloring issue is solved by this answer. – Diaa Dec 23 '19 at 22:10
  • I see. Very interesting – and subtle. Thank you for informing me! – Bernard Dec 23 '19 at 22:16