我正在使用装饰库将文本放在路径上。但文本没有出现在路径上,而是出现在节点中,我该如何纠正?另外,虚线路径不对称,有没有办法让它干净且看起来对称?如何制作长度相等的实线?
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric, arrows}
\usetikzlibrary{positioning}
\usetikzlibrary{calc,shapes.multipart,chains}
\begin{document}
\tikzstyle{block} = [rectangle, draw, fill=blue!20,
text width=5em, 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 = 2cm, auto]
% Place nodes
\node [block] (init) {model};
\node [cloud, left of=init] (expert) {$\mathbf{\Lambda}$ };
\node [cloud, right of=init] (system) {Predict};
\path [line,dashed] ([yshift=3ex]{init}) to[out=-90,in=-90,looseness=2.2] ([yshift=-.5ex]{expert}) node [midway, above, sloped] (TextNode) {path text};
\path [line] (init) -- (system);
\path [line] (expert) -- (init);
\end{tikzpicture}
\end{document}
答案1
有几个变化。
- 您应该使用
\tikzset
而不是\tikzstyle
。请参阅下面的代码以了解语法。 - 用于
anchor=
测量与节点边缘而不是中心的间距。 - 使用
decorations.text
图书馆。 - 为了使文本路径对称,将其绘制到与矩形底部相同的高度,然后将其延伸到圆形。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning, shapes.geometric, arrows, decorations.text}
\tikzset{block/.style={rectangle, draw, fill=blue!20, text width=5em,
text centered, rounded corners, minimum height=4em},
cloud/.style={draw, ellipse,fill=red!20, node distance=3cm, minimum height=2em},
line/.style={draw, -latex'}
}
\begin{document}
\begin{tikzpicture}[node distance = 2cm, auto]
% Place nodes
\node [block] (init) {model};
\node [cloud, left of=init, anchor=east] (expert) {$\mathbf{\Lambda}$ };
\node [cloud, right of=init, anchor=west] (system) {Predict};
\path [line] (init) -- (system);
\path [line] (expert) -- (init);
\draw [line, dashed]
[postaction={decoration={text along path, reverse path, text align=center, text={path text}}, decorate}]
(init) to[out=-90, in=-90, looseness=2.2] ([yshift=-2em]expert) to (expert);
\end{tikzpicture}
\end{document}
答案2
不太清楚你在寻找什么。我猜,对于这样的事情:
即,文本位于第一个直实心箭头上,而不是虚线曲线上。
\documentclass[margin=3mm]{standalone}%{article}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,
calc, chains,
positioning,
quotes,
shapes, shapes.multipart}
\begin{document}
\begin{tikzpicture}[auto,
node distance = 22mm,
start chain = going right,
arr/.style = {-Stealth},
base/.style = {draw, semithick, minimum size=2.2em, font=\sffamily},
block/.style = {base, text width=5em, align=center, rounded corners, fill=blue!20},
cloud/.style = {base, ellipse, fill=red!20}
]
\begin{scope}[nodes={on chain, join=by arr}]
\node [cloud] (expert) {$\mathbf{\Lambda}$};
\node [block] (init) {model};
\node [cloud] (system) {Predict};
\end{scope}
\path (expert) to ["path text"] (init);
\draw[arr, dashed] (init) to [out=240, in=300] (expert);
\end{tikzpicture}
\end{document}
对于箭头上方的标签,使用\quotes
库,对于边缘选项,使用虚线bend left=60
。利用chains
库,该库已加载到文档序言中。节点样式在中定义为选项tikzpicture
。这样可以删除\tikzstyle
MWE 中使用的过时定义。