下面我添加了 tikzpicture 的“代码”,上部按预期工作。但是,即使“代码”写得完全相同,下部却不能按预期工作。两个外侧箭头似乎显示不正确。它们应该像上部一样,从南北边缘的中间离开左侧节点,从西边缘的中间进入右侧节点。虽然这两个箭头中的上箭头似乎确实按预期离开左侧节点,但右侧节点是从南边而不是西边进入的。下箭头没有按预期离开或进入节点。看起来下箭头是从北边离开左侧节点(但首先穿过节点),然后从南边进入右侧节点。
我已经反复检查了我的“代码”,也在这个论坛上搜索了几个小时,但所有针对相关问题提出的解决方案都没有解决这个特定问题。因此,我非常希望你能帮助我。
\documentclass[a4paper]{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usepackage{changepage}
\begin{document}
\begin{figure}[h]
\centering
\begin{adjustwidth}{-1.7cm}{}
\begin{tikzpicture}[
cur_state/.style={rectangle, minimum width=3cm, minimum height=1cm,text centered, draw=black, fill=blue!30},
new_state/.style={rectangle, rounded corners, minimum width=60mm, minimum height=1cm,text centered, draw=black, fill=red!30},
state/.style={rectangle, minimum width=3cm, minimum height=1cm,text centered},
empty_block/.style={rectangle, minimum width=3cm, minimum height=0cm,text centered},
node distance = 0cm,
auto]
%Drawing of the box and dashed line in middle
\draw[ultra thick] (-6,3.5) rectangle (11,-10);
\draw[dashed] (-6,-3.25) -- (11,-3.25);
% Top left
\node (sk) [cur_state] {top,left};
% Top right Top
\node (sk0) [new_state, above right=12.5mm and 30mm of sk] {top,right,1};
\node (sk1) [new_state,above right=-2.5mm and 30mm of sk] {top,right,2};
% Top right bottom
\node (sk2) [new_state, below right=-2.5mm and 30mm of sk] {top,right,3};
\node (sk3) [new_state, below right=12.5mm and 30mm of sk] {top,right,4};
% Bottom left
\node (sK) [cur_state, below=55mm of sk] {bottom,left};
% Bottom right top
\node (sK0) [new_state, above right=12.5mm and 30mm of sK] {bottom,right,1};
\node (sK1) [new_state,above right=-2.5mm and 30mm of sK] {bottom,right,2};
% Bottom right bottom
\node (sK2) [new_state, below right=-2.5mm and 30mm of sK] {bottom,right,3};
\node (sK3) [new_state, below right=12.5mm and 30mm of sK] {bottom,right,4};
%Titles above:
\node (s') [state, above=6mm of sk0] {Right title};
\node (s) [state, left=40mm of s'] {Left title};
% Titles left:
\node(k<K) [state, above left=-7.5mm and 5mm of sk] {Top title};
\node(k_K) [state, above left=-7.5mm and 5mm of sK] {Bottom title};
% Arrows top part
\draw[->, thick] (sk) |- node[above] {$1 - \sum_{i=1}^{3} p_i$} ++ (sk0);
\draw[->, thick] (sk) -- node[above] {$p_1$} ++ (sk1);
\draw[->, thick] (sk) -- node[below] {$p_2$} ++ (sk2);
\draw[->, thick] (sk) |- node[below] {$p_3$} ++ (sk3);
% Arrows bottom part
\draw[->, thick] (sK) |- node[above] {$1 - \sum_{i=1}^{3} p_i$} ++ (sK0);
\draw[->, thick] (sK) -- node[above] {$p_1$} ++ (sK1);
\draw[->, thick] (sK) -- node[below] {$p_2$} ++ (sK2);
\draw[->, thick] (sK) |- node[below] {$p_3$} ++ (sK3);
\end{tikzpicture}
\end{adjustwidth}
\caption{Abc} \label{fig:transitions}
\end{figure}
\end{document}
答案1
欢迎!上半部分意外地起作用了,但您确实需要摆脱++
:它们意味着您在路径的原点添加了一些东西,但您确实想要绘制到这些节点。在上半部分的情况下,标记为的节点top,left
位于,(0,0)
因此它意外地给出了所需的输出。无论如何,您只需删除它们++
即可到达
\documentclass[a4paper]{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usepackage{changepage}
\begin{document}
\begin{figure}[h]
\centering
\begin{adjustwidth}{-1.7cm}{}
\begin{tikzpicture}[
cur_state/.style={rectangle, minimum width=3cm, minimum height=1cm,text centered, draw=black, fill=blue!30},
new_state/.style={rectangle, rounded corners, minimum width=60mm, minimum height=1cm,text centered, draw=black, fill=red!30},
state/.style={rectangle, minimum width=3cm, minimum height=1cm,text centered},
empty_block/.style={rectangle, minimum width=3cm, minimum height=0cm,text centered},
node distance = 0cm,
auto]
%Drawing of the box and dashed line in middle
\draw[ultra thick] (-6,3.5) rectangle (11,-10);
\draw[dashed] (-6,-3.25) -- (11,-3.25);
% Top left
\node (sk) [cur_state] {top,left};
% Top right Top
\node (sk0) [new_state, above right=12.5mm and 30mm of sk] {top,right,1};
\node (sk1) [new_state,above right=-2.5mm and 30mm of sk] {top,right,2};
% Top right bottom
\node (sk2) [new_state, below right=-2.5mm and 30mm of sk] {top,right,3};
\node (sk3) [new_state, below right=12.5mm and 30mm of sk] {top,right,4};
% Bottom left
\node (sK) [cur_state, below=55mm of sk] {bottom,left};
% Bottom right top
\node (sK0) [new_state, above right=12.5mm and 30mm of sK] {bottom,right,1};
\node (sK1) [new_state,above right=-2.5mm and 30mm of sK] {bottom,right,2};
% Bottom right bottom
\node (sK2) [new_state, below right=-2.5mm and 30mm of sK] {bottom,right,3};
\node (sK3) [new_state, below right=12.5mm and 30mm of sK] {bottom,right,4};
%Titles above:
\node (s') [state, above=6mm of sk0] {Right title};
\node (s) [state, left=40mm of s'] {Left title};
% Titles left:
\node(k<K) [state, above left=-7.5mm and 5mm of sk] {Top title};
\node(k_K) [state, above left=-7.5mm and 5mm of sK] {Bottom title};
% Arrows top part
\draw[->, thick] (sk) |- node[above] {$1 - \sum_{i=1}^{3} p_i$} (sk0);
\draw[->, thick] (sk) -- node[above] {$p_1$} (sk1);
\draw[->, thick] (sk) -- node[below] {$p_2$} (sk2);
\draw[->, thick] (sk) |- node[below] {$p_3$} (sk3);
% Arrows bottom part
\draw[->, thick] (sK) |- node[above] {$1 - \sum_{i=1}^{3} p_i$} (sK0);
\draw[->, thick] (sK) -- node[above] {$p_1$} (sK1);
\draw[->, thick] (sK) -- node[below] {$p_2$} (sK2);
\draw[->, thick] (sK) |- node[below] {$p_3$} (sK3);
\end{tikzpicture}
\end{adjustwidth}
\caption{Abc} \label{fig:transitions}
\end{figure}
\end{document}
答案2
您的问题已经由@Schrödinger's cat 的答案解决,即:符号++
表示您使用相对坐标,这在小节中描述13.4.1 指定相对坐标,TikZ 和 PGF 手册(版本 3.1.4b)第 143 页。使用相对坐标意味着,如果
\draw (1,1) -- ++ (2,2);
(1,1)
在绝对坐标和(1+2,1+2)
例如之间绘制一条线(3,3)
。在这种简单情况下,您也可以使用
\draw (1,1) -- + (2,2);
当您有坐标链时,这些符号之间的差异就会出现:
\draw (1,1) -- ++ (2,2) -- ++ (1,1);
\draw (1,1) -- + (2,2) -- + (1,1);
这相当于以下绝对坐标
\draw (1,1) -- (3,3) -- (4,4);
\draw (1,1) -- (3,3) -- (2,2);
这意味着,在第二种情况下,相对坐标被添加到起始坐标而不是前一个坐标。因此,在您喜欢在给定的绝对坐标之间绘制箭头的情况下,您将通过++
从绘制线条的代码中删除来获得所需的结果。
以下是一些你可能会感兴趣的题外话。使用额外的 TikZ 库,如calc
、chains
和fit
,quotes
你可以在内部节点、虚线和顶部标题周围绘制框架,而无需绝对定位,还可以使代码更短一些:
\documentclass[a4paper]{article}
\usepackage{tikz}
\usetikzlibrary{calc, chains,
fit,
positioning,
quotes}
\usepackage{changepage}
\begin{document}
\begin{figure}[ht]
\centering
\begin{adjustwidth}{-1.7cm}{}
\begin{tikzpicture}[auto,
node distance = 2mm and 12mm,
start chain = A going below,
cur_state/.style = {rectangle, draw, fill=blue!30,
minimum width=3cm, minimum height=1cm,
align=center},
new_state/.style = {rectangle, rounded corners, draw, fill=red!30,
minimum width=60mm, minimum height=1cm,
align=center, on chain=A},
state/.style = {rectangle, minimum width=3cm, minimum height=1cm, align=center},
empty_block/.style={rectangle, minimum width=3cm, minimum height=0cm, align=center}
]
\begin{scope}[every node/.style = {new_state}]
% Top right Top
\node {top,right,1}; % A-1: name of node
\node {top,right,2};
\node {top,right,3};
\node {top,right,4};
%
\coordinate[on chain]; % A-5
% Bottom right
\node {bottom,right,1};
\node {bottom,right,2};
\node {bottom,right,3};
\node {bottom,right,4}; % A-8
\end{scope}
% Top left
\node (sk1) [cur_state, left=of $(A-2.west)!0.5!(A-3.west)$] {top,left};
\node (k<K) [state, left=of sk1] {Top title};
% Bottom left
\node (sk2) [cur_state, left=of $(A-7.west)!0.5!(A-8.west)$] {bottom,left};
\node (k_K) [state, left=of sk2] {Bottom title};
\begin{scope}[every path/.style={draw, ->, thick}]
% Arrows top part
\path (sk1) |- node[above] {$- \sum_{i=1}^{3} p_i$} (A-1);
\path (sk1) to ["$p_1$",sloped] (A-2);
\path (sk1) to ["$p_2$" ',sloped] (A-3);
\path (sk1) |- node[below] {$p_3$} (A-4);
% Arrows bottom part
\path (sk2) |- node[above] {$- \sum_{i=1}^{3} p_i$} (A-6);
\path (sk2) to ["$p_1$",sloped] (A-7);
\path (sk2) to ["$p_2$" ',sloped] (A-8);
\path (sk2) |- node[below] {$p_3$} (A-9);
\end{scope}
%Drawing of the box and dashed line in middle
\node (FIT) [draw, ultra thick, inner sep=2mm,
fit=(A-1) (A-9) (k_K) (k<K)] {};
\draw[dashed] (FIT.west) -- (FIT.east);
%Titles above:
\node (s) [state, above=-2mm of sk1 |- FIT.north] {Left title};
\node (s') [state, above=-2mm of A-1 |- FIT.north] {Right title};
\end{tikzpicture}
\end{adjustwidth}
\caption{Abc}
\label{fig:transitions}
\end{figure}
\end{document}