修改图案看不到图案颜色选项

修改图案看不到图案颜色选项

我已经修改了north east lines图案以接受\LineSpace设置图案密度的选项(参见问题我可以控制 TikZ 中图案的密度吗?)。代码运行得很好,但是我无法pattern color像以前一样使用该选项修改图案的颜色。

\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}

例子

答案1

\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}

只需添加pgfsetcolor{\tikz@pattern@color}相关的makeatlettermakeatother

在此处输入图片描述

相关内容