2

I have a longtable and page headers which pick up the first and last item of the table on a page. The solution I achieved with pulling together various pieces of advice from e.g. here Incorrect running heads on last page of a long table and here longtable - how to put the first element on a page into the header but the result is not yet satisfactory; the margins for the header and the margins for the table do not agree.

enter image description here

I would prefer to make the header agree with the table (left and right margin) and cannot make the table narrower.

The MWE is:

\documentclass{scrbook}
\KOMAoptions{paper=     6.14in:9.21in,    
    BCOR=8mm,twoside,
    headinclude=false, footinclude=false, 
    headings=normal,
    titlepage=true,
    DIV=13,  % textbuecher 11 % textwidth 117
    fontsize=11pt,
}
\usepackage[german]{babel}  

\usepackage{booktabs,array,microtype, longtable}
\usepackage[automark,headsepline]{scrlayer-scrpage}

\rehead{\leftmark}  \rohead{\leftmark}
\lehead{\rightmark} \lohead{\rightmark}

\begin{document}

\newcommand{\dohang}{\hangindent1em\hangafter1 }


{\begin{scriptsize}
        \addtolength{\oddsidemargin}{-10mm}
        \addtolength{\evensidemargin}{-10mm}
        \begin{longtable}{@{}
                l
                >{\everypar{\dohang}\dohang\raggedright\arraybackslash}p{18mm} 
                >{\everypar{\dohang}\dohang\raggedright\arraybackslash}p{65mm} 
                >{\everypar{\dohang}\dohang\raggedright\arraybackslash}p{26mm}
                p{9mm}}
            {} & Lemma & Gedichtzeile & Gedichttitel & Index \\\\
            \endfirsthead 

        \markboth{Abendsäuseln}{Abendsäuseln}&Abendsäuseln & Eingelullt von Abendsäuseln, & Himmelsleiter & 5:1-3\\
        \markboth{abgedorrt}{abgedorrt}&abgedorrt & Doch endlich abgedorrt! & Am Sarg eines & 7:1-3\\
        \markboth{alt}{alt}&alt & Hockt vor der Tür und flickt die alten Schuh und wirkt und wirkt bis ans ende der Zeit. & Wie glänzt der helle & 2:1-14\\
        \markboth{alt}{alt}& & So ist die alte Eiche & Am Sarg eines & 7:1-2\\


        \end{longtable}
    \end{scriptsize}
}   
\end{document}

Any suggestion how to fix?

3

You tabular is wider than the linewidth and therefore overflows the right margin. You have to take into consideration the tabcolsep added between the columns when you calculate the column width. Your tabular is \linewidth wide, but since you have five columns, only \linewidth-10 tabcolsep is available for text inside the columns. Since I have used @{} to remove left and right side bearings (which each counts for 1 tabcolsep), I need to subtract in total eight additional tabcolseps.

The reason why you tabular seems to have been moved some millimetres to the right, is that the first l column adds a tabcolsep to the right. So even though the column is zero width, you get a tabcolsep between column 1 and 2, and it looks like the tabular does not line up with the left margin. Actually it does. Add add a \midrule, and you will see. Therefore, I have removed the tabcolsep by adding @{} to the right of the l column. In fact, this is exact the same problem you describe in your question: left alignment of longtable - unwanted minimal indent.

The @{} on after the last p-column removes the side bearings on the right side of the tabular, ensuring that right aligned or justified text will be set flush to the right margin. It will also add some extra space to text inside the column. If you do not have right aligned or justified text, you will not see the effect directly.

In addition, I suggest you calculate the column width relative to the linewidth. I loaded the package calc to make the calculation easier. You may do it without (use \dimexpr).

In your MWE, you use \setlength .... Those commands only move the text block 1 cm closer the inner margin, they do not add additional space for text. I have commented them out. I moved some other code around, too, but that does not matter. You may revert those changes.

Here is my MWE:

\documentclass{scrbook}
\KOMAoptions{paper=6.14in:9.21in,    
    BCOR=8mm,twoside,
    headinclude=false, footinclude=false, 
    headings=normal,
    titlepage=true,
    DIV=13,  % textbücher 11 % textwidth 117
    fontsize=11pt,
}
\usepackage[german]{babel}  

\usepackage{booktabs,array,microtype, longtable, calc}
\usepackage[automark,headsepline]{scrlayer-scrpage}

\rehead{\leftmark}  \rohead{\leftmark}
\lehead{\rightmark} \lohead{\rightmark}

\setlength\LTleft{0pt}
\setlength\LTright\fill

\newcommand{\dohang}{\hangindent1em\hangafter1}

\begin{document}

%\addtolength{\oddsidemargin}{-10mm}   % These two commands only move the textarea, they do not widen it.
%\addtolength{\evensidemargin}{-10mm}
\scriptsize % `longtable` should not be put in an environment
\begin{longtable}{@{}
                l@{}  % You need to remove the tabcolsep in a zero width column
                >{\everypar{\dohang}\dohang\raggedright\arraybackslash}p{2\linewidth/12-\tabcolsep} 
                >{\everypar{\dohang}\dohang\raggedright\arraybackslash}p{6.4\linewidth/12-2\tabcolsep} 
                >{\everypar{\dohang}\dohang\raggedright\arraybackslash}p{2.5\linewidth/12-2\tabcolsep}
                p{1.1\linewidth/12-\tabcolsep}@{}}  % You need to subtract the tabcolseps from tabular column width 
            {} & Lemma & Gedichtzeile & Gedichttitel & Index\\\midrule
            \endfirsthead 

        \markboth{Abendsäuseln}{Abendsäuseln}&Abendsäuseln & Eingelullt von Abendsäuseln, & Himmelsleiter & 5:1-3\\
        \markboth{abgedorrt}{abgedorrt}&abgedorrt & Doch endlich abgedorrt! & Am Sarg eines & 7:1-3\\
        \markboth{alt}{alt}&alt & Hockt vor der Tür und flickt die alten Schuh und wirkt und wirkt bis ans ende der Zeit. & Wie glänzt der helle & 2:1-14\\
        \markboth{alt}{alt}& & So ist die alte Eiche & Am Sarg eines & 7:1-2\\

        \end{longtable}
\normalsize

\end{document}

enter image description here

  • perfect! works exactly as intended. To help me understand: Could you explain what the @{} is doing at the last p ? - Thank you a lot! – user855443 Jul 15 at 9:37
  • 1
    @user855443 See updated answer – Sveinung Jul 15 at 9:49

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.