Debian Codegraph @

texmf-dist/doc/latex/codeanatomy/demo-anatomy-of-a-while-loop.tex

raw
1 %% LaTeX2e file `tmp-gen-anatomy-of-a-while-loop.tex'
2 %% generated by the `filecontents' environment
3 %% from source `codeanatomy.lstlisting' on 2025/10/19.
4 %%
5 \lstset{escapeinside={!}{!}}
6 \begin{lrbox}{\mycodebox}
7 \begin{lstlisting}
8 !\cPart{i}{\bgcode{int power = 1;}}\phantom{\rule[-2ex]{0.1ex}{0.1ex}}!
9 while ( !\cPart{c}{power <= n/2}! )
10 !\cPart{po}{\{}!
11 !\cPart{b}{power = 2*power;}!
12 !\cPart{pc}{\}}!
13 \end{lstlisting}
14 \end{lrbox}
15
16
17 \begin{tikzpicture}[remember picture]
18 % {[on background layer]\draw[code grid debug] (-2.5,-0.5) grid (2.5,2.5);}
19 \node(code) [anatomy] at (0,0) {\usebox\mycodebox};
20
21 % Annotation
22 \codeAnnotation{iText} (-1,3.25) {initialization is a\\separate statement}
23 \codeAnnotation{cText} (3.5,3) {loop-\\continuation\\condition}
24 \codeAnnotation{pText} (-1.5,0.5) {braces are\\optional\\when body\\is a single\\statement}
25 \codeAnnotation{bText} (2.125,-0.5) {body}
26 % Arrows
27 \draw[->,annotation] (iText) -- (i.north west);
28 \draw[->,annotation] (cText) -- (c);
29 \draw[->,annotation] (bText) -- (b);
30 \draw[->,annotation] (pText) -- (po);
31 \draw[->,annotation] (pText) -- (pc);
32 \end{tikzpicture}