如何为 tikz 定义一个有空间的变量?

如何为 tikz 定义一个有空间的变量?

我创建了一个模式,并希望在调用此模式时更改线条的密度。目前我得到了:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{patterns}

\newdimen\density
\tikzset{
    density/.code={\density=#1},
    density=3pt
}

\makeatletter
\pgfdeclarepatternformonly[\density]{resizable north east lines}
{\pgfqpoint{-1pt}{-1pt}}
{\pgfqpoint{\density}{\density}}
{\pgfqpoint{\density}{\density}}
{
    \pgfsetcolor{\tikz@pattern@color}
    \pgfsetlinewidth{0.4pt}
    \pgfpathmoveto{\pgfqpoint{0pt}{0pt}}
    \pgfpathlineto{\pgfqpoint{\density + 0.1pt}{\density + 0.1pt}}
    \pgfusepath{stroke}
}
\makeatother

\begin{document}
\begin{tikzpicture}

    \node [minimum size = 10 cm, draw = red, line width = 2pt] at (0,0) {};
    \draw [density=10pt, pattern=resizable north east lines, pattern color = red] (0,0) circle[radius = 5cm];

\end{tikzpicture}
\end{document}

如何定义带有空格的变量以便使用以下样式调用它?(请参阅图案密度

\draw [pattern density=10pt, pattern=resizable north east lines, pattern color = red] (0,0) circle[radius = 5cm];

答案1

像这样吗?

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{patterns}

\makeatletter
\newdimen\pattern@density
\tikzset{/tikz/.cd,
    pattern density/.store in=\density,
    pattern density=3pt
}

\pgfdeclarepatternformonly[\density]{resizable north east lines}
{\pgfqpoint{-1pt}{-1pt}}
{\pgfqpoint{\density}{\density}}
{\pgfqpoint{\density}{\density}}
{
    \pgfsetcolor{\tikz@pattern@color}
    \pgfsetlinewidth{0.4pt}
    \pgfpathmoveto{\pgfqpoint{0pt}{0pt}}
    \pgfpathlineto{\pgfqpoint{\density + 0.1pt}{\density + 0.1pt}}
    \pgfusepath{stroke}
}
\makeatother

\begin{document}
\begin{tikzpicture}

    \node [minimum size = 10 cm, draw = red, line width = 2pt] at (0,0) {};
    \draw [pattern density=9pt, pattern=resizable north east lines, pattern color = red] (0,0) circle[radius = 5cm];

\end{tikzpicture}
\end{document}

相关内容