[Tex/LaTex] Creating patterns

patterntikz-pgf

Using north east hatch from the answer to this question Filling rectangle in TikZ with more than one color I wanted to make a north west hatch. However, it has funny teeth I do not know how to get rid of. What is wrong and how can I correct that?

enter image description here

MWE:

\documentclass{minimal}

\usepackage{tikz}

\begin{document}

\begin{tikzpicture}


\usetikzlibrary{patterns}

\makeatletter
\tikzset{hatch distance/.store in=\hatchdistance,hatch distance=5pt,hatch thickness/.store in=\hatchthickness,hatch thickness=5pt}

\pgfdeclarepatternformonly[\hatchdistance,\hatchthickness]{north east hatch}% name
    {\pgfqpoint{-1pt}{-1pt}}% below left
    {\pgfqpoint{\hatchdistance}{\hatchdistance}}% above right
    {\pgfpoint{\hatchdistance-1pt}{\hatchdistance-1pt}}%
    {
        \pgfsetcolor{\tikz@pattern@color}
        \pgfsetlinewidth{\hatchthickness}
        \pgfpathmoveto{\pgfqpoint{0pt}{0pt}}
        \pgfpathlineto{\pgfqpoint{\hatchdistance}{\hatchdistance}}
        \pgfusepath{stroke}
    }
\pgfdeclarepatternformonly[\hatchdistance,\hatchthickness]{north west hatch}% name
    {\pgfqpoint{-1pt}{-1pt}}% below left
    {\pgfqpoint{\hatchdistance}{\hatchdistance}}% above right
    {\pgfpoint{\hatchdistance-1pt}{\hatchdistance-1pt}}%
    {
        \pgfsetcolor{\tikz@pattern@color}
        \pgfsetlinewidth{\hatchthickness}
        \pgfpathmoveto{\pgfqpoint{\hatchdistance}{0pt}}
        \pgfpathlineto{\pgfqpoint{0pt}{\hatchdistance}}
        \pgfusepath{stroke}
    }
\makeatother

\draw[pattern=north east hatch,hatch distance=7pt,hatch thickness=3pt,pattern color=orange] (0,0) rectangle +(1,1);
\draw[pattern=north west hatch,hatch distance=7pt,hatch thickness=3pt,pattern color=orange] (2,0) rectangle +(1,1);


\end{tikzpicture}
\end{document}

Best Answer

You do what one usually does: define the cell size a bit smaller than what the path actually covers. The problem is that if the width of the line exceeds 0.5pt, an overshoot by 1pt is no longer sufficient. So one may want to use a more generous overshoot. The following may be a bit "too generous" (but being generous has no real sideeffects here AFAIK), and this one seems to work for many viewers and zoom levels, i.e. some of the things but not all in your code are also viewer issues.

\documentclass[tikz,border=3.14mm]{standalone}

\begin{document}

\begin{tikzpicture}


\usetikzlibrary{patterns}

\makeatletter
\tikzset{hatch distance/.store in=\hatchdistance,hatch distance=5pt,hatch thickness/.store in=\hatchthickness,hatch thickness=5pt}

\pgfdeclarepatternformonly[\hatchdistance,\hatchthickness]{north east hatch}% name
    {\pgfqpoint{-\hatchthickness}{-\hatchthickness}}% below left
    {\pgfqpoint{\hatchdistance+\hatchthickness}{\hatchdistance+\hatchthickness}}% above right
    {\pgfpoint{\hatchdistance}{\hatchdistance}}%
    {
        \pgfsetcolor{\tikz@pattern@color}
        \pgfsetlinewidth{\hatchthickness}
        \pgfpathmoveto{\pgfqpoint{-\hatchthickness}{-\hatchthickness}}       
        \pgfpathlineto{\pgfqpoint{\hatchdistance+\hatchthickness}{\hatchdistance+\hatchthickness}}
        \pgfusepath{stroke}
    }
\pgfdeclarepatternformonly[\hatchdistance,\hatchthickness]{north west hatch}% name
    {\pgfqpoint{-\hatchthickness}{-\hatchthickness}}% below left
    {\pgfqpoint{\hatchdistance+\hatchthickness}{\hatchdistance+\hatchthickness}}% above right
    {\pgfpoint{\hatchdistance}{\hatchdistance}}%
    {
        \pgfsetcolor{\tikz@pattern@color}
        \pgfsetlinewidth{\hatchthickness}
        \pgfpathmoveto{\pgfqpoint{\hatchdistance+\hatchthickness}{-\hatchthickness}}
        \pgfpathlineto{\pgfqpoint{-\hatchthickness}{\hatchdistance+\hatchthickness}}
        \pgfusepath{stroke}
    }
\makeatother

\draw[pattern=north east hatch,hatch distance=7pt,hatch thickness=3pt,pattern color=orange] (0,0) rectangle +(1,1);
\draw[pattern=north west hatch,hatch distance=7pt,hatch thickness=3pt,pattern color=orange] (2,0) rectangle +(1,1);


\end{tikzpicture}
\end{document}

enter image description here

EXCITING NEWS: Soon the customization of patterns will be much easier, because of the pattern.meta library, see here. I thank JouleV for pointing this out.