如何仅修改longtable表头行的边框颜色?
仅修改longtable表头行边框颜色的实现方案
可以实现该需求,核心是通过\arrayrulecolor命令临时切换表格边框颜色,仅作用于表头行,之后再恢复默认颜色即可。
修改关键点
在定义printDbBreakableTable环境时,针对表头部分做如下调整:
- 在表头行背景色设置前,用
\arrayrulecolor{<目标颜色>}设置边框颜色(示例中用白色配合黑色表头背景,对比度更高) - 表头行内容结束后,用
\arrayrulecolor{black}恢复默认边框颜色,避免影响表格其他行
修改后的完整代码
\documentclass[11pt]{book} \usepackage[default]{opensans} \usepackage{alltt} \usepackage{amsfonts} \usepackage{amsmath} \usepackage{amssymb} \usepackage{array} \usepackage{colortbl} \usepackage{enumitem} \usepackage{fancyhdr} \usepackage{float} \usepackage[total={15.24cm,22.86cm}]{geometry} \usepackage{graphicx} \usepackage{hyperref} \usepackage{inconsolata} \usepackage{listings} \usepackage{longtable} \usepackage{mathabx} \usepackage{microtype} % % For creating new commands with an arbitrary number of arguments \usepackage{newfile} % % \usepackage{pgfplots} \usepackage{pifont} \usepackage{smartdiagram} \usepackage{tabularx} \usepackage[listings,skins,most]{tcolorbox} \usepackage{tikz} \usepackage[table,xcdraw]{xcolor} % % % % % Define a header style command \newcommand{\longTabHeaderstyle}[1]{\textcolor{white}{#1}} % % % % % Space before the table \setlength{\LTpre}{0pt} % % Space after the table \setlength{\LTpost}{0pt} % % Space from the left before the table \setlength{\LTleft}{0pt} % % Space from the right after the table \setlength{\LTright}{0pt} % % % Define the printDbBreakableTable environment \newenvironment{printDbBreakableTable}[4]{% \getLTwidth % Start the tcolorbox \begin{tcolorbox}[% % Make the box content breakable across pages breakable,% text width={\LTwidth}, % % Set background color of the box colback=white,% % % Set frame color of the box colframe=black,% % % Set space below the title bottomtitle=1mm,% % % Set space above the title toptitle=1mm,% % % Set thickness of left border leftrule=0.15mm,% % % Set thickness of right border rightrule=0.15mm,% % % Set thickness of bottom border bottomrule=0.15mm,% % % Center the title title=\centering #1,% % % Remove internal padding boxsep=0pt,% % % Remove top padding top=0pt,% % % Remove bottom padding bottom=0pt,% % % Remove left padding left=0pt,% % % Remove right padding right=0pt% ]% \par % % Begin table structure by indicating the number of columns \begin{longtable}% {#2}% % % 临时设置表头边框颜色为白色 \arrayrulecolor{white}% % Set header row color \rowcolor{black}% % % The content of the header row #3% % 恢复边框颜色为默认黑色 \arrayrulecolor{black}% \endhead \hline % % Insert the table's main content #4% \end{longtable}% % End the tcolorbox \end{tcolorbox}% } \makeatletter \newlength{\LTwidth} \newcommand{\getLTwidth}[1][empty]% #1 = longtable index (optional) {\bgroup% for count1 and LT@entry \ifx\empty#1\relax \count1=\value{LT@tables}% default \advance\count1 by 1 \else \count1=#1\relax \fi \@ifundefined{LT@\romannumeral\count1}{\LTwidth=\textwidth}{% \LTwidth=0pt% \def\LT@entry##1##2{\advance\LTwidth by ##2}% \csname LT@\romannumeral\count1\endcsname }% \global\LTwidth=\LTwidth \egroup} \makeatother \begin{document} \addtolength{\extrarowheight}{2pt} \begin{printDbBreakableTable} {Test a long table} {c|c|c|c} {% \longTabHeaderstyle{H1} & \longTabHeaderstyle{H2} & \longTabHeaderstyle{H3} & \longTabHeaderstyle{H4} \\\hline } {% CellValue-1-1 & CellValue-2-1 & CellValue-3-1 & CellValue-4-1 \\\hline CellValue-1-2 & CellValue-2-2 & CellValue-3-2 & CellValue-4-2 \\\hline CellValue-1-3 & CellValue-2-3 & CellValue-3-3 & CellValue-4-3 \\\hline CellValue-1-4 & CellValue-2-4 & CellValue-3-4 & CellValue-4-4 \\\hline CellValue-1-5 & CellValue-2-5 & CellValue-3-5 & CellValue-4-5 \\ } \end{printDbBreakableTable} \end{document}
效果说明
修改后,表头行的横向分隔线、纵向列分隔线都会变为设置的白色,与黑色的表头背景形成强烈对比,大幅提升表头辨识度,而表格主体行的边框仍保持默认黑色,互不影响。
内容的提问来源于stack exchange,提问作者user17911
相关产品推荐
相关产品推荐

