# Custom tikz pattern: hatchangle

In PSTricks there exist the fillstyles vlines, hlines, crosshatch which have counterparts in tikz patterns library.

However, in PSTricks they can be modified by following options: hatchwidth, hatchwidthinc, hatchsep, hatchsepinc, hatchcolor and hatchangle.

The answer to the question Custom and built in TikZ fill patterns declares a pgfpattern with variables: hatchthickness, hatchspread and hatchcolor.

I am missing hatchangle.

If someone is more familiar with pgf basic layer to adjust this, I am happy to see this as an answer.

BTW, I am aware of Rotate the fill pattern with the shape in tikz [duplicate]. But I think declaring a custom pattern with a variable hatchangle should be possible.

(pgf/tikz version 3.0, Dec 2013)

Relevant code which has to be modified

% defining the new dimensions and parameters
\newlength{\hatchthickness}
\newlength{\hatchshift}
\newcommand{\hatchcolor}{}
\newcommand{\hatchangle}{} % NEW
% declaring the keys in tikz
hatchthickness/.code={\setlength{\hatchthickness}{#1}},
hatchshift/.code={\setlength{\hatchshift}{#1}},% must be >= 0
hatchcolor/.code={\renewcommand{\hatchcolor}{#1}},
hatchangle/.code={\renewcommand{\hatchangle}{#1}}}
% setting the default values
hatchthickness=0.4pt,
hatchshift=0pt,% must be >= 0
hatchcolor=black,
hatchangle=0}
% declaring the pattern
{vlines}% name
{\pgfqpoint{\dimexpr-2\hatchthickness}{\dimexpr-2\hatchthickness}}% lower left corner
{% shape description
\pgfsetlinewidth{\hatchthickness}
\ifdim \hatchshift > 0pt
\pgfpathmoveto{\pgfqpoint{0pt}{\hatchshift}}
\pgfpathlineto{\pgfqpoint{\dimexpr0.15pt+\hatchshift}{-0.15pt}}
\fi
\pgfsetstrokecolor{\hatchcolor}
%    \pgfsetdash{{1pt}{1pt}}{0pt}% dashing cannot work correctly in all situation this way
\pgfusepath{stroke}
}

• Hatches are not continuous lines but small patches tiled up. If you have different angled lines you need to make sure that lines ... line up continously. That's why it is not that simple to achieve that. If you use angles then hatch density must be commensurable with the number of hatch lines to guarantee continuity. – percusse Jul 24 '14 at 10:25
• Do you know how pstricks achieves this? – Hotschke Jul 24 '14 at 10:27
• Have a look at TikZ patterns with dimensions parameters by Alberto Sartori. It shows how to modify patterns. – Andrew Jul 24 '14 at 12:38
• Hotschke: No, not really. @Andrew He is luckily one of the residents and here is a related one tex.stackexchange.com/questions/119710/…. – percusse Jul 24 '14 at 13:37

Mathematically this is how you implement line-patterns with various angles.

# CAUTION: Do not try this with angle closed to 0, 90, 180, etc!!

\documentclass[border=9,tikz]{standalone}
\usetikzlibrary{patterns}
\begin{document}

\newdimen\hatchthickness
\newdimen\hatchshift
\tikzset{
hatchthickness/.code={\hatchthickness=#1},
hatchshift/.code={\hatchshift=#1},
hatchcolor/.code={\def\hatchcolor{#1}},
hatchangle/.code={\pgfmathsetmacro\hatchangle{Mod(#1,180)}},
hatchthickness=0.4pt,
hatchshift=0pt,
hatchcolor=black,
hatchangle=-30
}
{lines}
{\pgfqpoint{\dimexpr-2\hatchthickness}{\dimexpr-2\hatchthickness}}
{\pgfqpoint{\dimexpr\patternwidth pt+2\hatchthickness}{\dimexpr\patternheight pt+2\hatchthickness}}
{\pgfpoint{\patternwidth}{\patternheight}} % \patternwidth and patternheight is calculated as follows
{
\pgfsetlinewidth{\hatchthickness}
\ifdim\hatchangle pt<90pt
\pgfpathmoveto{\pgfqpoint{0pt}{0pt}}
\pgfpathlineto{\pgfqpoint{\patternwidth pt}{\patternheight pt}}
\else
\pgfpathmoveto{\pgfqpoint{\patternwidth pt}{0pt}}
\pgfpathlineto{\pgfqpoint{0pt}{\patternheight pt}}
\fi
\pgfsetstrokecolor{\hatchcolor}
\pgfusepath{stroke}
}

\foreach\i in{5,15,...,360}{
\tikz\filldraw[pattern=lines,hatchangle=\i](0,0)rectangle(1,1);
}

\end{document}


By the way, I ignore the shift option. It is not the core part of the implementation.

### Tikz library patterns.meta by Mark Wibrow

provides the patterns Lines[angle=22.50] and Hatch[angle=22.5]

\documentclass[tikz]{standalone}
\usetikzlibrary{patterns, patterns.meta}
\begin{document}
\begin{tikzpicture}
\draw[pattern={horizontal lines},pattern color=orange]
(0,0) rectangle +(1,1);
\draw[pattern={Lines[yshift=-10pt]},pattern color=blue]
(0,0) rectangle +(1,1);
\draw[pattern={vertical lines},pattern color=orange]
(1,0) rectangle +(1,1);
\draw[pattern={Lines[angle=90,yshift=-2pt]},pattern color=blue]
(1,0) rectangle +(1,1);
\draw[pattern={north east lines},pattern color=orange]
(0,1) rectangle +(1,1);
\draw[pattern={Lines[angle=45,distance={3pt/sqrt(2)}]},pattern color=blue]
(0,1) rectangle +(1,1);
\draw[pattern={north west lines},pattern color=orange]
(1,1) rectangle +(1,1);
\draw[pattern={Lines[angle=-22.5,distance={8pt/sqrt(2)}]},pattern color=blue]
(1,1) rectangle +(1,1);
\end{tikzpicture}
\end{document}


This library is marked in pgf/tikz version 3.1 as experimental.