使用 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从第一行移到了最后一行,并使用-88

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

输出是一样的。

相关内容