我正在使用 TikZ 制作路径图。代码如下
\documentclass[landscape]{article}
\usepackage[latin1]{inputenc}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows}
\begin{document}
\pagestyle{empty}
% Define block styles
\tikzstyle{decision} = [diamond, draw, fill=blue!20,
text width=4.5em, text badly centered, node distance=3cm, inner sep=0pt]
\tikzstyle{block} = [rectangle, draw, fill=blue!20,
text width=6em, text centered, rounded corners, minimum height=4em]
\tikzstyle{line} = [draw, -latex']
\tikzstyle{cloud} = [draw, ellipse,fill=red!20, node distance=3cm,
minimum height=2em]
\begin{tikzpicture}[node distance = 3cm, auto][bend angle=45]
\node [block] (sq1) {PM};
\node [block, below of=sq1] (sq2) {KP};
\node [block, below of=sq2] (sq3) {TKW};
\node [block, left of=sq1,node distance=7cm] (sq4) {PM.GMXTD};
\node [block, left of=sq2,node distance=7cm] (sq5) {KP.GHM};
\node [block, left of=sq3,node distance=7cm] (sq6) {PM.GPRF};
\node [block, below of=sq6] (sq7) {KP.GMNTD};
\node [block, right of=sq2,node distance=7cm] (sq8) {Yield};
\path [line] (sq4) -- node {0.35}(sq1);
\path [line] (sq5) -- node {0.232}(sq2);
\path [line] (sq6) -- node {0.393}(sq3);
\path [line] (sq7) -- node {0.26}(sq3);
\path [line] (sq1) -- node {0.807}(sq8);
\path [line] (sq2) -- node {1.189}(sq8);
\path [line] (sq3) -- node {0.653}(sq8);
\path [line] (sq2) -- node {-0.926}(sq3);
\path [line] (sq1) -- node {-0.65}(sq2);
\path [line] (sq1) to [bend left=45] node {-0.528}(sq3);
\path [line] (sq4) to [bend left=45] node {0.158}(sq8);
\path [line] (sq7) to [bend right=45] node {0.207}(sq8);
\path [line] (sq5) to [bend left=55] node {0.456}(sq8);
\end{tikzpicture}
\end{document}
我有以下三个问题。
1)是否可以沿着箭头改变文本的位置?
2)是否可以沿箭头旋转文本?
3)我可以在两个框的左中间画一个框吗?
提前致谢。
答案1
以下是一些可以让事情变得更容易的想法,尽管需要进行大量的手动小调整:
\documentclass{minimal}
\usepackage{tikz}
\usetikzlibrary{matrix,calc}
\tikzset{
mymx/.style={
matrix of nodes, % this option is available after loading the matrix library
nodes=block,
row sep=2cm, % the amount of space separating the nodes vertically
column sep=3cm, % ^same, but horizontally
},
block/.style={
rectangle,
draw,
fill=blue!20,
rounded corners,
text width=6em,
align=center,
minimum height=4em
},
lbl/.style={
above,
sloped, % make the text follow the path
execute at begin node={$}, % begin math mode, for the minus signs etc.
execute at end node={$}, % end math mode
}
}
\begin{document}
\begin{tikzpicture}
\matrix[mymx] (mx) { % give the name "mx" to this matrix
PM.GMXTD & PM \\
KP.GHM & KP & Yield \\
PM.GPRF & TKW \\
KP.GMNTD \\
};
% now the cells of the matrix are available as, eg.:
% (mx-1-2) means the second column on the first row
%
\path[-latex,every node/.style=lbl]
(mx-1-1) edge[bend left=45] node {0.158} (mx-2-3)
edge node {0.35} (mx-1-2)
(mx-2-1) edge[bend left,pos=.4] node {0.456} ([yshift=-1ex]mx-2-3.north west)
edge node {0.232} (mx-2-2)
(mx-3-1) edge node {0.393} (mx-3-2)
(mx-4-1) edge node {0.26} (mx-3-2)
edge[bend right] node {0.207} (mx-2-3)
(mx-1-2) edge node[pos=.7] {-0.65} (mx-2-2)
edge[bend left,looseness=1.4,pos=.3] node {-0.528} (mx-3-2)
edge node {0.807} (mx-2-3)
(mx-2-2) edge node {1.189} (mx-2-3)
edge node {-0.926} (mx-3-2)
(mx-3-2) edge node {0.653} (mx-2-3)
;
% to put a node in the middle of something, using the calc library:
\begin{scope}[every node/.style={execute at begin node=\strut}]
\node at ($(mx-1-1)!.5!(mx-2-1)$) {mid};
\node[anchor=west] at ($(mx-1-1.west)!.5!(mx-2-1.west)$) {left};
\node[anchor=east] at ($(mx-1-1.east)!.5!(mx-2-1.east)$) {right};
\end{scope}
\end{tikzpicture}
\end{document}
答案2
对于文本位置使用 [pos=0.xxx]
\begin{tikzpicture}[node distance = 3cm, auto][bend angle=45]
\node [block] (sq1) {PM};
\node [block, below of=sq1] (sq2) {KP};
\node [block, below of=sq2] (sq3) {TKW};
\node [block, left of=sq1,node distance=7cm] (sq4) {PM.GMXTD};
\node [block, left of=sq2,node distance=7cm] (sq5) {KP.GHM};
\node [block, left of=sq3,node distance=7cm] (sq6) {PM.GPRF};
\node [block, below of=sq6] (sq7) {KP.GMNTD};
\node [block, right of=sq2,node distance=7cm] (sq8) {Yield};
\path [line] (sq4) -- node[pos=0.3] {0.35}(sq1);
\path [line] (sq5) -- node[pos=0.1] {0.232}(sq2);
\path [line] (sq6) -- node[pos=0.8] {0.393}(sq3);
\path [line] (sq7) -- node {0.26}(sq3);
\path [line] (sq1) -- node {0.807}(sq8);
\path [line] (sq2) -- node {1.189}(sq8);
\path [line] (sq3) -- node {0.653}(sq8);
\path [line] (sq2) -- node {-0.926}(sq3);
\path [line] (sq1) -- node {-0.65}(sq2);
\path [line] (sq1) to [bend left=45] node[pos=0.4] {-0.528}(sq3);
\path [line] (sq4) to [bend left=45] node {0.158}(sq8);
\path [line] (sq7) to [bend right=45] node {0.207}(sq8);
\path [line] (sq5) to [bend left=55] node[pos=0.4] {0.456}(sq8);
\end{tikzpicture}
对于旋转,使用倾斜
\path [line] (sq1) -- node[sloped] {0.807}(sq8);