2

Admitted I handle thing poorly when it comes to working with table.

In my report I reached a point that I want to insert table (landscape) that would span multiple pages. So I used \longtable.

Formatting is the challenge:

  1. columns containing list poorly displayed
  2. had to use \multicolumn repeatedly to adjust cells on several rows
  3. page number is poorly placed.

Below is a MWE of the problem I faced.

\documentclass{report}
\usepackage[utf8]{inputenc}
\usepackage{booktabs}
\usepackage{pdflscape}
\usepackage{afterpage}
\usepackage{makecell}
\renewcommand{\theadfont}{\normalsize\bfseries}
\usepackage{tabularx}
\usepackage{blindtext}
\usepackage{eso-pic,graphicx}
\usepackage{longtable}
\begin{document}

\section{Introduction}
\blindtext
\newpage
\begin{landscape}
\begin{longtable}{lclll }
\toprule
            \thead{Algorithm}  & \thead{Type of Problem} & \thead{Advantages}  & \thead{Disadvantages}  & \thead{Common Application areas} \\
            \midrule
 Decision Trees &  \multicolumn{1}{c}{supervised/unsupervised} & \multicolumn{1}{l}{
 \begin{itemize}
     \item robust to outliers and missing values
     \item item 2
     \item item 3
 \end{itemize}} &list advantages & some application areas\\
 \midrule
\end{longtable}
\end{landscape}
\end{document}

enter image description here

1

2 Answers 2

2

Here is a solution using longtable and enumitem. In addition, I have defined a column that get rid of the space above the itemise environment by injecting a \@minipagetrue and align the cells at the top base line by enclosing the itemize environment in a \parbox. As a bonus(?), I could remove the \thead-commands and all \multicolumns:

Example 1

enter image description here

\documentclass{report}
\usepackage[utf8]{inputenc}
\usepackage{booktabs}
\usepackage{pdflscape}

\usepackage{array}
\usepackage{blindtext}
\usepackage{longtable}
\usepackage{enumitem}

\makeatletter
\newcolumntype{P}{>{\raggedright\@minipagetrue}p{\dimexpr(3.2cm+2\tabcolsep)}}
\makeatother

\begin{document}

\section{Introduction}
\blindtext
\newpage
\begin{landscape}
\begin{longtable}[c]{@{}lcPll@{}}
\toprule
   \bfseries Algorithm  & \bfseries Type of Problem & \bfseries Advantages  & \bfseries Disadvantages  & \bfseries Common Application areas \\
   \midrule
 Decision Trees &  supervised/unsupervised & \parbox[t]{3.2cm}{\begin{itemize}[itemsep=0pt,topsep=0pt, leftmargin=*]
     \item robust to outliers and missing values
     \item item 2
     \item item 3
 \end{itemize}} &list advantages & some application areas\\
 \midrule
\end{longtable}
\end{landscape}
\end{document}

Example 2

enter image description here

\documentclass{report}
\usepackage[utf8]{inputenc}
\usepackage{booktabs}
\usepackage{pdflscape}
\usepackage{array}
\usepackage{blindtext}
\usepackage{longtable}
\usepackage{enumitem}

\makeatletter
\newcolumntype{P}{>{\raggedright\arraybackslash\@minipagetrue}p{\dimexpr(4.5cm+2\tabcolsep)}}
\newcolumntype{O}{>{\centering\arraybackslash}p{3cm}}
\makeatother

\begin{document}

\section{Introduction}
\blindtext
\newpage
\begin{landscape}
\begin{longtable}[c]{@{}lOPll@{}}
\toprule
   \bfseries Algorithm  & \bfseries Type of Problem & \bfseries Advantages  & \bfseries Disadvantages  & \bfseries Common Application areas \\
   \midrule
 Decision Trees &  supervised/ unsupervised & \parbox[t]{\linewidth}{\begin{itemize}[itemsep=0pt,topsep=0pt, leftmargin=*]

     \item robust to outliers and missing values
     \item item 2
     \item item 3
 \end{itemize}} &list advantages & some application areas\\
 \midrule
\end{longtable}
\end{landscape}
\end{document}
5
  • Yes, this solution works on first attempt. How do I adjust the width of Advantage column? Column in the middle.
    – arilwan
    Nov 21, 2019 at 21:49
  • 1
    @arilwan Change the width in the definition of the P-column (3.2 cm) and in the \parbox. Those two numbers has to be equal. However, 3.2 cm is maximum width before you get overfull hbox warnings. If you prefer a wider column, you need to shorten other columns. For example, column two can be converted to a p-column and the cell content broken into two lines.
    – Sveinung
    Nov 21, 2019 at 21:52
  • 1
    @arilwan I have included a new example how to manipulate the column width. If you were able to use xltabular, you could have used X-column instead of p-column, and the columns would have adapted automatically.
    – Sveinung
    Nov 21, 2019 at 22:06
  • Thank you all, you have been of great help. I go fro Example 2.
    – arilwan
    Nov 21, 2019 at 22:17
  • 1
    @arilwan I discovered that you may set the width of the \parbox to \linewidth inside the table, and it will adapt to the column width.
    – Sveinung
    Nov 21, 2019 at 22:36
1

I propose this solution, based on a redefinition of tabularx, the xltabular package, which brings the functionalities of longtable totabularx, andenumitem`. It is far from perfect, athe first two column heads are not vertically aligned, for a reason I don't see.

\documentclass{report}
\usepackage[utf8]{inputenc}
\usepackage{geometry}
\usepackage{booktabs}
\usepackage{pdflscape}
\usepackage{afterpage}
\usepackage{makecell}
\renewcommand{\theadfont}{\normalsize\bfseries}
\usepackage{tabularx}
\usepackage{blindtext}
\usepackage{eso-pic,graphicx}
\usepackage{longtable, xltabular}
\usepackage{ragged2e, enumitem}

\begin{document}

\section{Introduction}
\blindtext
\newpage
\begin{landscape}
\renewcommand{\tabularxcolumn}[1]{>{\RaggedRight\arraybackslash}m{#1}}
\setlist[itemize]{wide=0pt, leftmargin=*, nosep, before =\vspace*{-\baselineskip}, after=\vspace*{-\baselineskip}}
\begin{xltabular}{\linewidth}{@{}lc *{3}{X}@{}}
\toprule
            \thead{Algorithm} & \thead{Type of Problem} & \thead[l]{Advantages} & \thead[l]{Disadvantages}
             & \thead{Common Application\\ areas } \\
            \midrule
 Decision Trees & \multicolumn{1}{c}{supervised/unsupervised} &
 \begin{itemize}
     \item robust to outliers and missing values
     \item item 2
     \item item 3
 \end{itemize}
 &list advantages & some application areas\\
 \midrule
\end{xltabular}
\end{landscape}

\end{document} 

enter image description here

6
  • Thank you for your answer. How do I format the page number, as you can see from your answer, it is showing as on portrait orientation.
    – arilwan
    Nov 21, 2019 at 19:35
  • 2
    Yesz, but that is normal: it is just a convention the reader has to turn the page to read very wide tables, but he/she reads the table, not the page number, nor the headers.
    – Bernard
    Nov 21, 2019 at 19:41
  • One more thing please, I run into issues with xltabular package, using overleaf :! LaTeX Error: File xltabular.sty not found. Type X to quit or <RETURN> to proceed, or enter new name. (Default extension: sty)
    – arilwan
    Nov 21, 2019 at 21:15
  • Overleaf does not have this package installed? It's a comparatively recent package (2 years or so), but that is strange… In this case, you can try loading the ltablex package (xltabular is built on this one) and replace the xltabular environment with tabularx. It might be useful to add the keepXColumns directive.
    – Bernard
    Nov 21, 2019 at 21:21
  • 1
    For tabulary, I don't know, bur for tabularx, there should be no problem since it loads it via ltablex.
    – Bernard
    Nov 21, 2019 at 21:29

You must log in to answer this question.

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