我有一个tikzpicture
,在本例中是一个蓝色矩形,它将产生一个bounding box
,这里用绿色背景表示。现在我想node
在 的左上角放置一个bounding box
。在本例中,因为我知道 ,absolute coordinates
这很容易做到,但情况并非总是如此,特别是在使用外部图形或在旋转坐标系中绘制三维物体时。因此,能够将 相nodes
对于放置会很方便bounding box
。在axis
环境中,可以使用 将节点放置在相对坐标中rel axis cs
。在本例中代码将是\node at (rel axis cs:0,1) [anchor=north west] {node content}
。因为在我的例子中,没有预定义axis
,所以这里不可能。有没有类似的东西可以用于普通的tikzpicture
?
\documentclass[10pt,crop]{standalone}
\usepackage{tikz}
\usetikzlibrary{backgrounds}
\begin{document}
\begin{tikzpicture}[tight background,background rectangle/.style=
{fill=green},show background rectangle] \path[draw=black,fill=blue]
(0,-1) -- (-1,0) -- (1,2) -- (2,1) -- cycle;
\end{tikzpicture}
\end{document}
答案1
您可以使用current bounding box.north west
适当的anchor
。由于bounding box
添加某些内容时 会有所不同,因此最好通过添加来修复它use as bounding box
(感谢 zeroth)。然后我们就可以自由使用所有选项,例如positioning
库等。一个小例子:
\documentclass[10pt,crop]{standalone}
\usepackage{tikz}
\usetikzlibrary{backgrounds,calc,positioning}
\begin{document}
\begin{tikzpicture}[tight background,background rectangle/.style= {fill=green},show background rectangle, ]
\path[draw=black,fill=blue,use as bounding box] (0,-1) -- (-1,0) -- (1,2) -- (2,1) -- cycle; %%% note the use as bounding box here
\node [draw,rectangle,inner sep=0pt,anchor=north west] at (current bounding box.north west)(me) {me};
\node [draw,rectangle,inner sep=0pt,anchor=north west,below=.7cm of me] {me};
\node [draw,circle,inner sep=0pt,anchor=south] at ($(current bounding box.south east)+(-.5,.5)$) {you};
\end{tikzpicture}
\end{document}
可以利用边界框的可用锚点进行相对定位。一个简单的演示如下:
\documentclass[10pt,crop]{standalone}
\usepackage{tikz}
\usetikzlibrary{backgrounds,calc,positioning}
\begin{document}
\begin{tikzpicture}[tight background,background rectangle/.style= {fill=green},show background rectangle, ]
\path[draw=black,fill=blue,use as bounding box] (0,-1) -- (-1,0) -- (1,2) -- (2,1) -- cycle; %%% note the use as bounding box here
\node [draw,rectangle,inner sep=0pt,anchor=north west] at (current bounding box.north west)(me) {me};
\node [draw,rectangle,inner sep=0pt,anchor=west] at ($(current bounding box.north west)!.37!(current bounding box.south west)$) {me}; %% 37% from north west
\node [draw,circle,inner sep=0pt,anchor=center] at ($(current bounding box.south)!.5!(current bounding box.east)$) {you};
\end{tikzpicture}
\end{document}
另请注意,下面 zeroth 的评论以禁用边界框的更新。