2

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
  }%
}

\definecolor{headcolor}{gray}{0.8}


\begin{document}

\centering

\begin{longtable}{lll}

\toprule \belowrulesepcolor {headcolor}                         
\rowcolor{headcolor}
Col1 & Col2 \\ \aboverulesepcolor{headcolor} \midrule \endfirsthead
\toprule \belowrulesepcolor {headcolor}                         
\rowcolor{headcolor}
Col1 & Col2 \\ \aboverulesepcolor{headcolor} \midrule \endhead
\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}

Output of the MWE

3

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}


\colorlet{tableheadcolor}{gray!35} % Table header colour = 25% gray
\newcommand{\topline}{\arrayrulecolor{black}\specialrule{\heavyrulewidth}{\abovetopsep}{0pt}%
            \arrayrulecolor{tableheadcolor}\specialrule{\belowrulesep}{0pt}{0pt}%
            \arrayrulecolor{black}}
\newcommand{\midline}{\arrayrulecolor{tableheadcolor}\specialrule{\aboverulesep}{0pt}{0pt}%
            \arrayrulecolor{black}\specialrule{\lightrulewidth}{0pt}{0pt}%
            \arrayrulecolor{white}\specialrule{\belowrulesep}{0pt}{0pt}%
            \arrayrulecolor{black}}


\begin{document}

\centering

\begin{longtable}{ll}

\topline
\rowcolor{tableheadcolor}
Col1 & Col2 \\ \midline
\endfirsthead
\topline
\rowcolor{tableheadcolor}
Col1 & Col2 \\ 
\midline
\endhead
\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}

enter image description here

| improve this answer | |
  • 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
0

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
  \renewcommand\LT@entry[2]{\global\advance\longtablewidth by ##2\relax\gdef\LastLTentrywidth{##2}}%
  \@nameuse{LT@\roman{LT@tables}}%
  \fi
\endgroup}
\newcommand*{\topheadermark}{%   
  \noalign{\tikzmark{beforeheader\the\c@LT@tables}}}%
\newcommand*{\bottomheadermark}{%
  \noalign{\tikzmark{belowheader\the\c@LT@tables}}}%  
\newcommand\drawheaderbackground{%
 \tikz[overlay,remember picture]
  \fill[headcolor]([xshift=-0.5\longtablewidth,yshift=-\aboverulesep]pic cs:belowheader\the\c@LT@tables)
 rectangle ([yshift=\belowrulesep,xshift=0.5\longtablewidth]pic cs:beforeheader\the\c@LT@tables);}
\makeatother

\definecolor{headcolor}{gray}{0.8}


\begin{document}

\centering

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

\toprule \topheadermark
\drawheaderbackground
Col1111 & Col2 \\\bottomheadermark\midrule \endfirsthead
\toprule \topheadermark
\drawheaderbackground
Col1 & Col2 \\\bottomheadermark\midrule  \endhead
\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}

enter image description here

| improve this answer | |
0

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}

\definecolor{headcolor}{gray}{0.8}
\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
  \rowcolor{headcolor}
  \Gape[\defaultbelowrulesep][\defaultaboverulesep]{Col1} & Col2 \\
  \midrule
  \addlinespace[\defaultbelowrulesep]
  \endfirsthead
  \toprule
  \rowcolor{headcolor}
  \Gape[\defaultbelowrulesep][\defaultaboverulesep]{Col1} & Col2 \\ %\aboverulesepcolor{headcolor}
  \midrule\addlinespace[\defaultbelowrulesep]
  \endhead
  \addlinespace[\defaultaboverulesep]
  \bottomrule
  \endfoot
  \addlinespace[\defaultaboverulesep]
  \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}

enter image description here

| improve this answer | |

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.