3

For the following, why did tabularx decide to break the second column cell entry instead of reducing the separation of the columns?

Additionally, how can I make \rowstyle{\bfseries} work to bold any cell content whether it is text or math?

\documentclass[a4paper]{article}
\usepackage{booktabs,tabularx,mathtools,siunitx,ragged2e}
\begin{document}

    \newcolumntype{b}{>{\RaggedRight\hsize=1.5\hsize$}X<{$}}
    \newcommand{\heading}[1]{\multicolumn{1}{c}{#1}}
    \newcolumntype{m}{>{\RaggedLeft\hsize=0.5\hsize}X}
    \newcommand{\rowstyle}[1]{\gdef\currentrowstyle{#1}#1\ignorespaces}

    \begin{tabularx}{\linewidth}{@{}bm*{8}{%
                S[table-format=2.2,round-precision=2,round-mode=places,
                round-integer-to-decimal=true]%
            }@{}}
        \toprule
        \rowstyle{\bfseries}
        & $\delta$ [deg] & \heading{5} & \heading{10} & \heading{15} & \heading{20} & \heading{25} & \heading{30} & \heading{35} & \heading{40} \\
        \midrule
        \Gamma = x^2 & $\beta$ [deg] & 1 & 1 & 1 & 18 & 21 & 46 & 72 & 63 \\
        \bottomrule
    \end{tabularx}

\end{document}

enter image description here

Diaa
  • 9,599

2 Answers2

7

tabularx is completely unsuitable for numeric data tables really.

To answer your question it is important to realize that tabularx never adjusts inter-column space (tabular* does that) and never looks at the content of any cell in the table. It simply makes each X column into a p{...} column, choosing the value to use for ... so the table ends up at the specified total width. As such the only thing tabularx controls is the target width for line breaking within a cell, and in a table of numeric values there is no line breaking.

If you want adjustment of inter-columns space, and no line breaking then tabular* is what you want:

enter image description here

\documentclass[a4paper]{article}
\usepackage{booktabs,tabularx,mathtools,siunitx,ragged2e}
\begin{document}

    \newcolumntype{b}{>{\RaggedRight\hsize=1.5\hsize$}X<{$}}
    \newcommand{\heading}[1]{\multicolumn{1}{c}{#1}}
    \newcolumntype{m}{>{\RaggedLeft\hsize=0.5\hsize}X}
    \newcommand{\rowstyle}[1]{\gdef\currentrowstyle{#1}#1\ignorespaces}

\noindent
\begin{tabular*}{\linewidth}{
@{\extracolsep{0pt plus 5pt minus 3pt}}
>{$}l<{$}
l
*{8}{%
                S[table-format=2.2,round-precision=2,round-mode=places,
                round-integer-to-decimal=true]%
            }@{}}
        \toprule
        & $\delta$ [deg] & \heading{5} & \heading{10} & \heading{15} & \heading{20} & \heading{25} & \heading{30} & \heading{35} & \heading{40} \\
        \midrule
        \Gamma = x^2 & $\beta$ [deg] & 1 & 1 & 1 & 18 & 21 & 46 & 72 & 63 \\
        \bottomrule
    \end{tabular*}

\end{document}
Mico
  • 506,678
David Carlisle
  • 757,742
  • Many thanks for your explanation. I would be grateful if you could help me understand what @{\extracolsep{0pt plus 5pt minus 3pt}} means. Additionally, could you please give me a hint of why \rowstyle fails in my code? – Diaa May 03 '19 at 20:30
  • 1
    @Diaa tabular* meets the target width by adding the \extracolsep space between columns, normally you can just use \fill but here the table is too wide so actually you want to add negative space. there I added a space with natural width 0pt that can stretch to 5pt or shrink to -3pt which allows the table to be squeezed up a bit and fit. – David Carlisle May 03 '19 at 20:34
  • After some experimenting, I found that @{\extracolsep{0pt plus 5pt minus 3pt}} only affects locally the inter-column spacing based on where I typed it among the columns alignment parameter. If what I found is true, is it possible to make this command affect globally all the columns without having to type manually? – Diaa May 04 '19 at 15:55
  • 1
    @Diaa sorry i don't understand that comment. \extracolsep affects all columns from the point it is set within that table. – David Carlisle May 04 '19 at 16:29
  • Is it possible to have tabularx with different column separations? For example, in my question, 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:48
  • 1
    @Diaa you can set \extracolsep as many times as you link between any columns, also add additional space with @\hspace{2cm} or several other possibilities (basically all the features of a standard tabular* are available in tabularx) – David Carlisle Dec 21 '19 at 21:51
  • There is a problem with colored rows when using @\hspace{5ex} between the first two columns, this document has this ugly output. – Diaa Dec 21 '19 at 22:12
  • the tabularx document shows how to use the overhang arguments to span across @ – David Carlisle Dec 21 '19 at 22:13
  • Thanks for your patience, but I went through the manual, and my lack of enough knowledge didn't help me know how to do it. So, I would be grateful if you could refer me to a question addressing the usage of @{\hspace{}} and/or \extracolsep{} for tabularx while preserving the colored rows. – Diaa Dec 21 '19 at 22:21
2

I'd use tabular* like David, but in a different way.

\documentclass[a4paper]{article}
\usepackage{booktabs,mathtools,siunitx}

\begin{document}

\begin{table}[htp]

% initialization for this table
\newcommand{\heading}[1]{\multicolumn{1}{c}{\bfseries\boldmath#1}}
\setlength{\tabcolsep}{0pt} % let TeX compute
\sisetup{
  table-format=2.2,
  round-precision=2,
  round-mode=places,
  round-integer-to-decimal=true,
}

\begin{tabular*}{\columnwidth}{@{\extracolsep{\fill}} *{2}{l} *{8}{S} }
\toprule
& \heading{$\delta$ [deg]}
& \heading{5} & \heading{10} & \heading{15} & \heading{20}
& \heading{25} & \heading{30} & \heading{35} & \heading{40} \\
\midrule
$\Gamma = x^2$ & $\beta$ [deg] & 1 & 1 & 1 & 18 & 21 & 46 & 72 & 63 \\
\bottomrule
\end{tabular*}

\end{table}

\end{document}

enter image description here

egreg
  • 1,121,712
  • 1
    yes well that's probably what I'd do too, or more likely just use tabular and set tabcolsep to the right value calculated from an overfull box message, but I wanted to demonstrate the shrink possibility as there are not many examples of that on site (or anywhere) – David Carlisle May 05 '19 at 09:33
  • Thanks for showing this approach. Could you please explain the difference in interpretation between ‘\tabular{\linewidth}’ and ’\tabular{\columnwidth}’? – Diaa May 05 '19 at 10:51
  • 1
    @Diaa \linewidth stores the length of lines in list environments. It may coincide with \columnwidth. It mostly depends on where you want to set your table. If in a table* environment in a two-column document, \textwidth should be used instead. – egreg May 05 '19 at 10:54