我正在使用booktabs
和xcolor
(带有table
选项)在教学大纲中为我正在教授的课程创建时间表。表格中的每一行代表课程的一周。目前,行在两种颜色之间交替。这是我目前拥有的精简版本:
\documentclass[11pt]{article}
\usepackage[table]{xcolor}
\usepackage{booktabs}
\usepackage{multirow}
\newcolumntype{L}[1]{>{\raggedright\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{R}[1]{>{\raggedleft\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\begin{document}
\rowcolors*{2}{yellow!40}{green!40}
\renewcommand{\arraystretch}{1.4}
\begin{tabular}{ccL{1.3in}L{1.1in}L{2.2in}} Week
& Date
& Class topic
& Experiment start
& Work due
\\ \midrule
5
& 2/24
& Peer review; \newline curve fitting
&
& Peer review comments \newline
Lab notes
\\
6
& 3/3
& Electrons in magnetic fields
& Electron charge- to-mass ratio
& \textbf{First draft:} Mechanical resonance \newline
\textbf{Final draft:} Gamma-ray counting
\\
7
& 3/10
& \textsl{(no meeting)}
&
& Lab notes
% &
\\
\hiderowcolors
\multicolumn{5}{c}{ \cellcolor{red!20} \textsl{Spring Break} }
\\
\showrowcolors 8
& 3/31
& Diffraction \& interference
& Multiple-slit diffraction
& \textbf{First draft:} $e/m_e$ ratio \newline
\textbf{Final draft:} Mechanical resonance
\\
9
& 4/7
& \textsl{(no meeting)}
&
& Lab notes\\
\bottomrule
\end{tabular}
结果:
我希望第 7 周(春假前)和第 8 周(春假后)有不同的颜色,而春假前后部分的交替则相同。有没有简单的方法可以做到这一点,还是我必须手动为表格的所有行着色?(在我的完整版代码中,表格包含 15 行加上标题,所以如果没有必要,我不愿意采用手动着色的方式。)
答案1
关键是在“春假”这句话之前添加一句什么也不做的台词,形式是
\multicolumn{5}{c}{}\\[-\dimexpr5pt+\normalbaselineskip]
如果您的行距不同(OP 提供的不是完整的 MWE),则可选参数的值可能\\
需要调整。
这是 MWE
\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{booktabs}
\usepackage{multirow}
\newcolumntype{L}[1]{>{\raggedright\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{R}[1]{>{\raggedleft\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\begin{document}
\rowcolors*{2}{yellow!40}{green!40}
\renewcommand{\arraystretch}{1.4}
\begin{tabular}{ccL{1.3in}L{1.1in}L{2.2in}} Week
& Date
& Class topic
& Experiment start
& Work due
\\ \midrule
5
& 2/24
& Peer review; \newline curve fitting
&
& Peer review comments \newline
Lab notes
\\
6
& 3/3
& Electrons in magnetic fields
& Electron charge- to-mass ratio
& \textbf{First draft:} Mechanical resonance \newline
\textbf{Final draft:} Gamma-ray counting
\\
7
& 3/10
& \textsl{(no meeting)}
&
& Lab notes
% &
\\
\hiderowcolors
\multicolumn{5}{c}{}\\[-\dimexpr5pt+\normalbaselineskip]
\multicolumn{5}{c}{ \cellcolor{red!20} \textsl{Spring Break} }
\\
\showrowcolors 8
& 3/31
& Diffraction \& interference
& Multiple-slit diffraction
& \textbf{First draft:} $e/m_e$ ratio \newline
\textbf{Final draft:} Mechanical resonance
\\
9
& 4/7
& \textsl{(no meeting)}
&
& Lab notes\\[![enter image description here][1]][1]
\bottomrule
\end{tabular}
\end{document}
答案2
这是一个{NiceTabular}
使用 的解决方案nicematrix
。
\documentclass[11pt]{article}
\usepackage{nicematrix}
\begin{document}
\begin{NiceTabular}{ccc}[color-inside]
\CodeBefore
\rowcolors{1}{red!15}{blue!15}[respect-blocks]
\Body
A & one & 1 \\
B & two & 2 \\
A & three & 3 \\
B & four & 4 \\
A & five & 5 \\
\Block{2-1}{}\rowcolor{yellow!50}\Block{1-3}{Holidays} \\
B & six & 6 \\
A & seven & 7 \\
B & eight & 8 \\
A & nine & 9 \\
B & ten & 10 \\
A & eleven & 11 \\
B & twelve & 12 \\
\end{NiceTabular}
\end{document}
该命令\Block{2-1}{}
创建一个链接两行的空块。然后,主命令\rowcolors{1}{red!15}{blue!15}[respect-blocks]
将尊重该块,因为它使用了键respect-blocks
。