
Я использую библиотеку декораций, чтобы разместить текст вдоль пути. Но текст не отображается на пути, вместо этого он отображается в узле, как мне это исправить? Кроме того, пунктирный путь не симметричен, есть ли способ сделать его чистым и симметричным? Как мне сделать сплошные линии одинаковой длины?
\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.