| 1 |
\documentclass[full]{l3doc} |
| 2 |
\usepackage{fontspec} |
| 3 |
\usepackage{biblatex} |
| 4 |
\addbibresource{literatur.bib} |
| 5 |
\newcommand{\slsh}{\textbackslash{}} |
| 6 |
\newcommand{\TikZ}{Ti\textit{k}Z} |
| 7 |
\def\thinmargin{\list{}{\rightmargin-50pt\leftmargin-90pt}\item[]} |
| 8 |
\let\endthinmargin=\endlist |
| 9 |
|
| 10 |
\usepackage{listings} |
| 11 |
\lstset { |
| 12 |
basicstyle=\small\ttfamily |
| 13 |
,escapeinside={+}{+} |
| 14 |
} |
| 15 |
|
| 16 |
\usepackage{codeanatomy} |
| 17 |
|
| 18 |
\begin{document} |
| 19 |
|
| 20 |
\GetFileInfo{codeanatomy.sty} |
| 21 |
\DoNotIndex{} |
| 22 |
|
| 23 |
\title{ |
| 24 |
\pkg{codeanatomy} -- Draw Code Anatomy% |
| 25 |
\thanks{This file describes \fileversion, |
| 26 |
last revised \filedate.}\\[1ex] |
| 27 |
\normalsize{Usage} |
| 28 |
} |
| 29 |
|
| 30 |
\author{ |
| 31 |
Hồng-Phúc Bùi |
| 32 |
\thanks{ |
| 33 |
E-mail: |
| 34 |
\href{mailto:Hồng-Phúc Bùi} |
| 35 |
{hong-phuc.bui (at) htwsaar dot de} |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
\date{Released \filedate} |
| 40 |
|
| 41 |
\AtEndDocument{ |
| 42 |
\printbibliography |
| 43 |
} |
| 44 |
|
| 45 |
\maketitle |
| 46 |
\tableofcontents |
| 47 |
|
| 48 |
\begin{documentation} |
| 49 |
\section{Introduction} |
| 50 |
The idea of this Package is to typeset illustrations of pieces of code with |
| 51 |
annotations on each single parts of code (Code Anatomy). The origin of this idear is |
| 52 |
code illustrations in the texbook~\autocite{sedgewick-wayne-2016}. This package |
| 53 |
just provides tool to draw those figures. |
| 54 |
|
| 55 |
\section{Tutorial} |
| 56 |
In this tutorial we will draw an anatomy of a function like the |
| 57 |
figure~\ref{fig:tut1-function} step by step. |
| 58 |
|
| 59 |
\begin{figure} |
| 60 |
\begin{centering} |
| 61 |
\begin{tikzpicture}[remember picture] |
| 62 |
\codeBlock{ |
| 63 |
\cPart{functionHead}{function \cPart{functionName}{gcd} \cPart{parameterList}{(p, q)}}\{ \\[2.5pt] % |
| 64 |
\ptab{}\mtPoint{mostLeft}if (q === 0) \{\\ |
| 65 |
\ptab\ptab{}return p; \\ |
| 66 |
\ptab{}\} else \{\\ |
| 67 |
\ptab\ptab{}return gcd(q, p \% q);\extremPoint{mostRight}\\ |
| 68 |
\ptab{}\}\mbPoint{mostBottom}\\ |
| 69 |
\} |
| 70 |
}; |
| 71 |
|
| 72 |
\node(functionBody)[draw,code part,fit=(mostLeft) (mostRight) (mostBottom)] {}; |
| 73 |
|
| 74 |
% Anotation labels |
| 75 |
\codeAnnotation{functionHeadText}(-1,3) {Function\\Head} |
| 76 |
\codeAnnotation{functionBodyText}(-1,1.5){Function\\Body} |
| 77 |
\codeAnnotation{functionNameText}(1,4) {Function Name} |
| 78 |
\codeAnnotation{parameterListText} (4,4) {Paramter List} |
| 79 |
|
| 80 |
% Annotation labels to Code |
| 81 |
\draw[->, annotation] (functionBodyText) -- (pic cs:functionBody); |
| 82 |
\draw[->, annotation] (functionHeadText) -- (pic cs:functionHead); |
| 83 |
\draw[->, annotation] (functionNameText) -- (pic cs:functionName); |
| 84 |
\draw[->, annotation] (parameterListText) -- (pic cs:parameterList); |
| 85 |
\end{tikzpicture} |
| 86 |
|
| 87 |
\caption{Anatomy of a function\label{fig:tut1-function}} |
| 88 |
\end{centering} |
| 89 |
\end{figure} |
| 90 |
|
| 91 |
\subsection{Package Usage} |
| 92 |
To use this package, just insert |\usepackage{codeanatomy}| in your \LaTeX{} file. |
| 93 |
|
| 94 |
\subsection{Create an anatomy environment} |
| 95 |
Next step is to create a |tikzpicture| environment. All commands in this package must be placed in |
| 96 |
a |tikzpicture| environment with option |remember picture|. |
| 97 |
|
| 98 |
\begin{thinmargin} |
| 99 |
\begin{tikzpicture}[remember picture] |
| 100 |
\codeBlock{ |
| 101 |
\textbackslash{}begin\{tikzpicture\}[\cPart{option}{remember picture}]\\ |
| 102 |
\cPart{debugGrid}{\{[on background layer]\textbackslash{}draw[code grid debug] (-0.5,-0.5) grid (6.5,4.5);\}}\\ |
| 103 |
\% ... \\ |
| 104 |
\textbackslash{}end\{tikzpicture\} |
| 105 |
} |
| 106 |
|
| 107 |
% Annotation labels |
| 108 |
\codeAnnotation{optionText} (5,2.5) {necsessary for\\later mark} |
| 109 |
\codeAnnotation{debugGridText} (7,0) {plot a thin grey grid on background\\usefull to determinate coordinate of annotation} |
| 110 |
|
| 111 |
% Annotation labels to code |
| 112 |
\draw[->, annotation] (optionText) -- (option); |
| 113 |
\draw[->, annotation] (debugGridText) -- (debugGrid); |
| 114 |
\end{tikzpicture} |
| 115 |
\end{thinmargin} |
| 116 |
|
| 117 |
|
| 118 |
\subsection{Typeset Code Listing in a TikZ-Node} |
| 119 |
As next step we need to put the piece of code in the |tikzpicture| environment using |
| 120 |
the command |\codeBlock|. \medskip |
| 121 |
|
| 122 |
|
| 123 |
\begin{thinmargin} |
| 124 |
\begin{tikzpicture}[remember picture] |
| 125 |
\codeBlock{ |
| 126 |
\textbackslash{}begin\{tikzpicture\}[remember picture]\\ |
| 127 |
\{[on background layer]\textbackslash{}draw[code grid debug]\\ |
| 128 |
\ptab\ptab (-0.5,-0.5) grid (6.5,4.5);\}\\ |
| 129 |
\textbackslash{}codeBlock\{\% \\ |
| 130 |
\mtPoint{mostLeft}function gcd(p, q) \{ \\ |
| 131 |
\ptab{}if (q === 0) \{ \\ |
| 132 |
\ptab\ptab{}return p; \\ |
| 133 |
\ptab{}\} else \{ \\ |
| 134 |
\ptab\ptab{}return gcd(q, p\%q);\extremPoint{mostRight}\\ |
| 135 |
\ptab\} \\ |
| 136 |
\mbPoint{mostBottom}\}\\ |
| 137 |
\} \\ |
| 138 |
\textbackslash{}end\{tikzpicture\} |
| 139 |
} |
| 140 |
|
| 141 |
\node(codeBlockBody)[draw,code part,fit=(mostLeft) (mostRight) (mostBottom)] {}; |
| 142 |
|
| 143 |
\codeAnnotation{codeBlockBodyText} (7,2.5) {Anatomy of Code} |
| 144 |
\draw[->, annotation] (codeBlockBodyText) -- (codeBlockBody); |
| 145 |
\end{tikzpicture} |
| 146 |
\end{thinmargin} |
| 147 |
|
| 148 |
The result of the above code is shown in the figure~\ref{fig:function-compressed}, |
| 149 |
which is not what we really want. All extra whitespaces and newlines in the |
| 150 |
listing are removed, further more |{| and |}| are interpreted as \LaTeX{} tokens and are not |
| 151 |
displayed. |
| 152 |
|
| 153 |
\begin{figure}[h] |
| 154 |
\begin{centering} |
| 155 |
\begin{tikzpicture}[remember picture] |
| 156 |
{[on background layer]\draw[code grid debug] (-0.5,-0.5) grid (6.5,4.5);} |
| 157 |
\codeBlock{ |
| 158 |
function gcd(p, q) { |
| 159 |
if (q === 0) { |
| 160 |
return p; |
| 161 |
} else { |
| 162 |
return gcd(q, p\%q); |
| 163 |
} |
| 164 |
} |
| 165 |
} |
| 166 |
\end{tikzpicture} |
| 167 |
\caption{Unformated Code\label{fig:function-compressed}} |
| 168 |
\end{centering} |
| 169 |
\end{figure} |
| 170 |
|
| 171 |
|
| 172 |
We need to put |\ptab| and |\\| into code to keep whitespaces and newlines. The characters |{| and |}| |
| 173 |
also need to be escaped by prefixing a |\| before them. |
| 174 |
|
| 175 |
\begin{thinmargin} |
| 176 |
\begin{tikzpicture}[remember picture] |
| 177 |
\codeBlock{ |
| 178 |
\textbackslash{}begin\{tikzpicture\}[remember picture]\\ |
| 179 |
\{[on background layer]\textbackslash{}draw[code grid debug]\\ |
| 180 |
\ptab\ptab (-0.5,-0.5) grid (6.5,4.5);\}\\ |
| 181 |
\textbackslash{}codeBlock\{\%\\ |
| 182 |
function gcd(p, q) \slsh\{ \cPart{sh1}{\slsh\slsh}\\ |
| 183 |
\cPart{ptab1}{\slsh{}ptab\{\}}if (q === 0) \slsh\{ \cPart{sh2}{\slsh\slsh}\\ |
| 184 |
\cPart{ptab2}{\slsh{}ptab\slsh{}ptab\{\}}return p; \cPart{sh3}{\slsh\slsh}\\ |
| 185 |
\cPart{ptab3}{\slsh{}ptab}\slsh\} else \slsh\{ \cPart{sh4}{\slsh\slsh}\\ |
| 186 |
\cPart{ptab4}{\slsh{}ptab\slsh{}ptab\{\}}return gcd(q, p\slsh\%q); \cPart{sh5}{\slsh\slsh}\\ |
| 187 |
\cPart{ptab5}{\slsh{}ptab}\slsh\} \cPart{sh6}{\slsh\slsh}\\ |
| 188 |
\slsh\} \cPart{sh7}{\slsh\slsh}\\ |
| 189 |
\} \\ |
| 190 |
\textbackslash{}end\{tikzpicture\} |
| 191 |
} |
| 192 |
|
| 193 |
% Annotation labels |
| 194 |
\codeAnnotation{shText} (9,2.5) {Double backslash} |
| 195 |
\codeAnnotation{ptabText} (-2,2.5) {\texttt{\slsh{}ptab} proceduces\\whitespaces} |
| 196 |
% Annotation labels to code |
| 197 |
\draw[->, annotation] (shText.20) to[out=90,in=0] (sh1.east); |
| 198 |
\draw[->, annotation] (shText.80) to[out=95,in=0] (sh2.east); |
| 199 |
\draw[->, annotation] (shText) -- (sh3); |
| 200 |
\draw[->, annotation] (shText) -- (sh4); |
| 201 |
\draw[->, annotation] (shText) -- (sh5); |
| 202 |
\draw[->, annotation] (shText.270) to[out=260,in=0] (sh6); |
| 203 |
\draw[->, annotation] (shText.340) to[out=260,in=0] (sh7); |
| 204 |
|
| 205 |
\draw[->, annotation] (ptabText) -- (ptab1.west); |
| 206 |
\draw[->, annotation] (ptabText) -- (ptab2.west); |
| 207 |
\draw[->, annotation] (ptabText) -- (ptab3.west); |
| 208 |
\draw[->, annotation] (ptabText) -- (ptab4.west); |
| 209 |
\draw[->, annotation] (ptabText) -- (ptab5.west); |
| 210 |
\end{tikzpicture} |
| 211 |
\end{thinmargin} |
| 212 |
|
| 213 |
The result (figure~\ref{fig:function-formated}) is much more like what we expect |
| 214 |
than the version before (figure~\ref{fig:function-compressed}). |
| 215 |
|
| 216 |
\begin{figure}[h] |
| 217 |
\begin{centering} |
| 218 |
\begin{tikzpicture}[remember picture] |
| 219 |
{[on background layer]\draw[code grid debug] (-0.5,-0.5) grid (6.5,4.5);} |
| 220 |
\codeBlock{ |
| 221 |
function gcd(p, q) \{ \\ |
| 222 |
\ptab{}if (q === 0) \{ \\ |
| 223 |
\ptab\ptab{}return p; \\ |
| 224 |
\ptab\} else \{ \\ |
| 225 |
\ptab\ptab{}return gcd(q, p\%q); \\ |
| 226 |
\ptab\} \\ |
| 227 |
\} \\ |
| 228 |
} |
| 229 |
\end{tikzpicture} |
| 230 |
\caption{Formated Function\label{fig:function-formated}} |
| 231 |
\end{centering} |
| 232 |
\end{figure} |
| 233 |
|
| 234 |
\subsection{Mark Parts of Code} |
| 235 |
Now we can mark interesting parts of code with a blue boxes created by |\cPart|. At some positions |
| 236 |
we can use |\\[<length>]| to add a little amount of vertical space, sothat the boxes do not touch |
| 237 |
each others. |
| 238 |
|
| 239 |
\begin{thinmargin} |
| 240 |
{\footnotesize |
| 241 |
\begin{tikzpicture}[remember picture] |
| 242 |
{[on background layer]\draw[code grid debug] (-0.5,-0.5) grid (10.5,4.5);}\\ |
| 243 |
\codeBlock{ |
| 244 |
\textbackslash{}begin\{tikzpicture\}[remember picture]\\ |
| 245 |
\{[on background layer]\textbackslash{}draw[code grid debug]\\ |
| 246 |
\ptab\ptab (-0.5,-0.5) grid (6.5,4.5);\}\\ |
| 247 |
\textbackslash{}codeBlock\{\%\\ |
| 248 |
\cPart{nestedCodePart}{\slsh{}cPart\{functionHead\}\{function\slsh{}cPart\{functionName\}\{gcd\} \slsh{}cPart\{paramList\}\{(p, q)\}\}} \slsh\{\\ |
| 249 |
\slsh\slsh[2.5pt]\\ |
| 250 |
\slsh{}ptab\{\}\cPart{ep1}{\slsh{}mtPoint\{mostLeft\}}if (q === 0) \slsh\{ \slsh\slsh\\ |
| 251 |
\slsh{}ptab \slsh{}ptab\{\}return p; \slsh\slsh\\ |
| 252 |
\slsh{}ptab \slsh\} else \slsh\{ \slsh\slsh\\ |
| 253 |
\slsh{}ptab \slsh{}ptab\{\}return gcd(q, p\slsh\%q); \cPart{ep2}{\slsh{}extremPoint\{mostRight\}} \slsh\slsh\\ |
| 254 |
\slsh{}ptab\cPart{ep3}{\slsh{}mbPoint\{mostBottom\}}\slsh\} \slsh\slsh\\ |
| 255 |
\slsh\} \slsh\slsh\\ |
| 256 |
\} \\ |
| 257 |
\\ |
| 258 |
\cPart{fitExtemCmd}{\slsh{}fitExtrem\{functionBody\}\{(mostLeft) (mostRight) (mostBottom)\}}\\ |
| 259 |
\slsh{}end\{tikzpicture\} |
| 260 |
} |
| 261 |
|
| 262 |
\codeAnnotation{nestedCodePartText}(11,5){\texttt{cPart} can be nested} |
| 263 |
\codeAnnotation{exText} (15,2){\texttt{extremPoint}s\\are used to mark\\outer most points of\\a multiline code part} |
| 264 |
\codeAnnotation{fitExtemCmdText} (6,-1){\texttt{fitExtrem} draws a rectangle\\which covers all passed extrem points} |
| 265 |
|
| 266 |
\draw[->, annotation] (nestedCodePartText) to[out=200,in=40] (nestedCodePart); |
| 267 |
\draw[->, annotation] (exText) to[out=160,in=350] (ep1.south east); |
| 268 |
\draw[->, annotation] (exText) to[out=190,in=340] (ep2.320); |
| 269 |
\draw[->, annotation] (exText) to[out=195,in=350] (ep3.south east); |
| 270 |
\draw[->, annotation] (fitExtemCmdText) -- (fitExtemCmd); |
| 271 |
\end{tikzpicture} |
| 272 |
} |
| 273 |
\end{thinmargin} |
| 274 |
|
| 275 |
|
| 276 |
\begin{figure}[ht] |
| 277 |
\centering |
| 278 |
\begin{tikzpicture}[remember picture] |
| 279 |
{[on background layer]\draw[code grid debug] |
| 280 |
(-0.5,-0.5) grid (6.5,4.5);} |
| 281 |
\codeBlock{% |
| 282 |
\cPart{functionHead}{function\cPart{functionName}{gcd} \cPart{paramList}{(p, q)}} \{ |
| 283 |
\\[2.5pt] |
| 284 |
\ptab{}\mtPoint{mostLeft} if (q === 0) \{ \\ |
| 285 |
\ptab\ptab{}return p; \\ |
| 286 |
\ptab\} else \{ \\ |
| 287 |
\ptab\ptab{}return gcd(q, p\%q); \extremPoint{mostRight} \\ |
| 288 |
\ptab\mbPoint{mostBottom}\} \\ |
| 289 |
\} \\ |
| 290 |
} |
| 291 |
\fitExtrem{functionBody}{(mostLeft) (mostRight) (mostBottom)} |
| 292 |
\end{tikzpicture} |
| 293 |
\caption{Function with marked parts\label{fig:function-parts}} |
| 294 |
\end{figure} |
| 295 |
|
| 296 |
\subsection{Create Annotation Labels} |
| 297 |
We can use |\codeAnnotation| to create annotation labels for each parts of code. To draw an |
| 298 |
arrow from label to a code part we can use the \TikZ{}command\\ |
| 299 |
|\draw[->,annotation]| \parg{annotation label} |--| \parg{code part} |;|.\\ |
| 300 |
Whereas \parg{annotation label}s are the first argument of |\codeAnnotation|s and |
| 301 |
\parg{code part}s are the first argument of |\cPart|s. |
| 302 |
|
| 303 |
\begin{thinmargin} |
| 304 |
\begin{tikzpicture}[remember picture] |
| 305 |
\codeBlock{ |
| 306 |
\textbackslash{}begin\{tikzpicture\}[remember picture]\\ |
| 307 |
\{[on background layer]\textbackslash{}draw[code grid debug]\\ |
| 308 |
\ptab\ptab (-0.5,-0.5) grid (6.5,4.5);\}\\ |
| 309 |
\textbackslash{}codeBlock\{\%\\ |
| 310 |
\slsh{}cPart\{functionHead\} \{function \slsh{}cPart\{functionName\}\{gcd\} \slsh{}cPart\{paramList\}\{(p, q)\}\} \slsh\{\\ |
| 311 |
\slsh\slsh[2.5pt]\\ |
| 312 |
\slsh{}ptab\{\}\slsh{}mtPoint\{mostLeft\} if (q === 0) \slsh\{ \slsh\slsh\\ |
| 313 |
\slsh{}ptab\slsh{}ptab\{\} return p; \slsh\slsh\\ |
| 314 |
\slsh{}ptab\slsh \} else \slsh\{ \slsh\slsh\\ |
| 315 |
\slsh{}ptab\slsh{}ptab\{\} return gcd(q, p\slsh\%q); \slsh{}extremPoint\{mostRight\} \slsh\slsh\\ |
| 316 |
\slsh{}ptab\slsh{}mbPoint\{mostBottom\}\slsh\} \slsh\slsh\\ |
| 317 |
\slsh\} \\ |
| 318 |
\} \\ |
| 319 |
\\ |
| 320 |
\slsh{}fitExtrem\{functionBody\}\{(mostLeft) (mostRight) (mostBottom)\}\\ |
| 321 |
\\ |
| 322 |
\% Annotations \\ |
| 323 |
\slsh{}codeAnnotation\{functionHeadText\}(-1,3)\{Function\slsh\slsh{}head\} \\ |
| 324 |
\slsh{}codeAnnotation\{functionBodyText\}(-1,1)\{Function\slsh\slsh{}body\} \\ |
| 325 |
\slsh{}codeAnnotation\{functionNameText\}( 1,4)\{Function\slsh\slsh{}name\} \\ |
| 326 |
\slsh{}codeAnnotation\{paramListText\} ( 3,4)\{Parameter\slsh\slsh{}list\} \\ |
| 327 |
\\ |
| 328 |
\% Annotation labels to code parts\\ |
| 329 |
\slsh{}draw[->,annotation] (functionHeadText) -- (functionHead);\\ |
| 330 |
\slsh{}draw[->,annotation] (functionBodyText) -- (functionBody);\\ |
| 331 |
\slsh{}draw[->,annotation] (functionNameText) -- (functionName);\\ |
| 332 |
\slsh{}draw[->,annotation] (paramListText) -- (paramList);\\ |
| 333 |
\slsh{}end\{tikzpicture\} |
| 334 |
} |
| 335 |
|
| 336 |
\end{tikzpicture} |
| 337 |
\end{thinmargin} |
| 338 |
|
| 339 |
Instead of operator |--| we can use operator |to| \oarg{\TikZ{} options} to draw a path from |
| 340 |
\parg{annotation label} to \parg{code part}. Finally we can remove the command |
| 341 |
|\draw[code grid debug]...| at the begin of the |tikzpicture|. |
| 342 |
The final result is shown in the figure~\ref{fig:function-final}, which is almost the same |
| 343 |
as figure~\ref{fig:tut1-function}. |
| 344 |
|
| 345 |
\begin{figure}[ht] |
| 346 |
\centering |
| 347 |
\begin{tikzpicture}[remember picture] |
| 348 |
\codeBlock{% |
| 349 |
\cPart{functionHead} {function \cPart{functionName}{gcd} \cPart{paramList}{(p, q)}} \{ |
| 350 |
\\[2.5pt] |
| 351 |
\ptab{}\mtPoint{mostLeft}if (q === 0) \{ \\ |
| 352 |
\ptab\ptab{} return p; \\ |
| 353 |
\ptab\} else \{ \\ |
| 354 |
\ptab\ptab{} return gcd(q, p\%q);\extremPoint{mostRight} \\ |
| 355 |
\ptab\mbPoint{mostBottom}\} \\ |
| 356 |
\} |
| 357 |
} |
| 358 |
\fitExtrem{functionBody}{(mostLeft) (mostRight) (mostBottom)} |
| 359 |
% Annotations |
| 360 |
\codeAnnotation{functionHeadText}(-1,3){Function\\head} |
| 361 |
\codeAnnotation{functionBodyText}(-1,1){Function\\body} |
| 362 |
\codeAnnotation{functionNameText}( 1,4){Function\\name} |
| 363 |
\codeAnnotation{paramListText} ( 3,4){Parameter\\list} |
| 364 |
% Annotation labels to code parts |
| 365 |
\draw[->,annotation] (functionHeadText) -- (pic cs:functionHead); |
| 366 |
\draw[->,annotation] (functionBodyText) -- (pic cs:functionBody); |
| 367 |
\draw[->,annotation] (functionNameText) -- (pic cs:functionName); |
| 368 |
\draw[->,annotation] (paramListText) -- (pic cs:paramList); |
| 369 |
\end{tikzpicture} |
| 370 |
|
| 371 |
\caption{Function with Annotation Labels\label{fig:function-final}} |
| 372 |
\end{figure} |
| 373 |
|
| 374 |
\section{Usage in conjuntion with \pkg{listings}} |
| 375 |
|
| 376 |
As we see in the previous section, the command |\codeBlock| cannot typeset whitespaces correctly as we |
| 377 |
expect. A way to typeset code listing is using the package \pkg{listings}. See \texttt{codeanatomy.lstlisting.pdf} |
| 378 |
for more information. |
| 379 |
|
| 380 |
\section{Customize style} |
| 381 |
|
| 382 |
\changes{v0.4-Beta} |
| 383 |
{2023/01/24} |
| 384 |
{Add Option \oarg{style} to \texttt{cPart} and \texttt{iPart}} |
| 385 |
|
| 386 |
Maybe we want to highlight the function name \texttt{gcd} with some background color like |
| 387 |
figure~\ref{fig:function-with-highlight}. The best way to do this task is to assign this |
| 388 |
highlight format to a \TikZ{} style --e.g. \texttt{jsid}-- and apply the style to the highlighted nodes. So they |
| 389 |
have the same format. |
| 390 |
|
| 391 |
|
| 392 |
% output the anatomy |
| 393 |
\begin{figure}[htpb] |
| 394 |
\centering |
| 395 |
\begin{tikzpicture}[remember picture] |
| 396 |
\tikzstyle{jsid} = [code part, fill=red!10] |
| 397 |
%{[on background layer]\draw[code grid debug](-0.5,-0.5) grid (9.5,4.5);} |
| 398 |
\codeBlock{ |
| 399 |
\cPart{functionHead}{function \cPart[jsid]{functionName}{gcd} \cPart{parameterList}{(p, q)}}\{ \\[2.5pt] % |
| 400 |
\ptab{}\mtPoint{mostLeft} if (q === 0) \{\\ |
| 401 |
\ptab\ptab{} return p; \\ |
| 402 |
\ptab{}\} else \{\\ |
| 403 |
\ptab\ptab{} return \cPart[jsid]{recursive}{gcd}(q, p \% q);\extremPoint{mostRight}\\ |
| 404 |
\ptab{}\}\mbPoint{mostBottom}\\ |
| 405 |
\} |
| 406 |
} |
| 407 |
|
| 408 |
\fitExtrem{functionBody}{ (mostLeft) (mostRight) (mostBottom) }; |
| 409 |
|
| 410 |
% Anotation labels |
| 411 |
\codeAnnotation{functionHeadText}(-1,3) {Function\\Head} |
| 412 |
\codeAnnotation{functionBodyText}(-1,1.5){Function\\Body} |
| 413 |
\codeAnnotation{functionNameText} (1,4) {Function Name} |
| 414 |
\codeAnnotation{parameterListText}(4,4) {Paramter List} |
| 415 |
|
| 416 |
\codeAnnotation{recursiveText} (7.5,2) {Function can\\call itselft.} |
| 417 |
|
| 418 |
% Annotation labels to Code |
| 419 |
\draw[->, annotation] (functionBodyText) -- (pic cs:functionBody); |
| 420 |
\draw[->, annotation] (functionHeadText) -- (pic cs:functionHead); |
| 421 |
\draw[->, annotation] (functionNameText) -- (pic cs:functionName); |
| 422 |
\draw[->, annotation] (parameterListText) -- (pic cs:parameterList); |
| 423 |
|
| 424 |
\draw[->, annotation] (recursiveText) to[out=175,in=-15] (pic cs:functionName); |
| 425 |
\draw[->, annotation] (recursiveText) to[out=180,in=45] (pic cs:recursive.north east); |
| 426 |
|
| 427 |
\end{tikzpicture} |
| 428 |
\caption{Highlighted name of the function\label{fig:function-with-highlight}} |
| 429 |
\end{figure} |
| 430 |
|
| 431 |
|
| 432 |
%out the Source code |
| 433 |
|
| 434 |
\begin{thinmargin} |
| 435 |
{\footnotesize |
| 436 |
\begin{tikzpicture}[remember picture] |
| 437 |
\tikzstyle{smark} = [code part, fill=yellow] |
| 438 |
\codeBlock{ |
| 439 |
\textbackslash{}begin\{tikzpicture\}[remember picture]\\ |
| 440 |
\slsh{}tikzstyle\{\cPart[smark]{defstyle}{jsid}\} = [code part, fill=red!10]\\ |
| 441 |
\slsh{}codeBlock\{\\ |
| 442 |
\slsh{}cPart\{functionHead\} \{function \slsh{}cPart[\cPart[smark]{u1}{jsid}]\{functionName\}\{gcd\} \slsh{}cPart\{parameterList\}\{(p, q)\}\} \slsh\{ \slsh\slsh[2.5pt]\\ |
| 443 |
\slsh{}ptab\{\}\slsh{}mtPoint\{mostLeft\} if (q === 0) \slsh\{ \slsh\slsh\\ |
| 444 |
\slsh{}ptab\slsh{}ptab\{\} return p; \slsh\slsh\\ |
| 445 |
\slsh{}ptab\{\}\slsh \} else \slsh\{ \slsh\slsh\\ |
| 446 |
\slsh{}ptab\slsh{}ptab\{\} return \slsh{}cPart[\cPart[smark]{u2}{jsid}]\{recursive\}\{gcd\}(q, p\slsh\%q); \slsh{}extremPoint\{mostRight\}\slsh\slsh\\ |
| 447 |
\slsh{}ptab\{\}\slsh{}mbPoint\{mostBottom\}\slsh\} \slsh\slsh\\ |
| 448 |
\slsh\} \\ |
| 449 |
\} \\ |
| 450 |
\\ |
| 451 |
\slsh{}fitExtrem\{functionBody\}\{(mostLeft) (mostRight) (mostBottom)\}\\ |
| 452 |
\\ |
| 453 |
\% Annotations \\ |
| 454 |
\slsh{}codeAnnotation\{functionHeadText\} (-1,3) \{Function\slsh\slsh{}Head\} \\ |
| 455 |
\slsh{}codeAnnotation\{functionBodyText\} (-1,1.5) \{Function\slsh\slsh{}Body\} \\ |
| 456 |
\slsh{}codeAnnotation\{functionNameText\} ( 1,4) \{Function Name\} \\ |
| 457 |
\slsh{}codeAnnotation\{parameterListText\}( 4,4) \{Parameter List\} \\ |
| 458 |
\\ |
| 459 |
\% Annotation labels to code parts\\ |
| 460 |
\slsh{}draw[->,annotation] (functionBodyText) -- (functionBody);\\ |
| 461 |
\slsh{}draw[->,annotation] (functionHeadText) -- (functionHead);\\ |
| 462 |
\slsh{}draw[->,annotation] (functionNameText) -- (functionName);\\ |
| 463 |
\slsh{}draw[->,annotation] (parameterListText) -- (paramList);\\ |
| 464 |
\\ |
| 465 |
\slsh{}draw[->, annotation] (recursiveText) to[out=175,in=-15] (functionName);\\ |
| 466 |
\slsh{}draw[->, annotation] (recursiveText) to[out=180,in=45] (recursive.north east);\\ |
| 467 |
\slsh{}end\{tikzpicture\} |
| 468 |
} |
| 469 |
\end{tikzpicture} |
| 470 |
} |
| 471 |
\end{thinmargin} |
| 472 |
|
| 473 |
\PrintChanges |
| 474 |
\end{documentation} |
| 475 |
\end{document} |
| 476 |
|