5

I was redrawing a picture with TikZ and here is the original:

Screenshot

I don't have any problems to draw this, but as you can see in my MWE, I've just scopped some areas to make manually a pattern which have the density which I want. But first, here is my MWE:

\documentclass[border=5pt,tikz]{standalone}
\usetikzlibrary{arrows,patterns}
\begin{document}
    \begin{tikzpicture}[>=stealth,every node/.style={font=\tiny}]
            \draw[very thick] (0,5) -- (0,0) -- (5,0) -- (5,5);
                \fill[pattern=north east lines,very thick] (0,4.98) -- (0,0) -- (5,0) -- (5,4.98);
            \draw[very thick,fill=white] (.2,5) -- (.2,.2) -- (4.8,.2) -- (4.8,5);
            \draw[very thick] (.2,1) -- (4.8,1);
            \draw[very thick] (.4,5) -- (.4,1) -- (4.6,1) -- (4.6,5) to[bend left=30] (2.5,5) to[bend right=30] (.4,5) -- cycle;
                \draw[very thick] (4.6,5) to[bend right=30] (2.5,5);
                \begin{scope}
                    \clip[draw] (2.5,5) to[bend right=30] (4.6,5) to[bend right=30] (2.5,5) -- cycle;
                    \foreach \x in {1,2,...,14}
                    {
                        \draw[xshift=4.5cm,yshift=5cm,xshift=-2cm,xshift=4*\x] (120:1) -- (-60:1);
                    }
                \end{scope}
    %       \fill[pattern=north west lines] (2.5,5) to[bend right=30] (4.6,5) to[bend right=30] (2.55,5) -- cycle;
            \begin{scope}
                \clip[draw] (.4,1.2) -- (4.6,1.2) -- (4.6,1) -- (.4,1) -- cycle;
                \foreach \x in {1,2,...,70}
                {
                    \draw[xshift=2cm,yshift=1.1cm,xshift=-2cm,xshift=2*\x] (30:.5) -- (210:.5);
                }
            \end{scope}
            \draw[very thick] (.4,1.2) -- (4.6,1.2);
                \draw[very thick,fill=white] (2.2,.9) rectangle (2.8,1.3);
            \draw (2.2,1) -- (2.8,1);
            \draw (2.2,1.2) -- (2.8,1.2);
                    \draw[ultra thin,dash pattern={on 5pt off 2pt on 1pt off 3pt}] (2.5,.5) --+ (0,5);
                \draw[<->] (.4,3.7) -- (4.6,3.7) node[midway,fill=white] {$\stackrel{d_a}{(2r_a)}$};
            \draw[ultra thick,<-] (2.5,5) --+ (0,2) node[left] {$P$};
                \draw (2.2,.9) --+ (0,-1.5);
                \draw (2.8,.9) --+ (0,-1.5);
            \draw[thick,<-] (2.2,-.4) --+ (-.7,0);
            \draw[thick,<-] (2.8,-.4) --+ (.7,0);
                \node[below=2] at (2.5,0) {$d_i$};
    \end{tikzpicture}
\end{document}

Here is the output:

Screenshot

And now my question:

How can I change the predefined density of the patterns from the tikzlibrary patterns in generally (with some parameters, so that you have a usage like … density=200 … for example)?

P.S.: Not like here: How to change the density of a pattern?

  • 2
  • 1
    When you are saying not like this answer, which part of that answer do you dislike? Judging from @percusse's link and the pgfmanual, the only way to go seems to define your own pattern. – user121799 Aug 3 '18 at 22:13
  • 1
    Perhaps see also here in case you want to use the pattern color key. – user121799 Aug 3 '18 at 22:17
  • 1
    I guess you need to "redo" the patterns for this. The good news is that this will be rather straightforward: just copy the contents of the library and add some keys. – user121799 Aug 3 '18 at 22:19
  • 2
    Yes, but you can achieve this with keys. Just copy the file, rename e.g. north east lines to flexible north east lines and then add the keys. See e.g. this question and its answers to see how that works. Of course, you could just make your question here more specific and hope that someone does that. In general, I think that would be a rather nice and useful addition to the TikZ libraries (unless I am missing something). – user121799 Aug 3 '18 at 22:26
6

In his answer to Pgfplots: how to fill the area under a curve with oblique lines (hatching) as a pattern?, Jake defines a north east hatch whit two parameters: hatch distance and hatch thickness. You can use this pattern and adjust these parameters to your taste.

Here you have an example:

\documentclass[border=5pt,tikz]{standalone}
\usetikzlibrary{arrows,patterns}

%See https://tex.stackexchange.com/a/29367/1952
\makeatletter
\tikzset{% customization of pattern 
        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}
    }
\makeatother

\begin{document}
\begin{tikzpicture}[>=stealth,every node/.style={font=\tiny}]
    \draw[very thick] (0,5) -- (0,0) -- (5,0) -- (5,5);
   \fill[pattern=north east lines,very thick] (0,4.98) -- (0,0) -- (5,0) -- (5,4.98);
   \draw[very thick,fill=white] (.2,5) -- (.2,.2) -- (4.8,.2) -- (4.8,5);
   \draw[very thick] (.2,1) -- (4.8,1);
   \draw[very thick] (.4,5) -- (.4,1) -- (4.6,1) -- (4.6,5) to[bend left=30] (2.5,5) to[bend right=30] (.4,5) -- cycle;
   \draw[very thick] (4.6,5) to[bend right=30] (2.5,5);

   \pattern[pattern=north east hatch, hatch distance=3mm, hatch thickness=.5pt] (2.5,5) to[bend right=30] (4.6,5) to[bend right=30] (2.5,5) -- cycle;

   \pattern[pattern=north east hatch, hatch distance=1mm, hatch thickness=.5pt] (.4,1.2) -- (4.6,1.2) -- (4.6,1) -- (.4,1) -- cycle;

   \draw[very thick] (.4,1.2) -- (4.6,1.2);
   \draw[very thick,fill=white] (2.2,.9) rectangle (2.8,1.3);
   \draw (2.2,1) -- (2.8,1);
   \draw (2.2,1.2) -- (2.8,1.2);
   \draw[ultra thin,dash pattern={on 5pt off 2pt on 1pt off 3pt}] (2.5,.5) --+ (0,5);
   \draw[<->] (.4,3.7) -- (4.6,3.7) node[midway,fill=white] {$\stackrel{d_a}{(2r_a)}$};
   \draw[ultra thick,<-] (2.5,5) --+ (0,2) node[left] {$P$};
   \draw (2.2,.9) --+ (0,-1.5);
   \draw (2.8,.9) --+ (0,-1.5);
   \draw[thick,<-] (2.2,-.4) --+ (-.7,0);
   \draw[thick,<-] (2.8,-.4) --+ (.7,0);
   \node[below=2] at (2.5,0) {$d_i$};
\end{tikzpicture}
\end{document}

enter image description here

2

Warning: This is not an answer, there are still things that need to get fixed. But this might be a starting point. If somebody else completes this in the meantime, I will be happy to delete this post. What I did was to copy some of the patterns from the pgf library patterns and started to make them more flexible. This works, to some extent.

\documentclass[border=5pt,tikz]{standalone}
\usetikzlibrary{arrows,patterns}
\makeatletter
\newlength{\flex@pattern@density}
\newlength{\flex@pattern@linewidth}
\newlength{\flex@pattern@auxlength}
\newlength{\flex@pattern@auxlengthtwo}
\tikzset{/tikz/.cd,
    pattern density/.code={\setlength\flex@pattern@density{#1}
    \pgfmathsetmacro{\tmp}{1.1*#1}
    \setlength\flex@pattern@auxlength{\tmp pt}
    \setlength\flex@pattern@auxlengthtwo{#1}
    \advance\flex@pattern@auxlengthtwo by 0.1pt
    \typeout{\the\flex@pattern@density,\the\flex@pattern@auxlength}},
    pattern density=3pt,
    pattern line width/.code={\setlength\flex@pattern@linewidth{#1}},
    pattern line width=0.4pt,
}


\pgfdeclarepatternformonly[\flex@pattern@density,\flex@pattern@linewidth]{flexible horizontal lines}{\pgfpointorigin}{\pgfqpoint{100pt}{1pt}}{\pgfqpoint{100pt}{\flex@pattern@density}}%
{
  \pgfsetlinewidth{\flex@pattern@linewidth}
  \pgfsetcolor{\tikz@pattern@color}
  \pgfpathmoveto{\pgfqpoint{0pt}{0.5pt}}
  \pgfpathlineto{\pgfqpoint{100pt}{0.5pt}}
  \pgfusepath{stroke}
}

\pgfdeclarepatternformonly[\flex@pattern@density,\flex@pattern@linewidth]{flexible vertical lines}{\pgfpointorigin}{\pgfqpoint{1pt}{100pt}}{\pgfqpoint{\flex@pattern@density}{100pt}}%
{
  \pgfsetlinewidth{\flex@pattern@linewidth}
  \pgfsetcolor{\tikz@pattern@color}
  \pgfpathmoveto{\pgfqpoint{0.5pt}{0pt}}
  \pgfpathlineto{\pgfqpoint{0.5pt}{100pt}}
  \pgfusepath{stroke}
}

\pgfdeclarepatternformonly[\flex@pattern@auxlengthtwo,\flex@pattern@density,\flex@pattern@linewidth,\flex@pattern@auxlength]{flexible north east lines}{\pgfqpoint{-1pt}{-1pt}}{\pgfqpoint{\flex@pattern@auxlength}{\flex@pattern@auxlength}}{\pgfqpoint{\flex@pattern@density}{\flex@pattern@density}}%
{
  \pgfsetlinewidth{\flex@pattern@linewidth}
  \pgfsetcolor{\tikz@pattern@color}
  \pgfpathmoveto{\pgfqpoint{0pt}{0pt}}
  \pgfpathlineto{\pgfqpoint{\flex@pattern@auxlengthtwo}{\flex@pattern@auxlengthtwo}}
  \pgfusepath{stroke}
}

\pgfdeclarepatternformonly[\flex@pattern@auxlengthtwo,\flex@pattern@density,\flex@pattern@linewidth,\flex@pattern@auxlength]{flexible north west lines}{\pgfqpoint{-1pt}{-1pt}}{\pgfqpoint{\flex@pattern@auxlength}{\flex@pattern@auxlength}}{\pgfqpoint{\flex@pattern@density}{\flex@pattern@density}}%
{
  \pgfsetlinewidth{\flex@pattern@linewidth}
  \pgfsetcolor{\tikz@pattern@color}
  \pgfpathmoveto{\pgfqpoint{\flex@pattern@auxlengthtwo}{0pt}}
  \pgfpathlineto{\pgfqpoint{0pt}{\flex@pattern@auxlengthtwo}}
  \pgfusepath{stroke}
}


% Crossed lines in different directions

\pgfdeclarepatternformonly[\flex@pattern@density,\flex@pattern@linewidth,\flex@pattern@auxlength,\tikz@pattern@color]{flexible grid}{\pgfqpoint{-1pt}{-1pt}}{\pgfqpoint{\flex@pattern@auxlength}{\flex@pattern@auxlength}}{\pgfqpoint{\flex@pattern@density}{\flex@pattern@density}}%
{
  \pgfsetlinewidth{\flex@pattern@linewidth}
  \pgfsetcolor{\tikz@pattern@color}
  \pgfpathmoveto{\pgfqpoint{0pt}{0pt}}
  \pgfpathlineto{\pgfqpoint{0pt}{\flex@pattern@density}}
  \pgfpathmoveto{\pgfqpoint{0pt}{0pt}}
  \pgfpathlineto{\pgfqpoint{\flex@pattern@density}{0pt}}
  \pgfusepath{stroke}
}

\pgfdeclarepatternformonly[\flex@pattern@density,\flex@pattern@linewidth,\flex@pattern@auxlength,\tikz@pattern@color]{flexible crosshatch}{\pgfqpoint{-1pt}{-1pt}}{\pgfqpoint{\flex@pattern@auxlength}{\flex@pattern@auxlength}}{\pgfqpoint{3pt}{3pt}}%
{
  \pgfsetlinewidth{\flex@pattern@linewidth}
  \pgfsetcolor{\tikz@pattern@color}
  \pgfpathmoveto{\pgfqpoint{\flex@pattern@density}{0pt}}
  \pgfpathlineto{\pgfqpoint{0pt}{\flex@pattern@density}}
  \pgfpathmoveto{\pgfqpoint{0pt}{0pt}}
  \pgfpathlineto{\pgfqpoint{\flex@pattern@density}{\flex@pattern@density}}
  \pgfusepath{stroke}
}


% Dotted regions

\pgfdeclarepatternformonly[\flex@pattern@density,\flex@pattern@linewidth,\tikz@pattern@color]{flexible dots}{\pgfqpoint{-1pt}{-1pt}}{\pgfqpoint{1pt}{1pt}}{\pgfqpoint{\flex@pattern@density}{\flex@pattern@density}}%
{
  \pgfsetcolor{\tikz@pattern@color}
  \pgfpathcircle{\pgfqpoint{0pt}{0pt}}{\flex@pattern@linewidth}
  \pgfusepath{fill}
}

\pgfdeclarepatternformonly[\flex@pattern@density,\flex@pattern@linewidth,\tikz@pattern@color]{flexible crosshatch dots}{\pgfqpoint{-1pt}{-1pt}}{\pgfqpoint{2.5pt}{2.5pt}}{\pgfqpoint{\flex@pattern@density}{\flex@pattern@density}}%
{
  \pgfsetcolor{\tikz@pattern@color}
  \pgfpathcircle{\pgfqpoint{0pt}{0pt}}{\flex@pattern@linewidth}
  \pgfpathcircle{\pgfqpoint{1.5pt}{1.5pt}}{\flex@pattern@linewidth}
  \pgfusepath{fill}
}

\makeatother


\begin{document}
    \begin{tikzpicture}[>=stealth,every node/.style={font=\tiny}]
            \draw[very thick] (0,5) -- (0,0) -- (5,0) -- (5,5);
                \fill[pattern=flexible north east lines,pattern line
                width=0.8pt,pattern color=blue] (0,4.98) -- (0,0) -- (5,0) -- (5,4.98);
            \draw[very thick,fill=white] (.2,5) -- (.2,.2) -- (4.8,.2) -- (4.8,5);
            \draw[very thick] (.2,1) -- (4.8,1);
            \draw[very thick] (.4,5) -- (.4,1) -- (4.6,1) -- (4.6,5) to[bend left=30] (2.5,5) to[bend right=30] (.4,5) -- cycle;
                \draw[very thick] (4.6,5) to[bend right=30] (2.5,5);
                \begin{scope}
                    \draw[pattern=flexible north west lines,pattern density=9pt] (2.5,5) to[bend right=30] (4.6,5) to[bend right=30] (2.5,5) -- cycle;
%                     \foreach \x in {1,2,...,14}
%                     {
%                         \draw[xshift=4.5cm,yshift=5cm,xshift=-2cm,xshift=4*\x] (120:1) -- (-60:1);
%                     }
                \end{scope}
           \fill[pattern=north west lines] (2.5,5) to[bend right=30] (4.6,5) to[bend right=30] (2.55,5) -- cycle;
            \begin{scope}
                \draw[pattern=flexible north east lines,pattern color=red,
                pattern density=1pt,pattern line width=0.2pt] (.4,1.2) -- (4.6,1.2) -- (4.6,1) -- (.4,1) -- cycle;

            \end{scope}
            \draw[very thick] (.4,1.2) -- (4.6,1.2);
                \draw[very thick,fill=white] (2.2,.9) rectangle (2.8,1.3);
            \draw (2.2,1) -- (2.8,1);
            \draw (2.2,1.2) -- (2.8,1.2);
                    \draw[ultra thin,dash pattern={on 5pt off 2pt on 1pt off 3pt}] (2.5,.5) --+ (0,5);
                \draw[<->] (.4,3.7) -- (4.6,3.7) node[midway,fill=white] {$\stackrel{d_a}{(2r_a)}$};
            \draw[ultra thick,<-] (2.5,5) --+ (0,2) node[left] {$P$};
                \draw (2.2,.9) --+ (0,-1.5);
                \draw (2.8,.9) --+ (0,-1.5);
            \draw[thick,<-] (2.2,-.4) --+ (-.7,0);
            \draw[thick,<-] (2.8,-.4) --+ (.7,0);
                \node[below=2] at (2.5,0) {$d_i$};
    \end{tikzpicture}
\end{document}

enter image description here

The colors are just for fun, I was using Ignasis answer, and really could not make sense of the comment below it but may well be missing something.

There are several things that do not work or I am not sure about.

  • If I choose the pattern density to be 8pt instead of 9pt, the lines are no longer equidistant. Most likely there is something obvious I am missing, but it is late here and I don't see it.

  • Not sure if pattern density is a good name for that distance.

  • Can't make the pattern density very small either.

  • Only started to test the flexible north west lines and flexible north east lines, not the others.

  • This is nearly exactly what I want, but is it possible to put that all into pgfonlayer background, so that the lines don't intersect the figure (it doesn't matter if the line color is set to black) and when just the fix is done, your answer will be marked as the best one! ;) – current_user Aug 4 '18 at 8:40
  • 1
    @current_user I think Ignasis answer is better than mine. – user121799 Aug 4 '18 at 13:47

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.