我可以控制 TikZ 中图案的“密度”吗?

我可以控制 TikZ 中图案的“密度”吗?

...即我想控制填充图案中线条之间的间距。使用库中的预定义图案是否可能patternnorth east lines或者我应该创建自己的图案?我尝试了step修改器,但它没有效果,就像在最小不工作下面的例子。;)

\begin{tikzpicture}
    \draw [step=0.5cm, pattern=north east lines] (0,0) rectangle (1.4,1.4);
\end{tikzpicture}

编辑:我稍微修改了一下代码,因为我想控制间距任何模式类型,最初在示例中是grid

答案1

正如 Gonzalo 所说,图案是不可定制的。因此,这里有一种方法可以自定义图案的代码,grid以接受一个变量,该变量允许您通过调整来控制密度GridSize=1pt。默认值为GridSize=3pt

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

\makeatletter
\pgfdeclarepatternformonly[\GridSize]{MyGrid}{\pgfqpoint{-1pt}{-1pt}}{\pgfqpoint{4pt}{4pt}}{\pgfqpoint{\GridSize}{\GridSize}}%
{
  \pgfsetcolor{\tikz@pattern@color}
  \pgfsetlinewidth{0.3pt}
  \pgfpathmoveto{\pgfqpoint{0pt}{0pt}}
  \pgfpathlineto{\pgfqpoint{0pt}{3.1pt}}
  \pgfpathmoveto{\pgfqpoint{0pt}{0pt}}
  \pgfpathlineto{\pgfqpoint{3.1pt}{0pt}}
  \pgfusepath{stroke}
}
\makeatother

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

\begin{document}
\begin{tikzpicture}[grid/.style={pattern=MyGrid}]
   \draw [GridSize=1pt, pattern=MyGrid] (0,0) rectangle (1.4,1.4);
\end{tikzpicture}
\end{document}

答案2

Peter 的答案仅适用于\GridSize小于4pt,如果您想修改模式以接受所有\GrideSize您必须使用以下代码:

\makeatletter
\pgfdeclarepatternformonly[\GridSize]{MyGrid}{\pgfqpoint{-1pt}{-1pt}}{\pgfqpoint{\GridSize}{\GridSize}}{\pgfqpoint{\GridSize}{\GridSize}}%
{
     \pgfsetcolor{\tikz@pattern@color}
     \pgfsetlinewidth{0.3pt}
     \pgfpathmoveto{\pgfqpoint{0pt}{0pt}}
     \pgfpathlineto{\pgfqpoint{0pt}{\GridSize + 0.1pt}}
     \pgfpathmoveto{\pgfqpoint{0pt}{0pt}}
     \pgfpathlineto{\pgfqpoint{\GridSize + 0.1pt}{0pt}}
     \pgfusepath{stroke}
}
\makeatother

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

然后,如果您想修改所有类型的模式以接受此密度选项,您必须修改所有模式的代码,将它们插入到您的序言中或直接插入到文件中pgflibrarypatterns.code.tex。下面是此类修改模式的另外两个示例:

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

\pgfdeclarepatternformonly[\LineSpace]{my north west lines}{\pgfqpoint{-1pt}{-1pt}}{\pgfqpoint{\LineSpace}{\LineSpace}}{\pgfqpoint{\LineSpace}{\LineSpace}}%
{
    \pgfsetcolor{\tikz@pattern@color}
    \pgfsetlinewidth{0.4pt}
    \pgfpathmoveto{\pgfqpoint{0pt}{\LineSpace}}
    \pgfpathlineto{\pgfqpoint{\LineSpace + 0.1pt}{-0.1pt}}
    \pgfusepath{stroke}
}
\makeatother

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

无论如何,颜色规范存在问题(见修改图案看不到图案颜色选项

答案3

快速查看文件pgflibrarypatterns.code.tex可发现,预定义模式不接受通过键进行修改。您必须创建自己的模式,或修改现有模式以使其能够识别键。

以下代码显示了(更密集的网格)模式的定义my grid,与预定义模式并排grid

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

\pgfdeclarepatternformonly{my grid}{\pgfqpoint{-2pt}{-2pt}}{\pgfqpoint{4pt}{4pt}}{\pgfqpoint{2pt}{2pt}}%
{
  \pgfsetlinewidth{0.4pt}
  \pgfpathmoveto{\pgfqpoint{0pt}{0pt}}
  \pgfpathlineto{\pgfqpoint{0pt}{3.1pt}}
  \pgfpathmoveto{\pgfqpoint{0pt}{0pt}}
  \pgfpathlineto{\pgfqpoint{3.1pt}{0pt}}
  \pgfusepath{stroke}
}
\begin{document}

\begin{tikzpicture}
    \draw [step=0.5cm, pattern=my grid] (0,0) rectangle (1.4,1.4);
    \draw [step=0.5cm, pattern=grid] (2,0) rectangle (3.4,1.4);
\end{tikzpicture}

\end{document}

在此处输入图片描述

这是该模式的原始定义grid

\pgfdeclarepatternformonly{grid}{\pgfqpoint{-1pt}{-1pt}}{\pgfqpoint{4pt}{4pt}}{\pgfqpoint{3pt}{3pt}}%
{
  \pgfsetlinewidth{0.4pt}
  \pgfpathmoveto{\pgfqpoint{0pt}{0pt}}
  \pgfpathlineto{\pgfqpoint{0pt}{3.1pt}}
  \pgfpathmoveto{\pgfqpoint{0pt}{0pt}}
  \pgfpathlineto{\pgfqpoint{3.1pt}{0pt}}
  \pgfusepath{stroke}
}

答案4

或者您可以使用\hatchsep=3pt或更多,这样线条分离得更多,并且线条更少。

相关内容