calc 和 ehhline 一起使用时出现错误

calc 和 ehhline 一起使用时出现错误

前几天在处理用 绘制多行彩色单元格之间的横线的问题。我尝试了两种方法,最后选择了第二种。但是我发现如果加载 ,\hhline第一种方法有一个错误。calc

所有内容都在我的示例代码中。注释掉calc包,第三行下方的垂直线(红色和青色)将正常显示。

\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{tikz}
\usepackage{multirow}
\usepackage{colortbl}
\usepackage{ehhline}
% \usepackage{calc} % make method 1 not work

%% vertical line
% vertical colored line #1 color #2 width
\newcommand{\vsl}[2]{\color{#1}\vrule width #2}
% colored solid line pattern
% #1 color #2 width #3 height
\newcommand{\hsp}[3]{\hbox{\textcolor{#1}{\rule{#2}{#3}}}}
% #1 top fill #2 bottom fill
\newcommand{\leaderfill}[1]{%
  \xleaders\hbox{%
    \vbox{\baselineskip=0pt\lineskip=0pt#1}%
  }\hfill%
}
% #1 color #2 height
\newcommand{\sfill}[2]{%
  \leaderfill{\hsp{#1}{0.1pt}{#2}}%
}
%% vline settings
% #1 rule width #2 color
\newcommand{\setvline}[2]{%
  \global\setlength\arrayrulewidth{#2}\arrayrulecolor{#1}%
}

\begin{document}
\begin{tabular}{
  !{\vsl{red}{5pt}}c
  !{\vsl{teal}{5pt}}p{0.5cm}
  !{\vsl{blue}{3pt}}
}
% row 1
  \cellcolor{gray}
  & \cellcolor{yellow}\\
\hhline{
  ~ % blank in merged cell
  !{\sfill{black}{3pt}}
}
% row 2
  \cellcolor{gray}
  & \cellcolor{yellow}\\
\hhline{
  !{\sfill{gray}{3pt}} % overlap the vertical line
  !{\sfill{black}{3pt}}
}
% row 3
  \cellcolor{gray} third row
  & \cellcolor{orange}\\
% first method: set arrayrulewidth and arraycolor
% not work with calc
\hhline{
  >{\setvline{red}{5pt}}|
  !{\sfill{gray}{3pt}}
  >{\setvline{teal}{5pt}}|
  !{\sfill{black}{3pt}}
}
% row 4
  \cellcolor{gray}\tikz[->, baseline=-.5ex] \draw (0, 0) -- (-1ex, 1ex);
%   load calc
  not load
  & \cellcolor{pink}\\
% second method: use box
\hhline{
  >{\vsl{red}{5pt}}
  !{\sfill{gray}{3pt}}
  >{\vsl{teal}{5pt}}
  !{\sfill{black}{3pt}}
}
% row 5
  \cellcolor{gray}\multirow{-5}*{}
  & \cellcolor{cyan}\\
\end{tabular}
\end{document}

在此处输入图片描述 在此处输入图片描述

答案1

MWE 可以简化为

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

\usepackage{stackengine}

\begin{document}
\begin{tabular}{p{0.5cm} !{\color{yellow}\vrule width 5pt} p{0.5cm}}
  a & b \\
  \hhline{
    -
    >{\global\setlength\arrayrulewidth{5pt}\arrayrulecolor{yellow}}|
    >{\global\setlength\arrayrulewidth{.4pt}} -
  }
  c & d
\end{tabular}
\end{document}

在此处输入图片描述

从问题评论来看,正是calc由加载的stackengine,重新定义\setlength并禁止了从未支持的语法\global\setlength

要全局更改长度,你可以使用

\global\deflength{<length>}{<glue expression>}

其中\deflength由包定义etoolbox

或者,你可以使用新的序言\arrayrulewidth来避免全局更改>{...}hhlineV[<color>]{<width>}我之前的回答

在里面\hhlineV[<color>]{<width>}就像|具有特定颜色和宽度一样。完整示例:

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

\usepackage{stackengine}

\usepackage{xpatch} % or etoolbox
\usepackage{xparse}

\makeatletter
% based on \vline from latex2e
\def\HHvline#1{\vrule\@width#1}

\AtBeginDocument{
  \@ifpackageloaded{colortbl}{
    % based on \ifx\@tempb|...\else part of \HH@loop from colortbl.sty
    \NewDocumentCommand{\HH@preamble@V}{ m o m }{%
      \if@tempswa
        \ifx\CT@drsc@\relax
          \HH@add{\hskip\doublerulesep}%
        \else
          \HH@add{{\CT@drsc@\vrule\@width\doublerulesep}}%
        \fi
      \fi
      \@tempswatrue
      \IfNoValueTF{#2}
        {\HH@add{{\CT@arc@{\HHvline{#3}}}}}
        {\HH@add{{\color{#2}\HHvline{#3}}}}%
      \HH@let V%
    }
    \xpatchcmd\HH@loop
      {\PackageWarning{hhline}}
      {%
        \ifx\@tempb V% V[<color>]{<width>}
          \let\next\HH@preamble@V
        \else
        \PackageWarning{hhline}%
      }
      {}{\fail}
  }{
    % based on \ifx\@tempb|...\else part of \HH@loop from hhline.sty
    \NewDocumentCommand{\HH@preamble@V}{ m o m }{%
      \if@tempswa\HH@add{\hskip\doublerulesep}\fi\@tempswatrue
      \IfNoValueTF{#2}
        {\HH@add{\@tempc{\HHvline{#3}}\@tempc}}
        {\HH@add{\@tempc{\color{#2}\HHvline{#3}}\@tempc}}%
      \HH@let V%
    }
    \xpatchcmd\HH@loop
      {\PackageWarning{hhline}}
      {%
        \ifx\@tempb V% V[<color>]{<width>}
          \let\next\HH@preamble@V
        \else
      }
      {}{\fail}
  }
  \xpatchcmd\HH@loop
    {\fi\fi\fi}
    {\fi\fi\fi\fi}
    {}{\fail}
}
\makeatother

\begin{document}
\begin{tabular}{p{0.5cm} !{\color{yellow}\vrule width 5pt} p{0.5cm}}
  a & b \\
  \hhline{- V[yellow]{5pt} >{\arrayrulecolor{yellow}}-}
  c & d
\end{tabular}
\end{document}

相关内容