[Tex/LaTex] avoid overlapping between line label and nodes

tikz-pgf

I'm using tikz to draw a info flow diagram which contains a few long labels. What I get is unacceptably ugly, mainly resulting from various overlapping between labels and shapes. Ideally, I want the length of line segments dynamically harmonize with the length of texts over it. Is there a way to achieve so?

enter image description here

A minimal example is:

\documentclass[10pt]{article}
\usepackage[usenames]{color} %used for font color
\usepackage{amssymb} %maths
\usepackage{amsmath} %maths
\usepackage[utf8]{inputenc} %useful to type directly diacritic characters
\usepackage{tikz}

\usetikzlibrary{positioning}

\begin{document}
\tikzstyle{enc}=[circle, draw]
\tikzstyle{entity}=[rectangle, draw]
\begin{tikzpicture}
  \node[enc] (app enc) {AEnc};
  \node[enc] (quote enc) [below = of app enc] {QEnc};
  \node[entity] (app) [right = of app enc] {app};
  \node[entity] (challenger) [right=of app] {Challenger};
  \draw[<->] (challenger) to 
      node[auto, swap]{1) $(n,c)$} 
      node[auto] {5) d} 
      (app);
  \draw[->] (app) to node[auto,swap] {2) $(n,c)$} (app enc);
  \draw[dashed,->] (app enc) to node[auto] {3) $appenc_{id} || n||r||CMAC(RK_{QE},.)$} (quote enc);
  \draw[dashed, ->] (quote enc) to node[auto] {4) $appenc_{id} || n||r||Sig_{EPID_{sk}}[.]$} (challenger);
\end{tikzpicture}

\end{document}

Best Answer

You can increase the distance between the nodes using node distance as an argument to the tikzpicture environment. Secondly, rather than using auto to place the node labels using left=<dim> and right=<dim> to manually move them into better positions.

enter image description here

I have also used \DeclareMathOperator\appenc{\text{appenc}} for your appenc as this looks bad above. You probably should do the same with Sig and perhaps others. My guess is that your notation can be improved, but it is easy to say this without knowing the full details:)

Here is the full code:

\documentclass[10pt]{article}
\usepackage[usenames]{color} %used for font color
\usepackage{amssymb} %maths
\usepackage{amsmath} %maths
\usepackage[utf8]{inputenc} %useful to type directly diacritic characters
\usepackage{tikz}

\DeclareMathOperator\appenc{\text{appenc}}

\usetikzlibrary{positioning}

\begin{document}
\tikzstyle{enc}=[circle, draw]
\tikzstyle{entity}=[rectangle, draw]
\begin{tikzpicture}[node distance=25mm]
  \node[enc] (app enc) {AEnc};
  \node[enc] (quote enc) [below = of app enc] {QEnc};
  \node[entity] (app) [right = of app enc] {app};
  \node[entity] (challenger) [right=of app] {Challenger};
  \draw[<->] (challenger) to
      node[auto, swap]{1) $(n,c)$}
      node[auto] {5) d}
      (app);
  \draw[->] (app) to node[auto,swap] {2) $(n,c)$} (app enc);
  \draw[dashed,->] (app enc) to node[right=42mm] {3) $\appenc_{id} || n||r||CMAC(RK_{QE},.)$} (quote enc);
  \draw[dashed, ->] (quote enc) to node[left=38mm] {4) $\appenc_{id} || n||r||Sig_{EPID_{sk}}[.]$} (challenger);
\end{tikzpicture}

\end{document}