20

I have modified the north east lines pattern to accept a \LineSpace option that sets the density of the pattern (see the question Can I control the density of a pattern in TikZ?). The code works very well but I can't modify the color of the pattern using the pattern color option as I did before.

\documentclass{minimal}
\usepackage{pgfplots}
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}
\setlength\PreviewBorder{2mm}
\pgfplotsset{compat=newest}
\usetikzlibrary{patterns}

\pgfdeclarepatternformonly[\LineSpace]{my north east lines}{\pgfqpoint{-1pt}{-1pt}}{\pgfqpoint{\LineSpace}{\LineSpace}}{\pgfqpoint{\LineSpace}{\LineSpace}}%
{
    \pgfsetlinewidth{0.4pt}
    \pgfpathmoveto{\pgfqpoint{0pt}{0pt}}
    \pgfpathlineto{\pgfqpoint{\LineSpace + 0.1pt}{\LineSpace + 0.1pt}}
    \pgfusepath{stroke}
}

\newdimen\LineSpace
\tikzset{
    line space/.code={\LineSpace=#1},
    line space=3pt
}


\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        ymin=0, ymax=1,
        xmin=-1, xmax=1]        

        \addplot[
            pattern=my north east lines,
            line space=8pt,
            pattern color=green,
            draw=black] 
            {x} \closedcycle;       

        \addplot[
            pattern=north east lines,
            pattern color=red,
            draw=black]
            {-x} \closedcycle;
    \end{axis}    
\end{tikzpicture}
\end{document}

example

1 Answer 1

20
\documentclass{minimal}
\usepackage{pgfplots}
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}
\setlength\PreviewBorder{2mm}
\pgfplotsset{compat=newest}
\usetikzlibrary{patterns}

\makeatletter %new code
\pgfdeclarepatternformonly[\LineSpace,\tikz@pattern@color]{my north east lines}{\pgfqpoint{-1pt}{-1pt}}{\pgfqpoint{\LineSpace}{\LineSpace}}{\pgfqpoint{\LineSpace}{\LineSpace}}%
{
    \pgfsetcolor{\tikz@pattern@color} %new code
    \pgfsetlinewidth{0.4pt}
    \pgfpathmoveto{\pgfqpoint{0pt}{0pt}}
    \pgfpathlineto{\pgfqpoint{\LineSpace + 0.1pt}{\LineSpace + 0.1pt}}
    \pgfusepath{stroke}
}
\makeatother %new code
\newdimen\LineSpace
\tikzset{
    line space/.code={\LineSpace=#1},
    line space=3pt
}


\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        ymin=0, ymax=1,
        xmin=-1, xmax=1]        

        \addplot[
            pattern=my north east lines,
            line space=8pt,
            pattern color=green,
            draw=black] 
            {x} \closedcycle;       

        \addplot[
            pattern=north east lines,
            pattern color=red,
            draw=black]
            {-x} \closedcycle;
    \end{axis}    
\end{tikzpicture}
\end{document}

Just add pgfsetcolor{\tikz@pattern@color} and the relevant makeatletter, makeatother

enter image description here

3
  • Just for the record: If you have more than one my north east lines you are not able to change the color any more. A solution to this problem is e.g. given at tex.stackexchange.com/a/405505/95441 Commented Dec 11, 2017 at 18:30
  • @Stefan Pinnow: You are right. You cannot change the color if you use that pattern more than once with the same line space value. But I disagree about the solution. It is actually much simpler. Just add \tikz@pattern@color to the list of variables. I committed an edit of the answer.
    – John
    Commented Sep 13, 2018 at 7:25
  • @John, I noticed the edit already and voted to accept it. Thank you for sharing! Commented Sep 13, 2018 at 7:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .