summaryrefslogtreecommitdiff
path: root/graphics/pgf/contrib/codeanatomy/codeanatomy.dtx
blob: 8e4361b89704c76f848b6d08f524844b4dee9345 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
% \iffalse meta-comment
%
% File: codeanatomy.dtx Copyright (C) 2019 Hong-Phuc Bui
%
% It may be distributed and/or modified under the conditions of the
% LaTeX Project Public License (LPPL), either version 1.3c of this
% license or (at your option) any later version.  The latest version
% of this license is in the file
%
%    https://www.latex-project.org/lppl.txt
%
%
% -----------------------------------------------------------------------
%
% The development version of the bundle can be found at
%
%    https://github.com/hpb-htw/codeanatomy
%
% for those people who are interested.
%
% -----------------------------------------------------------------------
% \fi
%
% \iffalse
%<package>\NeedsTeXFormat{LaTeX2e}[2018/12/01]
%<package>\ProvidesPackage{codeanatomy}[2023/01/24 v0.4-Beta draw Code Anatomy]
% \fi
%
% \iffalse
%<*driver>
\documentclass[full]{l3doc}

\usepackage{fontspec}
\usepackage{luaotfload}


\usepackage{biblatex}    
\addbibresource{literatur.bib}
\newcommand{\slsh}{\textbackslash{}}
\newcommand{\TikZ}{Ti\textit{k}Z}
\newcommand{\fixedBug}[1]{\tikz[baseline=(X.base)]\node[cross out,draw] (X) {#1};}
\def\thinmargin{\list{}{\rightmargin-50pt\leftmargin-90pt}\item[]}
\let\endthinmargin=\endlist

\usepackage{listings}
\lstset {
    basicstyle=\small\ttfamily
    ,escapeinside={+}{+}
}

\usepackage{codeanatomy}
\EnableCrossrefs
\CodelineIndex
\RecordChanges

\begin{document}
  \DocInput{\jobname.dtx}
\end{document}
%</driver>
% \fi
%
% \GetFileInfo{\jobname.sty}
% \DoNotIndex{\definecolor, \colorlet}
% \DoNotIndex{\NewDocumentCommand, \node}
% \DoNotIndex{\phantom}
% \DoNotIndex{\RequirePackage, \rule}
% \DoNotIndex{\small, \sffamily}
% \DoNotIndex{\textcolor, \tikzmarknode, \tikzset, \ttfamily}
% \DoNotIndex{\usetikzlibrary}
%
% \title{^^A
%   \pkg{codeanatomy} -- Draw Code Anatomy^^A
%   \thanks{This file describes \fileversion, ^^A
%     last revised \filedate.}\\[1ex]^^A
%     \normalsize{Reference}^^A
% }^^A
%
% \author{^^A
%  Hồng-Phúc Bùi^^A
%  \thanks{^^A
%    E-mail:
%    \href{mailto:Hồng-Phúc Bùi}
%      {hong-phuc.bui (at) htwsaar dot de}^^A
%   }^^A
% }
%
% \date{Released \filedate}
%
% \parindent0pt
%
% \maketitle
% \tableofcontents
%
% \section{Hints}
% Usage of this Package can be found in \texttt{codeanatomy.usage.pdf} and \texttt{codeanatomy.lstlisting.pdf}.
% This document show only generated reference of commands in this Package.
%

% ^^A %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% \section{Implementation}
%
% \changes{v0.2-Alpha}
%         {2019/07/10}
%         {This package does not load \pkg{xcolor} anymore. 
%         It relies on \pkg{tikz}, that \pkg{tikz} loads \pkg{xcolor} 
%         in a way that \pkg{codeanatomy} can define RGB color}
%
%
% \subsection{Package Dependenies}
%
%    \begin{macrocode}
\RequirePackage{expl3}
\RequirePackage{xparse}
\RequirePackage{tikz}
%    \end{macrocode}
%
% Load necsessary \TikZ{} libraries.
%    \begin{macrocode}
\usetikzlibrary{
   tikzmark  
  ,fit
  ,arrows.meta
  ,bending
  ,shapes
  ,chains
  ,backgrounds
  ,scopes
  ,decorations
  ,decorations.pathmorphing
}
%    \end{macrocode}
%
% \subsection{Setup styles}
% ^^A+++++++++++++++++++++
% \subsubsection{Colors}
% ^^A-------------------
%  Define colors which are used in \pkg{codeanatomy}

% \DescribeMacro{annotationcolor}
% \tikz{\draw[fill=annotationcolor] circle(1ex); }
%    \begin{macrocode}
\definecolor{annotationcolor}
            {rgb}{0,0.50002,1} % Blue
%    \end{macrocode}
%
% \DescribeMacro{bgcmdcolor}
% \tikz{\draw[fill=bgcmdcolor] circle(1ex); }
%    \begin{macrocode}
\colorlet{bgcmdcolor}{gray}    % Grey
%    \end{macrocode}
%
% \subsubsection{\TikZ{} styles for code in a Code Anatomy}
% ^^A------------------------------------------------------
%
% \DescribeMacro{anatomy}
% \TikZ{} style for annotation labels: \\
% |\tikz{\node(code) [anatomy] at (0,0) {code line 1\\code line 2}; }|\\
% yields \tikz{\node(code) [anatomy] at (0,0) {code line 1\\code line 2}; }
%    \begin{macrocode}
\tikzset{anatomy/.style={%
      anchor=south west,%
      inner sep=0,%
      align=left,%
      font=\ttfamily
    }
}
%    \end{macrocode}
%
%
%
% \DescribeMacro{code part}
% \TikZ{} style to mark a piece of code in an anatomy: \\
% |\tikz{\node(code) [code part] at (0,0) {let a = 12;};}|\\
% yields  \tikz{\node(code) [code part] at (0,0) {let a = 12;};}
%    \begin{macrocode}
\tikzset{code part/.style={%
      rectangle,%
      draw=annotationcolor,%
      align=left,%
      minimum height=1.175em,%
      inner sep=1.75pt,%
      outer sep=0.1pt,%
      font=\ttfamily
    }
}
%    \end{macrocode}
%
%
% \DescribeMacro{ignored code part}
% \TikZ{} style to make a pice of code in an anatomy as not important in currently talking context
% |\tikz{\node(ignore code) [ignored code part] at (0,0) {/*some comment*/} }|
% yields \tikz{\node(ignore code) [ignored code part] at (0,0) {/*some comment*/} }
%
% \changes{v0.4-Beta}
%         {2023/01/24}
%         {Add new \TikZ{} Style \texttt{ignored code part}}%
%
%    \begin{macrocode}
\tikzset{ignored code part/.style={%
    code part,%
    draw=none,color=bgcmdcolor,%
    inner sep=0.75pt
  }
}
%    \end{macrocode}
%
%
%
%
% \DescribeMacro{fit extrem}
% \TikZ{} style to mark a piece of multiple line code in an anatomy:\\
% |\tikz{ \node(c)[fit extrem, fit={(0,0) (0.5,0.975) (1,0)}] {}; }|\\
% yields \tikz{ \node(c)[fit extrem, fit={(0,0) (0.5,0.975) (1,0)}] {}; }
%    \begin{macrocode}  
\tikzset{fit extrem/.style={%
      rectangle,%
      draw=annotationcolor,%
      align=left,%
      minimum height=1.175em,%
      inner sep=1.75pt,%
      outer sep=0.1pt,%
      font=\ttfamily
    }
}
%    \end{macrocode}
% 
%
%
%
% \DescribeMacro{annotation}
% \TikZ{} style of arrows from annotation labels to code parts:\\
% |\tikz{\draw[] (1,0) circle(3ex); \draw[->,annotation] (0,0) -- (1,0);}|\\
% yields 
% \tikz{\draw[] (1,0) circle(3ex); \draw[->,annotation] (0,0) -- (1,0);}
%
% \changes{v0.4-Alpha}
%         {2019/07/12}
%         {Set \texttt{fill} to \texttt{annotationcolor} explicit for arrow style}
%
%    \begin{macrocode}
\tikzset{annotation/.style={%
      preaction={
          draw=white,%
          line width=3.5pt,%
          arrows={-Triangle Cap[]},%
      },%
      draw=annotationcolor,%
      arrows={-Latex[%
          round,%
          color=annotationcolor,
          fill=annotationcolor
        ]
      },
      shorten >=0.25pt
    }
}
%    \end{macrocode}
%
%
% \DescribeMacro{code annotation}
% \TikZ{} style for a annotation label \tikz{\node(a)[code annotation] at (0,0) {function name};}
%    \begin{macrocode}  
\tikzset{code annotation/.style={%
      inner sep=2pt,%
      text=annotationcolor,%
      align=center,%
      font=\sffamily\small
    }
}
%    \end{macrocode}
%
%
%
% \DescribeMacro{code grid debug}
% \TikZ{} style to draw debug grid on the background of anatomy
%    \begin{macrocode}
\tikzset{code grid debug/.style={%
      step=1.0,%
      draw=gray!20,%
      very thin,%
      on background layer
    }
}
%    \end{macrocode}
%
%
% ^^A%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% 
% \subsection{Command used to set code and code anatomy}
% ^^A---------------------------------------------------
% 
%
% \DescribeMacro{\codeBlock} \marg{code}
%
% Complete code listing of a Code Anatomy figure is typeset by this command.
% Whereas \marg{code} is the \emph{formatted} code listing.
% This command can be used if there are no other packages to typeset code listing in use.
%    \begin{macrocode}
\NewDocumentCommand{\codeBlock}{m}%
    {\node(code) [anatomy] at (0,0) {#1};}%
%    \end{macrocode}
%
%
% 
% \DescribeMacro{\cPart} \oarg{style} \marg{node name}\marg{piece of code}
%
% Assign a piece of typeset code --typical in one line-- to a \TikZ{} Node, so that it can
% be annotated. 
% \begin{itemize}
%   \item \oarg{style} a defined \TikZ{} style to be applied to this node, the style |code part|
%         is applied to the node per default.
%   \item \marg{node name} is a unique \TikZ{} node name in the |tikzpicture| 
%   \item \marg{piece of code} is a single code part to be marked.    
% \end{itemize}
%
% \changes{v0.4-Beta}
%         {2023/01/24}
%         {Add option \oarg{style} to \texttt{cPart}}%
%
%    \begin{macrocode}
\NewDocumentCommand\cPart{O{code part}mm}
   {\tikzmarknode[#1]{#2}{#3}}
%    \end{macrocode} 
% 
%
%
% \DescribeMacro{\iPart} \marg{node name}\marg{piece of code}
%
% Assign a piece of typeset code --typical in one line-- to a \TikZ{} Node, so that it can
% be annotated. It does not plot border around the pice of code as |\cPart| does.
% \begin{itemize}
%   \item \oarg{style} a defined \TikZ{} style to be applied to this node, the style |ignored code part|
%         is applied to the node per default.
%   \item \marg{node name} is a unique \TikZ{} node name in the |tikzpicture| 
%   \item \marg{piece of code} is a single code part to be marked.
% \end{itemize}
%
% \changes{v0.4-Beta}
%         {2023/01/24}
%         {Add option \oarg{style} to \texttt{iPart}}
%
%    \begin{macrocode}
\NewDocumentCommand{\iPart}{O{ignored code part}mm} %
    {\tikzmarknode[#1]{#2}{#3}}
%    \end{macrocode} 
% 
%
%
%
% \DescribeMacro{\mtPoint}\marg{node name}
%
% Marks a point as a \textbf{m}ost \textbf{t}op in a Code Block.
%    \begin{macrocode}  
\NewDocumentCommand{\mtPoint}{m}
    {\tikzmarknode{#1}{\phantom{\rule[1.8ex]{0.1ex}{0.1ex}}}}
%    \end{macrocode}
%      
%
%
% \DescribeMacro{\hmtPoint}\marg{node name}
%
% Marks a point as a \textbf{h}eigher \textbf{m}ost \textbf{t}op point in a Code Block.  
%    \begin{macrocode}    
\NewDocumentCommand{\hmtPoint}{m}
    {\tikzmarknode{#1}{\phantom{\rule[2.5ex]{0.1ex}{0.1ex}}}}    
%    \end{macrocode}
%      
%
%
% \DescribeMacro{\mbPoint}\marg{node name}
%
% Marks a point as a \textbf{d}eeper \textbf{m}ost \textbf{b}ottom point in a Code Block.      
%    \begin{macrocode}    
\NewDocumentCommand{\mbPoint}{m}
    {\tikzmarknode{#1}{\phantom{\rule[-0.55ex]{0.1ex}{0.1ex}}}}
%    \end{macrocode}
%      
%
%
% \DescribeMacro{\dmbPoint}\marg{node name}
%
% Marks a point as a \textbf{d}eeper \textbf{m}ost \textbf{b}ottom point in a Code Block.    
%    \begin{macrocode}      
\NewDocumentCommand{\dmbPoint}{m}
    {\tikzmarknode{#1}{\phantom{\rule[-2ex]{0.1ex}{0.1ex}}}}    
%    \end{macrocode}
%  
%
% \DescribeMacro{\extremPoint}\marg{node name}\oarg{yshift}\oarg{xshift}\oarg{style}
%
% Create a \TikZ{} Node as reference point for later use in |\fitExtrem|. 
%
% \begin{itemize}
%      \item \marg{node name} is the \TikZ{} node name which is used in |\fitExtrem| to reference to this point 
%      \item \oarg{yshift} a length, default 0ex which places this markpoint on the base line, 
%                          shift this mark point vertical, for positive value over base line, negative value under
%                          base line.
%      \item \oarg{xshift} same as \oarg{yshift} but for horizontal direction.
%      \item \oarg{style} is a \TikZ{} style (may be defined by user).
%  \end{itemize}
%
%  For example:
%
% |\begin{tikzpicture}[remember picture]|\\
% |\node(code) [anatomy] at (0,0) {|\\
% |   \extremPoint{tl}[2ex]Line with some text\extremPoint{br}[-1ex]\\|\\
% |   \extremPoint{tl2}other Line with some text\\|\\
% |   some more line\extremPoint{br2}\\|\\
% |};|\\
% |\fitExtrem{box1}{(tl) (br)}|\\
% |\fitExtrem{box2}{(tl2) (br2)}|\\
% |\end{tikzpicture}|\\
%
% yields\\
%
% \begin{tikzpicture}[remember picture]
% \node(code) [anatomy] at (0,0) {
%    \extremPoint{tl}[2ex]Line with some text\extremPoint{br}[-1ex]\\
%    \extremPoint{tl2}other Line with some text\\
%    some more line\extremPoint{br2}\\
% };
% \fitExtrem{box1}{(tl) (br)}
% \fitExtrem{box2}{(tl2) (br2)}
% \end{tikzpicture}
%    \begin{macrocode}
\NewDocumentCommand{\extremPoint}{m O{0ex} O{0.1ex} O{} }
    {\tikzmarknode[#4]{#1}{\phantom{\rule[#2]{#3}{0.1ex}}}}
%    \end{macrocode}
%  
%
%
%
% \DescribeMacro{\fitExtrem} \marg{node name}\marg{extrem points}
%
% Create a rectangle box over given extrem points defined by |\*Point{}|.
%
% \begin{itemize}
%   \item \marg{node name} is a unique \TikZ{} node name in the current anatomy 
%   \item \marg{extrem points} is a list of \TikZ{} node name created by |\*Point|, 
%      each name is surrounded by |()|.
% \end{itemize}
% 
% Example:
%
% |\begin{tikzpicture}[remember picture]|\\
% |\node(code) [anatomy] at (0,0) {|\\
% |\mtPoint{left}Line 1\\|\\
% |Long Line 2\extremPoint{right}\\|\\
% |Line 3\mbPoint{bottom}|\\
% |};|\\
% |\fitExtrem{box} { (left) (bottom) (right) }|\\
% |\end{tikzpicture}|\\
%
% yields\\
%
% \begin{tikzpicture}[remember picture]
% \node(code) [anatomy] at (0,0) {
% \mtPoint{left}Line 1\\
% Long Line 2\extremPoint{right}\\
% Line 3\mbPoint{bottom}
% };
% \fitExtrem{box} { (left) (bottom) (right) }
% \end{tikzpicture}
%    \begin{macrocode}
\NewDocumentCommand{\fitExtrem}{mm}
    {\node(#1)[fit extrem,fit={#2}]{};}
%    \end{macrocode}
%
%
% \DescribeMacro{\bgcode} \marg{piece of code}
%
% Typeset a piece of code in color |bgcmdcolor|. 
% For example 
%
% |\tikz{\codeBlock{let a := 12\bgcode{;}}}|
%
% yields \tikz{\codeBlock{let a := 12\bgcode{;}}} 
%    \begin{macrocode}
\NewDocumentCommand{\bgcode}{m}{\textcolor{bgcmdcolor}{#1}}
%    \end{macrocode}
%
% 
% \DescribeMacro{\ptab}
% \DescribeMacro{\phspace} 
% Produce a horizontal space of 4 small characters |h| respective 1 small character |h|\\
% for example: |\tikz{\codeBlock{a\ptab{}b}}| yields \tikz{\codeBlock{a\ptab{}b}}
%    \begin{macrocode}
\NewDocumentCommand{\ptab}{}{\phantom{hhhh}}
\NewDocumentCommand{\phspace}{}{\phantom{h}}
%    \end{macrocode}
%
%
%
% \DescribeMacro{\codeAnnotation}\marg{node name}\parg{coordinate}\marg{label text}
%
% Typeset Annotation labels for a code part. 
% \begin{itemize}
%  \item \marg{node name} is a unique \TikZ{} node name in the |tikzpicture|,
%  \item \parg{coordinate} is the coordinate of the annotation label, surrounded by a |()|,
%  \item \marg{label text} text content to be typeset.
% \end{itemize}
%
% For example:
%
% |\begin{tikzpicture}[remember picture]|\\
% |\codeBlock{a \cPart{a}{:=} 12 + 13}|\\
% |\codeAnnotation{codeLabel} (1,-0.5) {assignment}|\\
% |\draw[->,annotation] (codeLabel) -- (a);|\\
% |\end{tikzpicture}|\\
%
% yields\\ \medskip
%
% \begin{tikzpicture}[remember picture]
% \codeBlock{a \cPart{a}{:=} 12 + 13}
% \codeAnnotation{codeLabel} (1,-0.5) {assignment}
% \draw[->,annotation] (codeLabel) -- (a);
% \end{tikzpicture}
%    \begin{macrocode}
\NewDocumentCommand{\codeAnnotation}{m r() m } %
    { \node(#1)[code annotation] at (#2) {#3}; }
%    \end{macrocode}
%
%
%
%
% ^^A%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% \section{Known Bugs}
%
% \paragraph*{\fixedBug{Arrows color}}
% Arrows appear in some cases with mysterious color. I don't know why! 
% For example:
%
% |\begin{tikzpicture}[remember picture]|\\
% |\node(code) [anatomy] at (0,0) {|\\
% |\hmtPoint{a}Short line\\|\\
% |code with some long text\extremPoint{b}[-0.5ex]|\\
% |};|\\
% |\fitExtrem{l}{(a) (b)}|\\
% |\codeAnnotation{n} (-2,0){here is|\\
% |                            a\extremPoint{point}[0.75ex][0.5ex]|\\
% |                          long line}|\\
% |\draw[->, annotation] (point) -- (l);|\\
% |\end{tikzpicture}|
%
% yields
%
% \begin{tikzpicture}[remember picture]
% \node(code) [anatomy] at (0,0) {
% \hmtPoint{a}Short line\\
% code with some long text\extremPoint{b}[-0.5ex]
% };
% \fitExtrem{l}{(a) (b)}
% \codeAnnotation{n} (-2,0){here is\\
%                             a\extremPoint{point}[0.75ex][0.5ex]\\
%                          long line}
% \draw[->, annotation] (point) -- (l);
% \end{tikzpicture}
%    
%
% ^^A%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% \PrintIndex
% \PrintChanges
%
\endinput