TikZ:框内剪辑

TikZ:框内剪辑

我可以剪裁盒子外面的所有东西

\documentclass{article}

\usepackage{tikz}

\begin{document}
\begin{tikzpicture}[]
\clip (-1, 0) rectangle (1, 1);
\draw (0, 0) circle (2 and .5);
\end{tikzpicture}
\end{document}

但是我怎样才能剪切矩形内的东西并只绘制矩形外的东西呢?

我可以用复杂的多边形进行剪辑

\documentclass{article}

\usepackage{tikz}

\begin{document}
\begin{tikzpicture}[]
\clip (-1, 0) -- (1, 0) --  (1, 1) -- (3, 1) -- (3, -1) -- (-3, -1) -- (-3, 1) -- (-1, 1) -- (-1, 0) -- cycle;
\draw (0, 0) circle (2 and .5);
\end{tikzpicture}
\end{document}

但有没有更简单的方法可以做到这一点?

答案1

这里有几个关于even odd clip和 reversclip 的帖子可以使用。

\documentclass{article}
\usepackage{tikz}
% based on 
% https://tex.stackexchange.com/a/38995/121799 
% https://tex.stackexchange.com/a/76216 
% https://tex.stackexchange.com/a/59168/194703 
% https://tex.stackexchange.com/q/448920/194703 
\tikzset{even odd clip/.code={\pgfseteorule},
reverseclip/.style={overlay,insert path={(-16383.99999pt,-16383.99999pt) rectangle (16383.99999pt,16383.99999pt)}}}

\begin{document}
\noindent
\begin{tikzpicture}[]
\draw (-1, 0) rectangle (1, 1);
\draw (0, 0) circle (2 and .5);
\end{tikzpicture}

\noindent
\begin{tikzpicture}[]
\path[save path=\pathA] (0, 0) circle (2 and .5);
\clip[overlay,even odd clip,reverseclip] (-1, 0) rectangle (1, 1) ; 
\draw[use path=\pathA] (0, 0) circle (2 and .5);
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容