luametafun-text.tex /size: 5436 b    last modification: 2025-02-21 11:03
1% language=us runpath=texruns:manuals/luametafun
2
3\environment luametafun-style
4
5\startcomponent luametafun-text
6
7\startchapter[title={Text}]
8
9\startsection[title={Typesetting text}]
10
11The \METAFUN\ \type {textext} command normally can do the job of typesetting a
12text snippet quite well.
13
14\startbuffer
15\startMPcode
16    fill fullcircle xyscaled (8cm,1cm) withcolor "darkred" ;
17    draw textext("\bf This is text A") withcolor "white" ;
18\stopMPcode
19\stopbuffer
20
21\typebuffer[option=TEX]
22
23We get:
24
25\startlinecorrection
26\getbuffer
27\stoplinecorrection
28
29You can use regular \CONTEXT\ commands, so this is valid:
30
31\startbuffer
32\startMPcode
33    fill fullcircle xyscaled (8cm,1cm) withcolor "darkred" ;
34    draw textext("\framed{\bf This is text A}") withcolor "white" ;
35\stopMPcode
36\stopbuffer
37
38\typebuffer[option=TEX]
39
40Of course you can as well draw a frame in \METAPOST\ but the \type {\framed}
41command has more options, like alignments.
42
43\startlinecorrection
44\getbuffer
45\stoplinecorrection
46
47Here is a variant using the \METAFUN\ interface:
48
49\startbuffer
50\startMPcode
51    fill fullcircle xyscaled (8cm,1cm) withcolor "darkred" ;
52    draw lmt_text [
53        text  = "This is text A",
54        color = "white",
55        style = "bold"
56    ] ;
57\stopMPcode
58\stopbuffer
59
60\typebuffer[option=TEX]
61
62The outcome is more or less the same:
63
64\startlinecorrection
65\getbuffer
66\stoplinecorrection
67
68Here is another example. The \type {format} option is actually why this command
69is provided.
70
71\startbuffer
72\startMPcode
73    fill fullcircle xyscaled (8cm,1cm) withcolor "darkred" ;
74    draw lmt_text [
75        text   = decimal 123.45678,
76        color  = "white",
77        style  = "bold",
78        format = "@0.3F",
79    ] ;
80\stopMPcode
81\stopbuffer
82
83\typebuffer[option=TEX]
84
85\startlinecorrection
86\getbuffer
87\stoplinecorrection
88
89The following parameters can be set:
90
91\starttabulate[|T|T|T|p|]
92\FL
93\BC name     \BC type    \BC default \BC comment \NC \NR
94\ML
95\NC offset   \NC numeric \NC 0       \NC \NC \NR
96\NC strut    \NC string  \NC auto    \NC adapts the dimensions to the font (\type {yes} uses the the default strut) \NC \NR
97\NC style    \NC string  \NC         \NC \NC \NR
98\NC color    \NC string  \NC         \NC \NC \NR
99\NC text     \NC string  \NC         \NC \NC \NR
100\NC anchor   \NC string  \NC         \NC one of these \type {lft}, \type {urt} like anchors \NC \NR
101\NC format   \NC string  \NC         \NC a format specifier using \type {@} instead of a percent sign \NC \NR
102\NC position \NC pair    \NC origin  \NC \NC \NR
103\NC trace    \NC boolean \NC false   \NC \NC \NR
104\LL
105\stoptabulate
106
107The next example demonstrates the positioning options:
108
109\startbuffer
110\startMPcode
111    fill fullcircle xyscaled (8cm,1cm) withcolor "darkblue" ;
112    fill fullcircle scaled .5mm withcolor "white" ;
113    draw lmt_text [
114        text     = "left",
115        color    = "white",
116        style    = "bold",
117        anchor   = "lft",
118        position = (-1mm,2mm),
119    ] ;
120    draw lmt_text [
121        text   = "right",
122        color  = "white",
123        style  = "bold",
124        anchor = "rt",
125        offset = 3mm,
126    ] ;
127\stopMPcode
128\stopbuffer
129
130\typebuffer[option=TEX]
131
132\startlinecorrection
133\getbuffer
134\stoplinecorrection
135
136\stopsection
137
138\startsection[title=Strings]
139
140Those familiar with \TEX\ probably know that there's something called catcodes.
141These are properties that you assign to characters and that gives them some
142meaning, like regular letters, other characters, spaces, but also escape
143character (the backslash) or math shift (the dollar). Control over catcodes is
144what makes for instance verbatim possible.
145
146We show a few possibilities and start by defining a macro:
147
148\startbuffer
149\def\foo{x}
150\stopbuffer
151
152\typebuffer[option=TEX] \getbuffer
153
154\startbuffer
155\framed\bgroup
156    \startMPcode
157        interim catcoderegime := vrbcatcoderegime ;
158        draw textext("stream $\string\foo$") withcolor "darkred" ;
159    \stopMPcode
160\egroup
161\stopbuffer
162
163\typebuffer[option=TEX]
164
165\startlinecorrection \getbuffer \stoplinecorrection
166
167\startbuffer
168\framed\bgroup
169    \startMPcode
170        draw textext("stream $\foo$") withcolor "darkblue" ;
171    \stopMPcode
172\egroup
173\stopbuffer
174
175\typebuffer[option=TEX]
176
177\startlinecorrection \getbuffer \stoplinecorrection
178
179\startbuffer
180\framed\bgroup
181    \startMPcode
182        interim catcoderegime := vrbcatcoderegime ;
183        draw textext(stream "!" $\string\foo$) withcolor "darkgreen" ;
184    \stopMPcode
185\egroup
186\stopbuffer
187
188\typebuffer[option=TEX]
189
190\startlinecorrection \getbuffer \stoplinecorrection
191
192\startbuffer
193\framed\bgroup
194    \startMPcode
195        draw textext(stream "!" $\foo$) withcolor "darkyellow" ;
196    \stopMPcode
197\egroup
198\stopbuffer
199
200\typebuffer[option=TEX]
201
202\startlinecorrection \getbuffer \stoplinecorrection
203
204\startbuffer
205\framed\bgroup
206    \startMPcode
207        draw textext(\btx stream "!" $\string\foo$\etx) withcolor "darkgreen" ;
208    \stopMPcode
209\egroup
210\stopbuffer
211
212\typebuffer[option=TEX]
213
214\startlinecorrection \getbuffer \stoplinecorrection
215
216The \type {vrbcatcodesregime} switches to a verbatim catcode regime so the dollars
217remain dollars. But because we do expand control sequences we have to put \type
218{\string} in front.
219
220The (expandable) \type {\btx} and \type {\etx} commands are aliases for the
221control characters \type {0x02} and \type {0x03}. These are valid string fences
222in \LUAMETATEX's \METAPOST\ and thereby permit embedding of the double quotes.
223
224\stopsection
225
226\stopchapter
227
228\stopcomponent
229