3

I have read about hatching here and I understand how to color a cell. But I have a hard time getting a cell both colored and hatched at the same time.

Here's my MWE:

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

\definecolor{gry}{gray}{0.90}
\definecolor{grn}{RGB}{169,208,142}

\usetikzlibrary{calc,shadings,patterns,tikzmark}
\newcommand\HatchedCell[4][0pt]{%
  \begin{tikzpicture}[overlay,remember picture]%
    \fill[#4] ( $ (pic cs:#2) + (0,1.9ex) $ ) rectangle ( $ (pic cs:#3) + (0pt,-#1*\baselineskip-.8ex) $ );
  \end{tikzpicture}%
}%

\begin{document}

\arrayrulecolor{white}
\setlength\arrayrulewidth{1.5pt}
\newcolumntype{P}{>{\centering\arraybackslash}p{0.075\textwidth}}

\begin{tabular}{P|P|P}
\HatchedCell{start1}{end1}{pattern color=black!70,pattern=north east lines}
\cellcolor{grn}shelf\newline\#1 & 
\cellcolor{grn}shelf\newline\#2 & 
\cellcolor{gry}shelf\newline\#3 \\
\end{tabular}

\bigskip

\begin{tabular}{P|P|P}
\HatchedCell{start1}{end1}{pattern color=black!70,pattern=north east lines}
\cellcolor{grn}shelf\newline\#1 & 
\multicolumn{1}{!{\hspace*{-0.4pt}\vrule\tikzmark{start1}}c!{\vrule\tikzmark{end1}}}%
   {shelf\newline\#2} & 
\cellcolor{gry}shelf\newline\#3 \\
\end{tabular}

\bigskip

\begin{tabular}{P|P|P}
\HatchedCell{start2}{end2}{pattern color=black!70,pattern=north east lines}
\cellcolor{grn}shelf\newline\#1 & 
\multicolumn{1}{!{\hspace*{-0.4pt}\vrule\tikzmark{start2}}c!{\vrule\tikzmark{end2}}}%
   {\cellcolor{grn}shelf\newline\#2} & 
\cellcolor{gry}shelf\newline\#3 \\
\end{tabular}

\end{document}

The MWE results in this:

enter image description here

The first row shows pretty much what I want, except that I need the second entry "shelf #2" hatched. The second and third row show multiple issues. The second row doesn't show the hatching across the entire cell, has a vertical black bar and ignores the \newline entry. It's also missing the white vertical ruler to the right of the center cell. The third row shows that cell color appears to take precedence over the hatching.

How do I get this accomplished?


After marmot's great help, I've updated the code to make the table content easier to read by closely following the description further down the first answer.

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

\definecolor{gry}{gray}{0.90}
\definecolor{grn}{RGB}{169,208,142}

\usetikzlibrary{calc,shadings,patterns,tikzmark}
\newcommand\HatchedCell[5][0pt]{%
  \begin{tikzpicture}[overlay,remember picture]%
    \path ($(pic cs:#2)!0.5!(pic cs:#3)$) coordinate (aux1) 
    (pic cs:#4) coordinate (aux2);
    \fill[#5] ( $ (aux1) + (-0.67*0.075\textwidth,1.9ex) $ ) 
    rectangle ($ (aux1 |- aux2)  + (0.75*0.075\textwidth,-#1*\baselineskip-.8ex) $ );
  \end{tikzpicture}%
}%

\begin{document}

\arrayrulecolor{white}
\setlength\arrayrulewidth{1.5pt}
\newcolumntype{P}{>{\centering\arraybackslash}p{0.075\textwidth}}

\newcounter{hatchNumber}\setcounter{hatchNumber}{1}
\newcommand\myHatch[1]{
  \tikzmark{start\arabic{hatchNumber}}\cellcolor{grn}shelf%
  \tikzmark{middle\arabic{hatchNumber}}\newline\##1%
  \tikzmark{end\arabic{hatchNumber}}
  \HatchedCell{start\arabic{hatchNumber}}{middle\arabic{hatchNumber}}%
  {end\arabic{hatchNumber}}{pattern color=black!70,pattern=north east lines}
  \addtocounter{hatchNumber}{1}
}

\begin{tabular}{P|P|P}
\cellcolor{grn}shelf\newline\#1 & 
\myHatch{2} & 
\cellcolor{gry}shelf\newline\#3 \\
\end{tabular}

\begin{tabular}{P|P|P}
\cellcolor{grn}shelf\newline\#4 & 
\myHatch{5} & 
\cellcolor{gry}shelf\newline\#6 \\
\end{tabular}

\end{document}
2

All your observations have simple explanations and fixes.

  1. The linked answer clearly says that one is to call \HatchedCell outside the tabular, so I did that.
  2. You change the cell type to column type c, which is why \newline has no effect. It is actually not necessary to inorporate \tikzmark into a redefinition of the cell type. Since you play with hardcoded distances anyway, one can equally well put it into the cell. To make this work, though, you need to set three tikzmarks: one at the beginning of the cell, one at the end of the first line and one at the end of the cell.

Here's the code.

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

\definecolor{gry}{gray}{0.90}
\definecolor{grn}{RGB}{169,208,142}

\usetikzlibrary{calc,shadings,patterns,tikzmark}
\newcommand\HatchedCell[5][0pt]{%
  \begin{tikzpicture}[overlay,remember picture]%
    \path ($(pic cs:#2)!0.5!(pic cs:#3)$) coordinate (aux1) 
    (pic cs:#4) coordinate (aux2);
    \fill[#5] ( $ (aux1) + (-0.67*0.075\textwidth,1.9ex) $ ) 
    rectangle ($ (aux1 |- aux2)  + (0.75*0.075\textwidth,-#1*\baselineskip-.8ex) $ );
  \end{tikzpicture}%
}%

\begin{document}

\arrayrulecolor{white}
\setlength\arrayrulewidth{1.5pt}
\newcolumntype{P}{>{\centering\arraybackslash}p{0.075\textwidth}}

\begin{tabular}{P|P|P}
\cellcolor{grn}shelf\newline\#1 & 
\cellcolor{grn}shelf\newline\#2 & 
\cellcolor{gry}shelf\newline\#3 \\
\end{tabular}

\bigskip

\begin{tabular}{P|P|P}
\cellcolor{grn}shelf\newline\#1 & 
\tikzmark{start1}shelf\tikzmark{middle1}\newline\#2\tikzmark{end1} & 
\cellcolor{gry}shelf\newline\#3 \\
\end{tabular}
\HatchedCell{start1}{middle1}{end1}{pattern color=black!70,pattern=north east lines}

\bigskip

\begin{tabular}{P|P|P}
\cellcolor{grn}shelf\newline\#1 & 
\tikzmark{start2}\cellcolor{grn}shelf\tikzmark{middle2}\newline\#2\tikzmark{end2} & 
\cellcolor{gry}shelf\newline\#3 \\
\end{tabular}
\HatchedCell{start2}{middle2}{end2}{pattern color=black!70,pattern=north east lines}
\end{document}

enter image description here

I'd like to remark that if you were to draw this with the matrix library of TikZ, this would be way easier.

| improve this answer | |
  • Hi marmot, thank you very much for changing the code. This is really helpful. While the fixes may seem simple, I certainly couldn't have come up with the updated definition of the command \HatchedCell. What you've provided works like a charm. I need to study tikz more. Again, thanks for the time you've spent on this. I'm sure this is useful for other LaTeX users out there. – Hansel Oct 18 '18 at 17:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.