4

Why in the following MWE in all shapes the pattern color is as in the first one regardless that it is defined differently?

%%%% pattern-color
\documentclass[tikz,border=5mm]{standalone}
\usetikzlibrary{chains,patterns,backgrounds}
\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}
    }
\makeatother

\begin{document}
 \begin{tikzpicture}[
    start chain = going below,
  node distance = 2mm,
    Node/.style = {minimum width=#1,
                   shape=rectangle,
                   draw, fill=white,
                   on chain},
 Pattern/.style = {pattern=north east hatch,
                    pattern color=#1,%teal!30,
                    hatch distance=7pt,
                    hatch thickness=3pt},
    font=\small\sffamily]
%----------------
\node[Node=44mm,Pattern=red!30]     {desired pattern color: red};
\node[Node=44mm,Pattern=cyan!30,
      preaction={fill=yellow}]      {desired pattern color: cyan};
\node[Node=44mm]                    {without pattern};
\node[Node=44mm,Pattern=orange!30,
      preaction={fill=gray!30}]     {desired pattern color: orange};
%---
 \end{tikzpicture}
\end{document}

enter image description here

4

This is a bug in \pgfcorepatterns.code.tex. The internal macro \pgf@declarepatternmutable saves the pattern type as 7 instead of #7. The corrected line should read:

\expandafter\gdef\csname pgf@pattern@type@#1\endcsname{#7}%    

With this change the patterns work as expected.

  • Will be this bug fixed in the next CVS of TikZ? – Zarko Apr 19 '15 at 14:55
1

Here's a diagnostic, though not a solution.

The code for a form only pattern should not include colour code at all. So setting the colour within the definition is surely a no-no, if the documentation is to be believed.

However, that just turns everything black or white...

OK. Start with the code from page 1064 of the manual for the stars pattern. This works fine, as advertised:

blacks and reds

Now add your variables to it so that we have:

\documentclass[tikz,border=5mm]{standalone}
\usetikzlibrary{patterns}
\tikzset{
  hatch distance/.store in=\hatchdistance,
  hatch distance=5pt,
  hatch thickness/.store in=\hatchthickness,
  hatch thickness=5pt
}

\pgfdeclarepatternformonly[\hatchdistance,\hatchthickness]{stars}
{\pgfpointorigin}
{\pgfpoint{1cm}{1cm}}
{\pgfpoint{1cm}{1cm}}
{
  \pgftransformshift{\pgfpoint{.5cm}{.5cm}}
  \pgfpathmoveto{\pgfpointpolar{0}{4mm}}
  \pgfpathlineto{\pgfpointpolar{144}{4mm}}
  \pgfpathlineto{\pgfpointpolar{288}{4mm}}
  \pgfpathlineto{\pgfpointpolar{72}{4mm}}
  \pgfpathlineto{\pgfpointpolar{216}{4mm}}
  \pgfpathclose%
  \pgfusepath{fill}
}

\begin{document}
  \begin{tikzpicture}
    \filldraw[pattern=stars] (0,0) rectangle (1.5,2);
    \filldraw[pattern=stars,pattern color=red](1.5,0) rectangle (3,2);
  \end{tikzpicture}
\end{document}

All is less fine:

all blacks

So it does not work as advertised...

There's another question about code in this part of the manual not working. I'll see if I can find it (I asked it). Not the same issue, but maybe it will provide a clue.

EDIT

It is not that it is impossible to change the colour...

\documentclass[tikz,border=5mm]{standalone}

\usetikzlibrary{patterns,chains}

\pgfdeclarepatternformonly{north east hatch}% name
{\pgfqpoint{-1pt}{-1pt}}% below left
{\pgfqpoint{7pt}{3pt}}% above right
{\pgfpoint{6pt}{6pt}}%
{
  \pgfsetlinewidth{3pt}
  \pgfpathmoveto{\pgfqpoint{0pt}{0pt}}
  \pgfpathlineto{\pgfqpoint{7pt}{3pt}}
  \pgfpathclose
  \pgfusepath{stroke}
}


\begin{document}
  \begin{tikzpicture}[
    start chain = going below,
    node distance = 2mm,
    Node/.style =
    {
      minimum width=#1,
      shape=rectangle,
      draw, fill=white,
      on chain
    },
    Pattern/.style =
    {
      pattern=north east hatch,
      pattern color=#1
    },
    font=\small\sffamily
    ]
    \node[Node=44mm, Pattern=red!30]     {desired pattern color: red};
    \node[Node=44mm, Pattern=cyan!30]      {desired pattern color: cyan};
    \node[Node=44mm]                    {without pattern};
    \node[Node=44mm, Pattern=orange!30]     {desired pattern color: orange};
  \end{tikzpicture}
\end{document}

colourful patterns

  • OK. I had this in mind but I don't think it is helpful. – cfr Apr 18 '15 at 23:41
  • Huh, here seems to be more problems than I imagine... The description of patterns in TikZ manual is to short to be useful. As I now understand, the pattern color is global defined and can not be change after it selection. I look in tex.stackexchange.com/questions/40854/…, but I didn't succeed to adopt it to my need, because I don't understand it. – Zarko Apr 19 '15 at 0:10
  • @Zarko What do you mean? You certainly can use pattern color to change the colour multiple times. See edit above. (I know I've made a mess of the pattern - it is just to show the colour changes.) – cfr Apr 19 '15 at 0:29
  • Sorry. I don't see what that link has which is relevant here. It seems entirely unrelated to me. – cfr Apr 19 '15 at 0:32

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.