TeX - LaTeX Asked by Hansel on August 5, 2021
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}
newcommandHatchedCell[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}
setlengtharrayrulewidth{1.5pt}
newcolumntype{P}{>{centeringarraybackslash}p{0.075textwidth}}
begin{tabular}{P|P|P}
HatchedCell{start1}{end1}{pattern color=black!70,pattern=north east lines}
cellcolor{grn}shelfnewline#1 &
cellcolor{grn}shelfnewline#2 &
cellcolor{gry}shelfnewline#3
end{tabular}
bigskip
begin{tabular}{P|P|P}
HatchedCell{start1}{end1}{pattern color=black!70,pattern=north east lines}
cellcolor{grn}shelfnewline#1 &
multicolumn{1}{!{hspace*{-0.4pt}vruletikzmark{start1}}c!{vruletikzmark{end1}}}%
{shelfnewline#2} &
cellcolor{gry}shelfnewline#3
end{tabular}
bigskip
begin{tabular}{P|P|P}
HatchedCell{start2}{end2}{pattern color=black!70,pattern=north east lines}
cellcolor{grn}shelfnewline#1 &
multicolumn{1}{!{hspace*{-0.4pt}vruletikzmark{start2}}c!{vruletikzmark{end2}}}%
{cellcolor{grn}shelfnewline#2} &
cellcolor{gry}shelfnewline#3
end{tabular}
end{document}
The MWE results in this:
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}
newcommandHatchedCell[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.075textwidth,1.9ex) $ )
rectangle ($ (aux1 |- aux2) + (0.75*0.075textwidth,-#1*baselineskip-.8ex) $ );
end{tikzpicture}%
}%
begin{document}
arrayrulecolor{white}
setlengtharrayrulewidth{1.5pt}
newcolumntype{P}{>{centeringarraybackslash}p{0.075textwidth}}
newcounter{hatchNumber}setcounter{hatchNumber}{1}
newcommandmyHatch[1]{
tikzmark{startarabic{hatchNumber}}cellcolor{grn}shelf%
tikzmark{middlearabic{hatchNumber}}newline##1%
tikzmark{endarabic{hatchNumber}}
HatchedCell{startarabic{hatchNumber}}{middlearabic{hatchNumber}}%
{endarabic{hatchNumber}}{pattern color=black!70,pattern=north east lines}
addtocounter{hatchNumber}{1}
}
begin{tabular}{P|P|P}
cellcolor{grn}shelfnewline#1 &
myHatch{2} &
cellcolor{gry}shelfnewline#3
end{tabular}
begin{tabular}{P|P|P}
cellcolor{grn}shelfnewline#4 &
myHatch{5} &
cellcolor{gry}shelfnewline#6
end{tabular}
end{document}
All your observations have simple explanations and fixes.
HatchedCell
outside the tabular
, so I did that.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 tikzmark
s: 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}
newcommandHatchedCell[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.075textwidth,1.9ex) $ )
rectangle ($ (aux1 |- aux2) + (0.75*0.075textwidth,-#1*baselineskip-.8ex) $ );
end{tikzpicture}%
}%
begin{document}
arrayrulecolor{white}
setlengtharrayrulewidth{1.5pt}
newcolumntype{P}{>{centeringarraybackslash}p{0.075textwidth}}
begin{tabular}{P|P|P}
cellcolor{grn}shelfnewline#1 &
cellcolor{grn}shelfnewline#2 &
cellcolor{gry}shelfnewline#3
end{tabular}
bigskip
begin{tabular}{P|P|P}
cellcolor{grn}shelfnewline#1 &
tikzmark{start1}shelftikzmark{middle1}newline#2tikzmark{end1} &
cellcolor{gry}shelfnewline#3
end{tabular}
HatchedCell{start1}{middle1}{end1}{pattern color=black!70,pattern=north east lines}
bigskip
begin{tabular}{P|P|P}
cellcolor{grn}shelfnewline#1 &
tikzmark{start2}cellcolor{grn}shelftikzmark{middle2}newline#2tikzmark{end2} &
cellcolor{gry}shelfnewline#3
end{tabular}
HatchedCell{start2}{middle2}{end2}{pattern color=black!70,pattern=north east lines}
end{document}
I'd like to remark that if you were to draw this with the matrix
library of TikZ, this would be way easier.
Correct answer by user121799 on August 5, 2021
It's easy with {NiceTabular}
of nicematrix
(≥ 5.19 of 2021-07-23).
documentclass{article}
usepackage{nicematrix}
usepackage{tikz}
usetikzlibrary{patterns}
definecolor{gry}{gray}{0.90}
definecolor{grn}{RGB}{169,208,142}
begin{document}
renewcommand{arraystretch}{1.2}
begin{NiceTabular}{ccc}[hvlines]
one & two & three
four & Block[fill=grn]{1-2}{five} &
Block[tikz={fill=grn},tikz={pattern = north west lines, pattern color = gry}]{1-2}{six}
&& Block[tikz={pattern = north west lines, pattern color = gry}]{1-1}{seven}
end{NiceTabular}
end{document}
You need several compilations (because nicematrix
uses PGF/Tikz nodes under the hood).
Answered by F. Pantigny on August 5, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP