How to draw a sequence in latex (using TIKZ)

Today, I will show how to draw a sequence in Latex using the TIKZ package. A sequence is an ordered list of symbols. I often draw sequences for my research paper about sequential pattern mining or episode mining. To draw a sequence, I first import the TIKZ package by adding this line in the section for packages:

\usepackage{tikz}

Example 1. To draw a sequence in a figure that looks like this:

draw a sequence in latex

I use this code:

\begin{figure}[ht]
\centering
\begin{tikzpicture}
%timeline
\draw (-0.4,0) -- (7,0);
% The labels
\node[] at (-1,0.6) {\textbf{event sets}};
\node[] at (-1,-0.6) {\textbf{timestamps}};
% first element
\draw (0.4,-0.2) -- (0.4,0.3); \node[] at (0.4,0.6) {${a,c}$}; \node[] at (0.4,-0.6) {$1$};
% second element
\draw (1.2,-0.2) -- (1.2,0.3); \node[] at (1.2,0.6) {${a}$}; \node[] at (1.2,-0.6) {$2$};
% third element
\draw (2,-0.2) -- (2,0.3); \node[] at (2,0.6) {${a,b}$}; \node[] at (2,-0.6) {$3$};
% next element
\draw (2.8,-0.2) -- (2.8,0.3); \node[] at (2.8,0.6) {${a}$}; \node[] at (2.8,-0.6) {$6$};
% next element
\draw (3.6,-0.2) -- (3.6,0.3); \node[] at (3.6,0.6) {${a,b}$}; \node[] at (3.6,-0.6) {$7$};
% next element
\draw (4.4,-0.2) -- (4.4,0.3); \node[] at (4.4,0.6) {${c}$}; \node[] at (4.4,-0.6) {$8$};
% next element
\draw (5.2,-0.2) -- (5.2,0.3); \node[] at (5.2,0.6) {${b}$}; \node[] at (5.2,-0.6) {$9$};
% next element
\draw (6,-0.2) -- (6,0.3); \node[] at (6,0.6) {${d}$}; \node[] at (6,-0.6) {$11$};
% The arrow
\draw (6.8,-0.13) -- (7,0);
\draw (6.8,0.13) -- (7,0);
\end{tikzpicture}
\caption{A complex event sequence with eight event sets}
\label{CES}
\end{figure}

You could improve upon this using other options in Tikz to add colors, etc.

Example 2. Here is a more complicated example. To draw a sequence that looks like this:

I use this code:

\begin{figure}
\centering
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{tikzpicture}[xscale=8]
\draw[-][draw=black, very thick] (-0.1,0) -- (.5,0);
\draw[->][draw=black, very thick] (.5,0) -- (1.1,0);
%\draw [thick] (0,-.1) node[below]{0} -- (0,0.1);
\draw [thick] (0.25,-.1) node[below]{$t-\frac{\gamma-1}{2}$} -- (0.25,0.1);
\draw [thick] (0,-.1) node[below]{$t-\frac{\lambda\times\gamma-1}{2}$} -- (0,0.1);
%%%%  WINDOW A
\draw [thick, dashed] (0.25,0) -- (0.25,.5) -- (.5,.5) node[above]{
\scriptsize a) short term window (of length $\gamma$)
} -- (0.75,.5) -- (0.75,0) ;
\draw [thick, dashed] (0,0) -- (0,1) -- (.5,1) node[above]{
\scriptsize b) long term window (of length $\lambda \times \gamma $)
} -- (1,1) -- (1,0) ;
\draw [thick] (0.5,-.1) node[below]{$t$} -- (0.5,0.1);
\draw [thick] (0.75,-.1) node[below]{$t+\frac{\gamma-1}{2}$} -- (0.75,0.1);
\draw [thick] (1,-.1) node[below]{$t+\frac{\lambda\times\gamma-1}{2}$} -- (1,0.1);
%\draw [thick] (1,-.1) node[below]{1} -- (1,0.1);
\end{tikzpicture}
%\caption{The windows for calculating the a) short term and b) long term moving average utility  for a timestamp $t$.}

\end{figure}

Example 3. And to draw this:

I use that code:

\begin{figure}[ht]
\centering
	\resizebox{\columnwidth}{!}{
\begin{tikzpicture}
%timeline
\draw (0,0) -- (19,0);
% first element
\draw (1,-0.2) -- (1,0.3);	 \node[] at (1,0.6) { }; 	\node[] at (1,-0.6) {100};
\draw (2,-0.2) -- (2,0.3);	 \node[] at (2,0.6) { }; 	\node[] at (2,-0.6) {101};
% Window 1
\draw[thick] (2,-1.1) -- (2,-1.3); \draw[dashed] (2,-1.2) -- (10,-1.2); \draw(10.1,-1.1) arc (90:270:0.1);
%%%%
\draw (3,-0.2) -- (3,0.3);	 \node[] at (3,0.6) {$x$}; 	\node[] at (3,-0.6) {102};
\draw (4,-0.2) -- (4,0.3);	 \node[] at (4,0.6) { }; 	\node[] at (4,-0.6) {103};
\draw (5,-0.2) -- (5,0.3);	 \node[] at (5,0.6) {$y$}; 	\node[] at (5,-0.6) {104};
\draw (6,-0.2) -- (6,0.3);	 \node[] at (6,0.6) { }; 	\node[] at (6,-0.6) {105};
\draw (7,-0.2) -- (7,0.3);	 \node[] at (7,0.6) {$z$}; 	\node[] at (7,-0.6) {106};
\draw (8,-0.2) -- (8,0.3);	 \node[] at (8,0.6) {$x$}; 	\node[] at (8,-0.6) {107};
\draw (9,-0.2) -- (9,0.3);	 \node[] at (9,0.6) {$y$}; 	\node[] at (9,-0.6) {108};
\draw (10,-0.2) -- (10,0.3);	 \node[] at (10,0.6) {$z$}; 	\node[] at (10,-0.6) {109};
\draw (11,-0.2) -- (11,0.3);	 \node[] at (11,0.6) { }; 	\node[] at (11,-0.6) {110};
\draw (12,-0.2) -- (12,0.3);	 \node[] at (12,0.6) { }; 	\node[] at (12,-0.6) {111};
\draw (13,-0.2) -- (13,0.3);	 \node[] at (13,0.6) { }; 	\node[] at (13,-0.6) {112};
\draw (14,-0.2) -- (14,0.3);	 \node[] at (14,0.6) {$x$}; 	\node[] at (14,-0.6) {113};
\draw (15,-0.2) -- (15,0.3);	 \node[] at (15,0.6) {$y$}; 	\node[] at (15,-0.6) {114};
\draw (16,-0.2) -- (16,0.3);	 \node[] at (16,0.6) {$z$}; 	\node[] at (16,-0.6) {115};
\draw (17,-0.2) -- (17,0.3);	 \node[] at (17,0.6) {}; 	\node[] at (17,-0.6) {116};
\draw (18,-0.2) -- (18,0.3);	 \node[] at (18,0.6) { }; 	\node[] at (18,-0.6) {117};

% The arrow
\draw (18.8,-0.13) -- (19,0);
\draw (18.8,0.13) -- (19,0);
\end{tikzpicture}
}
\caption{A sequence with many timestamps}
\label{CES}
\end{figure}

Besides, as a bonus, if I want to write a sequence using Latex math notation in a paragraph, I write like this:

$S=\langle ({a,c}, $ $ 1),$ $
({a},2),$ $({a,b},3),$ $({a},6),$ $({a,b},7),$ $({c},8),$ $({b},9),$ $({d},{11}) \rangle$

The result will be:

latex sequence math
t

Hope this will be useful for your Latex documents


Philippe Fournier-Viger is a distinguished professor working in China and founder of the SPMF open source data mining software.

This entry was posted in Latex and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published.