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.
\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}


longtableautomatically centers the table by default. This is configured by length registers\LTleftand\LTright. Therefore, environmentcenteraroundlongtableshould be removed. – Heiko Oberdiek Dec 25 '16 at 12:05