更改 tikz/pgf 中剪切区域的绘制颜色

更改 tikz/pgf 中剪切区域的绘制颜色

我用来\clip剪切图形中的矩形区域tikzpicture。我希望剪切区域中的线条具有与原始图形中不同的颜色。有没有更简单的方法可以做到这一点?或者这是否可能?

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[scale=0.5]
\clip[draw] (0,0) rectangle (2*1.732,-3);
\def\hexagonpath{ +(30:1) -- +(90:1) -- +(150:1) -- +(210:1) -- +(270:1) -- +(330:1) -- cycle }
\foreach \x in {0,...,3}
    \foreach \y in {0,...,3} {
        \ifodd\x
            \draw (0,0) ++(0,-1/2-3*\x/2) ++(30:1) ++(30:\y * 2) ++(0,-\y) \hexagonpath; 
        \else
            \draw (0,0) ++(0,-3*\x/2) ++(30:\y * 2) ++(0,-\y) \hexagonpath;
        \fi
    }
\end{tikzpicture}
\end{document}

答案1

这里最简单的事情就是将整个六边形网格包装到一个命令中,然后绘制两次,一次不剪切,然后再次剪切并使用不同的颜色。

原则上,您可以先绘制完整的网格,然后在其上绘制裁剪部分。但是,这可能会导致网格的第一个实例显示出来,特别是当您想用较细的笔触绘制裁剪的网格时。为了避免这种情况,您可以“反向裁剪”路径的第一个实例(请参阅如何在 TikZ 中反转“剪辑”选择?)。

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{scopes}
\begin{document}
\tikzstyle{reverseclip}=[insert path={(current page.north east) --
  (current page.south east) --
  (current page.south west) --
  (current page.north west) --
  (current page.north east)}
]

\begin{tikzpicture}[scale=0.5,remember picture,overlay]
\def\hexagonpath{ +(30:1) -- +(90:1) -- +(150:1) -- +(210:1) -- +(270:1) -- +(330:1) -- cycle }
\def\hexgrid{
\foreach \x in {0,...,3}
    \foreach \y in {0,...,3} {
        \ifodd\x
            \draw (0,0) ++(0,-1/2-3*\x/2) ++(30:1) ++(30:\y * 2) ++(0,-\y) \hexagonpath; 
        \else
            \draw (0,0) ++(0,-3*\x/2) ++(30:\y * 2) ++(0,-\y) \hexagonpath;
        \fi
    }
}
\begin{scope}
\path[clip] (0,0) rectangle (2*1.732,-3) [reverseclip];
\hexgrid
\end{scope}

\begin{scope}[red]
\draw[clip] (0,0) rectangle (2*1.732,-3);
\hexgrid
\end{scope}
\end{tikzpicture}
\end{document}

答案2

我有一个非常丑陋的 hack,基本上就是修改现有库。您可以将其保存为附加自定义库,或者如果您不打算在文档中spy使用该库,则保留原样。spy

我所做的很简单。就在 TikZ 准备在间谍窗口中重新绘制原始图片的一部分时,我挤进了一个描边颜色更改命令\pgfsetstrokecolor{},这样在执行绘制时,TikZ 会假定它已正确重置了选项。我还编写了一个自定义\spy命令,这样你就可以通过 明确地指定线条颜色with line color <color spec>

如果您更改命令中的颜色,这将不起作用,\draw并且几乎所有其他内容都可能被分解。

我认为可以直接修补它,但我不知道如何立即修补,因为我们需要降低一级才能捕捉到绘图。也许其他人可以,所以我就把它留在这里。

\documentclass[border=2mm]{standalone}
\usepackage{tikz}
\makeatletter
\tikzset{
  spy using ugly hack/.style={
    spy scope={
      every spy on node/.style={draw=none},
      every spy in node/.style={draw=none},
      #1
    }
  },
}

\newbox\tikz@lib@spybox

\let\tikz@lib@spy@collection=\pgfutil@empty%

\tikzset{spy scope/.style={
    size/.style={minimum size=##1},
    height/.style={minimum height=##1},
    width/.style={minimum width=##1},
    execute at begin scope={%
      \let\tikz@lib@spy@save=\tikz@lib@spy@collection%
      \setbox\tikz@lib@spybox=\hbox\bgroup%
      \let\spy=\tikz@lib@spy@parse
    },
    execute at end scope={%
      \egroup%
      {%
        \copy\tikz@lib@spybox%
        \tikz@lib@spy@collection%
      }%
      \global\let\tikz@lib@spy@collection=\tikz@lib@spy@save%
    },%
    tikz@lib@spy@style/.style={#1},
    tikz@lib@reset@gs
  },
  lens/.store in=\tikz@lib@spy@lens,
  lens=,
  magnification/.style={lens={scale=#1}},
  spy connection path/.store in=\tikz@lib@spy@path,
  spy connection path=
}

\tikzset{
  tikz@lib@reset@gs/.style={black,thin,solid,opaque,line cap=butt,line join=miter}
}

\def\tikz@lib@spy@parse{%
  \pgfutil@ifnextchar[{\tikz@lib@spy@parse@opt}{\tikz@lib@spy@parse@opt[]}%]
}
\def\tikz@lib@spy@parse@opt[#1]{
  \pgfutil@ifnextchar x{\tikz@lib@spy@parse@opta[#1]}{\tikz@lib@spy@parse@opta[#1]}%]
}
\def\tikz@lib@spy@parse@opta[#1]on#2in node#3 with line color #4;{%
  \pgfutil@g@addto@macro\tikz@lib@spy@collection{\tikz@lib@spy@do{#1}{#2}{#3}{#4}}%
}

\def\tikz@lib@spy@do#1#2#3#4{%
  \scope[tikz@lib@spy@style,#1]
    \node [alias=tikzspyonnode,inner sep=0pt,outer sep=0pt,every spy on node/.try,
    /utils/exec={
      {%
        \let\tikz@transform=\relax
        \pgftransformreset%
        \expandafter\tikzset\expandafter{\tikz@lib@spy@lens}
        \pgftransforminvert%
        \pgfgettransformentries\a\b\c\d\e\f%
        \global\let\pgf@lib@svg@a=\a%
        \global\let\pgf@lib@svg@b=\b%
        \global\let\pgf@lib@svg@c=\c%
        \global\let\pgf@lib@svg@d=\d%
      }%
      \tikz@addtransform{%
        \tikz@scan@one@point\pgftransformshift#2%
        \pgftransformcm{\pgf@lib@svg@a}{\pgf@lib@svg@b}{\pgf@lib@svg@c}{\pgf@lib@svg@d}{\pgfpointorigin}%
      }
    }]{};
    \node [alias=tikzspyinnode,inner sep=0pt,outer sep=0pt,at={#2},every spy in node/.try,
    path picture={\node[anchor=center,tikz@lib@reset@gs]{\nullfont%
        \pgftransformreset%
        \pgfsetstrokecolor{#4}
        \let\tikz@transform=\relax%
        \expandafter\tikzset\expandafter{\tikz@lib@spy@lens}%
        \pgflowlevelsynccm%
        \tikz@scan@one@point\tikz@lib@spy@shift#2%
        \pgflowlevelsynccm%
        \copy\tikz@lib@spybox};}]#3{};
    \tikz@lib@spy@path
  \endscope
}

\def\tikz@lib@spy@shift#1{%
  \pgf@process{#1}%
  \pgf@x=-\pgf@x%
  \pgf@y=-\pgf@y%
  \pgftransformshift{}%
}
\makeatother



\begin{document}
\begin{tikzpicture}[spy using ugly hack={magnification=1,size=2cm}]
%\clip[draw] (0,0) rectangle (2*1.732,-3);
\def\hexagonpath{ +(30:1) -- +(90:1) -- +(150:1) -- +(210:1) -- +(270:1) -- +(330:1) -- cycle }
\foreach \x in {0,...,3}
    \foreach \y in {0,...,3} {
        \ifodd\x
            \draw[thick] (0,0) ++(0,-1/2-3*\x/2) ++(30:1) ++(30:\y * 2) ++(0,-\y) \hexagonpath; 
        \else
            \draw[thick] (0,0) ++(0,-3*\x/2) ++(30:\y * 2) ++(0,-\y) \hexagonpath;
        \fi
    }
\spy on (3.5,-1.5) in node  with line color blue;
\spy on (0,-3) in node with line color yellow;
\spy on (6,-4) in node with line color red;
\end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容