有没有办法在计算图片的边界框时忽略由tikz
产生的空白?node
inner sep
考虑以下 MWE:
\documentclass{article}
\usepackage[showframe,pass]{geometry}
\usepackage{tikz}
\begin{document}
\noindent \begin{tikzpicture}
\node[inner sep=0pt] (X) {X};
\draw (X) -- ++ (0.5, 0);
\draw[gray] (current bounding box.south west) rectangle (current bounding box.north east);
\end{tikzpicture}
\noindent X
\noindent \begin{tikzpicture}
\node[inner sep=2pt] (X) {X};
\draw (X) -- ++ (0.5, 0);
\draw[gray] (current bounding box.south west) rectangle (current bounding box.north east);
\end{tikzpicture}
\end{document}
输出如下:
在此示例中,inner sep
在从节点/到节点绘制线条时很有用(参见示例中的水平线),但另一方面,它会X
相对于普通文本缩进第二个。某种将边界框指定为包含所有“墨水”的最小矩形(从而剪掉所有inner sep
s 以及可能不必要地扩展边界框的其他空白)的方法在这里会很棒...
答案1
该解决方案实际上并没有忽略inner sep
边界框计算,但在许多情况下可能会给出所需的结果。
看起来 的值outer sep
不会影响边界框。因此使用outer sep
并设置inner sep = 0
即可解决问题。
这种方法甚至对其他形状也有效。在这种情况下,负数inner sep
(或inner xsep
或inner ysep
)可能有助于快速而粗略的微调。
\documentclass[border = 1, varwidth]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\node[inner sep = 0, outer sep = 0.3333em] (X) {X};
\foreach \a in {0, 5, ..., 90} \draw (X) -- ++(\a:2em);
\fill[black, opacity = 0.25] (current bounding box.south west)
rectangle (current bounding box.north east);
\end{tikzpicture}
\begin{tikzpicture}
\node[inner sep = 0em, outer sep = 0.3333em, circle] (X) {X};
\foreach \a in {0, 5, ..., 90} \draw (X) -- ++(\a:2em);
\fill[black, opacity = 0.25] (current bounding box.south west)
rectangle (current bounding box.north east);
\end{tikzpicture}
\pgfkeysvalueof{/pgf/inner xsep}
\end{document}
我已打印出默认值以inner sep
供参考。
答案2
我喜欢 krnk 的回答,这正是我想要的。
从 Ti 复制的代码量钾可以使用形状继承机制来减少 Z(我不知道这在原始答案时可能还没有提供)。
我还更改了背景路径,这样无需更新边界框即可创建原始矩形。例如,这样就可以将矩形填充为白色。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{backgrounds}
\makeatletter
\pgfdeclareshape{tight}{
% inherit as much stuff as possible from the rectangle shape
\inheritsavedanchors[from=rectangle]
\foreach \x in {east,west} \foreach \y in {north,mid,base,south} {
\inheritanchor[from=rectangle]{\y\space\x}
}
\foreach \x in {east,west,north,mid,base,south,center,text} {
\inheritanchor[from=rectangle]{\x}
}
\inheritanchorborder[from=rectangle]
\inheritbackgroundpath[from=rectangle]
\savedanchor\tightnortheast{%
\pgfpoint{\wd\pgfnodeparttextbox}
{\ht\pgfnodeparttextbox}%
}
\savedanchor\tightsouthwest{%
\pgfpoint{0pt}
{-\dp\pgfnodeparttextbox}%
}
\backgroundpath{%
% move to each of the tight corners, updating the bounding box
\pgfpathmoveto\tightsouthwest
\pgfpathmoveto\tightnortheast
\pgfpathmoveto{\tightsouthwest\pgf@xa=\pgf@x\tightnortheast\pgf@x=\pgf@xa}%
\pgfpathmoveto{\tightsouthwest\pgf@ya=\pgf@y\tightnortheast\pgf@y=\pgf@ya}%
% draw the usual recangle border without affecting the bounding box
\pgf@relevantforpicturesizefalse
\pgfpathrectanglecorners\southwest
\northeast
% this is executed in a grop, so bounding box tracking automatically
% gets re-enabled if it was enabled to begin with
}
}
\makeatother
\begin{document}
\foreach \shape in {rectangle, tight} {
\par shape = \shape:\par
\begin{tikzpicture}[framed,tight background]
\node[\shape] at (0,0) (x) {x};
\node[\shape] at (1,0) (y) {y};
\draw (x) -- (y);
\end{tikzpicture}
}
\end{document}