给出下面的代码,谜题中缺少的部分是从内部确定边界框的宽度(这应该比从外面)。
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\usetikzlibrary{backgrounds, calc}
\begin{document}
\newlength{\myl}
\begin{tikzpicture}
\draw (-2,-1) rectangle (2,1);
\begin{pgfonlayer}{background}
\path[fill=red](current bounding box.south west)rectangle(current bounding box.north east);
\end{pgfonlayer}
%\settowidth{\myl}{current bounbind box.width} % HOW TO?
\coordinate (sw) at (current bounding box.south west);
%\node[black,above right] at (sw) {\the\myl}; % (0pt)
\node[above right,
text width=5em
% text width=\myl
] (bottom) at (current bounding box.south west) {\color{black}
%\makebox[\myl]{%
A\hspace*{\fill}Z
%}
};
% UPDATE:
\path (bottom.north west) coordinate (SW);
\node[black] (SW) {SW};
\end{tikzpicture}
\end{document}
更新#1:
辅助问题是,我本来预计位于(A)-(Z)边界框的西南西北方向:为什么它离得这么远?
更新 # 2:以下是澄清
\node[above right,
text width=5em
% text width=\myl
] (bottom) at (current bounding box.south west) {\color{black}
\frame{\makebox[5em]{A\hspace*{\fill}Z}}
};
答案1
您可以使用锚点来实现这种自动拉伸。这样就不需要计算矩形的宽度了。
\documentclass[tikz, border=5pt]{standalone}
\usetikzlibrary{backgrounds}
\begin{document}
\begin{tikzpicture}
\draw (-2,-1) rectangle (2,1);
\begin{pgfonlayer}{background}
\path[fill=red] (current bounding box.south west) rectangle (current bounding box.north east);
\end{pgfonlayer}
\node[text=black, anchor=south west] (bottoml) at (current bounding box.south west) {A};
\node[text=black, anchor=south east] (bottomr) at (current bounding box.south east) {Z};
\node[text=black, anchor=south west] at (bottoml.north west) {SW};
\end{tikzpicture}
\end{document}
顺便说一句,我可能会先定义矩形角的坐标,然后使用它们来绘制矩形并放置节点。您应该考虑一下是否真的需要这个backgrounds
库。带有黑色边框的红色矩形可以轻松用 绘制\draw[fill=red] (-2,-1) rectangle (2,1);
。
更新:
如果你真的想计算矩形的宽度,你确实可以使用所引用的方法:
\begin{tikzpicture}
\draw (-2,-1) rectangle (2,1);
\begin{pgfonlayer}{background}
\path[fill=red] (current bounding box.south west) rectangle (current bounding box.north east);
\end{pgfonlayer}
\newdimen\myl
\pgfextractx{\myl}{\pgfpointdiff{\pgfpointanchor{current bounding box}{west}}
{\pgfpointanchor{current bounding box}{east}}}
\node at (0,0) {\the\myl};
\node[text=black, draw, anchor=south west, text width={\myl-2*\pgfkeysvalueof{/pgf/inner xsep}-2*\pgflinewidth}] (bottoml) at (current bounding box.south west) {A \hfill Z};
\node[text=black, anchor=south west] at (bottoml.north west) {SW};
\end{tikzpicture}
代码将两个坐标current bounding box.west
和x 值的差值存储current bounding box.east
在新定义的维度宏 ( \myl
) 中。这几乎就是您已有的。但是,您不能直接使用它,因为您仍然需要从此宽度中减去节点的inner sep
(padding) 和边框宽度以使其适合。inner sep
可以用 评估的(horizontal) 的值\pgfkeysvalueof{/pgf/inner xsep}
以及可以用 评估的边框的值\pgflinewidth
都需要乘以 2,因为 padding 和 border 位于节点的两侧。
答案2
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\usetikzlibrary{backgrounds, calc, shapes, positioning}
\begin{document}
\newlength{\myl}
\begin{tikzpicture}
\node[rectangle, draw, fill=red, inner sep=2.5cm,]at(0,0)(a){};
\node at( a.south west) {\color{black}A};
\node at($( a.south west)+(5cm,0cm)$) {\color{black}Z};
% UPDATE:
\path (a.north west) coordinate (SW);
\node[black] at(SW.east) {SW};
\end{tikzpicture}
\end{document}