anch-box.mkxl /size: 8581 b    last modification: 2025-02-21 11:03
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   %
46   \doifnotanchorbox{\boxanchorone}{\defineboxanchor[\boxanchorone]}%
47   \doifnotanchorbox{\boxanchortwo}{\defineboxanchor[\boxanchortwo]}%
48   %
49   \edef\boxanchorposone{#1:#2:\namespacedboxanchor{\boxanchorone}}%
50   \edef\boxanchorpostwo{#1:#2:\namespacedboxanchor{\boxanchortwo}}%
51   %
52   \setboxanchor[\boxanchorone][#1][#3]\hpack{\xypos{\boxanchorposone}}%
53   \setboxanchor[\boxanchortwo][#2][#4]\hpack{\xypos{\boxanchorpostwo}}%
54   %
55   \ifparameter#3\or\setupboxanchorcontent[#1][#3]\fi
56   \ifparameter#4\or\setupboxanchorcontent[#2][#4]\fi % used ?
57   %
58   \normalexpanded{%
59     \startpositionoverlay{text+1}% will become configurable region
60       \setMPpositiongraphic
61         {\boxanchorposone}%
62         {\namedboxanchorcontentparameter{#1}\c!mp}%
63         {\anch_box_connect_parameters{#1}{#3}}%
64    \stoppositionoverlay
65   }%
66   \ifcstok{\namedboxanchorcontentparameter{#1}\c!page}\v!yes
67     \normalexpanded{%
68       \startpositionoverlay{text+1}% will become configurable region
69           \setMPpositiongraphic
70             {\boxanchorpostwo}%
71             {\namedboxanchorcontentparameter{#1}\c!mp}%
72             {\anch_box_connect_parameters{#1}{#3}}%
73       \stoppositionoverlay
74     }%
75   \fi
76   \endgroup}
77
78% dedicated or not
79
80\tolerant\permanent\protected\def\connectmatrixanchors[#1]#*[#2]#*[#S#3]#*[#S#4]#*#:#5#6%
81  {\begingroup
82   \doifelseinset{#5}{\m_anch_matrix_list}\donetrue\donefalse
83   \ifdone\else
84     \defineboxanchor[#5]%
85     \setboxanchor[#5][#1][#3]\hpack{\xypos{\namespacedboxanchor{#5}}}%
86     \xdef\m_anch_matrix_list{\m_anch_matrix_list,#5}%
87   \fi
88   \ifparameter#3\or\setupboxanchorcontent[#1][#3]\fi
89   \doifelseinset{#6}{\m_anch_matrix_list}\donetrue\donefalse
90   \ifdone\else
91     \defineboxanchor[#6]%
92     \setboxanchor[#6][#2][#4]\hpack{\xypos{\namespacedboxanchor{#6}}}%
93     \xdef\m_anch_matrix_list{\m_anch_matrix_list,#6}%
94   \fi
95   \ifparameter#4\or\setupboxanchorcontent[#1][#4]\fi
96   \startpositionoverlay{text-1}% will become configurable region
97     \setMPpositiongraphic
98       {\namespacedboxanchor{#5}}%
99       {\namedboxanchorcontentparameter{#1}\c!mp}%
100       {from=\namespacedboxanchor{#5},%
101        to=\namespacedboxanchor{#6},%
102        distance={\namedboxanchorcontentparameter{#1}\c!distance},%
103        dashtype={\namedboxanchorcontentparameter{#1}\c!dash},%
104        arrow={\namedboxanchorcontentparameter{#1}\c!arrow},%
105        alternative={\namedboxanchorcontentparameter{#1}\c!alternative},%
106        text={\namedboxanchorcontentparameter{#1}\c!text},%
107        rulethickness={\namedboxanchorcontentparameter{#1}\c!rulethickness},%
108        linecolor=\namedboxanchorcontentparameter{#1}{\c!rulecolor},%
109        % matrix specific:
110        leftedge=\namespacedboxanchor\c!leftedge,%
111        rightedge=\namespacedboxanchor\c!rightedge}%
112   \stoppositionoverlay
113   \endgroup}
114
115\lettonothing\m_anch_matrix_list
116
117\tolerant\permanent\protected\def\startmatrixanchors
118  {\glet\m_anch_matrix_list\empty
119   \defineboxanchor[\v!leftedge]%
120   \setboxanchor[\v!leftedge][\v!leftedge]\hpack{\xypos{\namespacedboxanchor\v!leftedge}}%
121   \defineboxanchor[\v!rightedge]%
122   \setboxanchor[\v!rightedge][\v!rightedge]\hpack{\xypos{\namespacedboxanchor\v!rightedge}}%
123   \setupmathfence[\c!leftsource=\v!leftedge,\c!rightsource=\v!rightedge]}
124
125\tolerant\permanent\protected\def\stopmatrixanchors
126  {\setupmathfence[\c!leftsource=,\c!rightsource=]%
127   \glet\m_anch_matrix_list\empty}
128
129\defineboxanchorcontent
130  [arrow]
131  [\c!mp=mypos:boxanchor:arrow,
132   \c!distance=1.5\exheight,
133   \c!rulethickness=\linewidth,
134   \c!rulecolor=textcolor]
135
136\defineboxanchorcontent
137  [\v!top]
138  [arrow]
139  [\c!corner=\v!height,
140   \c!location=\v!depth,
141   \c!yoffset=.25\exheight,
142   \c!alternative=\v!top]
143
144\defineboxanchorcontent
145  [\v!bottom]
146  [arrow]
147  [\c!corner=\v!depth,
148   \c!location=\v!height,
149   \c!yoffset=-\namedboxanchorcontentparameter{top}\c!yoffset,
150   \c!alternative=\v!bottom]
151
152\defineboxanchorcontent
153  [\v!left]
154  [arrow]
155  [\c!corner={\v!middle,\v!left},
156   \c!xoffset=-1.25\emwidth,
157   \c!alternative=\v!left]
158
159\defineboxanchorcontent
160  [\v!right]
161  [arrow]
162  [\c!corner={\v!middle,\v!right},
163   \c!xoffset=-\namedboxanchorcontentparameter{left}\c!xoffset,
164   \c!alternative=\v!right]
165
166\defineboxanchorcontent
167  [\v!leftedge]
168  [\v!left]
169  [\c!xoffset=-.25\exheight]
170
171\defineboxanchorcontent
172  [\v!rightedge]
173  [\v!right]
174  [\c!xoffset=0.25\exheight] % we are centered
175
176\defineboxanchorcontent[\v!middle:\v!bottom][\v!bottom][\c!alternative=\v!middle,\c!yoffset=-.5\exheight]
177\defineboxanchorcontent[\v!middle:\v!top   ][\v!top   ][\c!alternative=\v!middle,\c!yoffset=.5\exheight]
178\defineboxanchorcontent[\v!middle:\v!left  ][\v!left  ][\c!alternative=\v!middle,\c!xoffset=-.5\exheight]
179\defineboxanchorcontent[\v!middle:\v!right ][\v!right ][\c!alternative=\v!middle,\c!xoffset=.5\exheight]
180
181\defineboxanchorcontent
182  [\v!left:\v!bottom]
183  [\v!bottom]
184  [\c!alternative=\v!middle,
185   \c!corner={\v!left,\v!depth},
186   \c!xoffset=-.25\exheight,
187   \c!yoffset=-.25\exheight]
188
189\defineboxanchorcontent
190  [\v!right:\v!bottom]
191  [\v!bottom]
192  [\c!alternative=\v!middle,
193   \c!corner={\v!right,\v!depth},
194   \c!xoffset=.25\exheight,
195   \c!yoffset=-.25\exheight]
196
197\defineboxanchorcontent
198  [\v!left:\v!top]
199  [\v!top]
200  [\c!alternative=\v!middle,
201   \c!corner={\v!left,\v!height},
202   \c!xoffset=-.25\exheight,
203   \c!yoffset=.25\exheight]
204
205\defineboxanchorcontent
206  [\v!right:\v!top]
207  [\v!top]
208  [\c!alternative=\v!middle,
209   \c!corner={\v!right,\v!height},
210   \c!xoffset=.25\exheight,
211   \c!yoffset=.25\exheight]
212
213% \setupboxanchorcontent  [top]   [rulecolor=darkyellow]
214% \setupboxanchorcontent  [left]  [rulecolor=darkred]
215% \setupboxanchorcontent  [bottom][rulecolor=darkblue]
216% \setupboxanchorcontent  [right] [rulecolor=darkgreen]
217%
218% \startbuffer
219% \connectboxanchors[top]   [top]   [text={\small\small\strut\bf var 1}]{a1}{a2}
220% \connectboxanchors[top]   [top]   [text={\small\small\strut\bf var 2}]{b1}{b2}
221% \connectboxanchors[top]   [top]   [text={\small\small\strut\bf var 3}]{a1}{b1}
222% \connectboxanchors[bottom][bottom][text={\small\small\strut\bf var 4}]{b1}{b2}
223% \connectboxanchors[bottom][bottom][text={\small\small\strut\bf var 6}]{a1}{a2}
224% \connectboxanchors[bottom][bottom][rulecolor=darkgreen,distance=4ex,text={\small\small\strut\bf var 7}]{c1}{d1}
225%
226% % \start
227%
228% $\showboxes
229% % \mathboxanchored{a1}{a}^{2^x} +
230% % \mathboxanchored[nucleus]{a1}{a}^{2^x} +
231% % \mathboxanchored{a1}{g}^2 +
232% % \mathboxanchored{a1}{a} +
233% a^{\mathboxanchored{a1}{2}} +
234% % a^{\mathboxanchored{a1}{2}}_3 +
235% \mathboxanchored{b1}{b}
236% =
237% \mathboxanchored{c1}{c}^2 +
238% \mathboxanchored[nucleus]{d1}{d}^2
239% =
240% b_{\mathboxanchored{b2}{3}} +
241% \mathboxanchored{a2}{a}
242% $
243% \stopbuffer
244%
245% \starttext
246%   \startTEXpage[offset=10mm]\startboxanchoring[a]\getbuffer\stopboxanchoring\stopTEXpage
247%   \startTEXpage[offset=11mm]\startboxanchoring[b]\getbuffer\stopboxanchoring\stopTEXpage
248%   \startTEXpage[offset=12mm]\startboxanchoring[+]\getbuffer\stopboxanchoring\stopTEXpage
249% \stoptext
250
251\protect
252