# How do I eliminate the vertical realignment when using arrayrulecolor with cmidrule?

I'm using consecutive \cmidrule commands in conjunction with \arrayrulecolor to alternately hide and unhide horizontal rules in a table. However, when \arrayrulecolor is used with \cmidrule, some odd vertical spacing is introduced. I'd like to eliminate it.

(I'm using longtable, so a colorbox, minipage, or anything else tied to a page won't work.)

Below is an image that introduces the challenge. The first block shows a \toprule. My goal is to create a shaded border around the table, so I've added a dummy column. The second rule shows a \cmidrule that skips the first column. The third block shows how I can use a \cmidrule to fill in the whitespace with a shorter \cmidrule. Finally, I want to recolor this short \cmidrule to the background color and restore the color later.

Note the jagged vertical skipping in case 4 compared to case 3. Why would \arrayrulecolor change vertical alignment? How can I eliminate the jagged edges?

Here is the code:

\documentclass[11pt]{article}

\usepackage{longtable}
\usepackage{booktabs}
\usepackage{color}
\usepackage{xcolor}
\usepackage{colortbl}

\setlength{\aboverulesep}{0pt}
\setlength{\belowrulesep}{0pt}
\setlength{\extrarowheight}{0.25ex}

\newcolumntype{L}{>{\columncolor[gray]{0.8}}l}
\newcolumntype{C}{>{\columncolor[gray]{0.8}}c}
\newcolumntype{R}{>{\columncolor[gray]{0.8}}r}

\begin{document}

\textsf{
\begin{longtable}{LLLLL}
& \multicolumn{3}{@{}L}{\small {\tablename\ \thetable{}} - demonstration of odd vertical alignment with arrayrulecolor} & \\%
& \multicolumn{3}{@{}L}{\small {case 1: the line below is a toprule}} & \\%
\toprule
& \multicolumn{3}{@{}L}{\small {case 2: the line below is a cmidrule that spans the middle 3 columns.  Note the whitespace!}} & \\%
& \multicolumn{3}{@{}L}{\small {The challenge: hide the widespace giving the appearance of a spacing around a longtable.}} & \\%
\cmidrule[\heavyrulewidth]{2-4}
& \multicolumn{3}{@{}L}{\small {case 3: the line below is 3 cmidrules, 1 each for the first and last columns and 1 for the middle}} & \\%
& \multicolumn{3}{@{}L}{\small {note how the cmidrules align vertically ... perfect!}} & \\%
\cmidrule[\heavyrulewidth]{1-1}\cmidrule[\heavyrulewidth]{2-4}\cmidrule[\heavyrulewidth]{5-5}
& \multicolumn{3}{@{}L}{\small {case 4: Now, lets try recoloring the first and last cmidrules}} & \\%
& \multicolumn{3}{@{}L}{\small {how do I eliminate the odd vertical alignments?}} & \\%
\arrayrulecolor[gray]{0.80}\cmidrule[\heavyrulewidth]{1-1}\arrayrulecolor[gray]{0.0}\cmidrule[\heavyrulewidth]{2-4}\arrayrulecolor[gray]{0.80}\cmidrule[\heavyrulewidth]{5-5}\arrayrulecolor[gray]{0.00}
&  Arizona State University &  & ASU &\\
&   Baylor University &  & BAYLOR &\\
&   Boston University & 164988 & BU &\\
&   Brown University & 217156 & BROWN &\\
&   California Institute of Technology & 110404 & CALTECH &\\
&   Carnegie Mellon University & 211440 & CMU &\\
&   Case Western Reserve University & 201645 & CASE &\\
&   Columbia University & 190150 & COLUMBIA &\\
&   Cornell University & 190415 & CORNELL &\\
& Yale University & 130794 & YALE & %
\label{tab:schools}
\end{longtable}
}

\end{document}


Instead of trying to pad the non-ruled columns with a coloured \cmidrule attempt, draw a horizontal line (of the specified colour) across the entire table first, and then skip back (upward) and redraw your \cmidrules. This "skipping" is provided by

\specialrule{<wd>}{<above>}{<below>}


which draws a rule of width <wd> across the table width, leaving a gap of <above> above and <below> below the rule. For your specific instance,

\specialrule{\heavyrulewidth}{0pt}{-\heavyrulewidth}


does the trick. It's the colour changes that causes the "line breaks" in the table, pushing each subsequent \cmidrule down by the width of the previous \cmidrule. Since you need to change the colour for this specific rule, it's best to define your own new rule command:

\newcommand*{\rulefiller}{%
\arrayrulecolor[gray]{0.8}% change to cell colour
\specialrule{\heavyrulewidth}{0pt}{-\heavyrulewidth}% "invisible" rule
\arrayrulecolor{black}% revert to regular line colour
}


Here's your complete MWE (with some small code modifications):

\documentclass[11pt]{article}
\usepackage{longtable}% http://ctan.org/pkg/longtable
\usepackage{booktabs}% http://ctan.org/pkg/booktabs
\usepackage{xcolor}% http://ctan.org/pkg/xcolor
\usepackage{colortbl}% http://ctan.org/pkg/colortbl

\setlength{\aboverulesep}{0pt}
\setlength{\belowrulesep}{0pt}
\setlength{\extrarowheight}{0.25ex}

\newcolumntype{L}{>{\columncolor[gray]{0.8}}l}
\newcolumntype{C}{>{\columncolor[gray]{0.8}}c}
\newcolumntype{R}{>{\columncolor[gray]{0.8}}r}
\newcommand*{\rulefiller}{%
\arrayrulecolor[gray]{0.8}% change to cell colour
\specialrule{\heavyrulewidth}{0pt}{-\heavyrulewidth}% "invisible" rule
\arrayrulecolor{black}% revert to regular line colour
}

\begin{document}

{\sffamily
\begin{longtable}{LLLLL}
& \multicolumn{3}{@{}L}{\small \tablename\ \thetable\ - demonstration of odd vertical alignment with arrayrulecolor} & \\%
& \multicolumn{3}{@{}L}{\small case 1: the line below is a toprule} & \\%
\toprule
& \multicolumn{3}{@{}L}{\small case 2: the line below is a cmidrule that spans the middle 3 columns.  Note the whitespace!} & \\%
& \multicolumn{3}{@{}L}{\small The challenge: hide the widespace giving the appearance of a spacing around a longtable.} & \\%
\rulefiller\cmidrule[\heavyrulewidth]{2-4}
& \multicolumn{3}{@{}L}{\small case 3: the line below is 3 cmidrules, 1 each for the first and last columns and 1 for the middle} & \\%
& \multicolumn{3}{@{}L}{\small note how the cmidrules align vertically ... perfect!} & \\%
\cmidrule[\heavyrulewidth]{1-1}\cmidrule[\heavyrulewidth]{2-4}\cmidrule[\heavyrulewidth]{5-5}
& \multicolumn{3}{@{}L}{\small case 4: Now, lets try recoloring the first and last cmidrules} & \\%
& \multicolumn{3}{@{}L}{\small how do I eliminate the odd vertical alignments?} & \\%
\rulefiller\cmidrule[\heavyrulewidth]{2-4}
& Arizona State University &  & ASU & \\
& Baylor University &  & BAYLOR & \\
& Boston University & 164988 & BU & \\
& Brown University & 217156 & BROWN & \\
& California Institute of Technology & 110404 & CALTECH & \\
& Carnegie Mellon University & 211440 & CMU & \\
& Case Western Reserve University & 201645 & CASE & \\
& Columbia University & 190150 & COLUMBIA & \\
& Cornell University & 190415 & CORNELL & \\
& Yale University & 130794 & YALE & %
\label{tab:schools}
\end{longtable}
}
\end{document}


This is a side-note, but your table doesn't fit within the margins of the page, causing an "Overfull hbox" warning.

• Why is such a common thing so complicated to fix? This should be somehow done by default – luchonacho Sep 1 '16 at 10:16
• @luchonacho: You're invited to provide a package that automates this correction. Really. – Werner Sep 1 '16 at 15:24
• Nice! But do you know why it pushes them down? It wouldn't do this if you just used one \cmidrule and then another, without changing colour. – cfr Dec 23 '16 at 17:56
• This doesn't work this end -- the filler rule is way to thin -- it has to be either 6pt with the appropriate backshift or 3\cmidrulesep -- not just the width of the rule – Aubrey Blumsohn May 30 at 11:41
• @AubreyBlumsohn: This may depend on the zoom level of your PDF browser. – Werner May 30 at 19:00

Hope this helps someone

The answer doers not work when interspersing cmidrules in colored rows as opposed to columns - the filler needs to be much wider because the \cmidrulesep above and 2\cmidrulesep below are not filled. Without repeating the whole answer, for a normal (default) width cmidrule it needs to be

\newlength{\clinegapskip}
\setlength{\clinegapskip}{3\cmidrulesep}


and then

  \arrayrulecolor{tablerowcolor} \specialrule{\clinegapskip}{0pt}{-\clinegapskip} \arrayrulecolor{black}
\cmidrule{2-5}


For a thicker cmidrule:

\newlength{\clinegapskip}
\setlength{\clinegapskip}{3\cmidrulesep}


then

 \arrayrulecolor{tablerowcolor} \specialrule{\clinegapskip}{0pt}{-\clinegapskip} \arrayrulecolor{black}
\cmidrule[\heavyrulewidth]{2-5}


MWE (for the thicker rule version):

\documentclass{article}
\usepackage{booktabs,xcolor,,colortbl}

\newlength{\clinegapskip}
\setlength{\clinegapskip}{3\cmidrulesep}

\begin{document}
\begin{tabular}{llll}
\toprule
\textbf{Parameter} & Cats & Dogs & Fish \\
\midrule
Food&Bats&Eggs&Worms\\
\rowcolor{green} Food&Bats&Eggs&Worms\\
\cmidrule[\heavyrulewidth]{2-4}
\rowcolor{green} Food&Bats&Eggs&Worms\\
\rowcolor{green} Food&Bats&Eggs&Worms\\
\rowcolor{green} Food&Bats&Eggs&Worms\\
\arrayrulecolor{green} \specialrule{\clinegapskip}{0pt}{-\clinegapskip} \arrayrulecolor{black}
\cmidrule[\heavyrulewidth]{2-4}
\rowcolor{green} Food&Bats&Eggs&Worms\\
\rowcolor{green} Food&Bats&Eggs&Worms\\
Food&Bats&Eggs&Worms\\
\bottomrule
\end{tabular}
\end{document}