-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtikz-horner.sty
108 lines (105 loc) · 3.35 KB
/
tikz-horner.sty
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
\ProvidesPackage{tikz-horner}[2020/11/03 v1.0 Horner schema]
\RequirePackage{tikz,xstring}
\usetikzlibrary{arrows.meta}
% Just to avoid duplication in multiple
\newcounter{hornerLast}
% [#1] animation offset
% #2 list
% #3 multiplier
\newcommand*\tikzHorner[3][5]{
% bincode w/ shift of 3:
\setcounter{hornerLast}{-1}
\foreach[count=\i, evaluate=\i as \ci using {int(\i+#1)},
evaluate=\i as \xi using {1.55*\i}, remember=\i as \li (initially 0)] \b in {#2} {
% double last
\ifnum\i>1
\setcounter{hornerLast}{#3*\value{hornerLast}}
\fi
\onslide<\ci->{%
\node at (\xi,0) {\b};
\node at (\xi,-0.5) {+};
\ifnum\value{hornerLast}<0%
\node (p\i) at (\xi, -1) {};
\else
\node (p\i) at (\xi, -1) {\arabic{hornerLast}};
\fi
\node at (\xi,-1.5) {=};
% decode if hexadecimal?
\IfInteger{\b}{}{
\pgfmathsetmacro\db{int(0x\b)}
\xdef\b{\db}
}
\ifnum\i=1%
\setcounter{hornerLast}{\b}
\else
\addtocounter{hornerLast}{\b}
\fi
\node (\i) at (\xi, -2) {\arabic{hornerLast}};
\ifnum\i>1% arrow *2
\draw[-Kite] (\li) to[edge node={node[above,sloped,scale=0.75] {\({}\cdot{}\,\)#3}}] (p\i);
\fi
}
}
}
% [#1] animation offset
% #2 list
% #3 multiplier
\newcommand*\tikzHornerReverse[3][5]{
% bincode w/ shift of 3:
\setcounter{hornerLast}{0}
% First: get length
\foreach[count=\i] \ignored in {#2} {\setcounter{hornerLast}{\i}}
\xdef\TotalCount{\thehornerLast}% store value
\setcounter{hornerLast}{-1}% reset
\foreach[count=\i, evaluate=\i as \ci using {int(\TotalCount-\i+#1)},
evaluate=\i as \xi using {1.55*\i}, remember=\i as \li (initially 0)] \b in {#2} {
% double last
\ifnum\i>1
\setcounter{hornerLast}{#3*\value{hornerLast}}
\fi
\onslide<\ci->{%
\node at (\xi,0) {\b};
\node at (\xi,-0.5) {+};
\ifnum\value{hornerLast}<0%
\node (p\i) at (\xi, -1) {};
\else
\node (p\i) at (\xi, -1) {\arabic{hornerLast}};
\fi
\node at (\xi,-1.5) {=};
% decode if hexadecimal?
\IfInteger{\b}{}{
\pgfmathsetmacro\db{int(0x\b)}
\xdef\b{\db}
}
\ifnum\i=1%
\setcounter{hornerLast}{\b}
\else
\addtocounter{hornerLast}{\b}
\fi
\node (\i) at (\xi, -2) {\arabic{hornerLast}};
}
}
\foreach[count=\i, evaluate=\i as \ci using {int(\TotalCount-\i+#1+1)},
evaluate=\i as \xi using {1.55*\i}, remember=\i as \li (initially 0)] \b in {#2} {
\ifnum\i>1% arrow *2
\onslide<\ci->{%
\draw[Kite-] (\li) to[edge node={node[above,sloped,scale=0.75] {\({}\div{}\,\)#3}}] (p\i);
}
\fi
}
}
\newcommand*\TikzHorner[3][5]{%
\vfill\begin{center}
\begin{tikzpicture}
\tikzHorner[#1]{#2}{#3}
\end{tikzpicture}
\end{center}\vfill\null
}
\newcommand*\TikzHornerReverse[3][5]{%
\vfill\begin{center}
\begin{tikzpicture}
\tikzHornerReverse[#1]{#2}{#3}
\end{tikzpicture}
\end{center}\vfill\null
}
\endinput