# Longtable goes over the header in landscape page using \linewidth to set table width

I am trying to make a longtable with the page width in landscape mode. To make it easier to set the column width, I defined a new column type, which is a [p] column but the size is a fraction of \linewidth. But when I compile, the table goes over the document header.

\documentclass[a4paper,twoside,12pt,notitlepage,openright]{article}

\usepackage{amsmath}
\usepackage[english]{babel}
\usepackage[hmargin=3.0cm,vmargin=3.6cm]{geometry} % setting marginals
\usepackage{fancyhdr,extramarks}  % header ja footer manipulation
\usepackage{times}  % to change font to times
\usepackage{setspace} % for linespacing

% Table
\usepackage{booktabs}
\usepackage{lscape}
\usepackage{afterpage}
\usepackage{longtable}
\usepackage{threeparttablex}
\newcommand{\mtnote}[1]{\textsuperscript{\TPTtagStyle{#1}}}
\usepackage{array}
\newcolumntype{L}[1]{>{\raggedright\let\newline\\\arraybackslash}p{#1\linewidth}}
\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash}p{#1\linewidth}}
\newcolumntype{R}[1]{>{\raggedleft\let\newline\\\arraybackslash}p{#1\linewidth}}
\usepackage{caption}
\captionsetup{width=\textwidth}

\singlespacing

\pagestyle{fancy}
\fancyhf{}% clearing the header and footer

\renewcommand\sectionmark[1]
{\markboth{\thesection\ #1}{}}         % section name to header
\renewcommand\subsectionmark[1]
{\markright{\thesubsection\ #1}}       % subsection name to header
\renewcommand{\footrulewidth}{0pt}      % no ruler between body and footer

\numberwithin{table}{section}       % table numbers with section numbers

\usepackage{lipsum}

\begin{document}

\section{Section 1}
\lipsum[1-2]
\the\textheight is the text height portrait mode

\afterpage{
\begin{landscape}
\singlespacing
\footnotesize
\setlength\LTcapwidth{\linewidth}  % default: 4in
%\setlength{\tabcolsep}{2pt}       % default: 6pt
\setlength\LTleft{0pt}            % default: \parindent
\setlength\LTright{3.6cm}           % default: \fill
\begin{ThreePartTable}
\begin{TableNotes} \footnotesize
\item [a] Something
\end{TableNotes}
\renewcommand*{\arraystretch}{1.4}
\begin{longtable}{L{.1} L{.14} L{.14} L{.1} L{.12} L{.14} L{.12} L{.14}}
%@{\extracolsep{\fill}}L{.1} L{.14} L{.14} L{.1} L{.12} L{.14} L{.12} L{.14}@{}
\caption{Table caption is also not very small and it seems to be places wrongly as well, like it is centered with the table that goes over the header here it is the linewidth which seems to be correct \the\linewidth} \label{table:articles} \\\toprule
Column 1 & Column 2 & Column 3 & Column 4 & Column 5 & Column 6 & Column 7 & Column 8  \\\midrule
\multicolumn{8}{@{}l}{Table \ref{table:articles} continued.}\\\toprule
Column 1 & Column 2 & Column 3 & Column 4 & Column 5 & Column 6 & Column 7 & Column 8  \\\midrule

\bottomrule
\endfoot
\bottomrule
\insertTableNotes
\endlastfoot

Some author name here 1 & Something goes here and it is quite big, it fills several lines in the table describing boring stuff  & The sizes vary for each column, sometimes there is more boring stuff and others & Some are usually short & So the column width is set to more or less fit these differences  & I describe some aspects of scientific articles & I dont understand why this doesnt work for me & It goes on for a few pages but here I added just two lines  \\

Some author name here 1 & Something goes here and it is quite big, it fills several lines in the table describing boring stuff  & The sizes vary for each column, sometimes there is more boring stuff and others & Some are usually short & So the column width is set to more or less fit these differences  & I describe some aspects of scientific articles & I dont understand why this doesnt work for me & It goes on for a few pages but here I added just two lines  \\
\end{longtable}
\end{ThreePartTable}
\end{landscape}
}

\end{document}


Take into account the value of \tabcolsep in your new column types definition: using this code
\newcolumntype{L}[1]{>{\raggedright\let\newline\\\arraybackslash}p{\dimexpr#1\linewidth-2\tabcolsep\relax}}

and likewise for R and C, solves the problem. Alternatively, you can use package ltablex, which brings the functionalities of longtable to tabularx.
• Thanks a lot, both solutions worked very well. Using itablexmakes it much easier though. – Mariana Jul 1 '18 at 11:57
• Bud did you use the X column type with ltablex? The advantage is you don't have to meddle with \tabcolsep – it's the package which takes care of that. – Bernard Jul 1 '18 at 12:06
• Yes, I used the X column type and it is much better because it sets the column width automatically. But for some reason the text was justified. I made new column type using \newcolumntype{Y}{>{\raggedright\arraybackslash}X} and now it works fine. I found it odd, because I thought Xtype was aligned left by default... – Mariana Jul 1 '18 at 12:27
• Note you also can have X columns of different sizes within a table, adding directives such as >{\hsize=1.4\hsize}, the condition being that such coefficients sum up to the total number of columns. – Bernard Jul 1 '18 at 12:50