| 1 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2 |
%% latex.mp Parametrizable latex labels for metapost |
| 3 |
%% Copyright (c) 2003 JL Diaz |
| 4 |
%% based on an idea taken from Metafun, by Hans Hagen |
| 5 |
% |
| 6 |
% This program may be distributed and/or modified under the conditions |
| 7 |
% of the LaTeX Project Public License, either version 1.2 of this |
| 8 |
% license or (at your option) any later version. The latest version |
| 9 |
% of this license is in http://www.latex-project.org/lppl.txt and |
| 10 |
% version 1.2 or later is part of all distributions of LaTeX version |
| 11 |
% 1999/12/01 or later. |
| 12 |
% |
| 13 |
% This program consists of the file latex.mp |
| 14 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 15 |
% For the impatient: Jump to "USAGE" |
| 16 |
% |
| 17 |
% Introduction: The problem |
| 18 |
% |
| 19 |
% Let us suppose that you need to typeset several labels of the form |
| 20 |
% $x_1$, $x_2$, ... $x_{10}$. Since each label is a formula, the |
| 21 |
% construction btex/etex is required to typeset it through TeX (or |
| 22 |
% LaTeX). So, the straighforward solution is: |
| 23 |
% |
| 24 |
% label(btex $x_1$ etex, (1u,0u)); |
| 25 |
% label(btex $x_2$ etex, (2u,0u)); |
| 26 |
% ... |
| 27 |
% label(btex $x_{10}$ etex, (10u,0u)); |
| 28 |
% |
| 29 |
% This repeated structure is inviting us to convert it into a for |
| 30 |
% loop. A naive attempt would be: |
| 31 |
% |
| 32 |
% for i=1 upto 10: |
| 33 |
% label (btex $x_{i}$ etex, (i*u, 0)); |
| 34 |
% endfor; |
| 35 |
% |
| 36 |
% Unfortunately the above would produce ten times the same label |
| 37 |
% $x_{i}$, because i is not evaluated in this context as a variable. |
| 38 |
% |
| 39 |
% A second, more clever attempt would be: |
| 40 |
% |
| 41 |
% for i=1 upto 10: |
| 42 |
% label("btex $x_{" & decimal i & "}$ etex", (i*u, 0)); |
| 43 |
% endfor; |
| 44 |
% |
| 45 |
% where we are making use of the string concatenation operator (&), |
| 46 |
% and the "decimal" operator, which produces an string whith the |
| 47 |
% decimal representation of its argument. This way, the macro label |
| 48 |
% will receive successively as first argument the strings: "btex |
| 49 |
% $x_{1}$ etex", "btex $x_{2}$ etex", end so on. |
| 50 |
% |
| 51 |
% This solution does not work either, because label is receiving a |
| 52 |
% string, instead of a picture (which is the result of a proper |
| 53 |
% btex/etex construction). When a string is received, label simply |
| 54 |
% outputs that string, without calling TeX (LaTeX) for formatting it. |
| 55 |
% |
| 56 |
% Metapost provides a way of "executing" the code which is stored in |
| 57 |
% a string, it is the scantokens primitive. So, it appears that a |
| 58 |
% modification of the label primitive would be possible, which |
| 59 |
% applies the scantokens primitive on the string which it receives as |
| 60 |
% parameter. Unfortunately this does not work either, because the way |
| 61 |
% in which btex/etex works procludes it use as argument of |
| 62 |
% scantokens. |
| 63 |
% |
| 64 |
% So, apparently, there is not an easy solution to the problem of |
| 65 |
% passing arguments to the etex/btex construct. |
| 66 |
% |
| 67 |
% The solution |
| 68 |
% |
| 69 |
% Since the construct btex/etex can only be used at the "top level" |
| 70 |
% of a source file, and not embedded in any string, the only way to |
| 71 |
% achieve the desired result is to store the string "btex ... etex" |
| 72 |
% in an external temporal file, and input back that file. |
| 73 |
% |
| 74 |
% This solution is implemented in the macro package "TEX.mp" which is |
| 75 |
% part of the standard metapost distribution. Using this package, the |
| 76 |
% problem of printing the ten labels $x_1$ .. $x_{10}$ becomes simple: |
| 77 |
% |
| 78 |
% for i=0 upto 10: |
| 79 |
% label(TEX("$x_{" & decimal i & "}$", (u*i,0)); |
| 80 |
% endfor; |
| 81 |
% |
| 82 |
% The macro TEX receives an string as argument, and uses TeX to |
| 83 |
% typeset it. This is achieved, as already explained, by creating a |
| 84 |
% new string which encapsulated the given argument into a btex/etex |
| 85 |
% pair, writing this new string in an external file, and inputting |
| 86 |
% that file back. |
| 87 |
% |
| 88 |
% This approach has two main drawbacks: |
| 89 |
% |
| 90 |
% 1) An external file is created, and processed each time TEX is |
| 91 |
% used. This is very inefficient, and the slowdown is noticeable if |
| 92 |
% TEX is used often. |
| 93 |
% |
| 94 |
% 2) If we want to use latex istead of tex, the TEX macro will not |
| 95 |
% work, because the external file created through this macro does |
| 96 |
% not include the verbatimtex header required to initialize latex. |
| 97 |
% |
| 98 |
% Metafun approach: a clever solution |
| 99 |
% |
| 100 |
% In the excelent macro package Metafun (by Hans Hagens), a nice |
| 101 |
% solution is found to the first problem. Instead of writing the |
| 102 |
% auxiliar file and inputting it again, what is wrote is an auxiliar |
| 103 |
% metapost program which defines a serie of picture variables, each |
| 104 |
% one with the result of a btex/etex pair. In the first run of |
| 105 |
% metapost, this auxiliar file is write-only. In the second run, the |
| 106 |
% file is read at the beginning, so all picture variables get |
| 107 |
% defined. In this second run, each time the TEX macro is called, it |
| 108 |
% checks if the corresponding picture variable is already defined, |
| 109 |
% and in this case it simply returns its value. |
| 110 |
% |
| 111 |
% This approach is much more efficient, since a single externa file |
| 112 |
% is produced and processed, no matter how much the TEX macro is |
| 113 |
% called. The only drawback is that two metapost runs are required. |
| 114 |
% |
| 115 |
% latex.mp: my solution |
| 116 |
% |
| 117 |
% I liked very much the metafun solution, but I did not want to use |
| 118 |
% the whole Metafun package only to obtain this |
| 119 |
% functionality. Moreover, metafun relies in a set of external |
| 120 |
% scripts, which decide to input or not the external file, depending |
| 121 |
% on the existence of that file. I would prefer do not require perl |
| 122 |
% nor any other scripting language. Only metapost. |
| 123 |
% |
| 124 |
% This is why I wrote "latex.mp". It implements the idea in the |
| 125 |
% metafun package, but it does not require any external script in |
| 126 |
% order to check the existence of the auxiliar file. |
| 127 |
% |
| 128 |
% ================================================================ |
| 129 |
% |
| 130 |
% USAGE |
| 131 |
% |
| 132 |
% input "latex"; |
| 133 |
% initlatex(...); |
| 134 |
% ... |
| 135 |
% |
| 136 |
% latex("string to be formatted by latex"); |
| 137 |
% % Returns a picture which can be used in a label |
| 138 |
% |
| 139 |
% The initlatex macro writes a latex header in an auxiliar file. You |
| 140 |
% do not need to call this macro if you use TeX as typesetting engine. |
| 141 |
% |
| 142 |
% On the other hand, if you want to use LaTeX as typesetting engine, |
| 143 |
% you need to call this macro before the first beginfig(). The macro |
| 144 |
% requires a mandatory argument, which is a string with adittional |
| 145 |
% macro commands to be placed before the "\begin{document}". For |
| 146 |
% example: |
| 147 |
% |
| 148 |
% initlatex("\usepackage[latin1]{inputenc}\usepackage{amsmath}"); |
| 149 |
% |
| 150 |
% If you do not need any aditional package, you can use "" as |
| 151 |
% argument. |
| 152 |
% |
| 153 |
% You can use the latex macro in any place where a picture is |
| 154 |
% expected, as in the first argument of a label comand. For example: |
| 155 |
% |
| 156 |
% for i=0 upto 10: |
| 157 |
% label(latex("$x_{$ & decimal i & "}$", (u*i,0)); |
| 158 |
% endfor; |
| 159 |
% |
| 160 |
% In order to correctly obtain the labels which use the latex macro, |
| 161 |
% you need to process your source file twice through metapost. The |
| 162 |
% first run all labels will show as "[?]", but the second run they |
| 163 |
% will have the correct value. |
| 164 |
% |
| 165 |
% IMPORTANT: |
| 166 |
% |
| 167 |
% If the text passed to the latex macro contains errors, latex will be |
| 168 |
% unable to complete its run. This will make impossible to run mpost |
| 169 |
% on the source, even if the errors are corrected, until you delete |
| 170 |
% the auxiliar file (which has .mpt extension) |
| 171 |
% |
| 172 |
% |
| 173 |
%================================================================ |
| 174 |
% Implementation |
| 175 |
% |
| 176 |
numeric NumPic; % Total number of times that latex() macro has been |
| 177 |
% called |
| 178 |
picture LaTeXPic[]; % Picture array which stores the result of each |
| 179 |
% latex macro call. This array is filled when the |
| 180 |
% axiliar file is read. |
| 181 |
string LaTeXsetup; % additional packages and macro definitions for latex |
| 182 |
|
| 183 |
% Initialization |
| 184 |
NumPic:=0; |
| 185 |
LaTeXsetup:="\documentclass{article}"; |
| 186 |
|
| 187 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 188 |
% Entry point |
| 189 |
% |
| 190 |
% The auxiliar file is read, but only if it exists |
| 191 |
% |
| 192 |
if readfrom(jobname&".mpt") <> EOF: |
| 193 |
scantokens ("input "&jobname & ".mpt"); |
| 194 |
fi; |
| 195 |
|
| 196 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 197 |
% initlatex() |
| 198 |
% |
| 199 |
% By calling this function, the user can specify aditional packages and |
| 200 |
% macro definitions to the be used by latex. |
| 201 |
% |
| 202 |
% For example: |
| 203 |
% |
| 204 |
% initlatex("\usepackage[latin1]{inputenc}\usepackage{amsmath}"); |
| 205 |
% |
| 206 |
% The global variable LaTeXsetup is also provided, mainly for third |
| 207 |
% party libraries which require to define their own macros. The |
| 208 |
% programmers of these libraries should append their own material to |
| 209 |
% the LaTeXsetup variable, by using the & operator. For example: |
| 210 |
% |
| 211 |
% LaTeXsetup:=LaTeXsetup & "\def\TO{\Longrightarrow}"; |
| 212 |
% |
| 213 |
def initlatex primary s = |
| 214 |
write "verbatimtex" to jobname & ".mpt"; |
| 215 |
write "%&latex" to jobname & ".mpt"; |
| 216 |
write LaTeXsetup to jobname & ".mpt"; |
| 217 |
write s to jobname & ".mpt"; |
| 218 |
write "\begin{document}" to jobname & ".mpt"; |
| 219 |
write "etex" to jobname & ".mpt"; |
| 220 |
enddef; |
| 221 |
|
| 222 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 223 |
% latex() |
| 224 |
% |
| 225 |
% This macro receives a string, and returns a picture which contains |
| 226 |
% the result of running the string by latex. The result can be used |
| 227 |
% whenever a picture argument is expected, as for example in a label. |
| 228 |
|
| 229 |
vardef latex@#(expr txt) = |
| 230 |
NumPic := NumPic + 1 ; |
| 231 |
if string txt : |
| 232 |
write "% figure " & decimal charcode & " : " & char(10) & |
| 233 |
"LaTeXPic[" & decimal NumPic & "] := btex " & |
| 234 |
txt & " etex ;" to jobname & ".mpt" ; |
| 235 |
if unknown LaTeXPic[NumPic] : |
| 236 |
thelabel@#("[??]",origin) |
| 237 |
else : |
| 238 |
thelabel@#(LaTeXPic[NumPic],origin) |
| 239 |
fi |
| 240 |
else : |
| 241 |
thelabel@#(txt,origin) |
| 242 |
fi |
| 243 |
enddef ; |
| 244 |
|
| 245 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 246 |
% EOF |