1

Doesn't \clip work (= clip all subsequent drawing) if it is defined inside a pic?

MWE

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

Screenshot

enter image description here

  • related : https://tex.stackexchange.com/q/518638/138900 – AndréC Dec 22 '19 at 19:59
  • A pic is a little bit like a scope. If you say \begin{scope} \clip (current bounding box.south west) rectangle (current bounding box.north east); \end{scope} the clip won't affect the stuff after \end{scope} either. So the bottom-line is that a clip in a pic only affects stuff inside the pic. (Otherwise it would be fatal for many pics which precisely use local clips.) –  Dec 22 '19 at 20:08

1 Answers1

1

A pic is in this regard like a scope. It only clips what is inside the pic. Recall that for scopes

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

the clip affects only the <stuff> in the scope but not the <more stuff> not that follows afterwards. So, yes, you cannot use crop as you intend. Your \newcommand or some pgf key is fine.

However, pics also allow you to make this unnecessary. You can define a pic, plot, say, that does all of that and takes the function as a parameter,

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

Here \clip works again, as the MWE shows.

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

Please note that you can make the plot pic such more flexible if needed by using pgf keys.