7

I attempt to make a flow chart looks like this:

enter image description here

My question is that, is there some way that I can add some text descriptions below and above the line/body of the arrows that linked between the boxes and the ellipses? For example, we can use \stackrel or \underset or \overset on the \leftarrow or \rightarrow, when we want to add descriptions above/below the arrows when we write the text. How about in tikz?

    \documentclass[letter,10pt]{standalone}
    \usepackage[utf8]{inputenc}
    \usepackage{tikz}

    \usetikzlibrary{matrix,shapes,arrows,positioning,chains, calc, decorations.pathmorphing,decorations.markings}

    \usepackage{pifont}
    \newcommand{\cmark}{\ding{51}}%
    \newcommand{\xmark}{\ding{55}}%
    \newcommand{\done}{\rlap{$\square$}{\raisebox{2pt}{\large\hspace{1pt}\cmark}}
    \hspace{-2.5pt}}
    \newcommand{\wontfix}{\rlap{$\square$}{\large\hspace{1pt}\xmark}}


    \begin{document}

    \tikzset{
    box/.style={
        rectangle,
        draw,
        text width=8em,
        text centered,
        },
    block/.style={
        rectangle,
        draw,
        text width=10em,
        text centered,
        rounded corners
    },
    cloud/.style={
        draw,
        ellipse,
        minimum height=3em,
         text width=6.5em
    }
    }



\begin{tikzpicture}

\matrix (m)[matrix of nodes, column  sep=2cm,row  sep=8mm, align=center, nodes={rectangle,draw, anchor=center} ]{
  &  |[box]| {1}              &  \\
|[box]| {2}    &       &\\
&  |[cloud]| {3}   & |[cloud]| {4} \\
&|[cloud]| {5} &\\
};
\draw [line width=1.pt,  dashed, ->] (m-1-2) edge (m-3-2);
\draw [line width=1.pt, dotted, <->]  (m-2-1) edge (m-3-2);
\path [>=latex, line width=2.pt, <->] (m-3-2) edge (m-3-3);
\path [>=latex, thick, dash dot, ->] (m-3-2) edge (m-4-2);
\path [>=latex, thick,  dash dot, ->] (m-3-3) edge (m-4-2);
\draw [line width=1.pt,dotted, ->] (m-2-1) edge (m-4-2);



\end{tikzpicture}

    \end{document}
  • 3
    Sure, \path [>=latex, line width=2.pt, <->] (m-3-2) edge node[above] {Above}(m-3-3); would place on the arrow – percusse Jul 2 '18 at 4:52
  • Actually, for the standard LaTeX case I would use \xrightarrow{abov} (mathtools package) instead of \stackrel. – user121799 Jul 2 '18 at 5:08
  • Not much of a challenge :(. – cfr Jul 2 '18 at 23:35
9

With the use of the quotes tikz library:

enter image description here

I took a liberty and also changed the positioning of the nodes. You can still stick with your solution with matrix and adequately changes the node names:

\documentclass[letter,margin=3mm, 10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta, positioning, quotes, shapes}% removed not used libraries
% removed not used fonts and new commands

\begin{document}
    \begin{tikzpicture}[auto,
node distance = 6mm and 18mm,
   box/.style = {rectangle,
                 draw,
                 text width=8em,
                 align=center,
                 },
 block/.style = {box,
                 rounded corners
                 text width=10em,
                 },
 cloud/.style = {ellipse,
                 draw,
                 minimum height=3em,
                 text width=6.5em,
                 align=center,
                 },
 every edge quotes/.append style = {font=\footnotesize, text=blue}
                            ]
\node (n1) [box]                        {1};
\node (n2) [box,below left=of n1]       {2};
\node (n3) [cloud,below=of n1 |- n2]    {3};
\node (n4) [cloud,right=of n3]          {4};
\node (n5) [cloud,below=of n3]          {5};
    \begin{scope}[line width=1 pt, >=Stealth]
\draw [dashed,->]   (n1) edge ["text"] (n3);
\draw [dotted,->]   (n2) edge ["text"] (n3);
\draw [dotted,<->]  (n2) edge ["text"] (n5);
\draw [line width=2 pt, <->]    (n3) edge ["text"] (n4);
\draw [dash dot, ->]    (n3) edge ["text"] (n5)
                        (n4) edge ["text"] (n5);
    \end{scope}
    \end{tikzpicture}
\end{document}

addendum: in case, that you like to have some edge labels sloped, you only need to add option sloped to edge label- for example:

\documentclass[margin=3mm, 10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta, positioning, quotes, shapes}

\begin{document}
    \begin{tikzpicture}[auto,
node distance = 6mm and 18mm,
   box/.style = {rectangle,
                 draw,
                 text width=8em,
                 align=center,
                 },
 block/.style = {box,
                 rounded corners
                 text width=10em,
                 },
 cloud/.style = {ellipse,
                 draw,
                 minimum height=3em,
                 text width=6.5em,
                 align=center,
                 },
 every edge quotes/.append style = {font=\footnotesize, text=blue}
                            ]
\node (n1) [box]                        {1};
\node (n2) [box,below left=of n1]       {2};
\node (n3) [cloud,below=of n1 |- n2]    {3};
\node (n4) [cloud,right=of n3]          {4};
\node (n5) [cloud,below=of n3]          {5};
    \begin{scope}[line width=1 pt, >=Stealth]
\draw [dashed,->]   (n1) edge ["text"] (n3);
\draw [dotted,->]   (n2) edge ["text",sloped] (n3);
\draw [dotted,<->]  (n2) edge ["text",sloped] (n5);
\draw [line width=2 pt, <->]    (n3) edge ["text"] (n4);
\draw [dash dot, ->]    (n3) edge ["text"] (n5)
                        (n4) edge ["text" ', sloped] (n5);
    \end{scope}
    \end{tikzpicture}
\end{document}

enter image description here

note: ["text" ', sloped] is shorter version for ["text", swap, sloped]. both move labels on opposite side of edges.

6

As percusse mentions:

\documentclass[letter,10pt]{standalone}
\usepackage[utf8]{inputenc}
\usepackage{tikz}

\usetikzlibrary{matrix,shapes,arrows,positioning,chains, calc, decorations.pathmorphing,decorations.markings}

\usepackage{pifont}
\newcommand{\cmark}{\ding{51}}%
\newcommand{\xmark}{\ding{55}}%
\newcommand{\done}{\rlap{$\square$}{\raisebox{2pt}{\large\hspace{1pt}\cmark}}
\hspace{-2.5pt}}
\newcommand{\wontfix}{\rlap{$\square$}{\large\hspace{1pt}\xmark}}


\begin{document}

\tikzset{
box/.style={
    rectangle,
    draw,
    text width=8em,
    text centered,
    },
block/.style={
    rectangle,
    draw,
    text width=10em,
    text centered,
    rounded corners
},
cloud/.style={
    draw,
    ellipse,
    minimum height=3em,
     text width=6.5em
}
}



\begin{tikzpicture}

\matrix (m)[matrix of nodes, column  sep=2cm,row  sep=8mm, align=center, nodes={rectangle,draw, anchor=center} ]{
  &  |[box]| {1}              &  \\
|[box]| {2}    &       &\\
&  |[cloud]| {3}   & |[cloud]| {4} \\
&|[cloud]| {5} &\\
};
\draw [line width=1.pt,  dashed, ->] (m-1-2) edge node[midway,left]{A} (m-3-2);
\draw [line width=1.pt, dotted, <->]  (m-2-1) edge node[midway,above]{B} (m-3-2);
\path [>=latex, line width=2.pt, <->] (m-3-2) edge node[midway,above]{C} (m-3-3);
\path [>=latex, thick, dash dot, ->] (m-3-2) edge  (m-4-2);
\path [>=latex, thick,  dash dot, ->] (m-3-3) edge node[midway,below]{D} (m-4-2);
\draw [line width=1.pt,dotted, ->] (m-2-1) edge (m-4-2);

\end{tikzpicture}

\end{document}

enter image description here

6

This should be seen as an addition to @marmot's answer. The positioning of the label with e.g. below or left can mean that the label overwrites the line if you have sloped lines. For example the line between 2 and 5 can be

\draw [line width=1.pt,dotted, ->] (m-2-1) edge node[midway,left]{Label} (m-4-2);

to get

enter image description here

To avoid this you can add a distance to the position, like left=10pt. Alternatively you can position the label relative the label instead of the line. While left means left of the line, you can instead state anchor=north east to put the upper right corner of the label midway of the line. Another alternative is to use sloped labels to let it follow the line. In the second case I used pos=0.6 to avoid having the label too close to the line 2<->3 (midway is the same as pos=0.5).

\draw [line width=1.pt,dotted, ->] (m-2-1) edge
node[midway,left=10pt]{Label} 
node[midway,anchor=north east,blue]{Label 2}
node[pos=0.6,above,sloped,red]{Label 3} 
(m-4-2);

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.