| 1 |
% !TEX spellcheck = en_GB |
| 2 |
|
| 3 |
\documentclass{article} |
| 4 |
|
| 5 |
\usepackage{fontspec} |
| 6 |
|
| 7 |
\usepackage[backend=biber]{biblatex} |
| 8 |
\addbibresource{literatur.bib} |
| 9 |
|
| 10 |
|
| 11 |
\usepackage{codeanatomy} |
| 12 |
\usepackage{listings} |
| 13 |
\lstset { |
| 14 |
basicstyle=\small\ttfamily% |
| 15 |
,language=% |
| 16 |
,escapeinside={!}{!}% |
| 17 |
,resetmargins=true% |
| 18 |
,columns=flexible% |
| 19 |
,literate={-}{-}1% |
| 20 |
,keepspaces=true |
| 21 |
} |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
% command from ltxdoc |
| 26 |
\newcommand{\pkg}[1]{\textsf{#1}} |
| 27 |
% others shortcuts |
| 28 |
\newcommand{\slsh}{\textbackslash{}} |
| 29 |
\newcommand{\TikZ}{Ti\textit{k}Z} |
| 30 |
\newcommand{\inputlisting}[1]{% |
| 31 |
\lstinputlisting[% |
| 32 |
basicstyle=\footnotesize\ttfamily% |
| 33 |
,xleftmargin=-40pt% |
| 34 |
,resetmargins=true% |
| 35 |
,firstline=5% |
| 36 |
,language=% |
| 37 |
,columns=flexible% |
| 38 |
,escapeinside={}{}% |
| 39 |
,keepspaces=true |
| 40 |
]{#1}% |
| 41 |
} |
| 42 |
\def\thinmargin{\list{}{\rightmargin-30pt\leftmargin-40pt}\item[]} |
| 43 |
\let\endthinmargin=\endlist |
| 44 |
%\newenvironment{thinmargin}{}{} |
| 45 |
|
| 46 |
\usepackage{readprov} % need for \GetFileInfo{} |
| 47 |
\usepackage{hyperref} |
| 48 |
|
| 49 |
\title{ |
| 50 |
\pkg{codeanatomy} -- Draw Code Anatomy% |
| 51 |
\thanks{This file describes \fileversion, |
| 52 |
last revised \filedate.}\\\vspace*{2ex} |
| 53 |
\normalsize{Usage with \pkg{listings}} |
| 54 |
} |
| 55 |
|
| 56 |
\author{ |
| 57 |
Hồng-Phúc Bùi% |
| 58 |
\thanks{ |
| 59 |
E-mail: |
| 60 |
\href{mailto:Hồng-Phúc Bùi} |
| 61 |
{hong-phuc.bui (at) htwsaar dot de} |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
\date{Released \filedate} |
| 66 |
|
| 67 |
\newsavebox\codelst |
| 68 |
\newsavebox\mycodebox |
| 69 |
|
| 70 |
|
| 71 |
\begin{document} |
| 72 |
|
| 73 |
\GetFileInfo{codeanatomy.sty} |
| 74 |
|
| 75 |
\maketitle |
| 76 |
\tableofcontents |
| 77 |
|
| 78 |
|
| 79 |
\section{General Usage with Package \pkg{listings}} |
| 80 |
\subsection{Setup Package \pkg{listings}} |
| 81 |
The most important setup for the package \pkg{listings} is the delimiter to escape \LaTeX{} |
| 82 |
commands in Listing. With this escape delimiter we can mark a piece of code as with \verb|\cPart|. |
| 83 |
In this example we use \verb|!| and \verb|!| as delimiter. Code between \verb|!| and \verb|!| is evaluated as |
| 84 |
\LaTeX{}-code. |
| 85 |
|
| 86 |
\lstset { |
| 87 |
escapeinside={+}{+} |
| 88 |
} |
| 89 |
|
| 90 |
\begin{lrbox}{\codelst} |
| 91 |
\begin{lstlisting} |
| 92 |
\usepackage{codeanatomy} |
| 93 |
\usepackage{listings} |
| 94 |
\lstset { |
| 95 |
basicstyle=\small\ttfamily |
| 96 |
,escapeinside=+\cPart{delimiter}{\{!\}\{!\}}+ |
| 97 |
} |
| 98 |
+\cPart{declareBox}{\slsh{}newsavebox\{\slsh{}mycodebox\}}+ |
| 99 |
\end{lstlisting} |
| 100 |
\end{lrbox} |
| 101 |
|
| 102 |
\begin{thinmargin} |
| 103 |
\begin{tikzpicture}[remember picture] |
| 104 |
% {[on background layer]\draw[code grid debug] (-3.5,-0.5) grid (5.5,4.5);} |
| 105 |
\node(code) [anatomy] at (0,0){\usebox\codelst};% |
| 106 |
|
| 107 |
\codeAnnotation{delimiterText} (6,1.152) {Setup \texttt{!} and \texttt{!}\\as delimiter} |
| 108 |
\codeAnnotation{declareBoxText}(-2.5,0.2) {Savebox to typeset \\ \texttt{lstlisting}\\ inside TikZ-Node} |
| 109 |
|
| 110 |
\draw[->, annotation] (delimiterText) -- (delimiter); |
| 111 |
\draw[->, annotation] (declareBoxText) -- (declareBox); |
| 112 |
\end{tikzpicture} |
| 113 |
\end{thinmargin} |
| 114 |
|
| 115 |
|
| 116 |
Delimiter can also be reset in \verb|document|-Environment, typical just before a new \verb:\begin{lstlisting}: |
| 117 |
environment so each anatomy can have different delimiter. |
| 118 |
The fact is, in this document I use \verb|+| and \verb|+| for the above listing, |
| 119 |
so that I can typeset \verb|!| in this listing. |
| 120 |
|
| 121 |
You may also want to set option \verb|keepspaces| to \verb|true|, so that your reader can easy copy past |
| 122 |
your example code. |
| 123 |
|
| 124 |
|
| 125 |
\subsection{Typeset Code} |
| 126 |
% ----------------------- |
| 127 |
The command \verb|\codeBlock| does not work if the environment \verb|lstlisting| is passed to its argument. So instead of |
| 128 |
\verb|\codeBlock| we must use the \TikZ{} command \verb|\node|: |
| 129 |
|
| 130 |
|
| 131 |
\begin{lrbox}{\codelst} |
| 132 |
\begin{lstlisting} |
| 133 |
+\mtPoint{lstsMT}+\begin{lrbox}{\mycodebox}+\extremPoint{lstsML}+ |
| 134 |
+\slsh{}begin\{lstlisting\}\vspace{6pt}\mbPoint{lstsMB}+ |
| 135 |
+\mtPoint{mostLeft}+function gcd(p,q) { |
| 136 |
if(q === 0) { |
| 137 |
return q; |
| 138 |
} else { |
| 139 |
const r = p % q; |
| 140 |
return gcd(q, r);+\extremPoint{mostRight}+ |
| 141 |
} |
| 142 |
}+\mbPoint{mostBottom}\vspace{6pt}+ |
| 143 |
+\mtPoint{lsteMT}\slsh{}end\{lstlisting\}\extremPoint{lsteML}+ |
| 144 |
\end{lrbox}+\mbPoint{lsteMB}+ |
| 145 |
|
| 146 |
\begin{tikzpicture}[remember picture] |
| 147 |
+\cPart{tikzNode}{\slsh{}node(code) [anatomy] at (0,0)\{\slsh{}usebox\slsh{}mycodebox\}}\cPart{semiColon}{;}+ |
| 148 |
\end{tikzpicture} |
| 149 |
\end{lstlisting} |
| 150 |
\end{lrbox} |
| 151 |
|
| 152 |
\begin{thinmargin} |
| 153 |
\begin{tikzpicture}[remember picture] |
| 154 |
\node(code) [anatomy] at (0,0) {\usebox\codelst}; |
| 155 |
|
| 156 |
\fitExtrem{listingBegin}{(lstsMT) (lstsML) (lstsMB)} |
| 157 |
\fitExtrem{listingEnd}{ (lsteMT) (lsteML) (lsteMB)} |
| 158 |
\fitExtrem{listingContent}{(mostLeft) (mostRight) (mostBottom)} |
| 159 |
|
| 160 |
% Annotations |
| 161 |
\codeAnnotation{tikzNodeText} (-2, 0) {use \texttt{\slsh{}node}\\instead of\\\texttt{\slsh{}codeBlock}} |
| 162 |
\codeAnnotation{listingText} (-2, 4) {typeset code in\\\texttt{lrbox} / \texttt{lstlisting}\\environment} |
| 163 |
\codeAnnotation{listingContentText} (6.5, 3) {whitespaces\\in code\\are kept} |
| 164 |
\codeAnnotation{semiColonText} (8.5, -0.5) {don't forget\\semicolon} |
| 165 |
|
| 166 |
% Arrows from labels to code parts |
| 167 |
\draw[->,annotation] (tikzNodeText) -- (tikzNode.west); |
| 168 |
\draw[->,annotation] (listingText) -- (listingBegin.west); |
| 169 |
\draw[->,annotation] (listingText) -- (listingEnd.west); |
| 170 |
\draw[->,annotation] (listingContentText) -- (listingContent); |
| 171 |
\draw[->,annotation] (semiColonText) -- (semiColon); |
| 172 |
\end{tikzpicture} |
| 173 |
\end{thinmargin} |
| 174 |
|
| 175 |
|
| 176 |
Figure~\ref{fig:full-formatted-code} shows result of the above code. |
| 177 |
|
| 178 |
\begin{figure}[ht] |
| 179 |
\begin{lrbox}{\codelst} |
| 180 |
\begin{lstlisting} |
| 181 |
function gcd(p,q) { |
| 182 |
if (q === 0) { |
| 183 |
return q; |
| 184 |
} else { |
| 185 |
const r = p % q; |
| 186 |
return gcd(q, r) |
| 187 |
} |
| 188 |
} |
| 189 |
\end{lstlisting} |
| 190 |
\end{lrbox} |
| 191 |
|
| 192 |
\begin{tikzpicture}[remember picture] |
| 193 |
\node(code) [anatomy] at (0,0) {\usebox\codelst}; |
| 194 |
\end{tikzpicture} |
| 195 |
|
| 196 |
\caption{Formatted Code Listing\label{fig:full-formatted-code}} |
| 197 |
\end{figure} |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
\subsection{Mark Code} |
| 202 |
% -------------------- |
| 203 |
|
| 204 |
The command \verb|\cPart| can be used to mark single-line code parts. For |
| 205 |
multiple-line code parts once can use \verb|\xxxPoint| family to mark the outer most |
| 206 |
points of code parts and \verb|\fitExtrem| to cover extreme points of a code part. |
| 207 |
These commands must be put between escape delimiter, here \verb|!| and \verb|!|. |
| 208 |
|
| 209 |
\begin{lrbox}{\codelst} |
| 210 |
\begin{lstlisting} |
| 211 |
\begin{lrbox}{\mycodebox} |
| 212 |
+\slsh{}begin\{lstlisting\}+ |
| 213 |
!\cPart{fnHead}{function \cPart{fnName}{gcd} \cPart{paramList}{(p,q)}}\vspace{3pt}!{ |
| 214 |
+\cPart{ep1}{!\slsh{}mtPoint\{mostLeft\}!}+if (q === 0) { |
| 215 |
return q; |
| 216 |
}else{ |
| 217 |
+\cPart{cp}{!\slsh{}cPart\{localVar\}\{const r\}!}+ = p % q; |
| 218 |
return gcd(q, r);+\cPart{ep2}{!\slsh{}extremPoint\{mostRight\}!}+ |
| 219 |
}+\cPart{ep3}{!\slsh{}mbPoint\{mostBottom\}!}+ |
| 220 |
} |
| 221 |
+\slsh{}end\{lstlisting\}+ |
| 222 |
\end{lrbox} |
| 223 |
|
| 224 |
\begin{tikzpicture} |
| 225 |
\node(code) [anatomy] at (0,0) {\usebox\mycodebox}; |
| 226 |
\fitExtrem{fnBody}{(mostLeft) (mostRight) (mostBottom)} |
| 227 |
\end{tikzpicture} |
| 228 |
\end{lstlisting} |
| 229 |
\end{lrbox} |
| 230 |
|
| 231 |
\begin{thinmargin} |
| 232 |
\begin{tikzpicture}[remember picture] |
| 233 |
\node(code) [anatomy] {\usebox\codelst}; |
| 234 |
|
| 235 |
% Annotations |
| 236 |
\codeAnnotation{epText} (12,2.75) {xxx\texttt{Point}-s mark\\outer most\\of the function body} |
| 237 |
\codeAnnotation{cpText} (-1,3.25) {\texttt{cPart} marks a\\single line\\code part} |
| 238 |
% Arrows |
| 239 |
\draw[->,annotation] (epText) -- (ep1.south east); |
| 240 |
\draw[->,annotation] (epText) -- (ep2.east); |
| 241 |
\draw[->,annotation] (epText) -- (ep3.south east); |
| 242 |
\draw[->,annotation] (cpText) -- (cp.west); |
| 243 |
\end{tikzpicture} |
| 244 |
\end{thinmargin} |
| 245 |
|
| 246 |
Figure~\ref{fig:listing-code-parts} shows the result of the above code. |
| 247 |
|
| 248 |
\begin{figure}[ht] |
| 249 |
\begin{lrbox}{\codelst} |
| 250 |
\lstset{escapeinside={!}{!}} |
| 251 |
\begin{lstlisting} |
| 252 |
!\cPart{fnHead}{function \cPart{fnName}{gcd} \cPart{paramList}{(p,q)}}\vspace{3pt}! { |
| 253 |
!\mtPoint{mostLeft}!if (q === 0) { |
| 254 |
return q; |
| 255 |
} else { |
| 256 |
!\cPart{localVar}{const r}! = p % q; |
| 257 |
return gcd(q, r);!\extremPoint{mostRight}! |
| 258 |
}!\mbPoint{mostBottom}! |
| 259 |
} |
| 260 |
\end{lstlisting} |
| 261 |
\end{lrbox} |
| 262 |
|
| 263 |
\begin{tikzpicture}[remember picture] |
| 264 |
\node(code) [anatomy] at (0,0) {\usebox\codelst}; |
| 265 |
\fitExtrem{fnBody}{(mostLeft) (mostRight) (mostBottom)} |
| 266 |
\end{tikzpicture} |
| 267 |
\caption{Code Listing with mark of code parts\label{fig:listing-code-parts}} |
| 268 |
\end{figure} |
| 269 |
|
| 270 |
\subsection{Add Annotations to Listing} |
| 271 |
% ------------------------------------- |
| 272 |
This step is the same as the description in the usage document of package \pkg{codeanatomy}. |
| 273 |
Readers can typeset annotations to the above listing like an exercise. |
| 274 |
|
| 275 |
|
| 276 |
\subsection{Highlight some tokens} |
| 277 |
|
| 278 |
\LaTeX-Code: |
| 279 |
|
| 280 |
\begin{thinmargin} |
| 281 |
\inputlisting{demo-recursive-with-bug.tex} |
| 282 |
\end{thinmargin} |
| 283 |
|
| 284 |
Result: |
| 285 |
|
| 286 |
\input{demo-recursive-with-bug.tex} |
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
\section{Some examples} |
| 292 |
% ==================== |
| 293 |
|
| 294 |
Most of examples in this section are redrawn from the textbook~\autocite{sedgewick-wayne-2016}. |
| 295 |
|
| 296 |
\subsection{Anatomy of a Java Program~\autocite[5]{sedgewick-wayne-2016}} |
| 297 |
% ----------------------------------- |
| 298 |
|
| 299 |
\begin{thinmargin} |
| 300 |
\inputlisting{demo-java-program.tex} |
| 301 |
\end{thinmargin} |
| 302 |
|
| 303 |
\input{demo-java-program.tex} |
| 304 |
|
| 305 |
|
| 306 |
\subsection{Anatomy of an expression~\autocite[17]{sedgewick-wayne-2016}} |
| 307 |
% ----------------------------------- |
| 308 |
\begin{thinmargin} |
| 309 |
\inputlisting{demo-java-expression.tex} |
| 310 |
\end{thinmargin} |
| 311 |
|
| 312 |
\input{demo-java-expression.tex} |
| 313 |
|
| 314 |
|
| 315 |
\subsection{Using a primitive Data Type~\autocite[17]{sedgewick-wayne-2016}} |
| 316 |
% ------------------------------------- |
| 317 |
|
| 318 |
\begin{thinmargin} |
| 319 |
\inputlisting{demo-using-a-primitive-data-type.tex} |
| 320 |
\end{thinmargin} |
| 321 |
|
| 322 |
\input{demo-using-a-primitive-data-type.tex} |
| 323 |
|
| 324 |
|
| 325 |
\subsection{Anatomy of a method signature~\autocite[30]{sedgewick-wayne-2016}} |
| 326 |
% --------------------------------------- |
| 327 |
|
| 328 |
\begin{thinmargin} |
| 329 |
\inputlisting{demo-anatomy-of-a-method-signature.tex} |
| 330 |
\end{thinmargin} |
| 331 |
|
| 332 |
\input{demo-anatomy-of-a-method-signature.tex} |
| 333 |
|
| 334 |
\subsection{Using a library method~\autocite[30]{sedgewick-wayne-2016}} |
| 335 |
% -------------------------------- |
| 336 |
|
| 337 |
\begin{thinmargin} |
| 338 |
\inputlisting{demo-using-a-library-method.tex} |
| 339 |
\end{thinmargin} |
| 340 |
|
| 341 |
\input{demo-using-a-library-method.tex} |
| 342 |
|
| 343 |
|
| 344 |
\subsection{Anatomy of an \texttt{if} statement~\autocite[51]{sedgewick-wayne-2016}} |
| 345 |
% --------------------------------------------- |
| 346 |
\begin{thinmargin} |
| 347 |
\inputlisting{demo-anatomy-of-an-if-statement.tex} |
| 348 |
\end{thinmargin} |
| 349 |
|
| 350 |
\input{demo-anatomy-of-an-if-statement.tex} |
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
\subsection{Anatomy of a \texttt{while} loop~\autocite[54]{sedgewick-wayne-2016}} |
| 355 |
% ------------------------------------------ |
| 356 |
\begin{thinmargin} |
| 357 |
\inputlisting{demo-anatomy-of-a-while-loop.tex} |
| 358 |
\end{thinmargin} |
| 359 |
|
| 360 |
\input{demo-anatomy-of-a-while-loop.tex} |
| 361 |
|
| 362 |
\subsection{Anatomy of a \texttt{for} loop~\autocite[59]{sedgewick-wayne-2016}} |
| 363 |
% ---------------------------------------- |
| 364 |
\begin{thinmargin} |
| 365 |
\inputlisting{demo-anatomy-of-a-for-loop.tex} |
| 366 |
\end{thinmargin} |
| 367 |
|
| 368 |
\input{demo-anatomy-of-a-for-loop.tex} |
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
\subsection{Anatomy of a static method~\autocite[196]{sedgewick-wayne-2016}} |
| 373 |
% ---------------------------------------- |
| 374 |
|
| 375 |
\begin{thinmargin} |
| 376 |
\inputlisting{demo-anatomy-of-a-static-method.tex} |
| 377 |
\end{thinmargin} |
| 378 |
|
| 379 |
\input{demo-anatomy-of-a-static-method.tex} |
| 380 |
|
| 381 |
|
| 382 |
\printbibliography |
| 383 |
|
| 384 |
\end{document} |
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|