3

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{\hatchspread}
\newlength{\hatchthickness}
\newlength{\hatchshift}
\newcommand{\hatchcolor}{}
\newcommand{\hatchangle}{} % NEW
% declaring the keys in tikz
\tikzset{hatchspread/.code={\setlength{\hatchspread}{#1}},
         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
\tikzset{hatchspread=3pt,
         hatchthickness=0.4pt,
         hatchshift=0pt,% must be >= 0
         hatchcolor=black,
         hatchangle=0}
% declaring the pattern
\pgfdeclarepatternformonly[\hatchangle,\hatchspread,\hatchthickness,\hatchshift,\hatchcolor]% variables
   {vlines}% name
   {\pgfqpoint{\dimexpr-2\hatchthickness}{\dimexpr-2\hatchthickness}}% lower left corner
   {\pgfqpoint{\dimexpr\hatchspread+2\hatchthickness}{\dimexpr\hatchspread+2\hatchthickness}}% upper right corner
   {\pgfqpoint{\dimexpr\hatchspread}{\dimexpr\hatchspread}}% tile size
   {% shape description
    \pgfsetlinewidth{\hatchthickness}
    \pgfpathmoveto{\pgfqpoint{0pt}{\dimexpr\hatchspread+\hatchshift}}
    \pgfpathlineto{\pgfqpoint{\dimexpr\hatchspread+0.15pt+\hatchshift}{-0.15pt}}
    \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
  • 1
    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
  • 1
    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
3

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\hatchspread
\newdimen\hatchthickness
\newdimen\hatchshift
\tikzset{
    hatchspread/.code={\hatchspread=#1},
    hatchthickness/.code={\hatchthickness=#1},
    hatchshift/.code={\hatchshift=#1},
    hatchcolor/.code={\def\hatchcolor{#1}},
    hatchangle/.code={\pgfmathsetmacro\hatchangle{Mod(#1,180)}},
    hatchspread=3pt,
    hatchthickness=0.4pt,
    hatchshift=0pt,
    hatchcolor=black,
    hatchangle=-30
}
\pgfdeclarepatternformonly[\hatchangle,\hatchspread,\hatchthickness,\hatchshift,\hatchcolor]
    {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
            \pgfmathsetmacro\patternwidth{\hatchspread*cosec(\hatchangle)}\xdef\patternwidth{\patternwidth}
            \pgfmathsetmacro\patternheight{\hatchspread*sec(\hatchangle)}\xdef\patternheight{\patternheight}
            \pgfpathmoveto{\pgfqpoint{0pt}{0pt}}
            \pgfpathlineto{\pgfqpoint{\patternwidth pt}{\patternheight pt}}
        \else
            \pgfmathsetmacro\patternwidth{\hatchspread*cosec(\hatchangle)}\xdef\patternwidth{\patternwidth}
            \pgfmathsetmacro\patternheight{-\hatchspread*sec(\hatchangle)}\xdef\patternheight{\patternheight}
            \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.

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.