anch-box.mkxl /size: 8779 b    last modification: 2024-01-16 10:22
1%D \module
2%D   [       file=anch-box,
3%D        version=2022.03.25,
4%D          title=\CONTEXT\ Anchoring Macros,
5%D       subtitle=Boxes,
6%D         author=Hans Hagen,
7%D           date=\currentdate,
8%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
9%C
10%C This module is part of the \CONTEXT\ macro||package and is
11%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
12%C details.
13
14\writestatus{loading}{ConTeXt Anchoring Macros / Boxes}
15
16\unprotect
17
18\startMPpositiongraphic{mypos:boxanchor:arrow}{}% from,to,linecolor,text,alternative,leftedge,rightedge,distance}
19    anch_box_arrows_draw ;
20\stopMPpositiongraphic
21
22\def\anch_box_connect_parameters#1#2%
23  {span=no,
24   from=\boxanchorposone,%
25   to=\boxanchorpostwo,%
26   distance={\namedboxanchorcontentparameter{#1}\c!distance},%
27   dashtype={\namedboxanchorcontentparameter{#1}\c!dash},%
28   arrow={\namedboxanchorcontentparameter{#1}\c!arrow},%
29   alternative={\namedboxanchorcontentparameter{#1}\c!alternative},%
30   text={\namedboxanchorcontentparameter{#1}\c!text},%
31   rulethickness={\namedboxanchorcontentparameter{#1}\c!rulethickness},%
32   linecolor=\namedboxanchorcontentparameter{#1}{\c!rulecolor},
33   #2}
34
35\mutable\lettonothing\boxanchorone
36\mutable\lettonothing\boxanchortwo
37\mutable\lettonothing\boxanchorposone
38\mutable\lettonothing\boxanchorpostwo
39
40\tolerant\permanent\protected\def\connectboxanchors[#1]#*[#2]#*[#S#3]#*[#S#4]#*#:#5#6%
41  {\begingroup
42   %
43   \edef\boxanchorone{\boxanchoringclass:#5}%
44   \edef\boxanchortwo{\boxanchoringclass:#6}%
45%    \edef\boxanchorone{#5}%
46%    \edef\boxanchortwo{#6}%
47   %
48   \doifnotanchorbox{\boxanchorone}{\defineboxanchor[\boxanchorone]}%
49   \doifnotanchorbox{\boxanchortwo}{\defineboxanchor[\boxanchortwo]}%
50   %
51   \edef\boxanchorposone{#1:#2:\namespacedboxanchor{\boxanchorone}}%
52   \edef\boxanchorpostwo{#1:#2:\namespacedboxanchor{\boxanchortwo}}%
53% \edef\boxanchorposone{#1:#2:\number\namedboxanchor{\boxanchorone}}%
54% \edef\boxanchorpostwo{#1:#2:\number\namedboxanchor{\boxanchortwo}}%
55   %
56   \setboxanchor[\boxanchorone][#1][#3]\hpack{\xypos{\boxanchorposone}}%
57   \setboxanchor[\boxanchortwo][#2][#4]\hpack{\xypos{\boxanchorpostwo}}%
58   %
59   \ifparameter#3\or\setupboxanchorcontent[#1][#3]\fi
60   \ifparameter#4\or\setupboxanchorcontent[#2][#4]\fi % used ?
61   %
62   \normalexpanded{%
63     \startpositionoverlay{text+1}% will become configurable region
64       \setMPpositiongraphic
65         {\boxanchorposone}%
66         {\namedboxanchorcontentparameter{#1}\c!mp}%
67         {\anch_box_connect_parameters{#1}{#3}}%
68    \stoppositionoverlay
69   }%
70   \ifcstok{\namedboxanchorcontentparameter{#1}\c!page}\v!yes
71     \normalexpanded{%
72       \startpositionoverlay{text+1}% will become configurable region
73           \setMPpositiongraphic
74             {\boxanchorpostwo}%
75             {\namedboxanchorcontentparameter{#1}\c!mp}%
76             {\anch_box_connect_parameters{#1}{#3}}%
77       \stoppositionoverlay
78     }%
79   \fi
80   \endgroup}
81
82% dedicated or not
83
84\tolerant\permanent\protected\def\connectmatrixanchors[#1]#*[#2]#*[#S#3]#*[#S#4]#*#:#5#6%
85  {\begingroup
86   \doifelseinset{#5}{\m_anch_matrix_list}\donetrue\donefalse
87   \ifdone\else
88     \defineboxanchor[#5]%
89     \setboxanchor[#5][#1][#3]\hpack{\xypos{\namespacedboxanchor{#5}}}%
90     \xdef\m_anch_matrix_list{\m_anch_matrix_list,#5}%
91   \fi
92   \ifparameter#3\or\setupboxanchorcontent[#1][#3]\fi
93   \doifelseinset{#6}{\m_anch_matrix_list}\donetrue\donefalse
94   \ifdone\else
95     \defineboxanchor[#6]%
96     \setboxanchor[#6][#2][#4]\hpack{\xypos{\namespacedboxanchor{#6}}}%
97     \xdef\m_anch_matrix_list{\m_anch_matrix_list,#6}%
98   \fi
99   \ifparameter#4\or\setupboxanchorcontent[#1][#4]\fi
100   \startpositionoverlay{text-1}% will become configurable region
101     \setMPpositiongraphic
102       {\namespacedboxanchor{#5}}%
103       {\namedboxanchorcontentparameter{#1}\c!mp}%
104       {from=\namespacedboxanchor{#5},%
105        to=\namespacedboxanchor{#6},%
106        distance={\namedboxanchorcontentparameter{#1}\c!distance},%
107        dashtype={\namedboxanchorcontentparameter{#1}\c!dash},%
108        arrow={\namedboxanchorcontentparameter{#1}\c!arrow},%
109        alternative={\namedboxanchorcontentparameter{#1}\c!alternative},%
110        text={\namedboxanchorcontentparameter{#1}\c!text},%
111        rulethickness={\namedboxanchorcontentparameter{#1}\c!rulethickness},%
112        linecolor=\namedboxanchorcontentparameter{#1}{\c!rulecolor},%
113        % matrix specific:
114        leftedge=\namespacedboxanchor\c!leftedge,%
115        rightedge=\namespacedboxanchor\c!rightedge}%
116   \stoppositionoverlay
117   \endgroup}
118
119\lettonothing\m_anch_matrix_list
120
121\tolerant\permanent\protected\def\startmatrixanchors
122  {\glet\m_anch_matrix_list\empty
123   \defineboxanchor[\v!leftedge]%
124   \setboxanchor[\v!leftedge][\v!leftedge]\hpack{\xypos{\namespacedboxanchor\v!leftedge}}%
125   \defineboxanchor[\v!rightedge]%
126   \setboxanchor[\v!rightedge][\v!rightedge]\hpack{\xypos{\namespacedboxanchor\v!rightedge}}%
127   \setupmathfence[\c!leftsource=\v!leftedge,\c!rightsource=\v!rightedge]}
128
129\tolerant\permanent\protected\def\stopmatrixanchors
130  {\setupmathfence[\c!leftsource=,\c!rightsource=]%
131   \glet\m_anch_matrix_list\empty}
132
133\defineboxanchorcontent
134  [arrow]
135  [\c!mp=mypos:boxanchor:arrow,
136   \c!distance=1.5\exheight,
137   \c!rulethickness=\linewidth,
138   \c!rulecolor=textcolor]
139
140\defineboxanchorcontent
141  [\v!top]
142  [arrow]
143  [\c!corner=\v!height,
144   \c!location=\v!depth,
145   \c!yoffset=.25\exheight,
146   \c!alternative=\v!top]
147
148\defineboxanchorcontent
149  [\v!bottom]
150  [arrow]
151  [\c!corner=\v!depth,
152   \c!location=\v!height,
153   \c!yoffset=-\namedboxanchorcontentparameter{top}\c!yoffset,
154   \c!alternative=\v!bottom]
155
156\defineboxanchorcontent
157  [\v!left]
158  [arrow]
159  [\c!corner={\v!middle,\v!left},
160   \c!xoffset=-1.25\emwidth,
161   \c!alternative=\v!left]
162
163\defineboxanchorcontent
164  [\v!right]
165  [arrow]
166  [\c!corner={\v!middle,\v!right},
167   \c!xoffset=-\namedboxanchorcontentparameter{left}\c!xoffset,
168   \c!alternative=\v!right]
169
170\defineboxanchorcontent
171  [\v!leftedge]
172  [\v!left]
173  [\c!xoffset=-.25\exheight]
174
175\defineboxanchorcontent
176  [\v!rightedge]
177  [\v!right]
178  [\c!xoffset=0.25\exheight] % we are centered
179
180\defineboxanchorcontent[\v!middle:\v!bottom][\v!bottom][\c!alternative=\v!middle,\c!yoffset=-.5\exheight]
181\defineboxanchorcontent[\v!middle:\v!top   ][\v!top   ][\c!alternative=\v!middle,\c!yoffset=.5\exheight]
182\defineboxanchorcontent[\v!middle:\v!left  ][\v!left  ][\c!alternative=\v!middle,\c!xoffset=-.5\exheight]
183\defineboxanchorcontent[\v!middle:\v!right ][\v!right ][\c!alternative=\v!middle,\c!xoffset=.5\exheight]
184
185\defineboxanchorcontent
186  [\v!left:\v!bottom]
187  [\v!bottom]
188  [\c!alternative=\v!middle,
189   \c!corner={\v!left,\v!depth},
190   \c!xoffset=-.25\exheight,
191   \c!yoffset=-.25\exheight]
192
193\defineboxanchorcontent
194  [\v!right:\v!bottom]
195  [\v!bottom]
196  [\c!alternative=\v!middle,
197   \c!corner={\v!right,\v!depth},
198   \c!xoffset=.25\exheight,
199   \c!yoffset=-.25\exheight]
200
201\defineboxanchorcontent
202  [\v!left:\v!top]
203  [\v!top]
204  [\c!alternative=\v!middle,
205   \c!corner={\v!left,\v!height},
206   \c!xoffset=-.25\exheight,
207   \c!yoffset=.25\exheight]
208
209\defineboxanchorcontent
210  [\v!right:\v!top]
211  [\v!top]
212  [\c!alternative=\v!middle,
213   \c!corner={\v!right,\v!height},
214   \c!xoffset=.25\exheight,
215   \c!yoffset=.25\exheight]
216
217% \setupboxanchorcontent  [top]   [rulecolor=darkyellow]
218% \setupboxanchorcontent  [left]  [rulecolor=darkred]
219% \setupboxanchorcontent  [bottom][rulecolor=darkblue]
220% \setupboxanchorcontent  [right] [rulecolor=darkgreen]
221%
222% \startbuffer
223% \connectboxanchors[top]   [top]   [text={\small\small\strut\bf var 1}]{a1}{a2}
224% \connectboxanchors[top]   [top]   [text={\small\small\strut\bf var 2}]{b1}{b2}
225% \connectboxanchors[top]   [top]   [text={\small\small\strut\bf var 3}]{a1}{b1}
226% \connectboxanchors[bottom][bottom][text={\small\small\strut\bf var 4}]{b1}{b2}
227% \connectboxanchors[bottom][bottom][text={\small\small\strut\bf var 6}]{a1}{a2}
228% \connectboxanchors[bottom][bottom][rulecolor=darkgreen,distance=4ex,text={\small\small\strut\bf var 7}]{c1}{d1}
229%
230% % \start
231%
232% $\showboxes
233% % \mathboxanchored{a1}{a}^{2^x} +
234% % \mathboxanchored[nucleus]{a1}{a}^{2^x} +
235% % \mathboxanchored{a1}{g}^2 +
236% % \mathboxanchored{a1}{a} +
237% a^{\mathboxanchored{a1}{2}} +
238% % a^{\mathboxanchored{a1}{2}}_3 +
239% \mathboxanchored{b1}{b}
240% =
241% \mathboxanchored{c1}{c}^2 +
242% \mathboxanchored[nucleus]{d1}{d}^2
243% =
244% b_{\mathboxanchored{b2}{3}} +
245% \mathboxanchored{a2}{a}
246% $
247% \stopbuffer
248%
249% \starttext
250%   \startTEXpage[offset=10mm]\startboxanchoring[a]\getbuffer\stopboxanchoring\stopTEXpage
251%   \startTEXpage[offset=11mm]\startboxanchoring[b]\getbuffer\stopboxanchoring\stopTEXpage
252%   \startTEXpage[offset=12mm]\startboxanchoring[+]\getbuffer\stopboxanchoring\stopTEXpage
253% \stoptext
254
255\protect
256