Skip to content

Instantly share code, notes, and snippets.

@lzqlzzq
Last active October 20, 2023 05:15
Show Gist options
  • Save lzqlzzq/c40d8aa90d8c11f253a0c9076aa4723e to your computer and use it in GitHub Desktop.
Save lzqlzzq/c40d8aa90d8c11f253a0c9076aa4723e to your computer and use it in GitHub Desktop.
A latex snippet using tikz to create beautiful token diagrams
\documentclass[border=1pt,tikz]{standalone}
\usepackage{listofitems}
\usepackage{tikz}
\usetikzlibrary{arrows}
\usetikzlibrary{backgrounds}
\newcounter{attributecount}
\NewDocumentCommand{\attribute}{mm} {
\draw[black, fill={#1}, rounded corners=6, thick] (0.1, \value{attributecount}+0.12) rectangle (3.9, \value{attributecount}+0.98) node at (2, \value{attributecount}+0.5) {#2};
\stepcounter{attributecount};
}
\newcounter{tokencount}
\newcounter{maxattribute}
\newenvironment{token} {
\begin{scope}[shift={(\value{tokencount}*4.1+0.1, 0.1)}]
\setcounter{attributecount}{0}
\stepcounter{tokencount}
} {
\begin{pgfonlayer}{background}
\draw[black, fill=gray!10, rounded corners=10, thick] (0, 0) rectangle (4, \value{attributecount}+0.1);
\end{pgfonlayer}
\end{scope};
\noindent
\ifnum\value{attributecount}>\value{maxattribute}
\setcounter{maxattribute}{\value{attributecount}}
\fi
}
\newenvironment{sequence}[2] {
\begin{scope}[shift={(#1, #2)}]
\setcounter{tokencount}{0}
\setcounter{maxattribute}{0}
} {
\draw[black, rounded corners=12, thick] (0, 0) rectangle (\value{tokencount}*4.1+0.1, \value{maxattribute}+0.3);
\end{scope}
}
\begin{document}
\definecolor{Pink}{HTML}{E16E97}
\definecolor{Blue}{HTML}{3C9CD1}
\definecolor{Yellow}{HTML}{F3AE48}
\definecolor{Green}{HTML}{A0CF67}
\definecolor{Grey}{HTML}{D1D3D4}
\begin{tikzpicture}[scale=1.0]
\begin{sequence}{0}{0}
\begin{token}
\attribute{Green}{a};
\attribute{Blue}{b};
\attribute{Green}{c};
\attribute{Pink}{d};
\attribute{Grey}{a};
\end{token}
\begin{token}
\attribute{Yellow}{b};
\attribute{Green}{c};
\attribute{Grey}{d};
\attribute{Green}{3};
\end{token}
\begin{token}
\attribute{Green}{b};
\attribute{Pink}{c};
\attribute{Yellow}{d};
\attribute{Blue}{3};
\end{token}
\begin{token}
\attribute{Yellow}{b};
\attribute{Green}{c};
\attribute{Grey}{d};
\attribute{Green}{3};
\end{token}
\end{sequence}
\end{tikzpicture}
\end{document}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment