# How to setup a longtable with booktabs and a colored (head) row

I'd like to make a longtable with the first (head) row colored in grey.

Since booktabs inserts white space below the \toprule and above the \midrule, i need to insert a colored rule:

\newcommand*{\belowrulesepcolor}[1]{%
\noalign{%
\kern-\belowrulesep
\begingroup
\color{#1}%
\hrule height\belowrulesep
\endgroup
}%
}
\newcommand*{\aboverulesepcolor}[1]{%
\noalign{%
\begingroup
\color{#1}%
\hrule height\aboverulesep
\endgroup
\kern-\aboverulesep
}%
}


This works well within the tabular environment. But if I use the longtable environment, the \hrule is getting too long.

Here is my MWE with a picture of the output:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage[T1]{fontenc}

\usepackage{geometry}
\geometry{paperwidth=5cm,paperheight=5cm}       % small page
\pagestyle{empty}

\usepackage{booktabs, longtable}
\usepackage[table]{xcolor}

\newcommand*{\belowrulesepcolor}[1]{%   fills the white space with a colored rule
\noalign{%
\kern-\belowrulesep
\begingroup
\color{#1}%
\hrule height\belowrulesep
\endgroup
}%
}
\newcommand*{\aboverulesepcolor}[1]{%
\noalign{%
\begingroup
\color{#1}%
\hrule height\aboverulesep
\endgroup
\kern-\aboverulesep
}%
}

\begin{document}

\centering

\begin{longtable}{lll}

\bottomrule
\endfoot
\bottomrule
\endlastfoot

A&B \\
A&B \\
A&B \\
A&B \\
A&B \\
A&B \\
A&B \\
A&B \\
A&B \\
A&B \\
A&B \\
A&B \\

\end{longtable}

\end{document}


I'm confused by the other answers. What is wrong with something much simpler? No extra packages needed

\documentclass{article}
\usepackage{geometry}
\geometry{paperwidth=5cm,paperheight=5cm}       % small page
\pagestyle{empty}

\usepackage{booktabs, longtable}
\usepackage[table]{xcolor}

\newcommand{\topline}{\arrayrulecolor{black}\specialrule{\heavyrulewidth}{\abovetopsep}{0pt}%
\arrayrulecolor{black}}
\arrayrulecolor{black}\specialrule{\lightrulewidth}{0pt}{0pt}%
\arrayrulecolor{white}\specialrule{\belowrulesep}{0pt}{0pt}%
\arrayrulecolor{black}}

\begin{document}

\centering

\begin{longtable}{ll}

\topline
Col1 & Col2 \\ \midline
\topline
Col1 & Col2 \\
\midline
\bottomrule
\endfoot
\bottomrule
\endlastfoot

A&B \\
A&B \\
A&B \\
A&B \\
A&B \\
A&B \\
A&B \\
A&B \\
A&B \\
A&B \\
A&B \\
A&B \\

\end{longtable}

\end{document}


• I normally try to avoid to put together a background from more then one piece: Depending on the viewer/zoom one often see small white lines between the pieces. If you look closely at your screenshots you can see them. – Ulrike Fischer Feb 19 '16 at 12:00
• Indeed. But these sorts of screen display artefacts are common at very low magnification, and will not appear in prints - but your point is taken. – Aubrey Blumsohn Feb 19 '16 at 12:12
• This is probably the most simple way. And as I'm writing my thesis right now, I don't have the time to learn much about TikZ ;) Like Aubrey said: The white lines don't appear in the document. Thank you all! One last thing: The default width of \toprule and \bottomrule in booktabs is \heavyrulewidth. Hence i've edited the 0.1em to \heavyrulewidth. – Dorian Feb 19 '16 at 12:35

I would use tikz to draw the background. Something like this (I didn't test thoroughly so perhaps some adjusting in your real case are needed):

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage[T1]{fontenc}

\usepackage{geometry}
\geometry{paperwidth=5cm,paperheight=5cm}       % small page
\pagestyle{empty}

\usepackage{booktabs}
\usepackage{longtable}
\usepackage[table]{xcolor}
\usepackage{tikz}
\usetikzlibrary{tikzmark}

\makeatletter
\newcommand\LastLTentrywidth{1em}
\newlength\longtablewidth
\setlength{\longtablewidth}{1in}
\newcommand\getlongtablewidth{%
\begingroup
\ifcsname LT@\roman{LT@tables}\endcsname
\global\longtablewidth=0pt
\@nameuse{LT@\roman{LT@tables}}%
\fi
\endgroup}
\tikz[overlay,remember picture]
\makeatother

\begin{document}

\centering

\begin{longtable}{lll}\noalign{\getlongtablewidth}

\bottomrule
\endfoot
\bottomrule
\endlastfoot

A&B \\
A&B \\
A&B \\
A&B \\
A&B \\
A&B \\
A&B \\
A&B \\
A&B \\
A&B \\
A&B \\
A&B \\

\end{longtable}

\end{document}


Here is a solution with the makecell package: it consists in setting the values of \above/belowrulesep to 0pt, replacing them with a \Gape command from makecell which uses the default values of \above/belowrulesep at appropriate places.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage[T1]{fontenc}

\usepackage{geometry}
\geometry{paperwidth=5cm,paperheight=5cm} % small page
\pagestyle{empty}

\usepackage{booktabs, longtable}
\usepackage[table]{xcolor}

\newlength\defaultaboverulesep
\setlength\defaultaboverulesep{\aboverulesep}
\newlength\defaultbelowrulesep
\setlength\defaultbelowrulesep{\belowrulesep}
\setlength\aboverulesep{0pt}
\setlength\belowrulesep{0pt}
\usepackage{makecell}

\begin{document}

\centering

\begin{longtable}{lll}

\toprule
\Gape[\defaultbelowrulesep][\defaultaboverulesep]{Col1} & Col2 \\
\midrule
\toprule
\bottomrule
\endfoot
\bottomrule
\endlastfoot

A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\
A & B \\

\end{longtable}

\end{document}