Debian Codegraph @

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

raw
1 %% LaTeX2e file `tmp-gen-anatomy-of-a-for-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 !\iPart{init}{\bgcode{int power = 1;}}!
9 for ( !\cPart{i}{int i = 0}!; !\cPart{c}{i <= n}!; !\cPart{u}{i++}! )
10 {
11 !\mtPoint{left}!System.out.println(i + " " + power);!\mtPoint{right}!
12 power *= 2;!\mbPoint{bottom}!
13 }
14 \end{lstlisting}
15 \end{lrbox}
16
17 \begin{tikzpicture}[
18 remember picture
19 ,code annotation/.append style={%
20 font=\sffamily\itshape\scriptsize
21 }
22 ]
23 % {[on background layer]\draw[code grid debug] (-2.5,-0.5) grid (5.5,3.5);}
24 \node(code) [anatomy] at (0,0){\usebox\mycodebox};
25 \fitExtrem{b}{(left) (right) (bottom)}
26 % Annotations
27 \codeAnnotation{initText} (-1.5,2.7) {initialize another\\
28 variable in a \extremPoint{initPoint}[0.75ex]\\
29 separate\\ statement}
30 \codeAnnotation{iText} (1.2,3.5) {declare and initialize\\ a loop control variable}
31 \codeAnnotation{cText} (3.5,3) {loop-\\continuation\\condition}
32 \codeAnnotation{uText} (6,3) {increment}
33 \codeAnnotation{bText} (3.5,-0.25) {body}
34 % arrows on the background
35 {[on background layer]
36 \draw[->,annotation] (initPoint) -- (init.north west);
37 \draw[->,annotation] (iText) -- (i);
38 \draw[->,annotation] (cText) -- (c);
39 \draw[->,annotation] (uText) -- (u);
40 \draw[->,annotation] (bText) -- (b);
41 }
42 \end{tikzpicture}