Cómo configurar una mesa larga con pestañas y una fila de color (cabeza)

Cómo configurar una mesa larga con pestañas y una fila de color (cabeza)

Me gustaría hacer una longtablecon la primera fila (cabeza) coloreada en gris.

Dado que booktabsse inserta un espacio en blanco debajo \topruley encima \midrule, necesito insertar una regla de color:

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

Esto funciona bien dentro del tabularmedio ambiente. Pero si uso el longtableentorno, se \hruleestá volviendo demasiado largo.

Aquí está mi MWE con una imagen del resultado:

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

Salida del MWE

Respuesta1

Estoy confundido por las otras respuestas. ¿Qué hay de malo en algo mucho más simple? No se necesitan paquetes adicionales

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

ingrese la descripción de la imagen aquí

Respuesta2

Usaría tikz para dibujar el fondo. Algo como esto (no lo probé a fondo, por lo que tal vez sea necesario realizar algunos ajustes en su caso real):

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

ingrese la descripción de la imagen aquí

Respuesta3

Aquí hay una solución con el makecellpaquete: consiste en establecer los valores de \above/belowrulesepto 0pt, reemplazándolos con un \Gapecomando de makecell que usa los valores predeterminados de \above/belowrulesep en los lugares apropiados.

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

ingrese la descripción de la imagen aquí

información relacionada