我开始使用 Tikz,取了一段代码样本,并尝试自己编写。我快完成了,但有几个问题让我很头疼。
首先,这是我的代码:
\documentclass{article}
\usepackage{tikz}
\usepackage{amssymb}
\usetikzlibrary{arrows}
\begin{document}
\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=2.5cm,
thick,main node/.style={rectangle,draw,font=\sffamily\Large\bfseries}]
\node[main node] (1) {1};
\node[main node] (2) [below of=1] {2};
\node[main node] (3) [below of=2] {3};
\node[main node] (4) [right of=1] {$\varnothing$};
\node[main node] (5) [right of=2] {$\varnothing$};
\node[main node] (6) [right of=3] {$\varnothing$};
\path[every node/.style={font=\sffamily\small}]
(1) edge node [left] {$p_1$} (2)
edge node [above] {$1-p_1$} (4)
(2) edge node [left] {$p_2$} (3)
edge node [above] {$1-p_2$} (5)
(3) edge [bend left] node [left] {$p_3$} (1)
edge node [above] {$1-p_3$} (6);
\end{tikzpicture}
\end{document}
我想要改变的是:
1/ 去掉空集节点上的矩形
2/ 带有 p3 的箭头,使其变成矩形。是否可以调整它在左侧的距离以确保它不会与其他东西发生冲突?
3/ 更改数字 1、2、3 与空集节点之间的距离。所以基本上,将其向右扩展。
如果有人能向我解释如何更改其中任何一项/全部,那就太好了!
谢谢
答案1
几乎可以肯定有更好的方法可以做到这一点。(我已经学习了一个多小时,但我的学习速度非常慢!)
\documentclass[tikz]{standalone}
\usepackage{amssymb}
\usetikzlibrary{arrows,positioning}
\begin{document}
\begin{tikzpicture}
[ ->,
>=stealth',
shorten >=1pt,
auto,
node distance=2.5cm,
thick,
main node/.style={rectangle,draw,font=\sffamily\Large\bfseries},
empty node/.style={font=\sffamily\Large\bfseries, xshift=50pt}
]% adjust xshift depending on the amount of expansion to the right you require; empty node sets a non-box style for the empty set nodes
\node[main node] (1) {1};
\node[main node] (2) [below of=1] {2};
\node[main node] (3) [below of=2] {3};
\node[empty node] (4) [right of=1] {$\varnothing$};
\node[empty node] (5) [right of=2] {$\varnothing$};
\node[empty node] (6) [right of=3] {$\varnothing$};
% This node is only here to show how to make sure you go around something: increase 1pt if you want a greater separation
\node[shape=circle,draw,outer sep=1pt] (extra7) [left of=2] {something else};
\path[every node/.style={font=\sffamily\small}]
(1) edge node [left] {$p_1$} (2)
edge node [above] {$1-p_1$} (4)
(2) edge node [left] {$p_2$} (3)
edge node [above] {$1-p_2$} (5)
(3) edge node [above] {$1-p_3$} (6);
% this will go to the left (west) of the new node extra7 and will go in straight lines rather than curving
\path[every node/.style={font=\sffamily\small}, draw]
(3.west) -- +(0,0) -| (extra7.west) -- +(0,0) |- node[near start, left] {$p_3$} (1);
\end{tikzpicture}
\end{document}
更新
这避免了 HarishKumar 指出的可能存在问题的语法。请注意,它还会显著改变间距:
\documentclass[tikz]{standalone}
\usepackage{amssymb}
\usetikzlibrary{arrows,positioning}
\begin{document}
\begin{tikzpicture}
[ ->,
>=stealth',
shorten >=1pt,
auto,
node distance=2.5cm,
thick,
main node/.style={rectangle,draw,font=\sffamily\Large\bfseries},
empty node/.style={font=\sffamily\Large\bfseries, xshift=50pt}
]
\node[main node] (1) {1};
\node[main node] (2) [below=of 1] {2};
\node[main node] (3) [below=of 2] {3};
\node[empty node] (4) [right=of 1] {$\varnothing$};
\node[empty node] (5) [right=of 2] {$\varnothing$};
\node[empty node] (6) [right=of 3] {$\varnothing$};
\node[shape=circle,draw,outer sep=1pt] (extra7) [left=of 2] {something else};
\path[every node/.style={font=\sffamily\small}]
(1) edge node [left] {$p_1$} (2)
edge node [above] {$1-p_1$} (4)
(2) edge node [left] {$p_2$} (3)
edge node [above] {$1-p_2$} (5)
(3) edge node [above] {$1-p_3$} (6);
\path[every node/.style={font=\sffamily\small}, draw]
(3.west) -- +(0,0) -| (extra7.west) -- +(0,0) |- node[near start, left] {$p_3$} (1);
\end{tikzpicture}
\end{document}
答案2
\documentclass{article}
\usepackage{tikz}
\usepackage{amssymb}
\usetikzlibrary{arrows,positioning}
\begin{document}
\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=2.5cm,
thick,main node/.style={rectangle,draw,font=\sffamily\Large\bfseries}] %% Use draw = none to get rid of border on all nodes.
\node[main node] (1) {1};
\node[main node] (2) [below = of 1] {2};
\node[main node] (3) [below = of 2] {3};
\node[main node,draw = none] (4) [right = 2in of 1] {$\varnothing$};
\node[main node,draw = none] (5) [right = 2in of 2] {$\varnothing$};
\node[main node,draw = none] (6) [right = 2in of 3] {$\varnothing$};
\path[every node/.style={font=\sffamily\small}]
(1) edge node [left] {$p_1$} (2)
edge node [above] {$1-p_1$} (4)
(2) edge node [left] {$p_2$} (3)
edge node [above] {$1-p_2$} (5)
(3) edge [bend left,distance=1.5in] node [left] {$p_3$} (1)
edge node [above] {$1-p_3$} (6);
\end{tikzpicture}
\end{document}
\documentclass{article}
\usepackage{tikz}
\usepackage{amssymb}
\usetikzlibrary{arrows,positioning}
\begin{document}
\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=2.5cm,
thick,main node/.style={rectangle,draw,font=\sffamily\Large\bfseries}] %% Use draw = none to get rid of border on all nodes.
\node[main node] (1) {1};
\node[main node] (2) [below = of 1] {2};
\node[main node] (3) [below = of 2] {3};
\node[main node,draw = none] (4) [right = 2in of 1] {$\varnothing$};
\node[main node,draw = none] (5) [right = 2in of 2] {$\varnothing$};
\node[main node,draw = none] (6) [right = 2in of 3] {$\varnothing$};
\path[every node/.style={font=\sffamily\small}]
(1) edge node [left] {$p_1$} (2)
edge node [above] {$1-p_1$} (4)
(2) edge node [left] {$p_2$} (3)
edge node [above] {$1-p_2$} (5)
(3) edge node [above] {$1-p_3$} (6);
\path[draw,every node/.style={font=\sffamily\small}]
(3.west) -- ([xshift=-2cm]3.west) -- ([xshift=-2cm]1.west) node [midway,left] {$p_3$} -- (1.west); %% change xshift as you wish
\end{tikzpicture}
\end{document}