
我正在使用裝飾庫將文字沿著路徑放置。但是文字沒有出現在路徑上,而是出現在節點中,我該如何更正它?另外,虛線路徑不對稱,有沒有辦法讓它乾淨並且看起來對稱?如何製作等長的實線?
\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 中所使用的過時定義。