避免线标签和节点重叠

避免线标签和节点重叠

我正在使用 tikz 绘制一个包含一些长标签的信息流图。结果却丑得令人无法接受,主要是因为标签和形状之间存在各种重叠。理想情况下,我希望线段的长度与其上方文本的长度动态协调。有办法实现吗?

在此处输入图片描述

一个简单的例子是:

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

答案1

node distance您可以使用作为环境参数来增加节点之间的距离tikzpicture。其次,而不是使用auto放置节点标签left=<dim>right=<dim>手动将它们移动到更好的位置。

在此处输入图片描述

我也用过\DeclareMathOperator\appenc{\text{appenc}}你的,appenc因为上面看起来很糟糕。你可能应该对其Sig他人也这样做。我猜你的符号可以改进,但在不知道全部细节的情况下很容易这么说:)

完整代码如下:

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

答案2

这不是一个答案,而是一个长评论安德鲁的回答。他使用node[right=42mm]node[left=38mm]手动调整从注释到节点之间路径的注释距离。TikZ可以通过正确的选择为我们做到这一点anchor

要将 commen 放置在和节点3)之间路径的左侧位置,命令如下quote encapp enc

  \draw[dashed,->] (app enc) to node[left] {3) $\appenc_{id} || n||r||CMAC(RK_{QE},.)$} (quote enc);

并且类似的(用below right而不是left)可用于quote encchallenger节点之间的路径。

完整代码:

\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[left] {3) $\appenc_{id} || n||r||CMAC(RK_{QE},.)$} (quote enc);
  \draw[dashed, ->] (quote enc) to node[below right] {4) $\appenc_{id} || n||r||Sig_{EPID_{sk}}[.]$} (challenger);

\end{tikzpicture}

\end{document}

结果

在此处输入图片描述

答案3

你可以有这样的事情:

在此处输入图片描述

\documentclass[10pt]{standalone}
\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,arrows}
\newcommand{\mi}[1]{\mathit{#1}}

\begin{document}\footnotesize 
\tikzstyle{enc}=[circle, draw]
\tikzstyle{entity}=[rectangle, draw]
\begin{tikzpicture}[node distance=1.5cm,>=latex']
  \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[left,align=center] {3) $\textup{appenc}_{id} \left|| n||r|\right|$\\ $\times\mi{CMAC}(RK_{\mi{QE}},.)$} (quote enc);
  \draw[dashed, ->] (quote enc) to node[below right,align=center] {4) $\textup{appenc}_{id} || n||r||$ \\ $\times \textup{Sig}^{}_{\mi{EPID}_{sk}}[.]$} (challenger);
\end{tikzpicture}

\end{document}

请注意,在这里自动执行操作会非常困难,并且可能会导致糟糕的结果。这尤其是因为您使用的是长公式,这些公式不容易被拆分。因此,唯一好看且更省时的解决方案是手动拆分公式,并通过按键将节点分开更远,node distance= <length>并将文本调整为更小的尺寸(\footnotesize例如,不会有坏处)。此外,auto在这种情况下保留该选项可能没有帮助。因此,您可以手动设置left、、等rightbelow right以更好地放置文本和公式。

最后,我更改了一些多字符变量以便\textup更加清晰,并且更改了一些大写字母变量以便\mathit获得更好的间距。

相关内容