Debian Codegraph @

texmf-dist/metapost/latexmp/latexmp.mp

raw
1 %D \module
2 %D [ file=latexmp.mp,
3 %D system={latexMP},
4 %D version=1.2.1,
5 %D title=latexMP,
6 %D subtitle=LaTeX typesetting in \METAPOST,
7 %D author=Jens-Uwe Morawski,
8 %D date={2005/04/06},
9 %D copyright={none; this module is Public Domain}]
10 %D
11 %C - all variables start with prefix "latexmp_"
12 %C - all internal macros have suffix "_latexmp"
13 %C - macros without "_latexmp" suffix are:
14 %C textext, setupLaTeXMP
15
16 %D \section{Initialisation}
17 if known latexmp_module: message(" not loaded again"); endinput ; fi ;
18 boolean latexmp_module ; latexmp_module := true ;
19
20 %D set the name of the temporary file
21 string latexmp_lmpfile ; latexmp_lmpfile := "ltx-" & jobname & ".tmp" ;
22
23 %D save some original \METAPOST\ definitions
24 inner end ; inner bye ;
25 let origEnd_latexmp=end ;
26 let origBye_latexmp=bye ;
27 let origShipit_latexmp=shipit ;
28 string latexmp_extrabeginfig, latexmp_extraendfig ;
29 extra_endfig := extra_endfig & "; resetattachstrings_latexmp ;" ;
30 latexmp_extrabeginfig := extra_beginfig ;
31 latexmp_extraendfig := extra_endfig ;
32 color latexmp_background ;
33 latexmp_background := background ;
34
35 %D The runtime control of latexMP states:
36 %D (1) is this the second run in \type{rerun} mode ; (2) has the temporary
37 %D mp file already been read; (3) is the LaTeX preamble already
38 %D been written to the temporary file
39 boolean latexmp_secondrun, latexmp_lmploaded, latexmp_texinit ;
40 latexmp_secondrun := false ;
41 latexmp_lmploaded := false ;
42 latexmp_texinit := false ;
43
44 %D The current number of \type{textext()} in the input file. The counter
45 %D is incremented at every \type{textext} call. It is used to assign the
46 %D pictures from the temporary file.
47 numeric latexmp_counter ; latexmp_counter := 0 ;
48
49 %D \section{Setup}
50 %D LaTeX preamble variables
51 string latexmp_docclass, latexmp_classoptions,
52 latexmp_inputenc, latexmp_fontenc, latexmp_babellang,
53 latexmp_packages, latexmp_preamble, latexmp_lmpprefix,
54 latexmp_preamblefile ;
55
56 latexmp_docclass := "article" ;
57 latexmp_classoptions := "" ;
58 latexmp_inputenc := "" ;
59 latexmp_fontenc := "" ;
60 latexmp_babellang := "" ;
61 latexmp_packages := "" ;
62 latexmp_preamble := "" ;
63 latexmp_preamblefile := "" ;
64
65 %D The setup variables that control activation of the specials:
66 numeric latexmp_mode ; latexmp_mode := 1 ;
67 boolean latexmp_subsinfont ; latexmp_subsinfont := false ;
68 boolean latexmp_multicolor ; latexmp_multicolor := false ;
69 boolean latexmp_debug ; latexmp_debug := false ;
70
71 %D The setup command:
72 def setupLaTeXMP(text kvps) =
73 if not latexmp_secondrun:
74 if latexmp_texinit:
75 message("LaTeXMP: setupLaTeXMP cannot be used if textext" &
76 "has already been used; set-up ignored.");
77 else:
78 begingroup;
79 %D declaration of key-value parameters for LaTeX preamble
80 save class, options, inputencoding, fontencoding, language,
81 packages, preamble, preamblefile ;
82 string class, options, inputencoding, fontencoding, language,
83 packages, preamble, preamblefile;
84 %D declaration of key and synonyms for \type{mode}
85 save mode, normal, rerun ;
86 numeric mode, normal, rerun ;
87 normal:=1 ; rerun := 2 ;
88 %D declaration of keys and synonyms for the specials
89 save textextlabel, multicolor, debug, enable, disable ;
90 boolean textextlabel, multicolor, debug, enable, disable ;
91 enable:=true ; disable := false ;
92
93 processKeyvals_latexmp(kvps) ;
94
95 if known class:
96 if (length class) >0: latexmp_docclass:=class ;
97 else: latex_docclass:="article";
98 fi;
99 fi;
100 if known options: latexmp_classoptions:=options ; fi;
101 if known inputencoding: latexmp_inputenc:=inputencoding ; fi;
102 if known fontencoding: latexmp_fontenc:=fontencoding ; fi;
103 if known language: latexmp_babellang:=language ; fi;
104 if known preamble:latexmp_preamble:=preamble ; fi;
105 if known preamblefile: latexmp_preamblefile:=preamblefile ; fi;
106 if known packages: latexmp_packages:=packages ; fi;
107 if known mode:
108 if (mode=normal) or (mode=rerun): latexmp_mode:=mode ;
109 else:
110 errmessage("latexMP error: set mode either to 'normal' or 'rerun'.") ;
111 latexmp_mode := 1 ;
112 fi;
113 fi;
114 if known textextlabel: latexmp_subsinfont:=textextlabel ; fi;
115 if known multicolor: latexmp_multicolor:=multicolor ; fi;
116 if known debug: latexmp_debug:=debug ; fi;
117 endgroup;
118
119 %D overwrite the default definition of \type{thelabel} if user has specified
120 %D \type{textextlabel=enable}:
121 if latexmp_subsinfont:
122 vardef thelabel@#(expr s,z) =
123 save p; picture p;
124 if picture s: p=s
125 else: p = textext(s)
126 fi;
127 p shifted (z + labeloffset*laboff@# -
128 (labxf@#*lrcorner p + labyf@#*ulcorner p
129 + (1-labxf@#-labyf@#)*llcorner p ))
130 enddef;
131 fi;
132
133 %D In \type{rerun} mode for the first run own definitions for \type{end}
134 %D and \type{bye} are used; output is disabled for first run.
135 if latexmp_mode=2:
136 let end=end_latexmp ;
137 let bye=end_latexmp ;
138 let shipit=relax ;
139 %D Since in the first run the temporary file will not contain
140 %D correct definitions automatic loading of that file by \type{textext}
141 %D is disabled (we fake here that it is already loaded). Loading of the
142 %D temporary file in \type{end_latexmp}.
143 latexmp_lmploaded := true ;
144 fi;
145 fi;fi;
146 enddef;
147
148 %D used in \type{writeLaTeXpreamble_latexmp}
149 %D to generate \type{\usepackage} statements
150 def writepackages_latexmp=
151 begingroup;
152 save inoptions, currentpackage, currentoptions, s ;
153 boolean inoptions ; inoptions:=false;
154 string currentpackage, currentoptions, s ;
155 currentpackage:=""; currentoptions:="" ;
156
157 for c=0 upto ((length latexmp_packages)-1):
158 s := substring (c,c+1) of latexmp_packages ;
159 if s="[": inoptions := true ;
160 elseif s="]": inoptions := false;
161 elseif s=",":
162 if inoptions: currentoptions:= currentoptions & s ;
163 else:
164 if (length currentpackage)>0:
165 s:="{" & currentpackage & "}" ;
166 if (length currentoptions)>0:
167 s:="[" & currentoptions & "]" & s ;
168 fi;
169 write ("\usepackage" & s) to latexmp_lmpfile ;
170 currentpackage:=""; currentoptions:="" ;
171 fi;
172 fi;
173 else:
174 if inoptions: currentoptions:= currentoptions & s ;
175 else: currentpackage:= currentpackage & s ;
176 fi;
177 fi;
178 endfor;
179 if (length currentpackage)>0: s:="{" & currentpackage & "}" ;
180 if (length currentoptions)>0: s:="[" & currentoptions & "]" & s ; fi;
181 write ("\usepackage" & s) to latexmp_lmpfile ;
182 fi;
183 endgroup;
184 enddef;
185
186 %D \section{LaTeX preamble}
187
188 def writeLaTeXpreamble_latexmp =
189 write "verbatimtex" to latexmp_lmpfile ;
190 write "%&latex" to latexmp_lmpfile ;
191 %D If preamble file has been specified in the setup only a
192 %D \type{\input} for this file will be used in the LaTeX preamble:
193 if (length latexmp_preamblefile)>0:
194 write ("\input{" & latexmp_preamblefile & "}") to latexmp_lmpfile ;
195 else:
196 %D Specific definitions from the setup parameters:
197 write ("\documentclass"
198 if (length latexmp_classoptions)>0:
199 & "[" & latexmp_classoptions & "]"
200 fi & "{" & latexmp_docclass & "}") to latexmp_lmpfile ;
201 if (length latexmp_fontenc)>0:
202 write ("\usepackage[" & latexmp_fontenc & "]{fontenc}")
203 to latexmp_lmpfile ;
204 fi
205 if (length latexmp_inputenc)>0:
206 write ("\usepackage[" & latexmp_inputenc & "]{inputenc}")
207 to latexmp_lmpfile ;
208 fi
209 if (length latexmp_babellang)>0:
210 write ("\usepackage[" & latexmp_babellang & "]{babel}")
211 to latexmp_lmpfile ;
212 fi
213 if (length latexmp_packages)>0: writepackages_latexmp ; fi;
214 if (length latexmp_preamble)>0:
215 write latexmp_preamble to latexmp_lmpfile ;
216 fi
217 fi;
218 if latexmp_multicolor:
219 write ("\makeatletter") to latexmp_lmpfile ;
220 write ("\font\latexmp@special=cmtt8 scaled 250") to latexmp_lmpfile ;
221 write ("\def\reset@color{" &
222 "\parbox[t][0pt][t]{0pt}{\makebox[0pt][l]{\latexmp@special ecolorxxx}}}")
223 to latexmp_lmpfile ;
224 write ("\newcommand*{\color}[2][named]{\parbox[t][0pt][t]{0pt}{" &
225 "\makebox[0pt][l]{\latexmp@special bcolorxxx:#1:#2}}" &
226 "\aftergroup\reset@color\ignorespaces}") to latexmp_lmpfile ;
227 write ("\def\textcolor#1#{\@textcolor{#1}}") to latexmp_lmpfile ;
228 write ("\def\@textcolor#1#2#3{\protect\leavevmode{\color#1{#2}#3}}")
229 to latexmp_lmpfile ;
230 write ("\newcommand*{\transparent}[3][1]{\parbox[t][0pt][t]{0pt}{"&
231 "\makebox[0pt][l]{\latexmp@special btransxxx:#1,#2}}"&
232 "#3\parbox[t][0pt][t]{0pt}{" &
233 "\makebox[0pt][l]{\latexmp@special etransxxx}}}") to latexmp_lmpfile ;
234 write "\makeatother" to latexmp_lmpfile ;
235 fi;
236 write "\begin{document}" to latexmp_lmpfile ;
237 write "etex" to latexmp_lmpfile ;
238 write "picture latexmp_picture[];" to latexmp_lmpfile ;
239 enddef;
240
241 %D \section{Main Macros}
242
243 %D The macro that does the job:
244 vardef textext@#(expr s)=
245 save pic ; picture pic ;
246 latexmp_counter:= latexmp_counter+1 ;
247 getpicture_latexmp ;
248 addstring_latexmp(s) ;
249 if (length (str @#))>0 :
250 % V. 1.1 compatible with MetaFun
251 thelabel@#(pic,origin)
252 else:
253 % V. 1.2 better compatibility with btex..etex
254 pic
255 fi
256 enddef;
257
258 %D strings \type{latexmp_prepend} and \type{latexmp_append} are attached
259 %D to the string passed to \type{textext}. This may be useful for package
260 %D authors to implement styling interfaces.
261 string latexmp_prepend, latexmp_append ;
262 latexmp_prepend:= "" ; latexmp_append := "" ;
263
264 %D is called at each endfig via extra_endfig
265 def resetattachstrings_latexmp =
266 latexmp_prepend:= "" ; latexmp_append := "" ;
267 enddef;
268
269 def addstring_latexmp(expr s)=
270 if not latexmp_texinit:
271 write EOF to latexmp_lmpfile ;
272 writeLaTeXpreamble_latexmp ;
273 latexmp_texinit := true ;
274 fi;
275 write "latexmp_picture[" & (decimal latexmp_counter) & "]:= btex " &
276 latexmp_prepend & s & latexmp_append & " etex ;" to latexmp_lmpfile ;
277 enddef;
278
279 def getpicture_latexmp =
280 if not latexmp_lmploaded:
281 if not (readfrom latexmp_lmpfile=EOF):
282 scantokens ("input " & latexmp_lmpfile) ;
283 fi;
284 latexmp_lmploaded := true ;
285 fi;
286 if known latexmp_picture[latexmp_counter]:
287 if latexmp_multicolor and not latexmp_debug:
288 pic := colorspecials_latexmp(latexmp_picture[latexmp_counter]) ;
289 else:
290 pic := latexmp_picture[latexmp_counter] ;
291 fi;
292 else:
293 pic := ("LaTeXMP " & (decimal latexmp_counter))
294 infont defaultfont ;
295 fi;
296 enddef;
297
298 %D The following is only used if option \type{multicolor} is set in
299 %D \type{setupLaTeXMP}. It is employed in \type{getpicture_latexmp}
300 %D in order to strip specials from the LaTeX picture and change
301 %D the colors of text elements accordigly.
302 vardef colorspecials_latexmp(expr p) =
303 save _P, _bb, _tp, _fp, _t, _addto_P ;
304 save _withcolor, _colorpointer, _transparent, _transpointer;
305 picture _P ; path _bb ;
306 string _withcolor[], _transparent[], _tp, _fp, _addto_P ;
307 numeric _colorpointer, _transpointer ;
308 _P := nullpicture ; _bb := pathpart p ;
309 _withcolor[0]:= "" ; _colorpointer := 0 ; _transparent[0]:="" ;
310 _transpointer:=0 ;
311 _addto_P := "addto _P also _tp infont _fp transformed _t " ;
312 for c within p:
313 if textual c:
314 _tp:= textpart c ;
315 _fp:= fontpart c ;
316 transform _t ;
317 (xpart _t,xxpart _t,xypart _t)=(xpart c,xxpart c,xypart c) ;
318 (ypart _t,yypart _t,yxpart _t)=(ypart c,yypart c,yxpart c) ;
319 if (_fp="cmtt8") and ((substring (1,9) of _tp)="colorxxx"):
320 % color special found
321 if (substring (0,1) of _tp)="b":
322 % begin color special
323 _colorpointer := _colorpointer+1;
324 _withcolor[_colorpointer]:= colormacro_latexmp(substring (10,(length _tp)) of _tp) ;
325 elseif (substring (0,1) of _tp)="e":
326 % end color special
327 if not (_colorpointer=0):
328 %C if \type{\color} is used ungrouped in a parbox the \type{ecolorxxx}
329 %C appears prior \type{bcolorxxx}. multiple instances of \type{ecolorxxx}
330 %C would result in counting \type{_colorpointer<0}. thus checking
331 %C for \type{_colorpointer=0} is a ugly hack for this problem.
332 _colorpointer := _colorpointer-1;
333 fi;
334 %C else:?? ooops; should not happen
335 fi;
336 elseif (_fp="cmtt8") and ((substring (1,9) of _tp)="transxxx"):
337 % transparency special found
338 if (substring (0,1) of _tp)="b":
339 % begin trans special
340 _transpointer := _transpointer+1;
341 _transparent[_transpointer]:= substring (10,(length _tp)) of _tp ;
342 elseif (substring (0,1) of _tp)="e":
343 % end trans special
344 _transpointer := _transpointer-1;
345 fi;
346 else:
347 scantokens (_addto_P & withcurrentcolor_latexmp ) ;
348 fi;
349 fi;
350 endfor;
351 setbounds _P to _bb ;
352 _P
353 enddef;
354
355 vardef colormacro_latexmp(expr colspec)=
356 save _cmd, _pos, _m, _c ; string _cmd, _m, _c ;
357 _pos := 0 ; _cmd:="" ;
358 for i=1 upto (length colspec):
359 if (substring (i-1,i) of colspec)=":":
360 _pos := i ;
361 fi;
362 endfor;
363 if _pos<2: % "x:..." --> no color model found
364 errmessage("LaTeXMP: no color model found in: " & colspec) ;
365 else:
366 _m := substring (0,(_pos-1)) of colspec ;
367 _c := substring (_pos,(length colspec)) of colspec ;
368 if expandafter known (scantokens ("latexmp_color_" & _m)):
369 _cmd := (scantokens ("latexmp_color_" & _m)) & "(" & _c & ")" ;
370 else:
371 errmessage("LaTeXMP: color model '" & _m & "' not supported)") ;
372 fi;
373 fi;
374 _cmd
375 enddef;
376
377 vardef withcurrentcolor_latexmp=
378 if (length _withcolor[_colorpointer])>0:
379 if (length _transparent[_transpointer])>0:
380 " withcolor transparent(" & _transparent[_transpointer] & "," &
381 _withcolor[_colorpointer] &")"
382 else: " withcolor " & _withcolor[_colorpointer] fi
383 else:
384 if (length _transparent[_transpointer])>0:
385 " withcolor transparent(" & _transparent[_transpointer] & ",black)"
386 else: "" fi
387 fi
388 enddef;
389
390 %D \section{The end macro for rerun mode}
391 string latexmp_beforererun ; latexmp_beforererun := "" ;
392
393 def end_latexmp =
394 %D read temporary file if strings have been written there
395 if latexmp_counter>0:
396 write EOF to latexmp_lmpfile;
397 scantokens ("input " & latexmp_lmpfile) ;
398 fi;
399 %D re-init
400 latexmp_counter := 0 ; latexmp_secondrun := true ; latexmp_texinit := false ;
401 %D restore defaults
402 extra_beginfig := latexmp_extrabeginfig ;
403 extra_endfig := latexmp_extraendfig ;
404 background := latexmp_background ;
405 let end=origEnd_latexmp ; let bye=origBye_latexmp ;
406 let shipit=origShipit_latexmp ;
407 clear_pen_memory; clearit;
408 pickup pencircle scaled .5bp; defaultpen := savepen;
409 %D run pre-rerun hooks
410 scantokens latexmp_beforererun ;
411 %D input current job again
412 scantokens ("input " & jobname) ;
413 enddef;
414
415 %D \section{color support}
416 vardef Hsvcolor_latexmp(expr H,s,v) =
417 save h, o, b, d, pc, sc; numeric h, o, b, d, pc, sc ;
418 h := H/60 ; pc := floor h ; sc := h - pc ;
419 o := (1-s)*v ; b := (1-(sc*s))*v ; d := (1-((1-sc)*s))*v ;
420 if (pc = 0) or (pc = 6): (v,d,o)
421 elseif pc = 1: (b,v,o)
422 elseif pc = 2: (o,v,d)
423 elseif pc = 3: (o,b,v)
424 elseif pc = 4: (d,o,v)
425 else:(v,o,b)
426 fi
427 enddef;
428 vardef cmykcolor_latexmp(expr c,m,y,k) =
429 %D use MetaFun cmyk-specials if available; macro \type{cmyk()}
430 %D checks if \type{cmykcolors=true}
431 if known context_spec: cmyk(c,m,y,k) else: (1-c-k,1-m-k,1-y-k) fi
432 enddef;
433 vardef graycolor_latexmp(expr g) = (g,g,g) enddef ;
434
435 %D which macro should be used for a given color model in multicolor mode.
436 string latexmp_color_Hsv, latexmp_color_rgb,
437 latexmp_color_gray, latexmp_color_cmyk, latexmp_color_named ;
438
439 latexmp_color_Hsv := "Hsvcolor_latexmp" ;
440 latexmp_color_gray := "graycolor_latexmp" ;
441 latexmp_color_rgb := "" ;
442 latexmp_color_cmyk := "cmykcolor_latexmp" ;
443 latexmp_color_named := "" ;
444
445 %D \section{Key-Value Support Macros}
446 def processKeyvals_latexmp (text _kvps_)=
447 begingroup;
448 save _equals, _assign ;
449 let _equals= = ;
450
451 primarydef _ll_ _assign _rr_ =
452 hide(_ll_ _equals _rr_ ) 1
453 enddef;
454
455 save = ;
456 let = _equals _assign ;
457 for _xx_ _equals _kvps_ : endfor ;% NOP
458 endgroup;
459 enddef;