TikZ 中的序列模型图

TikZ 中的序列模型图

我如何在 TikZ 中创建类似的东西,以便我可以重复使用该模板来创建其他类似的图表?

在此处输入图片描述

更新:使用 Gonzalo Medina 的代码,我收到以下错误:

! Package pgfkeys Error: I do not know the key '/tikz/myline/.pic' and I am going to ignore it. Perhaps you misspelled it.
See the pgfkeys package documentation for explanation.

实际的代码是:

\documentclass{article}

\usepackage[latin1]{inputenc}
\usepackage[danish]{babel}
\usepackage[T1]{fontenc}

\usepackage{tikz}
\usetikzlibrary{positioning}

\newcommand\Intent[1]{%
  \textbf{Intent:}~#1%
}
\newcommand\Trigger[1]{%
  \textbf{Trigger:}~#1%
}

\begin{document}

\begin{tikzpicture}[
myline/.pic={
  \draw[ultra thick] 
    ++(2.5pt,5pt) -- 
    ++(-5pt,-5pt) --
    ++(5pt,0pt) --
    ++(-5pt,-5pt)
    ;
    \node[anchor=west,font=\itshape,text width=3cm,align=left,xshift=10pt] {#1};
  },
mynode/.style={
  text width=4cm,
  align=left,
  anchor=west
  },
arr/.style={
  ->,
  >=latex
  }
]
\node[mynode] (l1) {\Intent{lorem ipsum}};
\node[mynode,right=of l1] (r1) {\Trigger{lorem ipsum}};
\node[mynode,below=of r1] (r2) {Some other text};
\node[mynode,below=of r2] (r3) {Some other text};
\node[mynode,left=of r3] (l2) {\Intent{lorem ipsum}};
\node[below=of r3] (r4) {Done};

\draw[arr] (r1) -- (r2);
\draw[arr] (r2) -- coordinate (aux) (r3);
\draw[arr] (r3) -- (r4);

\pic at (aux) {myline={some text goes here}};
\end{tikzpicture}

\end{document}

不确定是否重要,但我的乳胶版本是:

This is pdfTeX, Version 3.1415926-2.3-1.40.12 (MiKTeX 2.9)

答案1

有很多种方法可以做到这一点。下面我展示了一种使用 \nodes 和positioning库的简单方法;“弯曲”标志是使用pic路径完成的;定义样式,您可以轻松设置绘图各个元素的方面:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}

\newcommand\Intent[1]{%
  \textbf{Intent:}~#1%
}
\newcommand\Trigger[1]{%
  \textbf{Trigger:}~#1%
}

\begin{document}

\begin{tikzpicture}[
myline/.pic={
  \draw[ultra thick] 
    ++(2.5pt,5pt) -- 
    ++(-5pt,-5pt) --
    ++(5pt,0pt) --
    ++(-5pt,-5pt)
    ;
    \node[anchor=west,font=\itshape,text width=3cm,align=left,xshift=10pt] {#1};
  },
mynode/.style={
  text width=4cm,
  align=left,
  anchor=west
  },
arr/.style={
  ->,
  >=latex
  }
]
\node[mynode] (l1) {\Intent{lorem ipsum}};
\node[mynode,right=of l1] (r1) {\Trigger{lorem ipsum}};
\node[mynode,below=of r1] (r2) {Some other text};
\node[mynode,below=of r2] (r3) {Some other text};
\node[mynode,left=of r3] (l2) {\Intent{lorem ipsum}};
\node[below=of r3] (r4) {Done};

\draw[arr] (r1) -- (r2);
\draw[arr] (r2) -- coordinate (aux) (r3);
\draw[arr] (r3) -- (r4);

\pic at (aux) {myline={some text goes here}};
\end{tikzpicture}

\end{document}

在此处输入图片描述

没有路径的版本pic(适用于较旧的 PGF/TikZ 版本):

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}

\newcommand\Intent[1]{%
  \textbf{Intent:}~#1%
}
\newcommand\Trigger[1]{%
  \textbf{Trigger:}~#1%
}
\newsavebox\myline
\savebox\myline{
\tikz\draw[ultra thick] 
    ++(2.5pt,5pt) -- 
    ++(-5pt,-5pt) --
    ++(5pt,0pt) --
    ++(-5pt,-5pt)
    ;
}

\begin{document}

\begin{tikzpicture}[
mynode/.style={
  text width=4cm,
  align=left,
  anchor=west
  },
arr/.style={
  ->,
  >=latex
  }
]
\node[mynode] (l1) {\Intent{lorem ipsum}};
\node[mynode,right=of l1] (r1) {\Trigger{lorem ipsum}};
\node[mynode,below=of r1] (r2) {Some other text};
\node[mynode,below=of r2] (r3) {Some other text};
\node[mynode,left=of r3] (l2) {\Intent{lorem ipsum}};
\node[below=of r3] (r4) {Done};

\draw[arr] (r1) -- (r2);
\draw[arr] (r2) -- coordinate (aux) (r3);
\draw[arr] (r3) -- (r4);

\node at (aux) (bend) {\usebox\myline};
\node[anchor=west,right=0pt of bend,font=\itshape] {some text goes here};
\end{tikzpicture}

\end{document}

相关内容