0% found this document useful (0 votes)
6 views

Codeanatomy

Uploaded by

Habib Hosseini
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Codeanatomy

Uploaded by

Habib Hosseini
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

codeanatomy – Draw Code Anatomy∗

Reference

Hồng-Phúc Bùi†

Released 2019/07/12

Contents
1 Hints 1

2 Implementation 1
2.1 Package Dependenies . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
2.2 Setup styles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
2.2.1 Colors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
2.2.2 TikZ styles for code in a Code Anatomy . . . . . . . . . . . . . . . 2
2.3 Command used to set code and code anatomy . . . . . . . . . . . . . . 4

3 Known Bugs 6

Index 7

Change History 7

1 Hints
Usage of this Package can be found in codeanatomy.usage.pdf and codeanatomy.lstlisting.pdf.
This document show only generated reference of commands in this Package.

2 Implementation
2.1 Package Dependenies
1 \RequirePackage{expl3}
2 \RequirePackage{xparse}
3 \RequirePackage{tikz}
Load necsessary TikZ libraries.
4 \usetikzlibrary{
∗ This file describes v0.4-Alpha, last revised 2019/07/12.
† E-mail: hong-phuc.bui (at) htwsaar dot de

1
5 tikzmark
6 ,fit
7 ,arrows.meta
8 ,bending
9 ,shapes
10 ,chains
11 ,backgrounds
12 ,scopes
13 ,decorations
14 ,decorations.pathmorphing
15 }
2.2 Setup styles
2.2.1 Colors
Define colors which are used in codeanatomy
annotationcolor
16 \definecolor{annotationcolor}
17 {rgb}{0,0.50002,1} % Blue
bgcmdcolor
18 \colorlet{bgcmdcolor}{gray} % Grey

2.2.2 TikZ styles for code in a Code Anatomy


anatomy TikZ style for annotation labels:
\tikz{\node(code) [anatomy] at (0,0) {code line 1\\code line 2}; }
code line 1
yields code line 2
19 \tikzset{anatomy/.style={%
20 anchor=south west,%
21 inner sep=0,%
22 align=left,%
23 font=\ttfamily
24 }
25 }
code part TikZ style to marce a piece of code in an anatomy:
\tikz{\node(code) [code part] at (0,0) {let a = 12;};}
yields let a = 12;
26 \tikzset{code part/.style={%
27 rectangle,%
28 draw=annotationcolor,%
29 align=left,%
30 minimum height=1.175em,%
31 inner sep=1.75pt,%
32 outer sep=0.1pt,%
33 font=\ttfamily
34 }
35 }
fit extrem TikZ style to mark a piece of multiple line code in an anatomy:

2
\tikz{ \node(c)[fit extrem, fit={(0,0) (0.5,0.975) (1,0)}] {}; }

yields
36 \tikzset{fit extrem/.style={%
37 rectangle,%
38 draw=annotationcolor,%
39 align=left,%
40 minimum height=1.175em,%
41 inner sep=1.75pt,%
42 outer sep=0.1pt,%
43 font=\ttfamily
44 }
45 }
annotation TikZ style of arrows from annotation labels to code parts:
\tikz{\draw[] (1,0) circle(3ex); \draw[->,annotation] (0,0) -- (1,0);}

yields
46 \tikzset{annotation/.style={%
47 preaction={
48 draw=white,%
49 line width=3.5pt,%
50 arrows={-Triangle Cap[]},%
51 },%
52 draw=annotationcolor,%
53 arrows={-Latex[%
54 round,%
55 color=annotationcolor,
56 fill=annotationcolor
57 ]
58 },
59 shorten >=0.25pt
60 }
61 }
code annotation TikZ style for a annotation label function name
62 \tikzset{code annotation/.style={%
63 inner sep=2pt,%
64 text=annotationcolor,%
65 align=center,%
66 font=\sffamily\small
67 }
68 }
code grid debug TikZ style to draw debug grid on the background of anatomy
69 \tikzset{code grid debug/.style={%
70 step=1.0,%
71 draw=gray!20,%
72 very thin,%
73 on background layer
74 }
75 }

3
2.3 Command used to set code and code anatomy
\codeBlock {hcodei}
Complete code listing of a Code Anatomy figure is typeset by this command. Whereas
{hcodei} is the formatted code listing. This command can be used if there are no other
packages to typeset code listing in use.
76 \NewDocumentCommand{\codeBlock}{m}%
77 {\node(code) [anatomy] at (0,0) {#1};}
\cPart {hnode namei}{hpiece of codei}
Assign a piece of typeset code –typical in one line– to a TikZ Node, so that it can be
annotated.
• {hnode namei} is a unique TikZ node name in the tikzpicture
• {hpiece of codei} is a single code part to be marked.
78 \NewDocumentCommand{\cPart}{mm} %
79 {\tikzmarknode[code part]{#1}{#2}}
\iPart {hnode namei}{hpiece of codei}
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.
• {hnode namei} is a unique TikZ node name in the tikzpicture
• {hpiece of codei} is a single code part to be marked.
80 \NewDocumentCommand{\iPart}{mm} %
81 {\tikzmarknode[code part,draw=none,inner sep=0.75pt]{#1}{#2}}
\mtPoint {hnode namei}
Marks a point as a most top in a Code Block.
82 \NewDocumentCommand{\mtPoint}{m}
83 {\tikzmarknode{#1}{\phantom{\rule[1.8ex]{0.1ex}{0.1ex}}}}
\hmtPoint {hnode namei}
Marks a point as a heigher most top point in a Code Block.
84 \NewDocumentCommand{\hmtPoint}{m}
85 {\tikzmarknode{#1}{\phantom{\rule[2.5ex]{0.1ex}{0.1ex}}}}
\mbPoint {hnode namei}
Marks a point as a deeper most bottom point in a Code Block.
86 \NewDocumentCommand{\mbPoint}{m}
87 {\tikzmarknode{#1}{\phantom{\rule[-0.55ex]{0.1ex}{0.1ex}}}}
\dmbPoint {hnode namei}
Marks a point as a deeper most bottom point in a Code Block.
88 \NewDocumentCommand{\dmbPoint}{m}
89 {\tikzmarknode{#1}{\phantom{\rule[-2ex]{0.1ex}{0.1ex}}}}
\extremPoint {hnode namei}[hyshifti][hxshifti][hstylei]
Create a TikZ Node as reference point for later use in \fitExtrem.

• {hnode namei} is the TikZ node name which is used in \fitExtrem to reference to
this point

4
• [hyshifti] 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.
• [hxshifti] same as [hyshifti] but for horizontal direction.
• [hstylei] is a TikZ style (may be defined by user).
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

Line with some text


other Line with some text
some more line

90 \NewDocumentCommand{\extremPoint}{m O{0ex} O{0.1ex} O{} }


91 {\tikzmarknode[#4]{#1}{\phantom{\rule[#2]{#3}{0.1ex}}}}
\fitExtrem {hnode namei}{hextrem pointsi}
Create a rectangle box over given extrem points defined by \*Point{}.
• {hnode namei} is a unique TikZ node name in the current anatomy
• {hextrem pointsi} is a list of TikZ node name created by \*Point, each name is
surrounded by ().
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

Line 1
Long Line 2
Line 3
92 \NewDocumentCommand{\fitExtrem}{mm}
93 {\node(#1)[fit extrem,fit={#2}]{};}

5
\bgcode {hpiece of codei}
Typeset a piece of code in color bgcmdcolor. For example
\tikz{\codeBlock{let a := 12\bgcode{;}}}
yields let a := 12;
94 \NewDocumentCommand{\bgcode}{m}{\textcolor{bgcmdcolor}{#1}}
\ptab Produce a horizontal space of 4 small characters h respective 1 small character h
\phspace for example: \tikz{\codeBlock{a\ptab{}b}} yields a b
95 \NewDocumentCommand{\ptab}{}{\phantom{hhhh}}
96 \NewDocumentCommand{\phspace}{}{\phantom{h}}
\codeAnnotation {hnode namei}(hcoordinatei){hlabel texti}
Typeset Annotation labels for a code part.
• {hnode namei} is a unique TikZ node name in the tikzpicture,

• (hcoordinatei) is the coordinate of the annotation label, surrounded by a (),


• {hlabel texti} text content to be typeset.
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

a := 12 + 13

assignment
97 \NewDocumentCommand{\codeAnnotation}{m r() m } %
98 { \node(#1)[code annotation] at (#2) {#3}; }

3 Known Bugs
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

6
Short line
here is
a
code with some long text
long line

Index
The italic numbers denote the pages where the corresponding entry is described, numbers
underlined point to the definition, all others indicate the places where it is used.

A \extremPoint . . . . . . . . . . . . . . . . . . . . 90
\anatomy . . . . . . . . . . . . . . . . . . . . . . . . . 2
\annotation . . . . . . . . . . . . . . . . . . . . . . 3 F
\annotationcolor . . . . . . . . . . . . . . . . . . 2 \fit extrem . . . . . . . . . . . . . . . . . . . . . . 2
\fitExtrem . . . . . . . . . . . . . . . . . . . . . . . 5
B \fitExtrem . . . . . . . . . . . . . . . . . . . . . . 92
\bgcmdcolor . . . . . . . . . . . . . . . . . . . . . . 2
\bgcode . . . . . . . . . . . . . . . . . . . . . . . . . . 6 H
\bgcode . . . . . . . . . . . . . . . . . . . . . . . . . 94 \hmtPoint . . . . . . . . . . . . . . . . . . . . . . . . 4
\hmtPoint . . . . . . . . . . . . . . . . . . . . . . . 84
C
\code annotation . ... . . . . . . . . . . . . . . 3
I
\code grid debug . ... . . . . . . . . . . . . . . 3
\iPart . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
\code part . . . . . . ... . . . . . . . . . . . . . . 2
\iPart . . . . . . . . . . . . . . . . . . . . . . . . . . 80
\codeAnnotation . . ... . . . . . . . . . . . . . . 6
\codeAnnotation . . ... . . . . . . . . . . . . . 97
M
\codeBlock . . . . . . ... . . . . . . . . . . . . . . 4
\mbPoint . . . . . . . . ... . . . . . . . . . . . . . . 4
\codeBlock . . . . . . ... . . . . . . . . . . . . . 76
\mbPoint . . . . . . . . ... . . . . . . . . . . . . . 86
\cPart . . . . . . . . . . ... . . . . . . . . . . . . . . 4
\mtPoint . . . . . . . . ... . . . . . . . . . . . . . . 4
\cPart . . . . . . . . . . ... . . . . . . . . . . . . . 78
\mtPoint . . . . . . . . ... . . . . . . . . . . . . . 82
D
\dmbPoint . . . . . . . . . . . . . . . . . . . . . . . . 4 P
\dmbPoint . . . . . . . . . . . . . . . . . . . . . . . 88 \phspace . . . . . . . . ... . . . . . . . . . . . . . . 6
\phspace . . . . . . . . ... . . . . . . . . . . . . . 96
E \ptab . . . . . . . . . . . ... . . . . . . . . . . . . . . 6
\extremPoint ..................... 4 \ptab . . . . . . . . . . . ... . . . . . . . . . . . . . 95

Change History
v0.2-Alpha codeanatomy can define RGB color 1
General: This package does not load v0.4-Alpha
xcolor anymore. It relies on tikz, General: Set fill to annotationcolor
that tikz loads xcolor in a way that explicit for arrow style . . . . . . . . . 3

You might also like