0

The following is a mwe highlighting the issue of an extra white space in the header of a longtable I am working on. This occurs primarily due to @{\extracolsep{\fill}} if I am not mistaken.

enter image description here

\documentclass[letterpaper]{article}
\usepackage[letterpaper,tmargin=2cm,bmargin=2.5cm,lmargin=2.5cm,rmargin=2.5cm]{geometry}
\usepackage{booktabs,array,longtable}
\usepackage[dvipsnames,table]{xcolor}
\usepackage{enumitem}
\begin{document}
\renewcommand{\labelenumi}{\bfseries Unit\ \arabic{enumi}.}
\renewcommand{\arraystretch}{1.5}
\setlength\LTleft{0pt}%
\setlength\LTright{0pt}%
\begin{longtable}{@{\extracolsep{\fill}}|c|>{\centering\arraybackslash}p{8.5em}|>{\raggedright\arraybackslash}p{.5\textwidth}|p{6.5em}|@{}}
    \hline
        \rowcolor{gray!10}\textbf{text} & \textbf{text} & \textbf{text} & \textbf{text}\tabularnewline%
    \hline
    \endfirsthead
    \hline
        \rowcolor{gray!10}\textbf{text} & \textbf{text} & \textbf{text} & \textbf{text}\tabularnewline%
    \hline
    \endhead
    %\hline
    %--------------------------------------------- Week 1-3 ---------------------------------------------
    text 
    & 
    text
    &
    text
    & 
    text\tabularnewline
    \hline%
\end{longtable}
\end{document}

Any suggestions on how to remove that white space or at least what causes it?

  • 1
    white spaces are caused by @{\extracolsep{\fill}. don't use it. see, if ltablex is acceptable. in tabularx you not need to stretch columns apart. – Zarko Aug 1 '17 at 19:32
  • @Zarko I don't mind other solutions, but I am also interested in a fix for the current issue. – azetina Aug 1 '17 at 19:37
  • 1
    as far as know,there is not other fix as don't use such approach to stretching table to text width. in your case (maybe i'm wrong) is only way to redefine column width such, that the table width become equal text width. in this you can use \dimexpr, consider \tabcolsep and \arrayrulewidth. i'm pretty sure, that such solution you can find on the site. – Zarko Aug 1 '17 at 19:43
2

see, if the following solution is fine to you:

\documentclass[letterpaper]{article}
\usepackage[showframe, % only for see if table width fit text width               
           letterpaper,
           tmargin=2cm,bmargin=2.5cm,lmargin=2.5cm,rmargin=2.5cm]{geometry}
\usepackage{array, booktabs, longtable}
\usepackage[dvipsnames,table]{xcolor}
\usepackage{enumitem}

\begin{document}
\renewcommand{\labelenumi}{\bfseries Unit\ \arabic{enumi}.}
\renewcommand{\arraystretch}{1.5}
\begin{longtable}{|>{\centering}p{\dimexpr0.1\linewidth-2\tabcolsep-1.25\arrayrulewidth\relax}
                  |>{\centering}p{\dimexpr0.2\linewidth-2\tabcolsep-1.25\arrayrulewidth\relax}
                  |>{\raggedright}p{\dimexpr.5\textwidth-2\tabcolsep-1.25\arrayrulewidth\relax}
                  | p{\dimexpr0.2\linewidth-2\tabcolsep-1.25\arrayrulewidth\relax}|}
    \hline
        \rowcolor{gray!10}\textbf{text} & \textbf{text} & \textbf{text} & \textbf{text}\tabularnewline%
    \hline
    \endfirsthead
    \hline
        \rowcolor{gray!10}\textbf{text} & \textbf{text} & \textbf{text} & \textbf{text}\tabularnewline%
    \hline
    \endhead
    text
    &
    text
    &
    text
    &
    text\tabularnewline
    \hline%
\end{longtable}
\end{document}

enter image description here

i didn't bother with exact recalculating of columns' widths, but this should not be a big deal :)

2

To achieve a longtable exactly \textwidthwide, I suggest you load ltablex, which brings the functionalities of longtable to tabularx. I took the liberty to simplify your preamble, in particular you don't have to load array,longtable nor tabularx since ltablex does it for you :

\documentclass[letterpaper]{article}
\usepackage[tmargin=2cm,bmargin=2.5cm, hmargin=2.5cm, showframe]{geometry}
\usepackage{booktabs,ltablex}
\usepackage[dvipsnames,table]{xcolor}
\usepackage{enumitem}

\begin{document}

\renewcommand{\labelenumi}{\bfseries Unit\ \arabic{enumi}.}
\renewcommand{\arraystretch}{1.5}
\keepXColumns
\begin{tabularx}{\linewidth}{|c|>{\centering\arraybackslash}p{8.5em}|>{\raggedright\arraybackslash}X|p{6.5em}|@{}}
    \hline
        \rowcolor{gray!10}\textbf{text} & \textbf{text} & \textbf{text} & \textbf{text}\tabularnewline%
    \hline
    \endfirsthead
    \hline
        \rowcolor{gray!10}\textbf{text} & \textbf{text} & \textbf{text} & \textbf{text}\tabularnewline%
    \hline
    \endhead
    %\hline
    %--------------------------------------------- Week 1-3 ---------------------------------------------
    text
    &
    text
    &
    text
    &
    text \\
    \hline%
\end{tabularx}

\end{document} 

enter image description here

  • Appreciate it. Thanks. I will explore your solution a bit more. – azetina Aug 2 '17 at 0:12
  • @azetina: The main interest of tabularx is you don't have to make width computations yourself. – Bernard Aug 2 '17 at 0:13
  • I agree. Duly noted. Am still old fashioned :) – azetina Aug 2 '17 at 0:15

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.