3

I create a pattern and want to change the lines' density when I invoke this pattern. What I have got for now:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{patterns}

\newdimen\density
\tikzset{
    density/.code={\density=#1},
    density=3pt
}

\makeatletter
\pgfdeclarepatternformonly[\density]{resizable north east lines}
{\pgfqpoint{-1pt}{-1pt}}
{\pgfqpoint{\density}{\density}}
{\pgfqpoint{\density}{\density}}
{
    \pgfsetcolor{\tikz@pattern@color}
    \pgfsetlinewidth{0.4pt}
    \pgfpathmoveto{\pgfqpoint{0pt}{0pt}}
    \pgfpathlineto{\pgfqpoint{\density + 0.1pt}{\density + 0.1pt}}
    \pgfusepath{stroke}
}
\makeatother

\begin{document}
\begin{tikzpicture}

    \node [minimum size = 10 cm, draw = red, line width = 2pt] at (0,0) {};
    \draw [density=10pt, pattern=resizable north east lines, pattern color = red] (0,0) circle[radius = 5cm];

\end{tikzpicture}
\end{document}

How do I define a variable with space in order to invoke it using the following style? (see the pattern density)

\draw [pattern density=10pt, pattern=resizable north east lines, pattern color = red] (0,0) circle[radius = 5cm];
  • I didn't know you couls makeatletter without makeatother cool stuff. – Thorbjørn E. K. Christensen Mar 14 '18 at 13:14
  • I realize now that it was a mistake and not magic, how sad ;-( – Thorbjørn E. K. Christensen Mar 14 '18 at 13:55
  • that is because I have updated the code according to your comment. :) – aban Mar 14 '18 at 14:02
  • I figured, I was just hoping for magic. ;-) because magic is cool! – Thorbjørn E. K. Christensen Mar 14 '18 at 14:04
1

Something like this?

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{patterns}

\makeatletter
\newdimen\pattern@density
\tikzset{/tikz/.cd,
    pattern density/.store in=\density,
    pattern density=3pt
}

\pgfdeclarepatternformonly[\density]{resizable north east lines}
{\pgfqpoint{-1pt}{-1pt}}
{\pgfqpoint{\density}{\density}}
{\pgfqpoint{\density}{\density}}
{
    \pgfsetcolor{\tikz@pattern@color}
    \pgfsetlinewidth{0.4pt}
    \pgfpathmoveto{\pgfqpoint{0pt}{0pt}}
    \pgfpathlineto{\pgfqpoint{\density + 0.1pt}{\density + 0.1pt}}
    \pgfusepath{stroke}
}
\makeatother

\begin{document}
\begin{tikzpicture}

    \node [minimum size = 10 cm, draw = red, line width = 2pt] at (0,0) {};
    \draw [pattern density=9pt, pattern=resizable north east lines, pattern color = red] (0,0) circle[radius = 5cm];

\end{tikzpicture}
\end{document}
  • Very nice trick. May I define the pattern densitylike this \newdimen\pattern@density. Is there a similar way? – aban Mar 14 '18 at 13:19
  • @aban I made an update. – user121799 Mar 14 '18 at 13:25

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.