I'm trying to hatch some table cells by following the code here. It seems to work fine, but then when I change the positioning to \begin{table}[!h] the hatching stops showing up. The correct result should be as in the figure:

This is the MWE:
\documentclass[10pt,a4paper]{article}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor} % use color
\usepackage{booktabs} % commands for table rules
\usepackage{tikz}
\usetikzlibrary{calc,shadings,patterns}
% Andrew Stacey's code from
% https://tex.stackexchange.com/a/50054/3954
\makeatletter
\tikzset{%
remember picture with id/.style={%
remember picture,
overlay,
save picture id=#1,
},
save picture id/.code={%
\edef\pgf@temp{#1}%
\immediate\write\pgfutil@auxout{%
\noexpand\savepointas{\pgf@temp}{\pgfpictureid}}%
},
if picture id/.code args={#1#2#3}{%
\@ifundefined{save@pt@#1}{%
\pgfkeysalso{#3}%
}{
\pgfkeysalso{#2}%
}
}
}
\def\savepointas#1#2{%
\expandafter\gdef\csname save@pt@#1\endcsname{#2}%
}
\def\tmk@labeldef#1,#2\@nil{%
\def\tmk@label{#1}%
\def\tmk@def{#2}%
}
\tikzdeclarecoordinatesystem{pic}{%
\pgfutil@in@,{#1}%
\ifpgfutil@in@%
\tmk@labeldef#1\@nil
\else
\tmk@labeldef#1,(0pt,0pt)\@nil
\fi
\@ifundefined{save@pt@\tmk@label}{%
\tikz@scan@one@point\pgfutil@firstofone\tmk@def
}{%
\pgfsys@getposition{\csname save@pt@\tmk@label\endcsname}\save@orig@pic%
\pgfsys@getposition{\pgfpictureid}\save@this@pic%
\pgf@process{\pgfpointorigin\save@this@pic}%
\pgf@xa=\pgf@x
\pgf@ya=\pgf@y
\pgf@process{\pgfpointorigin\save@orig@pic}%
\advance\pgf@x by -\pgf@xa
\advance\pgf@y by -\pgf@ya
}%
}
\newcommand\tikzmark[2][]{%
\tikz[remember picture with id=#2] {#1;}}
\makeatother
% end of Andrew's code
\newcommand\ShadeCell[4][0pt]{%
\begin{tikzpicture}[overlay,remember picture]%
\shade[#4] ( $ (pic cs:#2) + (0pt,1.9ex) $ ) rectangle ( $ (pic cs:#3) + (0pt,-#1*\baselineskip-.8ex) $ );
\end{tikzpicture}%
}%
\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}
\HatchedCell{start1}{end1}{%
pattern color=black!70,pattern=north east lines}
\HatchedCell{start2}{end2}{%
pattern color=black!70,pattern=north west lines}
\HatchedCell{start3}{end3}{%
pattern color=black!70,pattern=north east lines}
\HatchedCell{start4}{end4}{%
pattern color=black!70,pattern=north west lines}
\HatchedCell{start5}{end5}{%
pattern color=black!70,pattern=north east lines}
\HatchedCell{start6}{end6}{%
pattern color=black!70,pattern=north west lines}
\newcommand*{\hatch}[2]{\multicolumn{#2}{!{\hspace*{-0.4pt}\tikzmark{start#1}}c!{\tikzmark{end#1}}}{}}
Testing table positioning:
\begin{table}
\centering
\rowcolors{2}{gray!10}{white}
\noindent\begin{tabular}{lrrrrrrrr}
\toprule
Activity & \multicolumn{8}{c}{month range} \\
& \scriptsize{0--6} & \scriptsize{7--12} & \scriptsize{12--18} & \scriptsize{19--24} & \scriptsize{25-30} & \scriptsize{31-36} & \scriptsize{37--42} & \scriptsize{42--48} \\
\toprule
row 1 & \hatch{1}{2} & & & & & & \\
row 2 & & \hatch{2}{2} & \\
row 3 & & \hatch{3}{3} & & & & \\
row 4 & & \hatch{4}{6} \\
row 5 & & & & & &\hatch{5}{3} \\
row 6 & \hatch{6}{8} \\
\bottomrule
\rowcolor{white}
\textsc{Milestones} & & & \multicolumn{1}{r|}{\rom{1}$\rightarrow$}& & \multicolumn{1}{r|}{\rom{2}$ \rightarrow$}&&&\\
\end{tabular}
\label{tab:work_plan}
\end{table}
\end{document}


tikzmarklibrary to avoid all those stuff. He packaged it on CTAN – percusse Mar 30 '15 at 21:12tableenvironment, you have a bug ! Call\Hatched...macros after your tabular... – Paul Gaborit Mar 31 '15 at 00:08