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

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}
  • 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 3 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 3 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 4 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 4 at 16:29
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

  • 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 5 at 9:33
  • Thanks for showing this approach. Could you please explain the difference in interpretation between ‘\tabular*{\linewidth}’ and ’\tabular*{\columnwidth}’? – Diaa May 5 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 5 at 10:54

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.