長表中的替代行顏色

長表中的替代行顏色

我正在使用 Nikos Alexandris 提出的方法“如何在 LyX 的長表中應用交替行著色?”

工作範圍有限,但陰影行不會填滿頁面的整個寬度。每個單元格都有陰影,但陰影會破壞列線並且陰影中存在間隙。

這是我的範例 tex 程式碼,它執行損壞的著色。 tex 是自動產生的,所以我只顯示一個簡短的表格。真實資料較長,通常應跨越具有重複標題的頁面。

\date{\today}

\documentclass[9pt]{article}

\usepackage{graphicx}

\usepackage{longtable}

\usepackage[margin=0.5in]{geometry}

\usepackage{fancyhdr}

\LTchunksize=200

\setlength{\parindent}{0cm}

\title{test}
\author{R}
\setlength{\headheight}{0.4in}
\setlength{\headwidth}{\textwidth}

\usepackage[table]{xcolor}

% define lightgray

\definecolor{lightgray}{gray}{0.9}

% alternate rowcolors for all long-tables

\let\oldlongtable\longtable

\let\endoldlongtable\endlongtable

\renewenvironment{longtable}{\rowcolors{2}{white}{lightgray}\oldlongtable} {
\endoldlongtable}

\begin{document}

\section{Sector}

\begin{longtable}{ @{\extracolsep{\fill}} |l | r | r | r | r | r|}

\hline 

{} & Beta & Phys  & Phys & Phys  & comp \\

Industry Code & {} & {} & {} & {} & {} \endhead

\hline 

Materials &   16,365,792 &   13,419,455 &   13,419,455 &       0.00 &     -201,593 \\
Goods &      471,925 &    1,110,412 &    1,110,412 &       0.00 &      -11,947 \\
Services &    7,122,991 &    8,511,225 &    9,492,570 &     -981,345 &      -75,117 \\
Banks &    5,078,280 &    6,123,238 &   12,084,671 &   -5,961,433 &      -69,738 \\
Health &    2,141,346 &    5,059,382 &    5,059,382 &       0.00 &      -26,319 \\
Industrials &   -6,013,491 &   -4,758,346 &    6,036,445 &  -10,794,791 &        3,722 \\
row &   -4,093,746 &   -3,755,730 &       0.00 &   -3,755,730 &       45,628 \\
row &   -1,753,606 &   -1,006,601 &    2,533,792 &   -3,540,393 &      -18,962 \\
Consumer &    2,227,195 &    2,317,581 &    2,317,581 &       0.00 &      -30,632 \\
All &   20,808,544 &   25,160,345 &   54,728,908 &  -29,568,563 & nan \\

\hline 

\end{longtable}

\end{document}

答案1

colortbl不太喜歡\extracolsep。對於單一單元格,您可以擺弄可選的懸垂參數,但對於行來說,這有點困難。

此外,您的桌子不是全寬,因為膠水是由桌子兩側的膠水平衡\extracolsep的。\LTleft\LTright

如果將其設為全寬,則製作色帶會容易得多,因為您事先知道它們需要有多大:

在此輸入影像描述

\documentclass[9pt]{article}

\usepackage{graphicx}

\usepackage{longtable}

\usepackage[margin=0.5in]{geometry}

\usepackage{fancyhdr}

\LTchunksize=200

\setlength{\parindent}{0cm}

\title{test}
\author{R}
\setlength{\headheight}{0.4in}
\setlength{\headwidth}{\textwidth}

\usepackage[table]{xcolor}

% define lightgray

\definecolor{lightgray}{gray}{0.9}

% alternate rowcolors for all long-tables

\let\oldlongtable\longtable

\let\endoldlongtable\endlongtable

%\renewenvironment{longtable}{\rowcolors{2}{white}{lightgray}\oldlongtable} {
%\endoldlongtable}

\def\zza{\global\let\zz\zzb
\fullwidthcolor{lightgray}}%

\def\zzb{\global\let\zz\zza}

\def\fullwidthcolor#1{\color{#1}\leaders\vrule\hskip\textwidth\hskip-\textwidth\kern0pt}
\def\resetLTcolor{\global\let\zz\zza}

\LTleft0pt
\LTright0pt
\begin{document}

\section{Sector}

\resetLTcolor
\begin{longtable}{ @{\zz\extracolsep{\fill}} |l | r | r | r | r | r|}

\hline 
\multicolumn{1}{@{\fullwidthcolor{yellow}\extracolsep{\fill}} |l |}{}
  & Beta & Phys  & Phys & Phys  & comp \\
\multicolumn{1}{@{\fullwidthcolor{yellow}\extracolsep{\fill}} |l |}{Industry Code}
 & {} & {} & {} & {} & {} \endhead

\hline 

Materials &   16,365,792 &   13,419,455 &   13,419,455 &       0.00 &     -201,593 \\
Goods &      471,925 &    1,110,412 &    1,110,412 &       0.00 &      -11,947 \\
Services &    7,122,991 &    8,511,225 &    9,492,570 &     -981,345 &      -75,117 \\
Banks &    5,078,280 &    6,123,238 &   12,084,671 &   -5,961,433 &      -69,738 \\
Health &    2,141,346 &    5,059,382 &    5,059,382 &       0.00 &      -26,319 \\
Industrials &   -6,013,491 &   -4,758,346 &    6,036,445 &  -10,794,791 &        3,722 \\
row &   -4,093,746 &   -3,755,730 &       0.00 &   -3,755,730 &       45,628 \\
row &   -1,753,606 &   -1,006,601 &    2,533,792 &   -3,540,393 &      -18,962 \\
Consumer &    2,227,195 &    2,317,581 &    2,317,581 &       0.00 &      -30,632 \\
All &   20,808,544 &   25,160,345 &   54,728,908 &  -29,568,563 & nan \\

\hline 

\end{longtable}

\end{document}

相關內容