使用 colortbl 套件和 xcolor 指定起始列

使用 colortbl 套件和 xcolor 指定起始列

我有一個表,我嘗試設定如下(人為的範例):

% Preview source code

%% LyX 2.3.2-2 created this file.  For more info, see http://www.lyx.org/.
%% Do not edit unless you really know what you are doing.
\documentclass[english]{article}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}
\usepackage{geometry}
\geometry{verbose,tmargin=0.25in,bmargin=0.25cm,lmargin=0.25cm,rmargin=0.25cm}
\usepackage{array}
\usepackage{multirow}
\usepackage{graphicx}

\makeatletter

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% LyX specific LaTeX commands.
%% Because html converters don't know tabularnewline
\providecommand{\tabularnewline}{\\}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% User specified LaTeX commands.
\usepackage[table]{xcolor} %for use in color links
\usepackage{colortbl}

\makeatother

\usepackage{babel}
\begin{document}
\noindent \begin{flushleft}
\rowcolors{3}{gray!20}{}%
\begin{tabular}{ccccccc}
 &  & \multicolumn{5}{c}{Lorem ipsum dolor sit amet}\tabularnewline
 &  & Header & Header & Header & Header & Header\tabularnewline
\multirow{8}{*}{\cellcolor{white}\rotatebox{90}{Lorem ipsum}} & Content & \#\#\# & \#\#\# & \#\#\# & \#\#\# & \#\#\#\tabularnewline
 & Content & \#\#\# & \#\#\# & \#\#\# & \#\#\# & \#\#\#\tabularnewline
 & Content & \#\#\# & \#\#\# & \#\#\# & \#\#\# & \#\#\#\tabularnewline
 & Content & \#\#\# & \#\#\# & \#\#\# & \#\#\# & \#\#\#\tabularnewline
 & Content & \#\#\# & \#\#\# & \#\#\# & \#\#\# & \#\#\#\tabularnewline
 & Content & \#\#\# & \#\#\# & \#\#\# & \#\#\# & \#\#\#\tabularnewline
 & Content & \#\#\# & \#\#\# & \#\#\# & \#\#\# & \#\#\#\tabularnewline
 & Content & \#\#\# & \#\#\# & \#\#\# & \#\#\# & \#\#\#\tabularnewline
\end{tabular}\rowcolors{2}{}{}
\par\end{flushleft}
\end{document}

提前為生成的程式碼表示歉意,但下面的評論者要求提供完整的可編譯範例,而不僅僅是我最初發布的表格片段。希望這會讓問題變得更清楚,但如果它混淆了問題,我提前道歉。

無論如何,這呈現為:

桌子

如圖所示,我在表格左側使用的橫向標題被行的顏色覆蓋,從而中斷了文字。

我想做的是讓包含左行標題的整個第一列在所有行上都是白色的,我嘗試透過添加來做到這一點\cellcolor{white}多行單元格來實現這一點,但如圖所示,它只清除了該行中頂部的灰色條帶單元格,並且沒有對第一列的其餘部分執行任何操作。

如何才能正確顯示標題的全文?

答案1

使用\multirow是給你帶來麻煩的原因。

\documentclass{article}
\usepackage{graphicx}
\usepackage[table]{xcolor} %for use in color links

\begin{document}

\begin{flushleft}
\begin{tabular}{@{}c}
\rotatebox[origin=c]{90}{Lorem ipsum\hspace{2\dimexpr\ht\strutbox+\dp\strutbox}}
\end{tabular}%
\rowcolors{3}{gray!20}{}%
\begin{tabular}{cccccc}
 & \multicolumn{5}{c}{Lorem ipsum dolor sit amet}\tabularnewline
 & Header & Header & Header & Header & Header\tabularnewline
Content & \#\#\# & \#\#\# & \#\#\# & \#\#\# & \#\#\#\tabularnewline
Content & \#\#\# & \#\#\# & \#\#\# & \#\#\# & \#\#\#\tabularnewline
Content & \#\#\# & \#\#\# & \#\#\# & \#\#\# & \#\#\#\tabularnewline
Content & \#\#\# & \#\#\# & \#\#\# & \#\#\# & \#\#\#\tabularnewline
Content & \#\#\# & \#\#\# & \#\#\# & \#\#\# & \#\#\#\tabularnewline
Content & \#\#\# & \#\#\# & \#\#\# & \#\#\# & \#\#\#\tabularnewline
Content & \#\#\# & \#\#\# & \#\#\# & \#\#\# & \#\#\#\tabularnewline
Content & \#\#\# & \#\#\# & \#\#\# & \#\#\# & \#\#\#\tabularnewline
\end{tabular}\rowcolors{2}{}{}
\end{flushleft}

\end{document}

我已將文件減少到必要的最低限度。

請注意,這\noindent\begin{flushleft}是無意義的,因為它只是添加了不需要的垂直空間(空行)。也\par\end{flushleft}沒有必要。

在此輸入影像描述

奇怪的命令是什麼\hspace?它添加了兩個標題行的垂直尺寸。

答案2

在下面的程式碼中,我\cellcolor{white}為灰色行中的所有單元格添加了一個命令,並且實際上應該具有白色背景。我還將\multirow命令從第一行移到最後一行並使用-8而不是8

\documentclass[english]{article}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}
\usepackage{geometry}
\geometry{verbose,tmargin=0.25in,bmargin=0.25cm,lmargin=0.25cm,rmargin=0.25cm}
\usepackage{array}
\usepackage{multirow}
\usepackage{graphicx}

\usepackage[table]{xcolor}


\begin{document}
\noindent 
\begin{flushleft}
\rowcolors{3}{gray!20}{}%
\begin{tabular}{ccccccc}
 &  & \multicolumn{5}{c}{Lorem ipsum dolor sit amet}\tabularnewline
 &  & Header & Header & Header & Header & Header\tabularnewline
\cellcolor{white} & Content & \#\#\# & \#\#\# & \#\#\# & \#\#\# & \#\#\#\tabularnewline
 & Content & \#\#\# & \#\#\# & \#\#\# & \#\#\# & \#\#\#\tabularnewline
\cellcolor{white} & Content & \#\#\# & \#\#\# & \#\#\# & \#\#\# & \#\#\#\tabularnewline
 & Content & \#\#\# & \#\#\# & \#\#\# & \#\#\# & \#\#\#\tabularnewline
\cellcolor{white} & Content & \#\#\# & \#\#\# & \#\#\# & \#\#\# & \#\#\#\tabularnewline
 & Content & \#\#\# & \#\#\# & \#\#\# & \#\#\# & \#\#\#\tabularnewline
\cellcolor{white} & Content & \#\#\# & \#\#\# & \#\#\# & \#\#\# & \#\#\#\tabularnewline
\multirow{-8}{*}{\rotatebox{90}{Lorem ipsum}} & Content & \#\#\# & \#\#\# & \#\#\# & \#\#\# & \#\#\#\tabularnewline
\end{tabular}
\par\end{flushleft}
\end{document}

在此輸入影像描述

答案3

{NiceTabular}of中nicematrix,您有一個內建命令\rowcolors,其中包含一個選項來控制受該命令影響的列。

\documentclass[english]{article}
\usepackage[T1]{fontenc}
\usepackage{geometry}
\geometry{verbose,tmargin=0.25in,bmargin=0.25cm,lmargin=0.25cm,rmargin=0.25cm}
\usepackage{graphicx}
\usepackage{nicematrix}

\begin{document}
\begin{NiceTabular}{ccccccc}[color-inside]
 &  & \Block{1-*}{Lorem ipsum dolor sit amet}\\
 &  & Header & Header & Header & Header & Header\\
\rowcolors{gray!20}{}[cols=2-7]
\Block{*-1}<\rotate>{Lorem ipsum}
 & Content & \#\#\# & \#\#\# & \#\#\# & \#\#\# & \#\#\#\\
 & Content & \#\#\# & \#\#\# & \#\#\# & \#\#\# & \#\#\#\\
 & Content & \#\#\# & \#\#\# & \#\#\# & \#\#\# & \#\#\#\\
 & Content & \#\#\# & \#\#\# & \#\#\# & \#\#\# & \#\#\#\\
 & Content & \#\#\# & \#\#\# & \#\#\# & \#\#\# & \#\#\#\\
 & Content & \#\#\# & \#\#\# & \#\#\# & \#\#\# & \#\#\#\\
 & Content & \#\#\# & \#\#\# & \#\#\# & \#\#\# & \#\#\#\\
 & Content & \#\#\# & \#\#\# & \#\#\# & \#\#\# & \#\#\#\\
\end{NiceTabular}
\end{document}

您需要多次編譯(因為nicematrix在背景使用 PGF/Tikz 節點)。

上述程式碼的輸出

對於這種情況,其實有一個更簡單的解決方案。您可以指定表格具有所謂的nicematrix“第一列”(使用鍵first-col),並且該命令\rowcolors將直接按預期運行。

\documentclass[english]{article}
\usepackage[T1]{fontenc}
\usepackage{geometry}
\geometry{verbose,tmargin=0.25in,bmargin=0.25cm,lmargin=0.25cm,rmargin=0.25cm}
\usepackage{graphicx}
\usepackage{nicematrix}

\begin{document}
\begin{NiceTabular}{cccccc}[first-col,color-inside]
 &  & \Block{1-*}{Lorem ipsum dolor sit amet}\\
 &  & Header & Header & Header & Header & Header\\
\rowcolors{gray!20}{}
\Block{*-1}<\rotate>{Lorem ipsum}
 & Content & \#\#\# & \#\#\# & \#\#\# & \#\#\# & \#\#\#\\
 & Content & \#\#\# & \#\#\# & \#\#\# & \#\#\# & \#\#\#\\
 & Content & \#\#\# & \#\#\# & \#\#\# & \#\#\# & \#\#\#\\
 & Content & \#\#\# & \#\#\# & \#\#\# & \#\#\# & \#\#\#\\
 & Content & \#\#\# & \#\#\# & \#\#\# & \#\#\# & \#\#\#\\
 & Content & \#\#\# & \#\#\# & \#\#\# & \#\#\# & \#\#\#\\
 & Content & \#\#\# & \#\#\# & \#\#\# & \#\#\# & \#\#\#\\
 & Content & \#\#\# & \#\#\# & \#\#\# & \#\#\# & \#\#\#\\
\end{NiceTabular}
\end{document}

輸出是相同的。

相關內容