| 1 |
%% memorygraphs.tex |
| 2 |
%% Copyright 2018-2019 C. Staps |
| 3 |
% |
| 4 |
% This work may be distributed and/or modified under the |
| 5 |
% conditions of the LaTeX Project Public License, either version 1.3 |
| 6 |
% of this license or (at your option) any later version. |
| 7 |
% The latest version of this license is in |
| 8 |
% http://www.latex-project.org/lppl.txt |
| 9 |
% and version 1.3 or later is part of all distributions of LaTeX |
| 10 |
% version 2005/12/01 or later. |
| 11 |
% |
| 12 |
% This work has the LPPL maintenance status “maintained”. |
| 13 |
% |
| 14 |
% The Current Maintainer of this work is C. Staps. |
| 15 |
% |
| 16 |
% This work consists of the files memorygraphs.sty and the documentation in |
| 17 |
% memorygraphs.tex and example-fibonacci.tex. |
| 18 |
\documentclass[a4paper]{article} |
| 19 |
|
| 20 |
\usepackage{geometry} |
| 21 |
\usepackage[hidelinks]{hyperref} |
| 22 |
\usepackage{csquotes} |
| 23 |
\usepackage{cleveref} |
| 24 |
|
| 25 |
\usepackage[nonewpage]{imakeidx} |
| 26 |
\makeindex[intoc] |
| 27 |
|
| 28 |
\usepackage{latexgit} |
| 29 |
\usepackage{fancyvrb} |
| 30 |
\usepackage{listings} |
| 31 |
\usepackage{color} |
| 32 |
\definecolor{codebg}{rgb}{0.8,0.8,1} |
| 33 |
\lstset{backgroundcolor=\color{codebg}, |
| 34 |
frame=single, |
| 35 |
framerule=0pt, |
| 36 |
basicstyle=\ttfamily\footnotesize, |
| 37 |
columns=fullflexible} |
| 38 |
|
| 39 |
\usepackage{memorygraphs} |
| 40 |
|
| 41 |
\newcommand{\memorygraphs}{\textsf{memorygraphs}} |
| 42 |
\newcommand{\TikZ}{Ti\textit{k}Z} |
| 43 |
|
| 44 |
\title{The \memorygraphs{} package} |
| 45 |
\author{Camil Staps\thanks{[email protected]}} |
| 46 |
\date{\gitcommitdate[formatDate]} |
| 47 |
|
| 48 |
\newif\ifexamplehorizontal |
| 49 |
\newenvironment{example}{% |
| 50 |
\medskip% |
| 51 |
\VerbatimOut{\jobname.tmp}} |
| 52 |
{\endVerbatimOut% |
| 53 |
\ifexamplehorizontal% |
| 54 |
\noindent\begin{minipage}{\linewidth} |
| 55 |
\centering |
| 56 |
\input{\jobname.tmp} |
| 57 |
\end{minipage} |
| 58 |
\par\bigskip |
| 59 |
\noindent\begin{minipage}{\linewidth} |
| 60 |
\lstinputlisting{\jobname.tmp} |
| 61 |
\end{minipage} |
| 62 |
\else% |
| 63 |
\hfill% |
| 64 |
\noindent\begin{minipage}{.4\linewidth} |
| 65 |
\centering |
| 66 |
\input{\jobname.tmp} |
| 67 |
\end{minipage}% |
| 68 |
\noindent\begin{minipage}{.6\linewidth} |
| 69 |
\lstinputlisting{\jobname.tmp} |
| 70 |
\end{minipage} |
| 71 |
\fi% |
| 72 |
} |
| 73 |
|
| 74 |
\makeatletter |
| 75 |
\definecolor{keyheading}{rgb}{0.75,0,0} |
| 76 |
\newcommand{\keyheading}[2][\pgfutil@empty]{% |
| 77 |
\index{#2@\texttt{#2} key}% |
| 78 |
\edef\pgf@test{#1}% |
| 79 |
\item\texttt{{\color{keyheading}{/tikz/#2}}\ifx\pgf@test\pgfutil@empty\else\,=\,#1\fi}\par} |
| 80 |
\newenvironment{keydescription}[2][\pgfutil@empty]{% |
| 81 |
\list{}{\leftmargin=2em\itemindent-\leftmargin} |
| 82 |
\itemsep=0pt |
| 83 |
\parskip=0pt |
| 84 |
\topsep=0pt |
| 85 |
\keyheading[#1]{#2} |
| 86 |
}{\endlist} |
| 87 |
\newenvironment{macrodescription}[2][\pgfutil@empty]{% |
| 88 |
\index{#2@\texttt{\textbackslash{}#2}}% |
| 89 |
\list{}{\leftmargin=2em\itemindent-\leftmargin} |
| 90 |
\itemsep=0pt |
| 91 |
\parskip=0pt |
| 92 |
\topsep=0pt |
| 93 |
\item\texttt{{\color{keyheading}{\textbackslash#2}}\,#1} |
| 94 |
\par\parskip3pt |
| 95 |
}{\endlist} |
| 96 |
\newcommand{\marg}[1]{\texttt{\textbraceleft}$#1$\texttt{\textbraceright}} |
| 97 |
\makeatother |
| 98 |
|
| 99 |
\newcommand{\opt}[1]{$\langle#1\rangle$} |
| 100 |
|
| 101 |
\begin{document} |
| 102 |
|
| 103 |
\maketitle |
| 104 |
|
| 105 |
\begin{center} |
| 106 |
\input{example-fibonacci} |
| 107 |
\end{center} |
| 108 |
|
| 109 |
\tableofcontents |
| 110 |
|
| 111 |
\section{Introduction} |
| 112 |
This is the documentation of the \LaTeX{} package \memorygraphs{}. |
| 113 |
It defines some \TikZ{} styles and adds anchors to existing styles that ease the declaration of \enquote{memory graphs}. |
| 114 |
It is intended for graphs that represent the memory of a computer program during its execution. |
| 115 |
|
| 116 |
\section{Functionality} |
| 117 |
|
| 118 |
\begin{keydescription}{memory graph} |
| 119 |
The \verb$memory graph$ style is to be used on \verb$tikzpicture$. |
| 120 |
It sets a different node distance that the author finds suitable for this kind of graphs. |
| 121 |
|
| 122 |
\begin{example} |
| 123 |
\begin{tikzpicture}[memory graph] |
| 124 |
\node[draw] (x) {37}; |
| 125 |
\node[draw,right=of x] {42}; |
| 126 |
\end{tikzpicture} |
| 127 |
\end{example} |
| 128 |
\end{keydescription} |
| 129 |
|
| 130 |
\subsection{Nodes} |
| 131 |
The following styles can be used to typeset memory blocks: |
| 132 |
|
| 133 |
\begin{keydescription}{block} |
| 134 |
This is the most basic style to define a memory block. |
| 135 |
By default, this shape is a rectangle with borders: |
| 136 |
|
| 137 |
\begin{example} |
| 138 |
\begin{tikzpicture}[memory graph] |
| 139 |
\node[block] {37}; |
| 140 |
\end{tikzpicture} |
| 141 |
\end{example} |
| 142 |
\end{keydescription} |
| 143 |
|
| 144 |
\begin{keydescription}[\opt{n}]{arity} |
| 145 |
The \verb$arity$ style can be used to create a node with arguments. |
| 146 |
This implies \verb$block$: |
| 147 |
|
| 148 |
\begin{example} |
| 149 |
\begin{tikzpicture}[memory graph] |
| 150 |
\node[arity=2] {Cons}; |
| 151 |
\end{tikzpicture} |
| 152 |
\end{example} |
| 153 |
\end{keydescription} |
| 154 |
|
| 155 |
\begin{macrodescription}[\marg{i}]{arg} |
| 156 |
Because blocks with \verb$arity$ are multipart rectangles, |
| 157 |
one can use \TikZ{}'s \verb$\nodepart$ to put contents in the arguments. |
| 158 |
However, it can be confusing that \verb$\nodepart{two}$ refers to the \emph{first} argument, |
| 159 |
so we redefine \verb$\arg$ in \verb$block$s to identify arguments of the memory block: |
| 160 |
|
| 161 |
\begin{example} |
| 162 |
\begin{tikzpicture}[memory graph] |
| 163 |
\node[arity=2] {Cons \arg{1} 37 \arg{2} \dots}; |
| 164 |
\end{tikzpicture} |
| 165 |
\end{example} |
| 166 |
|
| 167 |
Should one want to use math mode's \verb$\arg$ in a memory block, they can first rename it: |
| 168 |
|
| 169 |
\begin{example} |
| 170 |
\let\matharg\arg |
| 171 |
\begin{tikzpicture}[memory graph] |
| 172 |
\node[block] {$\matharg(1)$}; |
| 173 |
\end{tikzpicture} |
| 174 |
\end{example} |
| 175 |
\end{macrodescription} |
| 176 |
|
| 177 |
\subsection{Markings} |
| 178 |
It is possible to mark the head of memory blocks using triangles in the north east and south east corners. |
| 179 |
|
| 180 |
\begin{keydescription}[\opt{style}]{block mark north east} |
| 181 |
\keyheading[\opt{style}]{block mark north west} |
| 182 |
\keyheading[\opt{style}]{block mark south east} |
| 183 |
\keyheading[\opt{style}]{block mark south west} |
| 184 |
With this key, triangular marks can be added to the corners of the head of a node: |
| 185 |
|
| 186 |
\begin{example} |
| 187 |
\begin{tikzpicture}[memory graph] |
| 188 |
\node[arity=2,block mark north east] {Cons}; |
| 189 |
\end{tikzpicture} |
| 190 |
\end{example} |
| 191 |
|
| 192 |
It is optional to add a style: |
| 193 |
|
| 194 |
\begin{example} |
| 195 |
\begin{tikzpicture} |
| 196 |
[memory graph,every node/.style={block}] |
| 197 |
\node[block mark north east={fill,red}] at (1,1) {1}; |
| 198 |
\node[block mark north west={blue}] at (0,1) {2}; |
| 199 |
\node[block mark south west={fill,green}] at (0,0) {3}; |
| 200 |
\node[block mark south east={brown}] at (1,0) {4}; |
| 201 |
\end{tikzpicture} |
| 202 |
\end{example} |
| 203 |
|
| 204 |
The key is long to avoid clashes with other packages, |
| 205 |
and because it depends on the context what nodes should be marked for. |
| 206 |
It is of course possible to define a shorthand in your own document. |
| 207 |
One application is to mark nodes that are in head normal form (HNF), |
| 208 |
for which one may define the key \verb$hnf$: |
| 209 |
|
| 210 |
\begin{example} |
| 211 |
\tikzset{every block/.style={block mark south east}} |
| 212 |
\tikzset{hnf/.style={block mark south east=fill}} |
| 213 |
\begin{tikzpicture}[memory graph] |
| 214 |
\node[arity=2,hnf] |
| 215 |
{Cons \arg{1}$\dots$ \arg{2}$\dots$}; |
| 216 |
\node[arity=2] at (0,-1) |
| 217 |
{map \arg{1}$\dots$ \arg{2}$\dots$}; |
| 218 |
\end{tikzpicture} |
| 219 |
\end{example} |
| 220 |
|
| 221 |
The size of the rectangles is defined by \verb$\memorygraphs@marklength$, |
| 222 |
which can of course be changed. |
| 223 |
The default is \makeatletter\the\memorygraphs@marklength\makeatother. |
| 224 |
|
| 225 |
\begin{example} |
| 226 |
\makeatletter |
| 227 |
\memorygraphs@marklength=7pt |
| 228 |
\makeatother |
| 229 |
\begin{tikzpicture}[memory graph] |
| 230 |
\node[arity=2,block mark north west=fill] |
| 231 |
{Cons \arg{1}$\dots$ \arg{2}$\dots$}; |
| 232 |
\end{tikzpicture} |
| 233 |
\end{example} |
| 234 |
\end{keydescription} |
| 235 |
|
| 236 |
\subsection{Anchors} |
| 237 |
\index{anchors} |
| 238 |
Because blocks with \verb$arity$ are multipart rectangles, |
| 239 |
one can use anchors like \verb$two south$ to refer to the south of the second part of a node. |
| 240 |
These are aliased as \texttt{arg $i$ south} (and similar for other anchors on multipart nodes), |
| 241 |
where \verb$arg 1$ stands for \verb$two$. |
| 242 |
The first block of a node is aliased as \verb$head$ instead of \verb$arg 0$, |
| 243 |
so one can use \verb$head south$. |
| 244 |
For \verb$head$, anchors for the corners (\verb$head north east$, etc.) are defined as well. |
| 245 |
|
| 246 |
The parts of multipart rectangles do not normally have a \verb$center$ anchor, but \memorygraphs{} defines these. |
| 247 |
One can use both \verb$two center$ and \verb$arg 1 center$ to refer to the center of the first argument of a node. |
| 248 |
|
| 249 |
The additional anchors are shown below. |
| 250 |
See the \TikZ{} manual for the predefined anchors. |
| 251 |
|
| 252 |
{\examplehorizontaltrue |
| 253 |
\begin{example} |
| 254 |
\Large |
| 255 |
\begin{tikzpicture} |
| 256 |
\node |
| 257 |
[arity=2, |
| 258 |
line width=.25cm,inner xsep=1.25cm,inner ysep=.75cm, |
| 259 |
color=black!30,fill=yellow!30] |
| 260 |
(v) {Head \arg{1} 1 \arg{2} 2}; |
| 261 |
\foreach \anchor/\placement in |
| 262 |
{head north/above,head south/below,head center/below, |
| 263 |
head north east/below,head south east/above,head north west/left,head south west/left, |
| 264 |
arg 1 north/above,arg 1 south/below,arg 1 center/below, |
| 265 |
arg 2 north/above,arg 2 south/below,arg 2 center/below} |
| 266 |
\draw[shift=(v.\anchor)] plot[mark=x] coordinates{(0,0)} |
| 267 |
node[\placement] {\scriptsize\texttt{v.\anchor}}; |
| 268 |
\end{tikzpicture} |
| 269 |
\end{example} |
| 270 |
} |
| 271 |
|
| 272 |
\subsection{References} |
| 273 |
|
| 274 |
\begin{keydescription}{ref} |
| 275 |
This is a simple style for arrows with a circle at the start and slightly rounded corners: |
| 276 |
|
| 277 |
\begin{example} |
| 278 |
\begin{tikzpicture}[memory graph] |
| 279 |
\node[arity=2] (hd) {Cons}; |
| 280 |
\node |
| 281 |
[arity=2,below=of hd.arg 2 center,anchor=head north] |
| 282 |
(tl) {Cons}; |
| 283 |
\draw[ref] (hd.arg 2 center) |
| 284 |
-- (tl.head north); |
| 285 |
\draw[ref] (tl.arg 2 center) |
| 286 |
|- ($(hd.head north)+(0,.4)$) |
| 287 |
-- (hd.head north); |
| 288 |
\end{tikzpicture} |
| 289 |
\end{example} |
| 290 |
\end{keydescription} |
| 291 |
|
| 292 |
\section{Examples} |
| 293 |
\examplehorizontaltrue |
| 294 |
|
| 295 |
\begin{itemize} |
| 296 |
\item |
| 297 |
The linked list of Fibonacci numbers on the title page was generated with: |
| 298 |
|
| 299 |
\noindent\begin{minipage}{\linewidth} |
| 300 |
\centering |
| 301 |
\input{example-fibonacci.tex} |
| 302 |
\end{minipage} |
| 303 |
\par\bigskip |
| 304 |
\noindent\begin{minipage}{\linewidth} |
| 305 |
\lstinputlisting{example-fibonacci.tex} |
| 306 |
\end{minipage} |
| 307 |
|
| 308 |
\item |
| 309 |
A cyclical linked list, with unboxed integers: |
| 310 |
|
| 311 |
\begin{example} |
| 312 |
\begin{tikzpicture}[memory graph] |
| 313 |
\node[block,arity=2] (xs) {Cons \arg{1} 1}; |
| 314 |
|
| 315 |
\node[block,arity=2,right=of xs.arg 2 east] (xsb) {Cons \arg{1} 2}; |
| 316 |
\draw[ref] (xs.arg 2 center) -- (xsb); |
| 317 |
|
| 318 |
\node[block,arity=2,right=of xsb.arg 2 east] (xsc) {Cons \arg{1} 3}; |
| 319 |
\draw[ref] (xsb.arg 2 center) -- (xsc); |
| 320 |
|
| 321 |
\draw[ref] (xsc.arg 2 center) -- +(0,.6) -| (xs.head north); |
| 322 |
\end{tikzpicture} |
| 323 |
\end{example} |
| 324 |
\end{itemize} |
| 325 |
|
| 326 |
\printindex |
| 327 |
|
| 328 |
\end{document} |