1

I have the same problem as in Whitespaces in tabularx added after \extracolsep, but the solution does not work for me.

\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{booktabs}

\begin{document}
\noindent
\hspace*{\fill}
\begin{table*}[ht!]
\begin{tabular*}{\textwidth}{l @{\extracolsep{\fill}} llllll}
\toprule \rowcolor{light-gray} 
Test  & Test & \begin{tabular}[c]{@{}l@{}}Test \\ Test2 \end{tabular}  & \begin{tabular}[c]{@{}l@{}}Test \\ side\end{tabular} & \begin{tabular}[c]{@{}l@{}}Test \\ Test2\end{tabular} & \begin{tabular}[c]{@{}l@{}}Test \\ Test2\end{tabular}&  \begin{tabular}[c]{@{}l@{}}Test \\ Test2 \end{tabular} \\ \midrule
1 & X   & X     & X & 0  & X         & X           \\ \bottomrule
\end{tabular*}
\end{table*}
\hspace*{\fill}

\end{document}

As the title is a bit too long so I divided into two rows. The output is like this:here

Please can anyone help how can I fill the white spaces.

P.S: replacing tabular* with tabularx produces the same output.

11
  • 3
    your title asks about tabularx but your example uses tabular* which is a completely different environment. Which do you want to ask about? Commented Jul 31, 2018 at 12:56
  • 1
    your example produces the error ! Undefined control sequence. l.9 \toprule Commented Jul 31, 2018 at 12:58
  • @davidcarlisle edited. it's tabular not tabularx, get confused with another table myself. but the package behaves quite the same
    – Sharah
    Commented Jul 31, 2018 at 13:08
  • 1
    white space is introduced by @{\extracolsep{\fill}} , i suggest you to use tabularx .
    – Zarko
    Commented Jul 31, 2018 at 13:15
  • 1
    How (or where) are \belowrulesepcolor and \aboverulesepcolor defined? How or where is the color light-grey defined? Why does your contain an \end{table*} instruction without a corresponding \begin{table*} instruction? Are you really just asking us to perform basic debugging on your code?
    – Mico
    Commented Jul 31, 2018 at 13:25

2 Answers 2

1

The tabularx package with a modified column header definition can help:

\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{tabularx}
\usepackage{booktabs}

\colorlet{light-gray}{gray!35}

\begin{document}

\begin{table}
        \begin{tabularx}{\textwidth}{l*6X}
        \toprule
        \rowcolor{light-gray}
        Test  & Test & \begin{tabular}[c]{@{}l@{}}Test \\ Test2 \end{tabular}  & \begin{tabular}[c]{@{}l@{}}Test \\ side\end{tabular} & \begin{tabular}[c]{@{}l@{}}Test \\ Test2\end{tabular} & \begin{tabular}[c]{@{}l@{}}Test \\ Test2\end{tabular}&  \begin{tabular}[c]{@{}l@{}}Test \\ Test2 \end{tabular} \\
        \midrule
        1 & X   & X     & X & 0  & X         & X           \\
        \bottomrule
    \end{tabularx}
\end{table}

% Long column headers should not have the 'X' column type
\begin{table}
        \begin{tabularx}{\textwidth}{XXlXXXX}
        \toprule
        \rowcolor{light-gray}
        Test  & Test & \begin{tabular}[c]{@{}l@{}}A long title \\ Another long title \end{tabular}  & \begin{tabular}[c]{@{}l@{}}Test \\ side\end{tabular} & \begin{tabular}[c]{@{}l@{}}Test \\ Test2\end{tabular} & \begin{tabular}[c]{@{}l@{}}Test \\ Test2\end{tabular}&  \begin{tabular}[c]{@{}l@{}}Test \\ Test2 \end{tabular} \\
        \midrule
        1 & X   & X     & X & 0  & X         & X           \\
        \bottomrule
    \end{tabularx}
\end{table}

\end{document}

enter image description here enter image description here

7
  • i changed my tabular* to tabularx and it does not change the white spaces in between.
    – Sharah
    Commented Jul 31, 2018 at 13:26
  • @Sharah I had to change more than that. See \begin{tabularx}{\textwidth}{l*6{X}}
    – chrisma
    Commented Jul 31, 2018 at 13:31
  • my apologies, just noticed the recent edits. it works well for me now, although the spacing in between the column are not as nice as before -- columns with longer title will look awkward, i might need to define the width manually unlike before
    – Sharah
    Commented Jul 31, 2018 at 13:34
  • @zarko indeed, thank you. your solution was to change tabular* to tabularx, which does not make a difference to my original code. chrisma's colution changed more than that, thus solving the original problem.
    – Sharah
    Commented Jul 31, 2018 at 13:42
  • @Sharah I've edited the answer to include an example with longer column titles.
    – chrisma
    Commented Jul 31, 2018 at 13:51
2

Don't mix-and-(mis)match visual metaphores: If you wish to use color (or grayscale) to emphasize the header row, don't use \toprule and \midrule at all. Conversely, if you prefer to use \toprule and \midrule, don't add color to the header row. Recall the time-tested mantra of (successful) designers and artists: "Less is more."

enter image description here

\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{booktabs,tabularx,ragged2e}
\newcolumntype{C}{>{\Centering\arraybackslash}X}
\newcommand{\mycell}[1]{%
    \begin{tabular}{@{}c@{}} \strut #1\strut \end{tabular}}

\begin{document}
\setlength\extrarowheight{2pt}
\noindent       
    \begin{tabularx}{\textwidth}{*{7}{C}}
    \rowcolor{lightgray}
    Test  & Test & \mycell{Test \\ Test2}   
                 & \mycell{Test \\ side} 
                 & \mycell{Test \\ Test2} 
                 & \mycell{Test \\ Test2}
                 & \mycell{Test \\ Test2} \\
    1 & X & X & X & 0 & X & X \\ 
    \bottomrule
    \end{tabularx}
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .