1

I fit longtable to textwidth following fit longtable to textwidth

\documentclass{report}
\usepackage{longtable}
\usepackage{multirow}
\begin{document}

\begin{center}
  \begingroup
  \setlength\tabcolsep{4pt}
  \normalsize

  \setlength\LTcapwidth{\textwidth} % default: 4in (rather less than \textwidth...)                                                                                                                         

  \setlength\LTleft{0pt}            % default: \fill                                                                                                                                                        
  \setlength\LTright{0pt}
  \begin{longtable}{@{\extracolsep{\fill}}p{4.0cm}|rcc|rcc|}
    \multicolumn{1}{c|}{\multirow{2}{*}{\bf Sample}} & \multicolumn{3}{c|}{A}&\multicolumn{3}{c|}{B}\\
    \cline{2-7}  & a1 & b1 & c1 & a2 & b2 & c2 \\
  \end{longtable}
  \endgroup
\end{center}

\end{document}

In the output we see a white space inflicted on the cline: enter image description here

The problem has been mentioned in the comments of fit longtable to textwidth However, I decided to open a new question for the sake of clarity.

Maybe we can also use some other solution to fit longtable to textwidth?

2
  • not really related to longtable, that is how \extracolsep works it adds space, you could use \multicolumn{1} to add the | to the left of column 2 rather than the right of column1. Also note \bf has not been defined by default in LaTeX since the early 1990s. Jun 9, 2020 at 18:36
  • Depending on the actual contents of the table there might be alternative ways to ensure your table fints into the textwidth. Could you add some more information on the actual contents of the cells?
    – leandriis
    Jun 9, 2020 at 18:40

1 Answer 1

2

If you really must do this (generally forcing a table to be over-wide just separates the columns and makes the table harder to read) you could move the | to the left of the second column:

enter image description here

\documentclass{report}
\usepackage{longtable}
\usepackage{multirow}
\begin{document}

\begin{center}
  \begingroup
  \setlength\tabcolsep{4pt}
  \normalsize

  \setlength\LTcapwidth{\textwidth} % default: 4in (rather less than \textwidth...)                                                                                                                         

  \setlength\LTleft{0pt}            % default: \fill                                                                                                                                                        
  \setlength\LTright{0pt}
  \begin{longtable}{@{\extracolsep{\fill}}p{4.0cm}rcc|rcc|}
    \multicolumn{1}{c}{\multirow{2}{*}{\textbf{Sample}}} & \multicolumn{3}{|c|}{A}&\multicolumn{3}{c|}{B}\\
    \cline{2-7}  &\multicolumn{1}{|r}{a1} & b1 & c1 & a2 & b2 & c2 \\
  \end{longtable}
  \endgroup
\end{center}

\end{document}
2
  • Imagine 5 longtables with the same structure but with different data. Fitting them to a standard width is a way to make them look neat together.
    – Viesturs
    Jun 9, 2020 at 19:17
  • @Viesturs making them less readable but looking neat isn't always a good tradeoff. It's not always wrong but.... Also if you want that then it's better to force all the column widths to be the same, not just pad them all textwidth, so using fixed width p columns, or (as I give on an answer here somewhere) making them all use the same internal saved column width data from the aux file) would be better Jun 9, 2020 at 19:20

You must log in to answer this question.

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