tikz:在边界框计算中忽略一条路径

tikz:在边界框计算中忽略一条路径

我想忽略 tikz 图片中间的边界框计算的一个特定路径(我只需要该路径来获取一个交点,并且必须使其很长,以确保该点存在)。

我想出了以下解决方案,但我觉得它看起来有点奇怪。有更好的解决方案吗?

\documentclass{article}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
\draw(0,0)-- (2,2);

\begin{scope}
\path [use as bounding box] 
             (current bounding box.south west)
             rectangle (current bounding box.north east);
\path (-1,-1)--(5,5); %ignore this
\end{scope}

\fill[red](0,1)rectangle (1,3);
\draw [black] (0,0)rectangle (2,3); %wanted bounding box 
\draw [blue] (current bounding box.south west) 
             rectangle (current bounding box.north east);%current bounding box
\end{tikzpicture}
\end{document} 

答案1

对于这种边界框计算的暂停,我们有两个选择:overlayTikZ 前端的范围选项和pgfinterruptboundingbox基础层的环境选项。

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}
\draw(0,0)-- (2,2);
\begin{scope}[overlay]
\draw (current bounding box.south west)
             rectangle (current bounding box.north east);
\path (-1,-1)--(5,5); %ignore this
\end{scope}

\fill[red](0,1)rectangle (1,3);
\draw [yellow,ultra thick] (0,0)rectangle (2,3); %wanted bounding box 
\draw [blue,thick] (current bounding box.south west) 
             rectangle (current bounding box.north east);%current bounding box
\end{tikzpicture}
\end{document}

或使用

\begin{pgfinterruptboundingbox}
\draw (current bounding box.south west)
             rectangle (current bounding box.north east);
\path (-1,-1)--(5,5);
\end{pgfinterruptboundingbox}

均得出以下结果。

在此处输入图片描述

答案2

overlay您实际上可以在单个路径上使用该密钥:

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}
\draw(0,0)-- (2,2);
\draw (current bounding box.south west)
             rectangle (current bounding box.north east);
\path[overlay] (-1,-1)--(5,5); %ignore this

\fill[red](0,1)rectangle (1,3);
\draw [yellow,ultra thick] (0,0)rectangle (2,3); %wanted bounding box 
\draw [blue,thick] (current bounding box.south west) 
             rectangle (current bounding box.north east);%current bounding box
\end{tikzpicture}
\end{document}

生成:

覆盖在单个路径上

overlay设置\pgf@relevantforpicturesizefalse。这在根据路径调整全局边界框时使用:

\def\pgf@protocolsizes#1#2{%
  \ifpgf@relevantforpicturesize%
    \ifdim#1<\pgf@picminx\global\pgf@picminx#1\fi%
    \ifdim#1>\pgf@picmaxx\global\pgf@picmaxx#1\fi%
    \ifdim#2<\pgf@picminy\global\pgf@picminy#2\fi%
    \ifdim#2>\pgf@picmaxy\global\pgf@picmaxy#2\fi%
    \ifpgf@size@hooked%
      \let\pgf@size@hook@x#1\let\pgf@size@hook@y#2\pgf@path@size@hook%
    \fi%
  \fi%
  \ifdim#1<\pgf@pathminx\global\pgf@pathminx#1\fi%
  \ifdim#1>\pgf@pathmaxx\global\pgf@pathmaxx#1\fi%
  \ifdim#2<\pgf@pathminy\global\pgf@pathminy#2\fi%
  \ifdim#2>\pgf@pathmaxy\global\pgf@pathmaxy#2\fi%
}

因此,从中我们可以看出,如果路径是,relevantforpicturesize则图片边界框会调整以包含它,但如果不是,则不会。因此,虽然最常见的是在图片(或范围)上全局使用此键,但其效果实际上是逐个路径看到的,因此它可以用于单个路径。

相关内容