我正在创建一个包含两个层的框图:主层和背景层。我想做的是使用相对坐标绘制一个矩形作为背景。
我的代码是
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{backgrounds}
\begin{document}
\tikzstyle{block}=[fill=white,draw=red,minimum size=1.5cm, rounded corners,align=center]
\pgfdeclarelayer{background}
\pgfsetlayers{background,main}
\begin{tikzpicture}
\node[block] (2D) {$2$D\\Histogramm};
\node[draw=none,left of=2D,node distance=3cm] (dummy) {};
\node[block,above of=dummy,node distance=1cm] (E) {E\\signal};
\node[block,below of=dummy,node distance=1cm] (DE) {$\Delta$E\\signal};
\node[block,right of=2D,node distance=3cm] (region) {$p$ signal\\selection};
\node[block,right of=region,node distance=3cm] (projection) {E detector\\projection};
\node[block,right of=projection,node distance=3cm] (smoothing) {smoothing};
\node[block,right of=smoothing,node distance=3cm] (rebinning) {rebinning};
\begin{pgfonlayer}{background}
\draw[fill=gray!30,rounded corners] (E.north west)+(-0.5,0.5)rectangle(rebinning.south east)+(1,2);
\end{pgfonlayer}
\end{tikzpicture}
\end{document}
我的输出是
虽然(E.north west)+(-0.5,0.5)
可以工作,但我似乎不明白为什么(rebinning.south east)+(0.5,-2)
不行。有什么办法可以解决这个问题吗?
答案1
您可以使用 -library fit
。您可以将要包含的节点传递给fit
-option,然后将配件-背景层上的节点。
在您的示例中,只需拟合三个最外层节点即可获得配件-节点进入正确位置:
\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{backgrounds, fit}
\begin{document}
\tikzstyle{block}=[fill=white,draw=red,minimum size=1.5cm, rounded corners,align=center]
\begin{tikzpicture}
\node[block] (2D) {$2$D\\Histogramm};
\node[draw=none,left of=2D,node distance=3cm] (dummy) {};
\node[block,above of=dummy,node distance=1cm] (E) {E\\signal};
\node[block,below of=dummy,node distance=1cm] (DE) {$\Delta$E\\signal};
\node[block,right of=2D,node distance=3cm] (region) {$p$ signal\\selection};
\node[block,right of=region,node distance=3cm] (projection) {E detector\\projection};
\node[block,right of=projection,node distance=3cm] (smoothing) {smoothing};
\node[block,right of=smoothing,node distance=3cm] (rebinning) {rebinning};
\begin{scope}[on background layer]
\node [fit=(E) (DE) (rebinning), fill= gray!30, rounded corners, inner sep=.5cm] {};
\end{scope}
\end{tikzpicture}
\end{document}
这样,您还可以使用以下命令轻松地将标签文本添加到背景节点:
\node [fit=..., label={<position>:Your label text here}] {};
<position>
标签的放置角度/位置在哪里(例如,,,above
... )。有关更多信息,请查看。below
-45
pgfmanual
编辑:使用相对坐标的方法(见评论)
为了使上述代码按照您编写的方式工作,您可以使用calc
库并计算矩形的点。
\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{backgrounds, calc}
\begin{document}
\tikzstyle{block}=[fill=white,draw=red,minimum size=1.5cm, rounded corners,align=center]
\pgfdeclarelayer{background}
\pgfsetlayers{background,main}
\begin{tikzpicture}
\node[block] (2D) {$2$D\\Histogramm};
\node[draw=none,left of=2D,node distance=3cm] (dummy) {};
\node[block,above of=dummy,node distance=1cm] (E) {E\\signal};
\node[block,below of=dummy,node distance=1cm] (DE) {$\Delta$E\\signal};
\node[block,right of=2D,node distance=3cm] (region) {$p$ signal\\selection};
\node[block,right of=region,node distance=3cm] (projection) {E detector\\projection};
\node[block,right of=projection,node distance=3cm] (smoothing) {smoothing};
\node[block,right of=smoothing,node distance=3cm] (rebinning) {rebinning};
\begin{pgfonlayer}{background}
\draw[fill=gray!30,rounded corners] ($(DE.south west) +(-0.5,-0.5)$) rectangle ($(rebinning.north east) +(1,1.5)$);
\end{pgfonlayer}
\end{tikzpicture}
\end{document}
编辑:总结一下这个答案的评论和信息:
- 使用
fit
- 使用
positioning
- 在 background-node 上使用 node-label
代码:
\documentclass[tikz, border=5mm]{standalone}
\usetikzlibrary{backgrounds, positioning, fit}
\begin{document}
\begin{tikzpicture}
\tikzset{
block/.style={
draw=red,
fill=white,
minimum size=1.5cm,
rounded corners,
align=center,
},
}
\node[block] (2D) {$2$D\\Histogramm};
\node[block, above left=-.5cm and 1cm of 2D] (E) {E\\signal};
\node[block, below left=-.5cm and 1cm of 2D] (DE) {$\Delta$E\\signal};
\node[block, right=of 2D] (region) {$p$ signal\\selection};
\node[block, right=of region] (projection) {E detector\\projection};
\node[block, right=of projection] (smoothing) {smoothing};
\node[block, right=of smoothing] (rebinning) {rebinning};
\begin{scope}[on background layer]
\node [fit=(E) (DE) (rebinning), fill= gray!30, rounded corners, inner sep=.5cm, label={[red]below:Your label text here}] {};
\end{scope}
\end{tikzpicture}
\end{document}
渲染图像:
答案2
这与图层的使用无关,而与您指定矩形坐标的方式有关。
考虑这个更简单的例子:
\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{backgrounds}
\begin{document}
\begin{tikzpicture}
\node [draw] (A) {Node A};
\node [draw] (B) at (4,0) {Node B};
\draw (A.north west) + (-0.5,0.5) rectangle (B.south east) + (0.5,-0.5);
\end{tikzpicture}
\end{document}
此行\draw (A.north west) + (-0.5,0.5) rectangle (B.south east) + (0.5,-0.5);
的含义解释如下:
- 移动到节点的西北角
A
。 - 移动到距前一个点上方 0.5 个单位和左侧 0.5 个单位的点,不绘制任何内容。
- 在节点的东南角绘制一个矩形
B
。 - 移动到距上一点以下 0.5 个单位和右侧 0.5 个单位的点,不绘制任何内容。
为了使此示例按预期工作,您可以采取不同的措施:
使用
calc
库 (\usetikzlibrary{calc}
) 并将矩形指定为\draw [red] ($(A.north west) + (-0.5,0.5)$) rectangle ($(B.south east) + (0.5,-0.5)$);
或者
shift
在坐标规范中使用:\draw [red] ([shift={(-0.5,0.5)}]A.north west) rectangle ([shift={(0.5,-0.5)}]B.south east);
答案3
您最好使用fit
库。但是使用您的方法,您应该选择正确的坐标 - x 坐标为rebinning
和 y 坐标为DE
,并在此基础上添加(0.5,-0.5)
。
代码:
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{backgrounds,calc}
\begin{document}
\tikzstyle{block}=[fill=white,draw=red,minimum size=1.5cm, rounded corners,align=center]
\pgfdeclarelayer{background}
\pgfsetlayers{background,main}
\begin{tikzpicture}
\node[block] (2D) {$2$D\\Histogramm};
\node[draw=none,left of=2D,node distance=3cm] (dummy) {};
\node[block,above of=dummy,node distance=1cm] (E) {E\\signal};
\node[block,below of=dummy,node distance=1cm] (DE) {$\Delta$E\\signal};
\node[block,right of=2D,node distance=3cm] (region) {$p$ signal\\selection};
\node[block,right of=region,node distance=3cm] (projection) {E detector\\projection};
\node[block,right of=projection,node distance=3cm] (smoothing) {smoothing};
\node[block,right of=smoothing,node distance=3cm] (rebinning) {rebinning};
\begin{pgfonlayer}{background}
\draw[fill=gray!30,rounded corners] ($(E.north west)+(-0.5,0.5)$)rectangle($(rebinning.south east|-DE.south) +(0.5,-0.5)$) ;
\end{pgfonlayer}
\end{tikzpicture}
\end{document}
答案4
直接回答你的问题:
(E.north west)+(-0.5,0.5)
只是偶然起作用。+
操作符不会将两个坐标相加,而是相对于第一个坐标定义第二个坐标。
这意味着 tikz 移动到E.north west
,然后在 X 方向移动 -0.5,在 Y 方向移动 0.5,然后从那里开始绘制。但是它不吸引 (rebinning.south east)+(0.5,-2)
而是绘制到(rebinning.south east)
然后再次在 X 方向移动 0.5,在 Y 方向移动 -2。
tldr;执行的命令是(E.north west)+(-0.5,0.5)rectangle(rebinning.south east)
,并且附加坐标位于+(1,2)