# Creating patterns

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?

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}

• This is just to let you know that I really liked this question of yours, and would like to ask you not to get discouraged by comments of certain users. (BTW, this question has an arguably simpler answer since now the patterns.meta library is available.)
– user194703
Mar 7 '20 at 21:49

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}


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.

• When I use your solution I get teeth, even bigger ones, on the other side?! Jul 2 '19 at 19:13
• @Pygmalion Hmmh, strange. I slightly adjusted the parameters. Is it better now?
– user121799
Jul 2 '19 at 23:02
• Yes, now it is fine. Jul 4 '19 at 20:22

By enlarging the bounding box sufficiently, the problem is solved.

According to my calculations, it must be enlarged by at least 71%.

To simplify, I expanded it by an integer multiple of \hatchdistance.

{\pgfqpoint{-\hatchdistance}{-\hatchdistance}}% below left
{\pgfqpoint{2\hatchdistance}{2\hatchdistance}}% above right


### hatch not really straight

The hatches are not perfectly straight (like the default ones in the patterns library) but they remain unchanged by scaling as shown in the above result.

### Straight hatch

To do this, I enlarged the line that traces the hatching of an integer multiple \hatchdistance (noninteger multiples pose problems that I have not yet understood).

\pgfpathmoveto{\pgfqpoint{2\hatchdistance}{-\hatchdistance}}
\pgfpathlineto{\pgfqpoint{-\hatchdistance}{2\hatchdistance}}


### Code for really straight hatch:

\documentclass[tikz,border=5mm]{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{-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{-\hatchdistance}{-\hatchdistance}}% below left
{\pgfqpoint{2\hatchdistance}{2\hatchdistance}}% above right
{\pgfpoint{\hatchdistance}{\hatchdistance}}%
{
\pgfsetcolor{\tikz@pattern@color}
\pgfsetlinewidth{\hatchthickness}
\pgfpathmoveto{\pgfqpoint{2\hatchdistance}{-\hatchdistance}}
\pgfpathlineto{\pgfqpoint{-\hatchdistance}{2\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,scale=2] (.6,0) rectangle +(1,1);

\end{tikzpicture}
\end{document}


### Code for hatch that are not really straight:

\documentclass[tikz,border=5mm]{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{-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{-\hatchdistance}{-\hatchdistance}}% below left
{\pgfqpoint{2\hatchdistance}{2\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,scale=2] (1,0) rectangle +(1,1);

\end{tikzpicture}
\end{document}

• Not at all, your first answer adds dimensions with the macro \pgfqpoint. This is prohibited see the manual. Jul 3 '19 at 8:12
• @AndréC I am trying to see the statement that you have mentioned, since I am unfamiliar with \pgfqpoint, could you tell me where in the manual it is mentioned that the using dimensions with macro \pgfqpoint is not allowed/prohibited? Jul 3 '19 at 8:42
• @Raaja see page 1205 of 3.1.3 manual. I quote: This command does the same as \pgfpoint, but x and y must be simple dimensions like 1pt or 1cm. Things like 2ex or 2cm+1pt are not allowed. Jul 3 '19 at 8:52
• @AndréC Thanks for pointing me to the location. Correct me if I am wrong, doesn't it tries to say not to mix-up different units? I think using same units should not be a problem per se. Because as I see in the previous answer: \tikzset{hatch distance/.store in=\hatchdistance,hatch distance=5pt,hatch thickness/.store in=\hatchthickness,hatch thickness=5pt} both are in the same unit. So, I think it is correct. Jul 3 '19 at 9:22
• ???? It is you who uses \pgfpoint{\hatchdistance-1pt}{\hatchdistance-1pt}. Again, you write "By enlarging the bounding box sufficiently, the problem is solved.", which is incorrect, and also not what you are doing. Please stay with the facts and do not make incorrect statements about my answer, which explains things, and which you essentially copy.
– user121799
Jul 3 '19 at 12:24

Thanks to the patterns.meta library, this question has now an arguably simpler answer.

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{patterns.meta}
\pgfdeclarepattern{
name=hatch, parameters={\hatchsize,\hatchangle,\hatchlinewidth},
bottom left={\pgfpoint{-.1pt}{-.1pt}},
top right={\pgfpoint{\hatchsize+.1pt}{\hatchsize+.1pt}}, tile size={\pgfpoint{\hatchsize}{\hatchsize}},
tile transformation={\pgftransformrotate{\hatchangle}}, code={
\pgfsetlinewidth{\hatchlinewidth}
\pgfpathmoveto{\pgfpoint{-.1pt}{\hatchsize/2}}
\pgfpathlineto{\pgfpoint{\hatchsize+.1pt}{\hatchsize/2}}
\pgfusepath{stroke}
} }
\tikzset{
hatch size/.store in=\hatchsize,
hatch angle/.store in=\hatchangle,
hatch line width/.store in=\hatchlinewidth,
hatch size=5pt,
hatch angle=0pt,
hatch line width=.5pt,
}
\begin{document}
\begin{tikzpicture}
\draw [pattern=hatch, pattern color=orange, hatch size=7pt,hatch line width=4pt,
hatch angle=45] (0,0) rectangle ++(2,2);
\draw [pattern=hatch, pattern color=orange, hatch size=7pt,hatch line width=4pt,
hatch angle=-45] (2.4,0) rectangle ++(2,2);
\end{tikzpicture}
\end{document}