TikZ:\clip 里面的 \pic 不起作用?

TikZ:\clip 里面的 \pic 不起作用?

如果它是在?内部定义,则不起作用\clip(=剪切所有后续绘图)pic

平均能量损失

\documentclass[tikz]{standalone}

\tikzset{
   crop/.pic={
        \clip (current bounding box.south west) rectangle (current bounding box.north east);
    }
}

\newcommand*{\clipshape}{\clip (current bounding box.south west) rectangle (current bounding box.north east);}

\begin{document}

\begin{tikzpicture}
\draw[help lines] (-5,-5) grid (5,5);
\draw[very thick,color=black,domain=-5:5] plot (\x,{2*\x});
\end{tikzpicture}

\begin{tikzpicture}
\draw[help lines] (-5,-5) grid (5,5);
\clip (current bounding box.south west) rectangle (current bounding box.north east); % THIS WORKS
\draw[very thick,color=black,domain=-5:5] plot (\x,{2*\x});
\end{tikzpicture}

\begin{tikzpicture}
\draw[help lines] (-5,-5) grid (5,5);
\clipshape % THIS WORKS
\draw[very thick,color=black,domain=-5:5] plot (\x,{2*\x});
\end{tikzpicture}

\begin{tikzpicture}
\draw[help lines] (-5,-5) grid (5,5);
\pic{crop}; % THIS DOESN'T WORK
\draw[very thick,color=black,domain=-5:5] plot (\x,{2*\x});
\end{tikzpicture}

\end{document}

截屏

在此处输入图片描述

答案1

在这方面,图片就像一个示波器。它只剪辑图片内的内容。回想一下,对于示波器

\begin{scope} 
 \clip <some path>;
 <stuff> 
\end{scope}
<more stuff> 

剪辑仅影响<stuff>范围内的,而不<more stuff>影响其后的。所以,是的,您无法crop按预期使用。您的\newcommand或某些 pgf 密钥没问题。

但是,pics 也允许你让这变得不必要。你可以定义一个 pic,plot比如说,它完成所有这些并将函数作为参数,

\tikzset{pics/plot/.style={code={
    \draw[help lines] (-5,-5) grid (5,5);
    \clip(current bounding box.south west) rectangle (current bounding box.north east);
    \draw[very thick,color=black,domain=-5:5] plot (\x,{#1});}}
}

\clip正如 MWE 所示,这里再次起作用。

\documentclass[tikz]{standalone}

\tikzset{
   crop/.pic={
        \clip (current bounding box.south west) rectangle (current bounding box.north east);
    },
    pics/plot/.style={code={
    \draw[help lines] (-5,-5) grid (5,5);
    \clip(current bounding box.south west) rectangle (current bounding box.north east);
    \draw[very thick,color=black,domain=-5:5] plot (\x,{#1});}}
}

\newcommand*{\clipshape}{\clip (current bounding box.south west) rectangle (current bounding box.north east);}

\begin{document}

\begin{tikzpicture}
\draw[help lines] (-5,-5) grid (5,5);
\draw[very thick,color=black,domain=-5:5] plot (\x,{2*\x});
\end{tikzpicture}

\begin{tikzpicture}
\draw[help lines] (-5,-5) grid (5,5);
\clip (current bounding box.south west) rectangle (current bounding box.north east); % THIS WORKS
\draw[very thick,color=black,domain=-5:5] plot (\x,{2*\x});
\end{tikzpicture}

\begin{tikzpicture}
\draw[help lines] (-5,-5) grid (5,5);
\clipshape % THIS WORKS
\draw[very thick,color=black,domain=-5:5] plot (\x,{2*\x});
\end{tikzpicture}

\begin{tikzpicture}
\draw[help lines] (-5,-5) grid (5,5);
\pic{crop}; % THIS DOESN'T WORK
\draw[very thick,color=black,domain=-5:5] plot (\x,{2*\x});
\end{tikzpicture}

\begin{tikzpicture}
\pic{plot={2*\x}}; % works
\end{tikzpicture}
\end{document}

请注意,plot如果需要,您可以使用 pgf 键使图片更加灵活。

相关内容