編輯

編輯

為什麼在下面的 MWE 中,所有形狀的圖案顏色都與第一個形狀相同,儘管定義不同?

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

在此輸入影像描述

答案1

這是 中的一個錯誤\pgfcorepatterns.code.tex。內部巨集\pgf@declarepatternmutable將模式類型儲存為而7不是#7.更正後的行應為:

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

透過此更改,模式將按預期工作。

答案2

這是一個診斷,但不是解決方案。

僅表單模式的程式碼應該不是完全包括顏色代碼。因此,如果文件可信的話,在定義中設定顏色肯定是禁忌。

然而,這只會讓一切變得非黑即白…

好的。從模式手冊第 1064 頁的代碼開始stars。正如廣告所言,這工作正常:

黑人和紅人

現在將變數新增到其中,以便我們擁有:

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

一切都不太好:

全黑

所以它並沒有像宣傳的那樣工作......

還有一個關於手冊這部分的程式碼不起作用的問題。我看看我能否找到它(我問過它)。不是同一個問題,但也許它會提供線索。

編輯

並不是說不能改變顏色...

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

多彩圖案

相關內容