6

I am using booktabs and xcolor (with the table option) to create a schedule in the syllabus for a class I am teaching. Each row in the table stands for one week of the class. Currently, the rows are alternating between two colors. Here's a pared-down version of what I have so far:

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

Result:

enter image description here

I would like Week 7 (before Spring Break) and Week 8 (after Spring Break) to have different colors, and for the alternation to otherwise be the same in the sections before and after Spring Break. Is there an easy way to do this, or do I have to color all of the rows of the table manually? (In my full version of the code, the table contains 15 rows plus the header, so I'm loath to go the manual coloring route if I don't have to.)

2 Answers 2

4

The key is adding an extra do-nothing line before the "Spring Break" line, in the form of

  \multicolumn{5}{c}{}\\[-\dimexpr5pt+\normalbaselineskip]

If your line spacing is different (the OP provided NOT a full MWE), the value of the optional argument to \\ may need tweaking.

Here is the 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}

enter image description here

2
  • Thanks for the example! I've modified my example code so that it's a full MWE now (I think.) Is there a way to set the height of the dummy row automatically given the line-spacing & font size I'm using, or do I just have to tweak it whenever I change those parameters? Dec 26, 2016 at 18:42
  • @MichaelSeifert I think for a given document style, it will only need to be tweaked once. However, the tweak will be dependent on things like fontsize, \arraystretch, and perhaps other document-class settings. Dec 26, 2016 at 22:30
2

Here is a solution with {NiceTabular} of 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}

The command \Block{2-1}{} creates an empty block which links two rows. Then, the main command \rowcolors{1}{red!15}{blue!15}[respect-blocks] will respect that block because it uses the key respect-blocks.

Output of the above

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .