当使用 arrayrulecolor 和 cmidrule 时,如何消除垂直重新对齐?

当使用 arrayrulecolor 和 cmidrule 时,如何消除垂直重新对齐?

我使用连续\cmidrule命令与 结合\arrayrulecolor交替隐藏和取消隐藏表格中的水平线。但是,当\arrayrulecolor与 结合使用时\cmidrule,会出现一些奇怪的垂直间距。我想消除它。

(我正在使用longtable,因此colorboxminipage或与页面相关的任何其他内容均不起作用。)

下面是一张介绍挑战的图片。第一个块显示了一个\toprule。我的目标是在表格周围创建一个阴影边框,所以我添加了一个虚拟列。第二条规则显示了\cmidrule跳过第一列的。第三块显示了如何使用一个\cmidrule较短的填充空白\cmidrule。最后,我想将这个短的重新着色\cmidrule为背景颜色,稍后再恢复颜色。

请注意,与情况 3 相比,情况 4 中的锯齿状垂直跳跃。为什么要\arrayrulecolor改变垂直对齐?我该如何消除锯齿状边缘?

在此处输入图片描述

以下是代码:

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

答案1

不要尝试用彩色填充无标尺\cmidrule的列,而是在全部的桌子第一的,然后向后(向上)跳转并重新绘制\cmidrules。此“跳过”由

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

它会在表格宽度上绘制一条宽度规则,并在规则的上方和下方<wd>留出间隙。对于您的具体实例,<above><below>

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

成功了。颜色变化导致表格中的“换行符”,将每个后续行\cmidrule向下推前一个行的宽度\cmidrule。由于您需要更改此特定规则的颜色,最好定义您自己的新规则命令:

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

这是您的完整 MWE(带有一些小的代码修改):

在此处输入图片描述

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

这是一个侧注,但您的表格并不适合页面的边缘,从而导致“Overfull hbox”警告。

答案2

希望这对某人有帮助

当将 cmidrules 散布在彩色行而不是列中时,答案不起作用 - 填充需要更宽,因为上面的 \cmidrulesep 和下面的 2\cmidrulesep 未填充。不重复整个答案,对于正常(默认)宽度的 cmidrule,它需要

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

进而

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

对于更厚的厘米规:

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

然后

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

MWE(适用于较厚的规则版本):

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

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

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

在此处输入图片描述

相关内容