4

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:

enter image description here

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}
4
  • 1
    You can use tikzmark library to avoid all those stuff. He packaged it on CTAN – percusse Mar 30 '15 at 21:12
  • To avoid what exactly? – aaragon Mar 30 '15 at 21:16
  • The initial page long code. You make a table and tikzmark defines a position on the page but then TeX takes over and makes it a float and depending on the page fullness it moves it around so such that the patterns probably loose their initial position. I think the new version is more robust to such things. But I'm just speculating – percusse Mar 30 '15 at 21:28
  • Event without any table environment, you have a bug ! Call \Hatched... macros after your tabular... – Paul Gaborit Mar 31 '15 at 0:08
7

First, as percusse says, you won't need that page long initial code but load \usetikzlibrary{tikzmark}. Second, \HatchedCell should come after tabular as noted by Paul Gaborit and inside table environment. Further, I have used \newcommand{\rom}{} as you didn't tell. Also, watch out for some comments I have made inside the code.

Here is the polished code

\documentclass[10pt,a4paper]{article}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor} % use color
\usepackage{booktabs} % commands for table rules
\usepackage{tikz}
\usetikzlibrary{calc,shadings,patterns,tikzmark}

\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}%
}%

\newcommand*{\hatch}[2]{\multicolumn{#2}{!{\hspace*{-0.4pt}\tikzmark{start#1}}c!{\tikzmark{end#1}}}{}}
\newcommand{\rom}{}
\begin{document}



Testing table positioning:

\begin{table}[htb]  %% better use [htb]
  \centering
  \rowcolors{2}{gray!10}{white}
\begin{tabular}{lrrrrrrrr}        %% \noindent not needed here
\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}         %% \label without a \caption is useless
%
\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}
%
\end{table}
\end{document}

enter image description here

The multiple \HatchedCell lines can be reduced by using \foreach loops like

\foreach \x/\y  in {1/1,3/3,5/5}{
\HatchedCell{start\x}{end\y}{%
  pattern color=black!70,pattern=north east lines}
}
\foreach \x/\y  in {2/2,4/4,6/6}{
\HatchedCell{start\x}{end\y}{%
  pattern color=black!70,pattern=north west lines}
}
%
7
  • That did it, thanks! Is there a way to simplify the \HatchedCell code? There seems to be a lot of repetition that could probably be avoided. – aaragon Mar 31 '15 at 8:46
  • @aaragon Looks OK to me. Is there any thing missing? – user11232 Mar 31 '15 at 9:51
  • I mean, there is a \HatchedCell{start1}{end1}{% for each row, maybe that could me improved? – aaragon Mar 31 '15 at 12:34
  • 1
    @aaragon Oh. I got it now. You can use a \foreach loop. Crude one is \foreach \x/\y in {1/1,3/3,5/5}{ \HatchedCell{start\x}{end\y}{% pattern color=black!70,pattern=north east lines} } \foreach \x/\y in {2/2,4/4,6/6}{ \HatchedCell{start\x}{end\y}{% pattern color=black!70,pattern=north west lines} } – user11232 Mar 31 '15 at 12:57
  • That's indeed a more elegant way to do it. It would be nice to update your answer, wouldn't it? – aaragon Mar 31 '15 at 13:26

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.