9

My question is similar to the one here How do I eliminate the vertical realignment when using arrayrulecolor with cmidrule? but can not be solved with the suggested answer (because the lines are trimmed).

I'd like to make a table with trimmed horizontal lines (as they can be generated by \cmidrule from package booktabs) but with changing colors.

An example:

\documentclass{article}
\usepackage[utf8x]{inputenc}
\usepackage{booktabs}
\usepackage[table]{xcolor}

\begin{document}

\begin{tabular}{lll}
column 1 & column 2 & column 3 \\ \cmidrule[2pt](lr){1-1}\arrayrulecolor{blue}\cmidrule[2pt](lr){2-2}\arrayrulecolor{black}\cmidrule[2pt](lr){3-3}
text & text & text\\ \cmidrule[0.4pt](lr){1-1}\cmidrule[0.4pt](lr){2-2}\cmidrule[0.4pt](lr){3-3}
text & text & text\\ \cmidrule[0.4pt](lr){1-1}\cmidrule[0.4pt](lr){2-2}\cmidrule[0.4pt](lr){3-3}
\end{tabular}

\end{document}

enter image description here

This does pretty much what I want but the horizontal lines are realigned vertically after every color change. How can I omit this realignment?

1 Answers1

11

You can correct for this vertical re-alignment using the \corcmidrule[<len>] macro defined below:

enter image description here

\documentclass{article}
\usepackage{booktabs}
\usepackage[table]{xcolor}

% Correct for \cmidrule colour adjustment/vertical skip
\newcommand{\corcmidrule}[1][2pt]{% \corcmidrule[<len>]
  \\[\dimexpr-\normalbaselineskip-\belowrulesep-\aboverulesep-#1\relax]%
}

\begin{document}
\begin{tabular}{lll}
column 1 & column 2 & column 3 \\
  \cmidrule[2pt](lr){1-1}\corcmidrule\arrayrulecolor{blue}%
  \cmidrule[2pt](lr){2-2}\corcmidrule\arrayrulecolor{black}%
  \cmidrule[2pt](lr){3-3}
text & text & text\\ \cmidrule[0.4pt](lr){1-1}\cmidrule[0.4pt](lr){2-2}\cmidrule[0.4pt](lr){3-3}
text & text & text\\ \cmidrule[0.4pt](lr){1-1}\cmidrule[0.4pt](lr){2-2}\cmidrule[0.4pt](lr){3-3}
\end{tabular}

\begin{tabular}{lll}
column 1 & column 2 & column 3 \\
  \cmidrule[5pt](lr){1-1}\corcmidrule[5pt]\arrayrulecolor{blue}%
  \cmidrule[3pt](lr){2-2}\corcmidrule[3pt]\arrayrulecolor{orange}%
  \cmidrule[7pt](lr){3-3}\arrayrulecolor{black}
text & text & text\\ \cmidrule[0.4pt](lr){1-1}\cmidrule[0.4pt](lr){2-2}\cmidrule[0.4pt](lr){3-3}
text & text & text\\ \cmidrule[0.4pt](lr){1-1}\cmidrule[0.4pt](lr){2-2}\cmidrule[0.4pt](lr){3-3}
\end{tabular}

\end{document}​

The macro performs a traditional tabular line break \\[<len>] of specified length. The length is a correction taking into account the normal baseline skip \normalbaselineskip, the above/below rule skip enforced by booktabs called \aboverulesep and \belowrulesep, as well as a parameter specifying the \cmidrule width - default is 2pt (by virtue of your MWE). Alternatively, if you have a rule of width (say) 5pt, you could use \corcmidrule[5pt], as was used in the second example.

Moriambar
  • 11,466
Werner
  • 603,163