如何设置带有书本标签和彩色(头)行的长表

如何设置带有书本标签和彩色(头)行的长表

我想将longtable第一行(头部)颜色设为灰色。

由于在 下方和 上方booktabs插入空白,我需要插入一条彩色规则:\toprule\midrule

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

这在tabular环境中运行良好。但如果我使用longtable环境,它\hrule就会变得太长。

这是我的 MWE 和输出图片:

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

MWE 的输出

答案1

其他答案让我很困惑。更简单的东西有什么问题?不需要额外的包

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

在此处输入图片描述

答案2

我会使用 tikz 来绘制背景。类似这样(我没有彻底测试,因此可能需要根据实际情况进行一些调整):

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

在此处输入图片描述

答案3

这里有一个使用该软件包的解决方案:它包括将makecell的值设置为,并用 makecell 的命令替换它们,该命令在适当的位置使用 \above/belowrulesep 的默认值。\above/belowrulesep0pt\Gape

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

在此处输入图片描述

相关内容