4

When I color the header of a longtable, the color extends the table. This does not happen, when I use a normal table. I cannot figure out why this happens, and more important, how to solve it.

A small example of the code is shown, and a picture of the problem.enter image description here

\documentclass{report}

\usepackage[english]{babel}
\usepackage{lmodern}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{booktabs} 

\usepackage{longtable}
\usepackage[table]{xcolor}
\usepackage{float}

\definecolor{Gray}{gray}{0.95}

\def\ColorTabelHeader{Gray}

\newcommand*{\BeginHeadcolor}{%
\toprule
\rowcolor{\ColorTabelHeader}
  \noalign{\kern-\belowrulesep \begingroup
  \color{\ColorTabelHeader} \hrule height\belowrulesep
  \endgroup }}
\newcommand*{\EndHeadcolor}{%
  \noalign{\begingroup \color{\ColorTabelHeader}
  \hrule height\aboverulesep \endgroup
  \kern-\aboverulesep } \midrule}

\begin{document}

\begin{table}[H] 
\caption{A simple table example}
  \begin{center}
    \begin{tabular}{cccc}
    \BeginHeadcolor
\textbf{First entry} & \textbf{Second entry} & \textbf{Third entry} & \textbf{Fourth entry} \\
\rowcolor{\ColorTabelHeader}
\textbf{First entry} & \textbf{Second entry} & \textbf{Third entry} & \textbf{Fourth entry} \\ 
    \EndHeadcolor
1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3  & 4 \\
1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3  & 4 \\
1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3  & 4 \\
    \bottomrule
    \end{tabular} 
   \label{tab:oversigt_Forsogelementer}
  \end{center}
\end{table}


\begin{center}
\begin{longtable}{cccc}
\caption{A simple longtable example}\\
\BeginHeadcolor
\textbf{First entry} & \textbf{Second entry} & \textbf{Third entry} & \textbf{Fourth entry} \\
\rowcolor{\ColorTabelHeader}
\textbf{First entry} & \textbf{Second entry} & \textbf{Third entry} & \textbf{Fourth entry} \\
\EndHeadcolor
\endfirsthead
\multicolumn{4}{c}%
{\tablename\ \thetable\ -- \textit{Continued from previous page}} \\
\BeginHeadcolor
\textbf{First entry} & \textbf{Second entry} & \textbf{Third entry} & \textbf{Fourth entry} \\
\rowcolor{\ColorTabelHeader}
\textbf{First entry} & \textbf{Second entry} & \textbf{Third entry} & \textbf{Fourth entry} \\
\EndHeadcolor
\endhead
\hline \multicolumn{4}{r}{\textit{Continued on next page}} \\
\endfoot
\midrule
\endlastfoot
1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3  & 4 \\
1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3  & 4 \\
1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3  & 4 \\
1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3  & 4 \\
1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3  & 4 \\
1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3  & 4 \\
\end{longtable}
\end{center}
\end{document}
Stefan Pinnow
  • 29,535
Morten
  • 309
  • 3
  • 10
  • 1
    BTW, longtable automatically centers the table by default. This is configured by length registers \LTleft and \LTright. Therefore, environment center around longtable should be removed. – Heiko Oberdiek Dec 25 '16 at 12:05
  • Thanks for the input. I did not knew that. Always great to learn some more. – Morten Dec 25 '16 at 12:19

1 Answers1

2

The \hrule in the longtable goes from the left margin to the right margin, because it is not set inside the alignment. The longtable is processed in chunks to support page breaking.

The package redefines \hline, which suffers from the same problem, because it internally also used \hrule. The trick, which is used there, is to make a full row across all columns. The number of columns is available in macro \LT@cols.

\LTBeginHeadColor and \LTEndHeadColor for use in longtable:

\makeatletter
\newcommand*{\LTBeginHeadcolor}{%
  \toprule
  \multispan\LT@cols
    \vadjust pre{\kern-\belowrulesep}%
    \begingroup
      \color{\ColorTabelHeader}%
      \leaders\hrule height\belowrulesep\hfill
    \endgroup
  \cr
  \rowcolor{\ColorTabelHeader}%
}
\newcommand*{\LTEndHeadcolor}{%
  \multispan\LT@cols
    \begingroup
      \color{\ColorTabelHeader}%
      \leaders\hrule height\aboverulesep\hfill
    \endgroup
  \cr
  \noalign{%
    \kern-\aboverulesep
  }%
  \midrule
}
\makeatother

Full example:

\documentclass{report}

\usepackage[english]{babel}
\usepackage{lmodern}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{booktabs} 

\usepackage{longtable}
\usepackage[table]{xcolor}
\usepackage{float}

\definecolor{Gray}{gray}{0.95}

\def\ColorTabelHeader{Gray}

\newcommand*{\BeginHeadcolor}{%
  \toprule
  \rowcolor{\ColorTabelHeader}%
  \noalign{%
    \kern-\belowrulesep
    \begingroup
      \color{\ColorTabelHeader}\hrule height\belowrulesep
    \endgroup
  }%
}
\newcommand*{\EndHeadcolor}{%
  \noalign{%
    \begingroup
      \color{\ColorTabelHeader}%
      \hrule height\aboverulesep
    \endgroup
    \kern-\aboverulesep
  }%
  \midrule
}
\makeatletter
\newcommand*{\LTBeginHeadcolor}{%
  \toprule
  \multispan\LT@cols
    \vadjust pre{\kern-\belowrulesep}%
    \begingroup
      \color{\ColorTabelHeader}%
      \leaders\hrule height\belowrulesep\hfill
    \endgroup
  \cr
  \rowcolor{\ColorTabelHeader}%
}
\newcommand*{\LTEndHeadcolor}{%
  \multispan\LT@cols
    \begingroup
      \color{\ColorTabelHeader}%
      \leaders\hrule height\aboverulesep\hfill
    \endgroup
  \cr
  \noalign{%
    \kern-\aboverulesep
  }%
  \midrule
}
\makeatother

\begin{document}

\begin{table}[H] 
\caption{A simple table example}
  \begin{center}
    \begin{tabular}{cccc}
    \BeginHeadcolor
\textbf{First entry} & \textbf{Second entry} & \textbf{Third entry} & \textbf{Fourth entry} \\
\rowcolor{\ColorTabelHeader}
\textbf{First entry} & \textbf{Second entry} & \textbf{Third entry} & \textbf{Fourth entry} \\ 
    \EndHeadcolor
1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3  & 4 \\
1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3  & 4 \\
1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3  & 4 \\
    \bottomrule
    \end{tabular} 
   \label{tab:oversigt_Forsogelementer}
  \end{center}
\end{table}


\begin{longtable}{cccc}
  \caption{A simple longtable example}\\
  \LTBeginHeadcolor
  \textbf{First entry} & \textbf{Second entry} & \textbf{Third entry} & \textbf{Fourth entry} \\
  \rowcolor{\ColorTabelHeader}
  \textbf{First entry} & \textbf{Second entry} & \textbf{Third entry} & \textbf{Fourth entry} \\
  \LTEndHeadcolor
\endfirsthead
  \multicolumn{4}{c}%
  {\tablename\ \thetable\ -- \textit{Continued from previous page}} \\[\aboverulesep]
  \LTBeginHeadcolor
  \textbf{First entry} & \textbf{Second entry} & \textbf{Third entry} & \textbf{Fourth entry} \\
  \rowcolor{\ColorTabelHeader}
  \textbf{First entry} & \textbf{Second entry} & \textbf{Third entry} & \textbf{Fourth entry} \\
  \LTEndHeadcolor
\endhead
  \midrule
  \multicolumn{4}{r}{\textit{Continued on next page}} \\
\endfoot
  \midrule
\endlastfoot
  1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3  & 4 \\
  1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3  & 4 \\
  1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3  & 4 \\
  1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3  & 4 \\
  1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3  & 4 \\
  1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3  & 4 \\
\end{longtable}
\end{document}

Page 1 Page 2

Heiko Oberdiek
  • 271,626