![tikz:在边界框计算中忽略一条路径](https://linux22.com/image/259632/tikz%EF%BC%9A%E5%9C%A8%E8%BE%B9%E7%95%8C%E6%A1%86%E8%AE%A1%E7%AE%97%E4%B8%AD%E5%BF%BD%E7%95%A5%E4%B8%80%E6%9D%A1%E8%B7%AF%E5%BE%84.png)
我想忽略 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
对于这种边界框计算的暂停,我们有两个选择:overlay
TikZ 前端的范围选项和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
则图片边界框会调整以包含它,但如果不是,则不会。因此,虽然最常见的是在图片(或范围)上全局使用此键,但其效果实际上是逐个路径看到的,因此它可以用于单个路径。