templates-mkiv.tex /size: 8285 b    last modification: 2023-12-21 09:43
1% language=us runpath=texruns:manuals/templates
2
3% author    : Hans Hagen
4% copyright : ConTeXt Development Team
5% license   : Creative Commons Attribution ShareAlike 4.0 International
6% reference : pragma-ade.nl | contextgarden.net | texlive (related) distributions
7% origin    : the ConTeXt distribution
8%
9% comment   : Because this manual is distributed with TeX distributions it comes with a rather
10%             liberal license. We try to adapt these documents to upgrades in the (sub)systems
11%             that they describe. Using parts of the content otherwise can therefore conflict
12%             with existing functionality and we cannot be held responsible for that. Many of
13%             the manuals contain characteristic graphics and personal notes or examples that
14%             make no sense when used out-of-context.
15%
16% comment   : Some chapters might have been published in TugBoat, the NTG Maps, the ConTeXt
17%             Group journal or otherwise. Thanks to the editors for corrections. Also thanks
18%             to users for testing, feedback and corrections.
19
20\usemodule[art-01,abr-02]
21
22\definecolor[maincolor] [r=.4]
23\definecolor[extracolor][b=.4]
24
25\setupbodyfont
26  [10pt]
27
28\usesymbols
29  [cc]
30
31\setuptyping
32  [color=extracolor]
33
34\setuptype
35  [color=extracolor]
36
37\setuphead
38  [section]
39  [color=maincolor]
40
41\setupinteraction
42  [hidden]
43
44\startdocument
45  [metadata:author=Hans Hagen,
46   metadata:title=LMX Templates,
47   author=Hans Hagen,
48   affiliation=PRAGMA ADE,
49   location=Hasselt NL,
50   title=LMX Templates,
51   extra-1=LMX,
52   extra-2=TEMPLATES,
53   extra-3=HANS HAGEN,
54   support=www.contextgarden.net,
55   website=www.pragma-ade.nl]
56
57\startMPpage
58
59    StartPage;
60
61    numeric n, m ; n := 3 * 4 ; m := 4 * 4 ;
62    numeric w, h ; w := PaperWidth/n ; h := PaperHeight/m ;
63    numeric max ; max := 20 ;
64
65    for i=1 upto n :
66        for j=1 upto m :
67            fill
68                unitsquare
69                xysized (w+1/5,h+1/5) % a little overlap to get rid of viewer inaccuracies
70                shifted ((i-1)*w-1/10,(j-1)*h-1/10)
71                withcolor (.5[red,blue] randomized(.75,.75,.75))
72            ;
73        endfor ;
74    endfor ;
75
76    path p ; p := Page enlarged -5mm ;
77
78    pair a[] ; % <
79
80    a[1] := .80[lrcorner p,urcorner p] ;
81    a[2] := .50[llcorner p,ulcorner p] ;
82    a[3] := .20[lrcorner p,urcorner p] ;
83
84    pair b[] ; % \
85
86    b[1] := ulcorner p ;
87    b[2] := center   p ;
88    b[3] := lrcorner p ;
89
90    path c[] ; % from < (xml) to \ (tex)
91
92    c[1] := a[1] .. b[1] ;
93    c[2] := a[2] .. b[2] ;
94    c[3] := a[3] .. b[3] ;
95
96    linecap := butt ;
97
98    numeric fraction ;
99
100    for i=1 step 1 until max :
101        fraction := i/max ;
102        draw
103            ((point fraction along c[1]) -- (point fraction along c[2]) -- (point fraction along c[3]))
104            withpen pencircle scaled 5mm
105            withcolor .75[(max+1-i)*green/n,i*yellow/max]
106            withtransparency (1,.5)
107            ;
108    endfor ;
109
110    draw
111        textext.rt("\ssbf{\documentvariable{extra-1}}")
112        xsized (7w)
113        shifted (.8w,3h)
114        withcolor white
115    ;
116
117    draw
118        textext.rt("\ssbf{\documentvariable{extra-2}}")
119        xsized (8w)
120        shifted (w,h)
121        withcolor white
122    ;
123
124    draw
125        textext.ulft("\ssbf{\documentvariable{extra-3}}")
126        rotated 90
127        ysized (5.9h)
128        shifted (PaperWidth-1.2w,PaperHeight/2+2.95h)
129        withcolor white
130    ;
131
132    StopPage;
133
134\stopMPpage
135
136\startsubject[title={Contents}]
137
138\placelist[section][criterium=all,interaction=all]
139
140\stopsubject
141
142\startsection [title={Introduction}]
143
144{\em This manual is not finished yet. The main reason is that what is described
145here is an afternoon experiment resulting in a dozen lines of \LUA\ glue code
146that builds upon an already existing mechanism. When users like this, I will
147extend the basic \LMX\ handler to suit the \TEX\ end better. There will also be
148also support for cache based and in||document templates.)}
149
150The acronym \type {lmx} stands for document that are a mix of \LUA\ and \XML\ and
151is just the three letters \type {xml} reversed. Such documents showed up pretty
152soon in \MKIV\ while I was exploring ways to present debugging and error
153information to users using \XML. As a consequence this is one of the older
154mechanisms available, although I doubt if users start looking for it when they
155start using \CONTEXT.
156
157Anyhow, because we also use \LMX\ for populating web pages, at some point I wondered
158if using the same approach for \TEX\ files made sense. I'm still not sure about it
159but who knows where this ends up. Currently code is shared but in the future we might
160end up with a variant that adds some more flexibility.
161
162\stopsection
163
164\startsection [title={How it works}]
165
166First of all, using this mechanism involves yet another kind of \type {mk}, so
167now we have:
168
169\starttabulate[|TB||]
170\HL
171\NC mkii \NC The old version of \CONTEXT, using \PDFTEX,\XETEX, etc. \NC \NR
172\HL
173\NC mkiv \NC The new version of \CONTEXT, using \LUATEX. \NC \NR
174\NC mkvi \NC Similar to \MKIV\ but with named macro parameters. \NC \NR
175\HL
176\NC mkix \NC A \MKIV\ file mixed with \LUA\ wrapped in \XML\ processing instructions. \NC \NR
177\NC mkxi \NC Similar to \MKIX\ but with named macro parameters. \NC \NR
178\HL
179\stoptabulate
180
181The nice thing about sticking to wrapping in angle brackets is that it plays nice
182with syntax highlighting. Before we show an example of such a mix, we first point
183out that loading (and thereby conversion) happens automatically. A \type {mkix} or
184\type {mkxi} file is just a regular \CONTEXT\ file. In the test suite there
185is a demo file that can be included like this:
186
187\starttyping
188\input lmxlike-001.mkxi
189\stoptyping
190
191This file is loaded and converted on the fly. No caching takes place, but in due time
192we can use the cache built into the \LMX\ handlers if needed. The template itself
193can be fed with variables in the \type {document} namespace:
194
195\starttyping
196\starttext
197
198  \startluacode
199     document.variables.text = "set"
200  \stopluacode
201
202  \input lmxlike-001.mkxi
203
204\stoptext
205\stoptyping
206
207Instead of a special suffix, you can also force conversion with the \type {macros}
208directive:
209
210\starttyping
211% macros=mkix
212\stoptyping
213
214Part of the mentioned looks as follows:
215
216\starttyping
217\bTABLE
218  <?lua for i=1,40 do ?>
219    \bTR
220      <?lua for j=1,5 do ?>
221        \bTD
222          cell (<?lua inject(i) ?>,<?lua inject(j)?>)
223          is <?lua inject(variables.text or "unset") ?>
224        \eTD
225      <?lua end ?>
226    \eTR
227  <?lua end ?>
228\eTABLE
229\stoptyping
230
231The \type {<?lua ... ?>} command is conceptually different from (say) \type
232{\ctxlua} in the sense that the later executes some \LUA\ code at that spot,
233while in a template a \LUA\ function is constructed out of the whole that gets
234executed. In fact, we use \LUA\ to construct an input file.
235
236For the moment we only mention the predefined \type {inject} command. There are
237some more but they make more sense for \XML\ and \HTML\ and in due time this will
238be decoupled so that we can have dedicated helpers. Even the \XML\ and \HTML\
239part is somewhat in flux.
240
241The previous example can of course also be done differently. It's a matter of
242taste and usage what method gets used:
243
244\starttyping
245\startluacode
246  context.bTABLE()
247    for i=1,40 do
248      context.bTR()
249        for j=1,5 do
250          context.bTD()
251            context("cell (%s,%s) is %s",i,j,document.variables.text or "unset")
252          context.eTD()
253        end
254      context.eTR()
255    end
256  context.eTABLE()
257\stopluacode
258\stoptyping
259
260The difference between a \MKIX\ and \MKXI\ file is the same as between a \MKIV\
261and \MKVI\ file: the way macros can be defined:
262
263\starttyping
264\def\testmacro#one#two{[#one,#two]}
265
266\testmacro{1}{2}
267\testmacro{one}{two}
268\testmacro{second}{first}
269\stoptyping
270
271In practice one will seldom need macro definitions in a template file but the
272possibility is provided.
273
274\stopsection
275
276\startsubject[title={Colofon}]
277
278\starttabulate[|B|p|]
279\NC author    \NC \documentvariable{author}, \documentvariable{affiliation}, \documentvariable{location} \NC \NR
280\NC version   \NC \currentdate \NC \NR
281\NC website   \NC \documentvariable{website} \endash\ \documentvariable{support} \NC \NR
282\NC copyright \NC \symbol[cc][cc-by-sa] \NC \NR
283\stoptabulate
284
285\stopsubject
286
287\stopdocument
288