tikzpicture
我在另一个里面使用了两个tikzpicture
。我想从一张图片画一个箭头指向另一张图片。为此,我使用了[remember picture, overlay]
如下代码所示的属性。
新代码
\pgfdeclarelayer{background}
\pgfdeclarelayer{above_background}
\pgfdeclarelayer{main}
\pgfsetlayers{background,above_background,main}
\begin{center}
\begin{tikzpicture}[remember picture, overlay]
\begin{pgfonlayer}{background}
\node[cloud, cloud puffs=16, minimum width=8cm, minimum height=3cm, draw] (cloud) at (0,0) {};
\end{pgfonlayer}
\node[] {
\begin{tikzpicture}[auto]
\node[draw] (node1) at (0,0) {Node 1};
\node[draw] (node2) at (0,-1.5) {Node 2};
\node[draw] (node3) at (0,-3) {Node 3};
\node[draw] (node4) at (0,-4.5) {Node 4};
\begin{pgfonlayer}{main}
\path (node1.west |- node1.north)+(-0.3,0.8) node (a) {};
\path (node4.south -| node4.east)+(2.5,-1) node (b) {};
\path[fill=yellow!20,rounded corners, draw=black!50, dashed]
(a) rectangle (b);
\end{pgfonlayer}
\begin{pgfonlayer}{above_background}
\path (node1.west |- node1.north)+(-0.5,1) node (a) {};
\path (node4.south -| node4.east)+(1.2,-1.5) node (b) {};
\path[fill=black!10,rounded corners, draw=black!50,dashdotted]
(a) rectangle (b);
\end{pgfonlayer}
\end{tikzpicture}
\hspace{1cm}
\begin{tikzpicture}[auto]
\node[draw] (node5) at (0,0) {Node 5};
\node[draw] (node6) at (0,-1.5) {Node 6};
\node[draw] (node7) at (0,-3) {Node 7};
\node[draw] (node8) at (0,-4.5) {Node 8};
\begin{pgfonlayer}{main}
\path (node5.west |- node5.north)+(-0.3,0.8) node (a) {};
\path (node8.south -| node8.east)+(2.5,-1) node (b) {};
\path[fill=yellow!20,rounded corners, draw=black!50, dashed]
(a) rectangle (b);
\end{pgfonlayer}
\begin{pgfonlayer}{above_background}
\path (node5.west |- node5.north)+(-0.5,1) node (a) {};
\path (node8.south -| node8.east)+(1.2,-1.5) node (b) {};
\path[fill=black!10,rounded corners, draw=black!50,dashdotted]
(a) rectangle (b);
\end{pgfonlayer}
\draw[->,draw=black] (node5) to (node1);
\end{tikzpicture}
};
\end{tikzpicture}
\end{center}
旧代码
\begin{center}
\begin{tikzpicture}[remember picture, overlay]
\node[] {
\begin{tikzpicture}[auto]
\node[draw] (node1) at (0,0) {Node 1};
\end{tikzpicture}
\hspace{1cm}
\begin{tikzpicture}[auto]
\node[draw] (node2) at (2,2) {Node 2};
\draw[->,draw=black] (node2) to (node1);
\end{tikzpicture}
};
\end{tikzpicture}
\end{center}
当我编译上面的图片时,我得到了以下错误技术中心并且还特克斯工作室
!Undefined control sequence.
1.61 \savepicturepage
{pgfid2}{15}
我尝试删除 .aux 文件并再次重新编译它,但问题仍然存在。
答案1
你为什么要把事情弄复杂?你所需要的可以用更简单的方法实现。
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[remember picture, overlay,auto]
\node[draw] (node1) at (0,0) {Node 1};
\node[draw] (node2) at (3,2) {Node 2}; %% instead of hspace, I used 3 as x-coordinate.
\draw[->,draw=black] (node2) to (node1);
\end{tikzpicture}
%Some text here whose position won't be affected by the \verb|{tikzpicture}|.
\end{document}
remember picture
此外,如果您不使用诸如current page.north east
等坐标,则无需使用。
进一步减少复杂性,以下是代码:
\documentclass[a4paper,10pt]{report}
\usepackage{tikz}
\begin{document}
\usetikzlibrary{calc, shapes, backgrounds, arrows, decorations.pathmorphing, positioning, fit, petri, tikzmark}
\pgfdeclarelayer{cloud}
\pgfdeclarelayer{background}
\pgfdeclarelayer{above_background}
\pgfdeclarelayer{main}
\pgfsetlayers{cloud,background,above_background,main}
\tikzset{component/.style={draw, fill=blue!20, text width=5em,
text centered, minimum height=2.5em}}
\begin{center}
\begin{tikzpicture}%[remember picture, overlay]
\node[draw] (node1) [component] at (0,0) {Node 1};
\node[draw] (node2) [component] at (0,-1.5) {Node 2};
\node[draw] (node3) [component] at (0,-3) {Node 3};
\node[draw] (node4) [component] at (0,-4.5) {Node 4};
\node[draw,right=5cm of node1] (node5) [component] {Node 5};
\node[draw,right=5cm of node2] (node6) [component] {Node 6};
\node[draw,right=5cm of node3] (node7) [component] {Node 7};
\node[draw,right=5cm of node4] (node8) [component] {Node 8};
\begin{pgfonlayer}{above_background}
\node[fill=yellow!20,rounded corners, draw=black!50, dashed,fit=(node1)(node4),
inner sep=10pt] (left1) {};
\node[fill=yellow!20,rounded corners, draw=black!50, dashed,fit=(node5)(node8),
inner sep=10pt] (right1) {};
\end{pgfonlayer}
\begin{pgfonlayer}{background}
\node[fill=black!10,rounded corners, draw=black!50,dashdotted,fit=(left1),
inner sep=10pt] (left2) {};
\node[fill=black!10,rounded corners, draw=black!50,dashdotted,fit=(right1),
inner sep=10pt] (right2) {};
\end{pgfonlayer}
%% this is also possible
% \begin{pgfonlayer}{cloud}
% \node[cloud, cloud puffs=16,fit=(node1)(node8), draw] (cloud) {};
% \end{pgfonlayer}
%% or this
\begin{pgfonlayer}{cloud}
\node[cloud, cloud puffs=16,minimum width=14cm, minimum height=8cm, draw] (cloud)
at ($(left2)!0.5!(right2)$) {};
\end{pgfonlayer}
\draw[->,draw=black] (node5) to (node1);
\end{tikzpicture}
\end{center}
\end{document}