2

I'm using the decorations library to put text along a path. But the text does not appear on the path, instead, it appears in the node, how should I correct it? Also, the dotted path is not symmetrical, is there a way to make it clean and look symmetrical? How do I make solid lines of equal length?

\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}

enter image description here

2 Answers 2

6

Several changes.

  • You should use \tikzset instead of \tikzstyle. See the code below for the syntax.
  • Use anchor= to measure spacing from the edge of the node instead of the center.
  • Use the decorations.text library.
  • To get the text path symmetrical, draw it to the same height as the bottom of the rectangle and then extend it to the circle.

enter image description here

\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
  • Is it possible to place "path text" in a straight line along the path instead of curving around?
    – newbie
    Commented Apr 13, 2022 at 2:29
  • 1
    Yes. It's actually much easier: \draw [line, dashed] (init) to[out=-90, in=-90, looseness=2.2]node{path text} ([yshift=-2em]expert) to (expert);. Then you don't need decorations.text. "Text along a path" usually means curved along the path. You just want to label the path with a node.
    – Sandy G
    Commented Apr 13, 2022 at 2:33
0

It is not entirely clear, what you looking for. I guess, that for something like this:

enter image description here

i.e. that text is on the first straight solid arrow and not on dashed curve.

\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}

For labels above arrow is used \quotes library, for dashed line for edge option bend left=60. Exploited is chains library, which is loaded in document preamble. Nodes styles are defined as options in tikzpicture. By this are removed obsolete \tikzstyle definitions used in your MWE.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .