2

MWE:

\documentclass{scrreprt}

\usepackage{tabularx} 

\usepackage{booktabs}
\usepackage{threeparttable}
\usepackage{enumitem}

    \begin{document}

\begin{threeparttable}[!htbp]
        \begin{tabularx}{\linewidth}
        {
            >{\hsize=0.5\hsize}X
            >{\hsize=1\hsize}X
            >{\hsize=1\hsize}X
            >{\hsize=1\hsize}X
            >{\hsize=1.5\hsize}X
        }
        \toprule
        Head 1 & Head 2 & Head 3 & Head 4 & Head 5\\
        \midrule
        A 
        & 
        Some pretty long text that runs over several lines in this table cell 
        & 
        \begin{itemize}[leftmargin=*]
            \item Some pretty long text that runs over several lines 
            \item Some short text  
        \end{itemize}
        & 
        \begin{itemize}[leftmargin=*]
            \item Some short text 
            \item Some short text 
        \end{itemize}
        & 
        \begin{itemize}[leftmargin=*]
            \item Some even longer text that runs over several lines in this itemization within the last column of the table
            \item Some very long text that runs over several lines in this itemization within the last column of the table. I'm using it to fill up this cell.
        \end{itemize}
        \\
        \bottomrule
    \end{tabularx}
\end{threeparttable}

\end{document}

Why does the text in the last column not use the available width? When I adjust the width to anything larger than 1.5, the margin gets even larger, as if there is an "invisible" column next to the last one. What am I doing wrong?

enter image description here

7
  • 2
    The sum of the relative widths of the columns of type X must be equal to the number of columns of type X. In your table, the former is 4.5, whereas the latter is 5. E
    – Mico
    Commented Apr 11, 2020 at 9:25
  • 1
    If the first column only contain short text that does not need linebreaks, I would use {lXXXX} instead.
    – leandriis
    Commented Apr 11, 2020 at 9:27
  • 2
    Aside: the threeparttable enviroment does NOT take options à la [!htbp].
    – Mico
    Commented Apr 11, 2020 at 9:27
  • @Mico the width of the last column was 1.5, I played around with it and forgot to change it back before posting. Fixed it now in the MWE, the problem remains the same.
    – C.Carl
    Commented Apr 11, 2020 at 10:20
  • 1
    @AnneMorbach - I think I figured it out: because some of the X columns contain itemize-type lists, one actually has to write >{\hsize=...\hsize\linewidth=\hsize}X rather than just >{\hsize=...\hsize}X. (This issue is mentioned in section 4.3 of the user guide of the tabularx package.) I added the \linewidth=\hsize directive in my code more or less automatically, without actually thinking actively about this issue. For further information, see also my recent query on just this subject.
    – Mico
    Commented Apr 11, 2020 at 11:27

1 Answer 1

2

Some comments and suggestions:

  • In a tabularx environment, the sum of the relative widths of the columns of type X must be equal to the number of columns of type X. In your table, the former is 4.5, whereas the latter is 5. Moreover, since some of the columns of type X contain list-like environments, it's necessary to write

    >{\hsize=<x>\hsize\linewidth=\hsize}X
    

    rather than just

    >{\hsize=<x>\hsize}X
    

    when setting their relatie widths. This issue is mentioned in section 4.3, "Column widths", of the user guide of the tabularx package.

  • Given that the fifth column has far more material than any of the first four columns, it might make sense to assign a greater width to the final column. In the example below, the relative width of each of the first four columns is 0.85, whereas the relative width of the final column is 1.6. Note that 4*0.85+1.6=3.4+1.6=5, as required.

  • In addition, since the columns are all quite narrow, it's good idea to typeset their contents ragged-right rather than fully justified.

  • The threeparttable environment does not take options of the form [!htbp]. Since you're not really using the machinery of threeparttable environment (such as the \tnote macro and the tablenotes environment), I suggest you employ a table environment instead. Happily, the table environment does know what to do with [!htbp].

  • Since you're loading the enumitem package, I would also like to suggest that you set up a bespoke itemize-like environment that packs its contents tightly. In the code below, that environment is called myitemize. (You're obviously free to come up with a snazzier name.)

enter image description here

\documentclass{scrreprt}
\usepackage[english]{babel}
\usepackage{tabularx,ragged2e}
\newcolumntype{L}[1]{>{\RaggedRight\arraybackslash%
                       \hsize=#1\hsize\linewidth=\hsize}X}

\usepackage{booktabs,enumitem}
%% Create a bespoke itemize-like environment:
\newlist{myitemize}{itemize}{1}
\setlist[myitemize]{label=\textbullet,nosep,left=0pt,
          before=\begin{minipage}[t]{\hsize},
          after =\end{minipage}}

\begin{document}

\begin{table}[htbp]
\setlength\tabcolsep{4pt} % default: 6pt
\begin{tabularx}{\linewidth}
{@{} *{4}{L{0.85}} L{1.6} @{}}  % 4*0.85+1.6=5
\toprule
Head 1 & Head 2 & Head 3 & Head 4 & Head 5\\
\midrule
Some fairly short text
& 
Some pretty long text that runs over several lines in this table cell 
& 
\begin{myitemize}
    \item Some pretty long text that runs over several lines 
    \item Some short text  
\end{myitemize}
& 
\begin{myitemize}
    \item Some short text 
    \item Some short text 
\end{myitemize}
& 
\begin{myitemize}
    \item Some even longer text that runs over several lines in this itemization within the last column of the table
    \item Some very long text that runs over several lines in this itemization within the last column of the table. I'm using it to fill up this cell.
\end{myitemize}
\\
\bottomrule
\end{tabularx}
\end{table}

\end{document}

You must log in to answer this question.

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