0

When I fill the multirow cell whose horizontal lines inside the cell are drawn with \hhline{~}, there will be blanks, I try to fix it by >{\arrayrulecolor{gray}\doublerulesepcolor{gray}}=, but the color of vertical lines will be messed also.

\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{multirow, makecell}
\usepackage{colortbl}
\usepackage{dashrule}
\usepackage{ehhline}
\newcommand{\cdl}[2][black]{\leaders\hbox{\textcolor{#1}{\hdashrule{0.9mm}{#2}{0.3mm 0.3mm 0.3mm 0mm}}}\hfil}
\newcommand{\crs}[2][black]{\leaders\hbox{\textcolor{#1}{\rule{0.1pt}{#2}}}\hfil}
\newcommand{\vcl}[2][black]{\color{#1}\vrule width #2}
\setlength\doublerulesep{0.1pt}

\begin{document}
\begin{tabular}{!{\vcl{3pt}}c!{\vcl[green]{3pt}}c!{\vcl[yellow]{3pt}}}
\hhline{
  !{\cdl[red]{1pt}}
  !{\crs[red]{1pt}}
}
  \multicolumn{1}{!{\vcl[green]{3pt}}c!{\vcl{2pt}}}{\cellcolor{gray}test}
  & \multicolumn{1}{c!{\vcl{3pt}}}{test}\\
\hhline{
  !{\cdl[orange]{1pt}}
  !{\crs[teal]{2pt}}
}
  \cellcolor{gray} & test\\
\hhline{
  >{\arrayrulecolor{gray}\doublerulesepcolor{gray}}%
  =%
  !{\crs[red]{1pt}}
}
    \multirow{-2}{*}{\cellcolor{gray} test some thing}
  & test\\
\hhline{
  !{\cdl[red]{1pt}}
  !{\crs[red]{1pt}}
}
\end{tabular}
\end{document}

enter image description here I nearly successed by set hhline to

\hhline{
  >{\rule{3pt}{1pt} \kern -3pt \arrayrulecolor{gray}\doublerulesepcolor{gray}}%
  =
  !{\crs[red]{1pt}}
}

but there is a small blank sep. enter image description here

Solved but why?

I solve the problem, but I don't know why this will work

\hhline{
  >{\rule{3pt}{1pt} \kern -3.3pt \arrayrulecolor{gray}\doublerulesepcolor{gray}}%
  =
  >{\kern -3pt \textcolor{green}{\rule{3pt}{1pt}}}%
  !{\crs[red]{1pt}}
}

I just need change the width of the rule to make it the same with the width of the vertical line, but what are -3.3pt and -3pt represent?

ZhiyuanLck
  • 4,516
  • 2
    My feeling is that you're doing a drawing task in the form of a table rather than a real table with colored cells. In this case, why not use TikZ directly? It is very powerful and relatively easy, with great documentation. – js bibra Apr 20 '20 at 08:44
  • Because I am writing a script tool to convert excel to latex, so I have to perfect the details as well as I can. – ZhiyuanLck Apr 20 '20 at 08:54
  • @jsbibra It is possible to draw latex table with the same style that is in excel. I nearly make it. If you have some interests, see https://github.com/ZhiyuanLck/excel2tex – ZhiyuanLck Apr 20 '20 at 09:00
  • specify a black | in the hhline before the grey - – David Carlisle Apr 20 '20 at 09:35
  • @DavidCarlisle Can you give me an example? – ZhiyuanLck Apr 20 '20 at 09:36

1 Answers1

2

Update

Do not use syntax \global\setlength ..., it has never been supported. (see explanation here). To change a length globally, etoolbox's \deflength is your friend. It shares the same (usual) syntax with \setlength while supports the extra \global\deflength usage.


The width of vline is 3pt (due to the !{\vcl{3pt}} used in tabular preamble), but a | used in \hhline only adds a rule of width \arrarrulewidth. Change the \arrayfulewidth locally could be a workaround, while providing a new \hhline preamble character, like what ehhline package does, is more elegant.

% \usepackage{etoolbox}

\hhline{
  >{\global\deflength{\arrayrulewidth}{3pt}} |
  >{\global\deflength{\arrayrulewidth}{.4pt}\arrayrulecolor{gray}\doublerulesepcolor{gray}} =
  >{\global\deflength{\arrayrulewidth}{3pt}\arrayrulecolor{green}} |
  !{\crs[red]{1pt}}
  >{\arrayrulecolor{yellow}} |
}
% restore \arrayrulewidth for following use
\global\deflength{\arrayrulewidth}{.4pt}%

The \global is required since the \HH@loop redefined by colortbl adds an extra group of curly braces. Related code lines:

% from colortbl.sty
\def\HH@loop{%
  ... ...
  \ifx\@tempb|\if@tempswa
        \ifx\CT@drsc@\relax
         \HH@add{\hskip\doublerulesep}%
        \else
         \HH@add{{\CT@drsc@\vrule\@width\doublerulesep}}%
         \fi
        \fi\@tempswatrue
        %% \vline is just "\vrule \@width \arrayrulewidth"
        \HH@add{{\CT@arc@\vline}}\else
  ... ...
}

Note that since hhline v2.04 (2020/01/04), space token used inside \hhline is always ignored.


In the following example, the \HH@loop is patched to allow use of V[<color>]{<width>} inside \hhline{..}. This may shorten the length of arguments of \hhline in OP's example.

\documentclass{article}
\usepackage{array}
\usepackage{color}
% \usepackage{colortbl} % or uncomment this line
\usepackage{hhline}
\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}{!{\HHvline{1pt}}c!{\HHvline{2pt}}c!{\HHvline{3pt}}}
  \hline
  a & b \\ \hhline{V[red]{1pt} = V[blue]{2pt} = V[green]{3pt}}
  c & d \\ \hline
\end{tabular}
\end{document}

enter image description here

muzimuzhi Z
  • 26,474
  • Yes thanks I was going to suggest the global but then noticed that the OP was using ehhline which I hadn't seen before so got sidetracked checking that package:-) . I haven't traced exactly what ehhline is doing but I'd have thought !{\vrule width 3pt} would have worked (but it does not:( – David Carlisle Apr 20 '20 at 10:18
  • Ah ehhline is a small package newly uploaded to CTAN. It adds !{...}, which acts like a - but accepts leader, to the hhline preamble. – muzimuzhi Z Apr 20 '20 at 10:21
  • Yes and it makes ! a complete cell (it adds &) the functionality is OK but I wouldn't have used ! as that is unlike ! from array package, hmmmm – David Carlisle Apr 20 '20 at 10:23
  • @ZhiyuanLck Answer is updated to show a new hhline preamble char, "V[]{}". Hope that would help. – muzimuzhi Z Apr 20 '20 at 14:56