3

How can we add annotations in sequencediagrams? I want to add annotations (depicting some functionality) at the return of each response (on receiving ARes, BRes, CRes). How can I do that?

\documentclass[tikz]{standalone}
\usetikzlibrary{calc}
\usepackage{tikz}
\usepackage[underline=true]{pgf-umlsd}

\begin{document}
    \begin{sequencediagram}
    \newthread {r}    {R} {}
    \newinst [1] {a} {A}
    \newinst [1] {e} {E}
    \newinst [1] {u} {U}
    
        \begin {sdblock} {Block} {}
        \path (inst1.west) -- (inst4.east);
            \begin{call}{r}{ACall}{a}{ARes}
                \begin{call}{a}{B}{e}{BRes}
                    \begin{call}{e}{CCall}{u}{CRes}
                    \end{call}
                \end{call}
            \end{call}
        \end {sdblock}                                      
    \end{sequencediagram}
\end{document}

enter image description here

1

The package pgf-umlsd internally defines nodes cfn, ctn, rfn and rtn for call from/to and return from/to, for call number n. For example, the arrow labeled ACall in the code from the question goes from node (cf1) to (ct1).

The node labels that you want to annotate do not have a name in pgf-umlsd, unfortunately. However, using the calc tikzlibrary you can define a node as the midway point between two nodes, in this case the from/to nodes. The syntax for this is ($(first node)!0.5!(second node)$).

To put the annotation below the arrow you can use the .south anchor for the nodes when calculating the midway point.

For some more control over the position of the annotation node you can use xshift and yshift or the fine-grained control of the calc library itself.

MWE:

\documentclass[tikz]{standalone}
\usetikzlibrary{calc}
\usetikzlibrary{positioning}
\usepackage{tikz}
\usepackage[underline=true]{pgf-umlsd}

\begin{document}
    \begin{sequencediagram}
    \newthread {r}    {R} {}
    \newinst [1] {a} {A}
    \newinst [1] {e} {E}
    \newinst [1] {u} {U}
    
        \begin {sdblock} {Block} {}
        \path (inst1.west) -- (inst4.east);
            \begin{call}{r}{ACall}{a}{ARes}
                \begin{call}{a}{B}{e}{BRes}
                    \begin{call}{e}{CCall}{u}{CRes}
                    \end{call}
                \end{call}
            \end{call}
        \end {sdblock}
        \node (aannot) at ($(rf1.south)!0.5!(rt1.south)$) {annot A};                                
        \node[yshift=-3mm] (bannot) at ($(rf2.south)!0.5!(rt2.south)$) {annot B};
    \end{sequencediagram}
\end{document}

Result:

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.