| 1 |
/* |
| 2 |
|
| 3 |
Copyright 2008-2010 Taco Hoekwater <[email protected]> |
| 4 |
|
| 5 |
This file is part of LuaTeX. |
| 6 |
|
| 7 |
LuaTeX is free software; you can redistribute it and/or modify it under the terms |
| 8 |
of the GNU General Public License as published by the Free Software Foundation; |
| 9 |
either version 2 of the License, or (at your option) any later version. |
| 10 |
|
| 11 |
LuaTeX is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 |
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 |
PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 |
|
| 15 |
You should have received a copy of the GNU General Public License along with |
| 16 |
LuaTeX; if not, see <http://www.gnu.org/licenses/>. |
| 17 |
|
| 18 |
*/ |
| 19 |
|
| 20 |
#include "ptexlib.h" |
| 21 |
|
| 22 |
#define mode mode_par |
| 23 |
#define tail tail_par |
| 24 |
#define head head_par |
| 25 |
#define dir_save dirs_par |
| 26 |
|
| 27 |
/*tex |
| 28 |
|
| 29 |
Concerning display skips, \TEX\ normally always inserts before and only after |
| 30 |
when larger than zero. THis can ow be controlled with |\mathdisplayskipmode|: |
| 31 |
|
| 32 |
\starttabulate |
| 33 |
\NC 0 \NC normal \TEX \NC \NR |
| 34 |
\NC 1 \NC always \NC \NR |
| 35 |
\NC 2 \NC non-zero \NC \NR |
| 36 |
\NC 3 \NC ignore \NC \NR |
| 37 |
\stoptabulate |
| 38 |
|
| 39 |
*/ |
| 40 |
|
| 41 |
#define back_error(A,B) do { \ |
| 42 |
OK_to_interrupt=false; \ |
| 43 |
back_input(); \ |
| 44 |
OK_to_interrupt=true; \ |
| 45 |
tex_error(A,B); \ |
| 46 |
} while (0) |
| 47 |
|
| 48 |
int scan_math(pointer, int); |
| 49 |
int scan_math_style(pointer, int); |
| 50 |
pointer fin_mlist(pointer); |
| 51 |
|
| 52 |
/*tex |
| 53 |
|
| 54 |
When \TeX\ reads a formula that is enclosed between \.\$'s, it constructs an |
| 55 |
{\sl mlist}, which is essentially a tree structure representing that formula. |
| 56 |
An mlist is a linear sequence of items, but we can regard it as a tree |
| 57 |
structure because mlists can appear within mlists. For example, many of the |
| 58 |
entries can be subscripted or superscripted, and such ``scripts'' are mlists |
| 59 |
in their own right. |
| 60 |
|
| 61 |
An entire formula is parsed into such a tree before any of the actual |
| 62 |
typesetting is done, because the current style of type is usually not known |
| 63 |
until the formula has been fully scanned. For example, when the formula |
| 64 |
`\.{\$a+b \\over c+d\$}' is being read, there is no way to tell that |
| 65 |
`\.{a+b}' will be in script size until `\.{\\over}' has appeared. |
| 66 |
|
| 67 |
During the scanning process, each element of the mlist being built is |
| 68 |
classified as a relation, a binary operator, an open parenthesis, etc., or as |
| 69 |
a construct like `\.{\\sqrt}' that must be built up. This classification |
| 70 |
appears in the mlist data structure. |
| 71 |
|
| 72 |
After a formula has been fully scanned, the mlist is converted to an hlist so |
| 73 |
that it can be incorporated into the surrounding text. This conversion is |
| 74 |
controlled by a recursive procedure that decides all of the appropriate |
| 75 |
styles by a ``top-down'' process starting at the outermost level and working |
| 76 |
in towards the subformulas. The formula is ultimately pasted together using |
| 77 |
combinations of horizontal and vertical boxes, with glue and penalty nodes |
| 78 |
inserted as necessary. |
| 79 |
|
| 80 |
An mlist is represented internally as a linked list consisting chiefly of |
| 81 |
``noads'' (pronounced ``no-adds''), to distinguish them from the somewhat |
| 82 |
similar ``nodes'' in hlists and vlists. Certain kinds of ordinary nodes are |
| 83 |
allowed to appear in mlists together with the noads; \TeX\ tells the |
| 84 |
difference by means of the |type| field, since a noad's |type| is always |
| 85 |
greater than that of a node. An mlist does not contain character nodes, hlist |
| 86 |
nodes, vlist nodes, math nodes or unset nodes; in particular, each mlist item |
| 87 |
appears in the variable-size part of |mem|, so the |type| field is always |
| 88 |
present. |
| 89 |
|
| 90 |
Each noad is five or more words long. The first word contains the |type| and |
| 91 |
|subtype| and |link| fields that are already so familiar to us; the second |
| 92 |
contains the attribute list pointer, and the third, fourth an fifth words are |
| 93 |
called the noad's |nucleus|, |subscr|, and |supscr| fields. (This use of a |
| 94 |
combined attribute list is temporary. Eventually, each of fields need their |
| 95 |
own list) |
| 96 |
|
| 97 |
Consider, for example, the simple formula `\.{\$x\^2\$}', which would be |
| 98 |
parsed into an mlist containing a single element called an |ord_noad|. The |
| 99 |
|nucleus| of this noad is a representation of `\.x', the |subscr| is empty, |
| 100 |
and the |supscr| is a representation of `\.2'. |
| 101 |
|
| 102 |
The |nucleus|, |subscr|, and |supscr| fields are further broken into |
| 103 |
subfields. If |p| points to a noad, and if |q| is one of its principal fields |
| 104 |
(e.g., |q=subscr(p)|), |q=null| indicates a field with no value (the |
| 105 |
corresponding attribute of noad |p| is not present). Otherwise, there are |
| 106 |
several possibilities for the subfields, depending on the |type| of |q|. |
| 107 |
|
| 108 |
\startitemize |
| 109 |
|
| 110 |
\startitem |
| 111 |
|type(q)=math_char_node| means that |math_fam(q)| refers to one of |
| 112 |
the sixteen font families, and |character(q)| is the number of a |
| 113 |
character within a font of that family, as in a character node. |
| 114 |
\stopitem |
| 115 |
|
| 116 |
\startitem |
| 117 |
|type(q)=math_text_char_node| is similar, but the character is |
| 118 |
unsubscripted and unsuperscripted and it is followed immediately by |
| 119 |
another character from the same font. (This |type| setting appears |
| 120 |
only briefly during the processing; it is used to suppress unwanted |
| 121 |
italic corrections.) |
| 122 |
\stopitem |
| 123 |
|
| 124 |
\startitem |
| 125 |
|type(q)=sub_box_node| means that |math_list(q)| points to a box node |
| 126 |
(either an |hlist_node| or a |vlist_node|) that should be used as the |
| 127 |
value of the field. The |shift_amount| in the subsidiary box node is |
| 128 |
the amount by which that box will be shifted downward. |
| 129 |
\stopitem |
| 130 |
|
| 131 |
\startitem |
| 132 |
|type(q)=sub_mlist_node| means that |math_list(q)| points to an |
| 133 |
mlist; the mlist must be converted to an hlist in order to obtain the |
| 134 |
value of this field. |
| 135 |
\stopitem |
| 136 |
|
| 137 |
\startitem |
| 138 |
In the latter case, we might have |math_list(q)=null|. This is not |
| 139 |
the same as |q=null|; for example, `\.{\$P\_\{\}\$}' and `\.{\$P\$}' |
| 140 |
produce different results (the former will not have the ``italic |
| 141 |
correction'' added to the width of |P|, but the ``script skip'' will |
| 142 |
be added). |
| 143 |
\stopitem |
| 144 |
|
| 145 |
\startitemize |
| 146 |
|
| 147 |
*/ |
| 148 |
|
| 149 |
static void unsave_math(void) |
| 150 |
{ |
| 151 |
unsave(); |
| 152 |
decr(save_ptr); |
| 153 |
flush_node_list(text_dir_ptr); |
| 154 |
assert(saved_type(0) == saved_textdir); |
| 155 |
text_dir_ptr = saved_value(0); |
| 156 |
} |
| 157 |
|
| 158 |
/*tex |
| 159 |
|
| 160 |
Sometimes it is necessary to destroy an mlist. The following subroutine |
| 161 |
empties the current list, assuming that |abs(mode)=mmode|. |
| 162 |
|
| 163 |
*/ |
| 164 |
|
| 165 |
void flush_math(void) |
| 166 |
{ |
| 167 |
flush_node_list(vlink(head)); |
| 168 |
flush_node_list(incompleat_noad_par); |
| 169 |
vlink(head) = null; |
| 170 |
tail = head; |
| 171 |
incompleat_noad_par = null; |
| 172 |
} |
| 173 |
|
| 174 |
/*tex Before we can do anything in math mode, we need fonts. */ |
| 175 |
|
| 176 |
#define MATHFONTSTACK 8 |
| 177 |
#define MATHFONTDEFAULT 0 |
| 178 |
|
| 179 |
static sa_tree math_fam_head = NULL; |
| 180 |
|
| 181 |
int fam_fnt(int fam_id, int size_id) |
| 182 |
{ |
| 183 |
int n = fam_id + (256 * size_id); |
| 184 |
return (int) get_sa_item(math_fam_head, n).int_value; |
| 185 |
} |
| 186 |
|
| 187 |
void def_fam_fnt(int fam_id, int size_id, int f, int lvl) |
| 188 |
{ |
| 189 |
int n = fam_id + (256 * size_id); |
| 190 |
sa_tree_item sa_value = { 0 }; |
| 191 |
sa_value.int_value = f; |
| 192 |
set_sa_item(math_fam_head, n, sa_value, lvl); |
| 193 |
fixup_math_parameters(fam_id, size_id, f, lvl); |
| 194 |
if (tracing_assigns_par > 1) { |
| 195 |
begin_diagnostic(); |
| 196 |
tprint("{assigning"); |
| 197 |
print_char(' '); |
| 198 |
print_cmd_chr(def_family_cmd, size_id); |
| 199 |
print_int(fam_id); |
| 200 |
print_char('='); |
| 201 |
print_font_identifier(fam_fnt(fam_id, size_id)); |
| 202 |
print_char('}'); |
| 203 |
end_diagnostic(false); |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
static void unsave_math_fam_data(int gl) |
| 208 |
{ |
| 209 |
sa_stack_item st; |
| 210 |
if (math_fam_head->stack == NULL) |
| 211 |
return; |
| 212 |
while (math_fam_head->stack_ptr > 0 && |
| 213 |
abs(math_fam_head->stack[math_fam_head->stack_ptr].level) |
| 214 |
>= (int) gl) { |
| 215 |
st = math_fam_head->stack[math_fam_head->stack_ptr]; |
| 216 |
if (st.level > 0) { |
| 217 |
rawset_sa_item(math_fam_head, st.code, st.value); |
| 218 |
/*tex Now do a trace message, if requested. */ |
| 219 |
if (tracing_restores_par > 1) { |
| 220 |
int size_id = st.code / 256; |
| 221 |
int fam_id = st.code % 256; |
| 222 |
begin_diagnostic(); |
| 223 |
tprint("{restoring"); |
| 224 |
print_char(' '); |
| 225 |
print_cmd_chr(def_family_cmd, size_id); |
| 226 |
print_int(fam_id); |
| 227 |
print_char('='); |
| 228 |
print_font_identifier(fam_fnt(fam_id, size_id)); |
| 229 |
print_char('}'); |
| 230 |
end_diagnostic(false); |
| 231 |
} |
| 232 |
} |
| 233 |
(math_fam_head->stack_ptr)--; |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
/*tex Parameters */ |
| 238 |
|
| 239 |
#define MATHPARAMSTACK 8 |
| 240 |
#define MATHPARAMDEFAULT undefined_math_parameter |
| 241 |
|
| 242 |
static sa_tree math_param_head = NULL; |
| 243 |
|
| 244 |
// void def_math_param(int param_id, int style_id, scaled value, int lvl) |
| 245 |
// { |
| 246 |
// int n = param_id + (256 * style_id); |
| 247 |
// sa_tree_item sa_value = { 0 }; |
| 248 |
// sa_value.int_value = (int) value; |
| 249 |
// set_sa_item(math_param_head, n, sa_value, lvl); |
| 250 |
// if (tracing_assigns_par > 1) { |
| 251 |
// begin_diagnostic(); |
| 252 |
// tprint("{assigning"); |
| 253 |
// print_char(' '); |
| 254 |
// print_cmd_chr(set_math_param_cmd, param_id); |
| 255 |
// print_cmd_chr(math_style_cmd, style_id); |
| 256 |
// print_char('='); |
| 257 |
// print_int(value); |
| 258 |
// print_char('}'); |
| 259 |
// end_diagnostic(false); |
| 260 |
// } |
| 261 |
// } |
| 262 |
|
| 263 |
void def_math_param(int param_id, int style_id, scaled value, int lvl) |
| 264 |
{ |
| 265 |
int n = param_id + (256 * style_id); |
| 266 |
sa_tree_item sa_value = { 0 }; |
| 267 |
if (lvl <= 1) { |
| 268 |
if (param_id >= math_param_ord_ord_spacing && param_id <= math_param_inner_inner_spacing) { |
| 269 |
sa_tree_item ti = get_sa_item(math_param_head, n); |
| 270 |
if (ti.int_value > thick_mu_skip_code && valid_node(ti.int_value)) { |
| 271 |
free_node(ti.int_value, glue_spec_size); |
| 272 |
} |
| 273 |
} |
| 274 |
} |
| 275 |
sa_value.int_value = (int) value; |
| 276 |
set_sa_item(math_param_head, n, sa_value, lvl); |
| 277 |
if (tracing_assigns_par > 1) { |
| 278 |
begin_diagnostic(); |
| 279 |
tprint("{assigning"); |
| 280 |
print_char(' '); |
| 281 |
print_cmd_chr(set_math_param_cmd, param_id); |
| 282 |
print_cmd_chr(math_style_cmd, style_id); |
| 283 |
print_char('='); |
| 284 |
print_int(value); |
| 285 |
print_char('}'); |
| 286 |
end_diagnostic(false); |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
scaled get_math_param(int param_id, int style_id) |
| 291 |
{ |
| 292 |
int n = param_id + (256 * style_id); |
| 293 |
return (scaled) get_sa_item(math_param_head, n).int_value; |
| 294 |
} |
| 295 |
|
| 296 |
// static void unsave_math_param_data(int gl) |
| 297 |
// { |
| 298 |
// sa_stack_item st; |
| 299 |
// if (math_param_head->stack == NULL) |
| 300 |
// return; |
| 301 |
// while (math_param_head->stack_ptr > 0 && |
| 302 |
// abs(math_param_head->stack[math_param_head->stack_ptr].level) |
| 303 |
// >= (int) gl) { |
| 304 |
// st = math_param_head->stack[math_param_head->stack_ptr]; |
| 305 |
// if (st.level > 0) { |
| 306 |
// rawset_sa_item(math_param_head, st.code, st.value); |
| 307 |
// /*tex Do a trace message, if requested. */ |
| 308 |
// if (tracing_restores_par > 1) { |
| 309 |
// int param_id = st.code % 256; |
| 310 |
// int style_id = st.code / 256; |
| 311 |
// begin_diagnostic(); |
| 312 |
// tprint("{restoring"); |
| 313 |
// print_char(' '); |
| 314 |
// print_cmd_chr(set_math_param_cmd, param_id); |
| 315 |
// print_cmd_chr(math_style_cmd, style_id); |
| 316 |
// print_char('='); |
| 317 |
// print_int(get_math_param(param_id, style_id)); |
| 318 |
// print_char('}'); |
| 319 |
// end_diagnostic(false); |
| 320 |
// } |
| 321 |
// } |
| 322 |
// (math_param_head->stack_ptr)--; |
| 323 |
// } |
| 324 |
// } |
| 325 |
|
| 326 |
static void unsave_math_param_data(int gl) |
| 327 |
{ |
| 328 |
sa_stack_item st; |
| 329 |
if (math_param_head->stack == NULL) |
| 330 |
return; |
| 331 |
while (math_param_head->stack_ptr > 0 && |
| 332 |
abs(math_param_head->stack[math_param_head->stack_ptr].level) |
| 333 |
>= (int) gl) { |
| 334 |
st = math_param_head->stack[math_param_head->stack_ptr]; |
| 335 |
if (st.level > 0) { |
| 336 |
int param_id = st.code % 256; |
| 337 |
int style_id = st.code / 256; |
| 338 |
if (param_id >= math_param_ord_ord_spacing && param_id <= math_param_inner_inner_spacing) { |
| 339 |
sa_tree_item ti = get_sa_item(math_param_head, st.code); |
| 340 |
if (ti.int_value > thick_mu_skip_code && valid_node(ti.int_value)) { |
| 341 |
free_node(ti.int_value, glue_spec_size); |
| 342 |
} |
| 343 |
} |
| 344 |
rawset_sa_item(math_param_head, st.code, st.value); |
| 345 |
/*tex Do a trace message, if requested. */ |
| 346 |
if (tracing_restores_par > 1) { |
| 347 |
begin_diagnostic(); |
| 348 |
tprint("{restoring"); |
| 349 |
print_char(' '); |
| 350 |
print_cmd_chr(set_math_param_cmd, param_id); |
| 351 |
print_cmd_chr(math_style_cmd, style_id); |
| 352 |
print_char('='); |
| 353 |
print_int(get_math_param(param_id, style_id)); |
| 354 |
print_char('}'); |
| 355 |
end_diagnostic(false); |
| 356 |
} |
| 357 |
} |
| 358 |
(math_param_head->stack_ptr)--; |
| 359 |
} |
| 360 |
} |
| 361 |
|
| 362 |
/*tex Saving and unsaving of both: */ |
| 363 |
|
| 364 |
void unsave_math_data(int gl) |
| 365 |
{ |
| 366 |
unsave_math_fam_data(gl); |
| 367 |
unsave_math_param_data(gl); |
| 368 |
} |
| 369 |
|
| 370 |
/*tex Dumping and undumping: */ |
| 371 |
|
| 372 |
void dump_math_data(void) |
| 373 |
{ |
| 374 |
sa_tree_item sa_value = { 0 }; |
| 375 |
if (math_fam_head == NULL) { |
| 376 |
sa_value.int_value = MATHFONTDEFAULT; |
| 377 |
math_fam_head = new_sa_tree(MATHFONTSTACK, 1, sa_value); |
| 378 |
} |
| 379 |
dump_sa_tree(math_fam_head, "mathfonts"); |
| 380 |
if (math_param_head == NULL) { |
| 381 |
sa_value.int_value = MATHPARAMDEFAULT; |
| 382 |
math_param_head = new_sa_tree(MATHPARAMSTACK, 1, sa_value); |
| 383 |
} |
| 384 |
dump_sa_tree(math_param_head, "mathparameters"); |
| 385 |
} |
| 386 |
|
| 387 |
void undump_math_data(void) |
| 388 |
{ |
| 389 |
math_fam_head = undump_sa_tree("mathfonts"); |
| 390 |
math_param_head = undump_sa_tree("mathparameters"); |
| 391 |
} |
| 392 |
|
| 393 |
void initialize_math(void) |
| 394 |
{ |
| 395 |
sa_tree_item sa_value = { 0 }; |
| 396 |
if (math_fam_head == NULL) { |
| 397 |
sa_value.int_value = MATHFONTDEFAULT; |
| 398 |
math_fam_head = new_sa_tree(MATHFONTSTACK, 1, sa_value); |
| 399 |
} |
| 400 |
if (math_param_head == NULL) { |
| 401 |
sa_value.int_value = MATHPARAMDEFAULT; |
| 402 |
math_param_head = new_sa_tree(MATHPARAMSTACK, 1, sa_value); |
| 403 |
initialize_math_spacing(); |
| 404 |
} |
| 405 |
return; |
| 406 |
} |
| 407 |
|
| 408 |
/*tex |
| 409 |
|
| 410 |
Each portion of a formula is classified as Ord, Op, Bin, Rel, Ope, Clo, Pun, |
| 411 |
or Inn, for purposes of spacing and line breaking. An |ord_noad|, |op_noad|, |
| 412 |
|bin_noad|, |rel_noad|, |open_noad|, |close_noad|, |punct_noad|, or |
| 413 |
|inner_noad| is used to represent portions of the various types. For example, |
| 414 |
an `\.=' sign in a formula leads to the creation of a |rel_noad| whose |
| 415 |
|nucleus| field is a representation of an equals sign (usually |fam=0|, |
| 416 |
|character=075|). A formula preceded by \.{\\mathrel} also results in a |
| 417 |
|rel_noad|. When a |rel_noad| is followed by an |op_noad|, say, and possibly |
| 418 |
separated by one or more ordinary nodes (not noads), \TeX\ will insert a |
| 419 |
penalty node (with the current |rel_penalty|) just after the formula that |
| 420 |
corresponds to the |rel_noad|, unless there already was a penalty immediately |
| 421 |
following; and a ``thick space'' will be inserted just before the formula |
| 422 |
that corresponds to the |op_noad|. |
| 423 |
|
| 424 |
A noad of type |ord_noad|, |op_noad|, \dots, |inner_noad| usually has a |
| 425 |
|subtype=normal|. The only exception is that an |op_noad| might have |
| 426 |
|subtype=limits| or |no_limits|, if the normal positioning of limits has been |
| 427 |
overridden for this operator. |
| 428 |
|
| 429 |
A |radical_noad| also has a |left_delimiter| field, which usually represents |
| 430 |
a square root sign. |
| 431 |
|
| 432 |
A |fraction_noad| has a |right_delimiter| field as well as a |
| 433 |
|left_delimiter|. |
| 434 |
|
| 435 |
Delimiter fields have four subfields called |small_fam|, |small_char|, |
| 436 |
|large_fam|, |large_char|. These subfields represent variable-size delimiters |
| 437 |
by giving the ``small'' and ``large'' starting characters, as explained in |
| 438 |
Chapter~17 of {\sl The \TeX book}. |
| 439 |
|
| 440 |
A |fraction_noad| is actually quite different from all other noads. It has |
| 441 |
|thickness|, |denominator|, and |numerator| fields instead of |nucleus|, |
| 442 |
|subscr|, and |supscr|. The |thickness| is a scaled value that tells how |
| 443 |
thick to make a fraction rule; however, the special value |default_code| is |
| 444 |
used to stand for the |default_rule_thickness| of the current size. The |
| 445 |
|numerator| and |denominator| point to mlists that define a fraction; we |
| 446 |
always have $$\hbox{|type(numerator)=type(denominator)=sub_mlist|}.$$ The |
| 447 |
|left_delimiter| and |right_delimiter| fields specify delimiters that will be |
| 448 |
placed at the left and right of the fraction. In this way, a |fraction_noad| |
| 449 |
is able to represent all of \TeX's operators \.{\\over}, \.{\\atop}, |
| 450 |
\.{\\above}, \.{\\overwithdelims}, \.{\\atopwithdelims}, and |
| 451 |
\.{\\abovewithdelims}. |
| 452 |
|
| 453 |
The |new_noad| function creates an |ord_noad| that is completely null |
| 454 |
|
| 455 |
*/ |
| 456 |
|
| 457 |
pointer new_noad(void) |
| 458 |
{ |
| 459 |
pointer p; |
| 460 |
p = new_node(simple_noad, ord_noad_type); |
| 461 |
/*tex All noad fields are zero after this. */ |
| 462 |
return p; |
| 463 |
} |
| 464 |
|
| 465 |
pointer new_sub_box(pointer curbox) |
| 466 |
{ |
| 467 |
pointer p, q; |
| 468 |
p = new_noad(); |
| 469 |
q = new_node(sub_box_node, 0); |
| 470 |
nucleus(p) = q; |
| 471 |
math_list(nucleus(p)) = curbox; |
| 472 |
return p; |
| 473 |
} |
| 474 |
|
| 475 |
/*tex |
| 476 |
|
| 477 |
A few more kinds of noads will complete the set: An |under_noad| has its |
| 478 |
nucleus underlined; an |over_noad| has it overlined. An |accent_noad| places |
| 479 |
an accent over its nucleus; the accent character appears as |
| 480 |
|math_fam(accent_chr(p))| and |math_character(accent_chr(p))|. A |
| 481 |
|vcenter_noad| centers its nucleus vertically with respect to the axis of the |
| 482 |
formula; in such noads we always have |type(nucleus(p))=sub_box|. |
| 483 |
|
| 484 |
And finally, we have the |fence_noad| type, to implement \TeX's \.{\\left} |
| 485 |
and \.{\\right} as well as eTeX's \.{\\middle}. The |nucleus| of such noads |
| 486 |
is replaced by a |delimiter| field; thus, for example, `\.{\\left(}' produces |
| 487 |
a |fence_noad| such that |delimiter(p)| holds the family and character codes |
| 488 |
for all left parentheses. A |fence_noad| of subtype |left_noad_side| never |
| 489 |
appears in an mlist except as the first element, and a |fence_noad| with |
| 490 |
subtype |right_noad_side| never appears in an mlist except as the last |
| 491 |
element; furthermore, we either have both a |left_noad_side| and a |
| 492 |
|right_noad_side|, or neither one is present. |
| 493 |
|
| 494 |
Math formulas can also contain instructions like \.{\\textstyle} that |
| 495 |
override \TeX's normal style rules. A |style_node| is inserted into the data |
| 496 |
structure to record such instructions; it is three words long, so it is |
| 497 |
considered a node instead of a noad. The |subtype| is either |display_style| |
| 498 |
or |text_style| or |script_style| or |script_script_style|. The second and |
| 499 |
third words of a |style_node| are not used, but they are present because a |
| 500 |
|choice_node| is converted to a |style_node|. |
| 501 |
|
| 502 |
\TeX\ uses even numbers 0, 2, 4, 6 to encode the basic styles |
| 503 |
|display_style|, \dots, |script_script_style|, and adds~1 to get the |
| 504 |
``cramped'' versions of these styles. This gives a numerical order that is |
| 505 |
backwards from the convention of Appendix~G in {\sl The \TeX book\/}; i.e., a |
| 506 |
smaller style has a larger numerical value. |
| 507 |
|
| 508 |
*/ |
| 509 |
|
| 510 |
const char *math_style_names[] = { |
| 511 |
"display", "crampeddisplay", |
| 512 |
"text", "crampedtext", |
| 513 |
"script", "crampedscript", |
| 514 |
"scriptscript", "crampedscriptscript", |
| 515 |
NULL |
| 516 |
}; |
| 517 |
|
| 518 |
const char *math_param_names[] = { |
| 519 |
"quad", "axis", "operatorsize", |
| 520 |
"overbarkern", "overbarrule", "overbarvgap", |
| 521 |
"underbarkern", "underbarrule", "underbarvgap", |
| 522 |
"radicalkern", "radicalrule", "radicalvgap", |
| 523 |
"radicaldegreebefore", "radicaldegreeafter", "radicaldegreeraise", |
| 524 |
"stackvgap", "stacknumup", "stackdenomdown", |
| 525 |
"fractionrule", "fractionnumvgap", "fractionnumup", |
| 526 |
"fractiondenomvgap", "fractiondenomdown", "fractiondelsize", |
| 527 |
"skewedfractionhgap", "skewedfractionvgap", |
| 528 |
"limitabovevgap", "limitabovebgap", "limitabovekern", |
| 529 |
"limitbelowvgap", "limitbelowbgap", "limitbelowkern", |
| 530 |
"nolimitsubfactor", "nolimitsupfactor", /* bonus */ |
| 531 |
"underdelimitervgap", "underdelimiterbgap", |
| 532 |
"overdelimitervgap", "overdelimiterbgap", |
| 533 |
"subshiftdrop", "supshiftdrop", "subshiftdown", |
| 534 |
"subsupshiftdown", "subtopmax", "supshiftup", |
| 535 |
"supbottommin", "supsubbottommax", "subsupvgap", |
| 536 |
"spaceafterscript", "connectoroverlapmin", |
| 537 |
"ordordspacing", "ordopspacing", "ordbinspacing", "ordrelspacing", |
| 538 |
"ordopenspacing", "ordclosespacing", "ordpunctspacing", "ordinnerspacing", |
| 539 |
"opordspacing", "opopspacing", "opbinspacing", "oprelspacing", |
| 540 |
"opopenspacing", "opclosespacing", "oppunctspacing", "opinnerspacing", |
| 541 |
"binordspacing", "binopspacing", "binbinspacing", "binrelspacing", |
| 542 |
"binopenspacing", "binclosespacing", "binpunctspacing", "bininnerspacing", |
| 543 |
"relordspacing", "relopspacing", "relbinspacing", "relrelspacing", |
| 544 |
"relopenspacing", "relclosespacing", "relpunctspacing", "relinnerspacing", |
| 545 |
"openordspacing", "openopspacing", "openbinspacing", "openrelspacing", |
| 546 |
"openopenspacing", "openclosespacing", "openpunctspacing", |
| 547 |
"openinnerspacing", |
| 548 |
"closeordspacing", "closeopspacing", "closebinspacing", "closerelspacing", |
| 549 |
"closeopenspacing", "closeclosespacing", "closepunctspacing", |
| 550 |
"closeinnerspacing", |
| 551 |
"punctordspacing", "punctopspacing", "punctbinspacing", "punctrelspacing", |
| 552 |
"punctopenspacing", "punctclosespacing", "punctpunctspacing", |
| 553 |
"punctinnerspacing", |
| 554 |
"innerordspacing", "inneropspacing", "innerbinspacing", "innerrelspacing", |
| 555 |
"inneropenspacing", "innerclosespacing", "innerpunctspacing", |
| 556 |
"innerinnerspacing", |
| 557 |
NULL |
| 558 |
}; |
| 559 |
|
| 560 |
pointer new_style(small_number s) |
| 561 |
{ |
| 562 |
m_style = s; |
| 563 |
return new_node(style_node, s); |
| 564 |
} |
| 565 |
|
| 566 |
/*tex |
| 567 |
|
| 568 |
Finally, the \.{\\mathchoice} primitive creates a |choice_node|, which has |
| 569 |
special subfields |display_mlist|, |text_mlist|, |script_mlist|, and |
| 570 |
|script_script_mlist| pointing to the mlists for each style. |
| 571 |
|
| 572 |
*/ |
| 573 |
|
| 574 |
static pointer new_choice(void) |
| 575 |
{ |
| 576 |
return new_node(choice_node, 0); |
| 577 |
} |
| 578 |
|
| 579 |
/*tex |
| 580 |
|
| 581 |
Let's consider now the previously unwritten part of |show_node_list| that |
| 582 |
displays the things that can only be present in mlists; this program |
| 583 |
illustrates how to access the data structures just defined. |
| 584 |
|
| 585 |
In the context of the following program, |p| points to a node or noad that |
| 586 |
should be displayed, and the current string contains the ``recursion |
| 587 |
history'' that leads to this point. The recursion history consists of a dot |
| 588 |
for each outer level in which |p| is subsidiary to some node, or in which |p| |
| 589 |
is subsidiary to the |nucleus| field of some noad; the dot is replaced by |
| 590 |
`\.\_' or `\.\^' or `\./' or `\.\\' if |p| is descended from the |subscr| or |
| 591 |
|supscr| or |denominator| or |numerator| fields of noads. For example, the |
| 592 |
current string would be `\.{.\^.\_/}' if |p| points to the |ord_noad| for |x| |
| 593 |
in the (ridiculous) formula `\.{\$\\sqrt\{a\^\{\\mathinner\{b\_\{c\\over |
| 594 |
x+y\}\}\}\}\$}'. |
| 595 |
|
| 596 |
*/ |
| 597 |
|
| 598 |
void display_normal_noad(pointer p); |
| 599 |
void display_fence_noad(pointer p); |
| 600 |
void display_fraction_noad(pointer p); |
| 601 |
|
| 602 |
void show_math_node(pointer p) |
| 603 |
{ |
| 604 |
switch (type(p)) { |
| 605 |
case style_node: |
| 606 |
print_cmd_chr(math_style_cmd, subtype(p)); |
| 607 |
break; |
| 608 |
case choice_node: |
| 609 |
tprint_esc("mathchoice"); |
| 610 |
append_char('D'); |
| 611 |
show_node_list(display_mlist(p)); |
| 612 |
flush_char(); |
| 613 |
append_char('T'); |
| 614 |
show_node_list(text_mlist(p)); |
| 615 |
flush_char(); |
| 616 |
append_char('S'); |
| 617 |
show_node_list(script_mlist(p)); |
| 618 |
flush_char(); |
| 619 |
append_char('s'); |
| 620 |
show_node_list(script_script_mlist(p)); |
| 621 |
flush_char(); |
| 622 |
break; |
| 623 |
case simple_noad: |
| 624 |
case radical_noad: |
| 625 |
case accent_noad: |
| 626 |
display_normal_noad(p); |
| 627 |
break; |
| 628 |
case fence_noad: |
| 629 |
display_fence_noad(p); |
| 630 |
break; |
| 631 |
case fraction_noad: |
| 632 |
display_fraction_noad(p); |
| 633 |
break; |
| 634 |
default: |
| 635 |
tprint("Unknown node type!"); |
| 636 |
break; |
| 637 |
} |
| 638 |
} |
| 639 |
|
| 640 |
/*tex Here are some simple routines used in the display of noads. */ |
| 641 |
|
| 642 |
static void print_fam_and_char(pointer p) |
| 643 |
{ |
| 644 |
tprint_esc("fam"); |
| 645 |
print_int(math_fam(p)); |
| 646 |
print_char(' '); |
| 647 |
print(math_character(p)); |
| 648 |
} |
| 649 |
|
| 650 |
static void print_delimiter(pointer p) |
| 651 |
{ |
| 652 |
int a; |
| 653 |
if (delimiteroptionset(p)) { |
| 654 |
tprint(" [ "); |
| 655 |
if (delimiteraxis(p)) |
| 656 |
tprint("axis "); |
| 657 |
if (delimiternoaxis(p)) |
| 658 |
tprint("noaxis "); |
| 659 |
if (delimiterexact(p)) |
| 660 |
tprint("exact "); |
| 661 |
tprint("]"); |
| 662 |
} |
| 663 |
if (delimiterheight(p)) { |
| 664 |
tprint("height="); |
| 665 |
print_scaled(delimiterheight(p)); |
| 666 |
tprint(" "); |
| 667 |
} |
| 668 |
if (delimiterdepth(p)) { |
| 669 |
tprint("depth="); |
| 670 |
print_scaled(delimiterdepth(p)); |
| 671 |
tprint(" "); |
| 672 |
} |
| 673 |
if (delimiterclass(p)) { |
| 674 |
tprint("class="); |
| 675 |
print_int(delimiterclass(p)); |
| 676 |
tprint(" "); |
| 677 |
} |
| 678 |
if (small_fam(p) < 0) { |
| 679 |
/*tex This should never happen. */ |
| 680 |
print_int(-1); |
| 681 |
} else if (small_fam(p) < 16 && large_fam(p) < 16 && small_char(p) < 256 && large_char(p) < 256) { |
| 682 |
/*tex Traditional tex style. */ |
| 683 |
a = small_fam(p) * 256 + small_char(p); |
| 684 |
a = a * 0x1000 + large_fam(p) * 256 + large_char(p); |
| 685 |
print_qhex(a); |
| 686 |
} else if ((large_fam(p) == 0 && large_char(p) == 0) || small_char(p) > 65535 || large_char(p) > 65535) { |
| 687 |
/*tex \LUATEX\ style. */ |
| 688 |
print_qhex(small_fam(p)); |
| 689 |
print_qhex(small_char(p)); |
| 690 |
} |
| 691 |
} |
| 692 |
|
| 693 |
/*tex |
| 694 |
|
| 695 |
The next subroutine will descend to another level of recursion when a |
| 696 |
subsidiary mlist needs to be displayed. The parameter |c| indicates what |
| 697 |
character is to become part of the recursion history. An empty mlist is |
| 698 |
distinguished from a missing field, because these are not equivalent (as |
| 699 |
explained above). |
| 700 |
|
| 701 |
*/ |
| 702 |
|
| 703 |
static void print_subsidiary_data(pointer p, ASCII_code c) |
| 704 |
{ |
| 705 |
if ((int) cur_length >= depth_threshold) { |
| 706 |
if (p != null) |
| 707 |
tprint(" []"); |
| 708 |
} else { |
| 709 |
/*tex Include |c| in the recursion history. */ |
| 710 |
append_char(c); |
| 711 |
if (p != null) { |
| 712 |
switch (type(p)) { |
| 713 |
case math_char_node: |
| 714 |
print_ln(); |
| 715 |
print_current_string(); |
| 716 |
print_fam_and_char(p); |
| 717 |
break; |
| 718 |
case sub_box_node: |
| 719 |
show_node_list(math_list(p)); |
| 720 |
break; |
| 721 |
case sub_mlist_node: |
| 722 |
if (math_list(p) == null) { |
| 723 |
print_ln(); |
| 724 |
print_current_string(); |
| 725 |
tprint("{}"); |
| 726 |
} else { |
| 727 |
show_node_list(math_list(p)); |
| 728 |
} |
| 729 |
break; |
| 730 |
} |
| 731 |
} |
| 732 |
/*tex Remove |c| from the recursion history. */ |
| 733 |
flush_char(); |
| 734 |
} |
| 735 |
} |
| 736 |
|
| 737 |
void display_normal_noad(pointer p) |
| 738 |
{ |
| 739 |
switch (type(p)) { |
| 740 |
case simple_noad: |
| 741 |
switch (subtype(p)) { |
| 742 |
case ord_noad_type: |
| 743 |
tprint_esc("mathord"); |
| 744 |
break; |
| 745 |
case op_noad_type_normal: |
| 746 |
case op_noad_type_limits: |
| 747 |
case op_noad_type_no_limits: |
| 748 |
tprint_esc("mathop"); |
| 749 |
if (subtype(p) == op_noad_type_limits) |
| 750 |
tprint_esc("limits"); |
| 751 |
else if (subtype(p) == op_noad_type_no_limits) |
| 752 |
tprint_esc("nolimits"); |
| 753 |
break; |
| 754 |
case bin_noad_type: |
| 755 |
tprint_esc("mathbin"); |
| 756 |
break; |
| 757 |
case rel_noad_type: |
| 758 |
tprint_esc("mathrel"); |
| 759 |
break; |
| 760 |
case open_noad_type: |
| 761 |
tprint_esc("mathopen"); |
| 762 |
break; |
| 763 |
case close_noad_type: |
| 764 |
tprint_esc("mathclose"); |
| 765 |
break; |
| 766 |
case punct_noad_type: |
| 767 |
tprint_esc("mathpunct"); |
| 768 |
break; |
| 769 |
case inner_noad_type: |
| 770 |
tprint_esc("mathinner"); |
| 771 |
break; |
| 772 |
case over_noad_type: |
| 773 |
tprint_esc("overline"); |
| 774 |
break; |
| 775 |
case under_noad_type: |
| 776 |
tprint_esc("underline"); |
| 777 |
break; |
| 778 |
case vcenter_noad_type: |
| 779 |
tprint_esc("vcenter"); |
| 780 |
break; |
| 781 |
default: |
| 782 |
tprint("<unknown noad type!>"); |
| 783 |
break; |
| 784 |
} |
| 785 |
break; |
| 786 |
case radical_noad: |
| 787 |
if (subtype(p) == 7) |
| 788 |
tprint_esc("Uhextensible"); |
| 789 |
else if (subtype(p) == 6) |
| 790 |
tprint_esc("Udelimiterover"); |
| 791 |
else if (subtype(p) == 5) |
| 792 |
tprint_esc("Udelimiterunder"); |
| 793 |
else if (subtype(p) == 4) |
| 794 |
tprint_esc("Uoverdelimiter"); |
| 795 |
else if (subtype(p) == 3) |
| 796 |
tprint_esc("Uunderdelimiter"); |
| 797 |
else if (subtype(p) == 2) |
| 798 |
tprint_esc("Uroot"); |
| 799 |
else |
| 800 |
tprint_esc("radical"); |
| 801 |
print_delimiter(left_delimiter(p)); |
| 802 |
if (degree(p) != null) { |
| 803 |
print_subsidiary_data(degree(p), '/'); |
| 804 |
} |
| 805 |
if (radicalwidth(p)) { |
| 806 |
tprint("width="); |
| 807 |
print_scaled(radicalwidth(p)); |
| 808 |
tprint(" "); |
| 809 |
} |
| 810 |
if (radicaloptionset(p)) { |
| 811 |
tprint(" [ "); |
| 812 |
if (radicalexact(p)) |
| 813 |
tprint("exact "); |
| 814 |
if (radicalleft(p)) |
| 815 |
tprint("left "); |
| 816 |
if (radicalmiddle(p)) |
| 817 |
tprint("middle "); |
| 818 |
if (radicalright(p)) |
| 819 |
tprint("right "); |
| 820 |
tprint("]"); |
| 821 |
} |
| 822 |
break; |
| 823 |
case accent_noad: |
| 824 |
if (top_accent_chr(p) != null) { |
| 825 |
if (bot_accent_chr(p) != null) { |
| 826 |
tprint_esc("Umathaccent both"); |
| 827 |
} else { |
| 828 |
tprint_esc("Umathaccent"); |
| 829 |
} |
| 830 |
} else if (bot_accent_chr(p) != null) { |
| 831 |
tprint_esc("Umathaccent bottom"); |
| 832 |
} else { |
| 833 |
tprint_esc("Umathaccent overlay"); |
| 834 |
} |
| 835 |
if (accentfraction(p)) { |
| 836 |
tprint(" fraction="); |
| 837 |
print_int(accentfraction(p)); |
| 838 |
tprint(" "); |
| 839 |
} |
| 840 |
switch (subtype(p)) { |
| 841 |
case 0: |
| 842 |
if (top_accent_chr(p) != null) { |
| 843 |
if (bot_accent_chr(p) != null) { |
| 844 |
print_fam_and_char(top_accent_chr(p)); |
| 845 |
print_fam_and_char(bot_accent_chr(p)); |
| 846 |
} else { |
| 847 |
print_fam_and_char(top_accent_chr(p)); |
| 848 |
} |
| 849 |
} else if (bot_accent_chr(p) != null) { |
| 850 |
print_fam_and_char(bot_accent_chr(p)); |
| 851 |
} else { |
| 852 |
print_fam_and_char(overlay_accent_chr(p)); |
| 853 |
} |
| 854 |
break; |
| 855 |
case 1: |
| 856 |
if (top_accent_chr(p) != null) { |
| 857 |
tprint(" fixed "); |
| 858 |
print_fam_and_char(top_accent_chr(p)); |
| 859 |
if (bot_accent_chr(p) != null) { |
| 860 |
print_fam_and_char(bot_accent_chr(p)); |
| 861 |
} |
| 862 |
} else { |
| 863 |
confusion("display_accent_noad"); |
| 864 |
} |
| 865 |
break; |
| 866 |
case 2: |
| 867 |
if (bot_accent_chr(p) != null) { |
| 868 |
if (top_accent_chr(p) != null) { |
| 869 |
print_fam_and_char(top_accent_chr(p)); |
| 870 |
} |
| 871 |
tprint(" fixed "); |
| 872 |
print_fam_and_char(bot_accent_chr(p)); |
| 873 |
} else{ |
| 874 |
confusion("display_accent_noad"); |
| 875 |
} |
| 876 |
break; |
| 877 |
case 3: |
| 878 |
if (top_accent_chr(p) != null && bot_accent_chr(p) != null) { |
| 879 |
tprint(" fixed "); |
| 880 |
print_fam_and_char(top_accent_chr(p)); |
| 881 |
tprint(" fixed "); |
| 882 |
print_fam_and_char(bot_accent_chr(p)); |
| 883 |
} else { |
| 884 |
confusion("display_accent_noad"); |
| 885 |
} |
| 886 |
break; |
| 887 |
} |
| 888 |
break; |
| 889 |
} |
| 890 |
print_subsidiary_data(nucleus(p), '.'); |
| 891 |
print_subsidiary_data(supscr(p), '^'); |
| 892 |
print_subsidiary_data(subscr(p), '_'); |
| 893 |
} |
| 894 |
|
| 895 |
void display_fence_noad(pointer p) |
| 896 |
{ |
| 897 |
if (subtype(p) == right_noad_side) |
| 898 |
tprint_esc("right"); |
| 899 |
else if (subtype(p) == left_noad_side) |
| 900 |
tprint_esc("left"); |
| 901 |
else |
| 902 |
tprint_esc("middle"); |
| 903 |
print_delimiter(delimiter(p)); |
| 904 |
} |
| 905 |
|
| 906 |
void display_fraction_noad(pointer p) |
| 907 |
{ |
| 908 |
tprint_esc("fraction, thickness "); |
| 909 |
if (thickness(p) == default_code) |
| 910 |
tprint("= default"); |
| 911 |
else |
| 912 |
print_scaled(thickness(p)); |
| 913 |
if ((left_delimiter(p) != null) && |
| 914 |
((small_fam(left_delimiter(p)) != 0) || (small_char(left_delimiter(p)) != 0) || |
| 915 |
(large_fam(left_delimiter(p)) != 0) || (large_char(left_delimiter(p)) != 0))) { |
| 916 |
tprint(", left-delimiter "); |
| 917 |
print_delimiter(left_delimiter(p)); |
| 918 |
} |
| 919 |
if ((right_delimiter(p) != null) && |
| 920 |
((small_fam(right_delimiter(p)) != 0) || (small_char(right_delimiter(p)) != 0) || |
| 921 |
(large_fam(right_delimiter(p)) != 0) || (large_char(right_delimiter(p)) != 0))) { |
| 922 |
tprint(", right-delimiter "); |
| 923 |
print_delimiter(right_delimiter(p)); |
| 924 |
} |
| 925 |
print_subsidiary_data(numerator(p), '\\'); |
| 926 |
print_subsidiary_data(denominator(p), '/'); |
| 927 |
} |
| 928 |
|
| 929 |
/*tex |
| 930 |
|
| 931 |
The routines that \TeX\ uses to create mlists are similar to those we have |
| 932 |
just seen for the generation of hlists and vlists. But it is necessary to |
| 933 |
make ``noads'' as well as nodes, so the reader should review the discussion |
| 934 |
of math mode data structures before trying to make sense out of the following |
| 935 |
program. |
| 936 |
|
| 937 |
Here is a little routine that needs to be done whenever a subformula is about |
| 938 |
to be processed. The parameter is a code like |math_group|. |
| 939 |
|
| 940 |
*/ |
| 941 |
|
| 942 |
static void new_save_level_math(group_code c) |
| 943 |
{ |
| 944 |
set_saved_record(0, saved_textdir, 0, text_dir_ptr); |
| 945 |
text_dir_ptr = new_dir(math_direction_par); |
| 946 |
incr(save_ptr); |
| 947 |
new_save_level(c); |
| 948 |
eq_word_define(int_base + body_direction_code, math_direction_par); |
| 949 |
eq_word_define(int_base + par_direction_code, math_direction_par); |
| 950 |
eq_word_define(int_base + text_direction_code, math_direction_par); |
| 951 |
} |
| 952 |
|
| 953 |
static void push_math(group_code c, int mstyle) |
| 954 |
{ |
| 955 |
if (math_direction_par != text_direction_par) |
| 956 |
dir_math_save = true; |
| 957 |
push_nest(); |
| 958 |
mode = -mmode; |
| 959 |
incompleat_noad_par = null; |
| 960 |
m_style = mstyle; |
| 961 |
new_save_level_math(c); |
| 962 |
} |
| 963 |
|
| 964 |
static void enter_ordinary_math(void) |
| 965 |
{ |
| 966 |
push_math(math_shift_group, text_style); |
| 967 |
eq_word_define(int_base + cur_fam_code, -1); |
| 968 |
if (every_math_par != null) |
| 969 |
begin_token_list(every_math_par, every_math_text); |
| 970 |
} |
| 971 |
|
| 972 |
void enter_display_math(void); |
| 973 |
|
| 974 |
/*tex |
| 975 |
|
| 976 |
We get into math mode from horizontal mode when a `\.\$' (i.e., a |
| 977 |
|math_shift| character) is scanned. We must check to see whether this `\.\$' |
| 978 |
is immediately followed by another, in case display math mode is called for. |
| 979 |
|
| 980 |
*/ |
| 981 |
|
| 982 |
void init_math(void) |
| 983 |
{ |
| 984 |
if (cur_cmd == math_shift_cmd) { |
| 985 |
/*tex |get_x_token| would fail on \.{\\ifmmode}\thinspace! */ |
| 986 |
get_token(); |
| 987 |
if ((cur_cmd == math_shift_cmd) && (mode > 0)) { |
| 988 |
enter_display_math(); |
| 989 |
} else { |
| 990 |
back_input(); |
| 991 |
enter_ordinary_math(); |
| 992 |
} |
| 993 |
} else if (cur_cmd == math_shift_cs_cmd && cur_chr == display_style && (mode > 0)) { |
| 994 |
enter_display_math(); |
| 995 |
} else if (cur_cmd == math_shift_cs_cmd && cur_chr == text_style) { |
| 996 |
enter_ordinary_math(); |
| 997 |
} else { |
| 998 |
you_cant(); |
| 999 |
} |
| 1000 |
} |
| 1001 |
|
| 1002 |
/*tex |
| 1003 |
|
| 1004 |
We get into ordinary math mode from display math mode when `\.{\\eqno}' or |
| 1005 |
`\.{\\leqno}' appears. In such cases |cur_chr| will be 0 or~1, respectively; |
| 1006 |
the value of |cur_chr| is placed onto |save_stack| for safe keeping. |
| 1007 |
|
| 1008 |
When \TeX\ is in display math mode, |cur_group=math_shift_group|, so it is |
| 1009 |
not necessary for the |start_eq_no| procedure to test for this condition. |
| 1010 |
|
| 1011 |
*/ |
| 1012 |
|
| 1013 |
void start_eq_no(void) |
| 1014 |
{ |
| 1015 |
set_saved_record(0, saved_eqno, 0, cur_chr); |
| 1016 |
incr(save_ptr); |
| 1017 |
enter_ordinary_math(); |
| 1018 |
} |
| 1019 |
|
| 1020 |
/*tex |
| 1021 |
|
| 1022 |
Subformulas of math formulas cause a new level of math mode to be entered, on |
| 1023 |
the semantic nest as well as the save stack. These subformulas arise in |
| 1024 |
several ways: (1)~A left brace by itself indicates the beginning of a |
| 1025 |
subformula that will be put into a box, thereby freezing its glue and |
| 1026 |
preventing line breaks. (2)~A subscript or superscript is treated as a |
| 1027 |
subformula if it is not a single character; the same applies to the nucleus |
| 1028 |
of things like \.{\\underline}. (3)~The \.{\\left} primitive initiates a |
| 1029 |
subformula that will be terminated by a matching \.{\\right}. The group codes |
| 1030 |
placed on |save_stack| in these three cases are |math_group|, |math_group|, |
| 1031 |
and |math_left_group|, respectively. |
| 1032 |
|
| 1033 |
Here is the code that handles case (1); the other cases are not quite as |
| 1034 |
trivial, so we shall consider them later. |
| 1035 |
|
| 1036 |
*/ |
| 1037 |
|
| 1038 |
void math_left_brace(void) |
| 1039 |
{ |
| 1040 |
pointer q; |
| 1041 |
tail_append(new_noad()); |
| 1042 |
q = new_node(math_char_node, 0); |
| 1043 |
nucleus(tail) = q; |
| 1044 |
back_input(); |
| 1045 |
(void) scan_math(nucleus(tail), m_style); |
| 1046 |
} |
| 1047 |
|
| 1048 |
/*tex |
| 1049 |
|
| 1050 |
If the inline directions of \.{\\pardir} and \.{\\mathdir} are opposite, then |
| 1051 |
this function will return true. Discovering that fact is somewhat odd because |
| 1052 |
it needs traversal of the |save_stack|. The occurance of displayed equations |
| 1053 |
is weird enough that this is probably still better than having yet another |
| 1054 |
field in the |input_stack| structures. |
| 1055 |
|
| 1056 |
None of this makes much sense if the inline direction of either one of |
| 1057 |
\.{\\pardir} or \.{\\mathdir} is vertical, but in that case the current math |
| 1058 |
machinery is ill suited anyway so I do not bother to test that. |
| 1059 |
|
| 1060 |
*/ |
| 1061 |
|
| 1062 |
static boolean math_and_text_reversed_p(void) |
| 1063 |
{ |
| 1064 |
int i = save_ptr - 1; |
| 1065 |
while (save_type(i) != level_boundary) |
| 1066 |
i--; |
| 1067 |
while (i < save_ptr) { |
| 1068 |
if (save_type(i) == restore_old_value && |
| 1069 |
save_value(i) == int_base + par_direction_code) { |
| 1070 |
if (textdir_opposite(math_direction_par, save_value(i - 1))) |
| 1071 |
return true; |
| 1072 |
} |
| 1073 |
i++; |
| 1074 |
} |
| 1075 |
return false; |
| 1076 |
} |
| 1077 |
|
| 1078 |
/*tex |
| 1079 |
|
| 1080 |
When we enter display math mode, we need to call |line_break| to process the |
| 1081 |
partial paragraph that has just been interrupted by the display. Then we can |
| 1082 |
set the proper values of |display_width| and |display_indent| and |
| 1083 |
|pre_display_size|. |
| 1084 |
|
| 1085 |
*/ |
| 1086 |
|
| 1087 |
static int only_dirs(halfword n) |
| 1088 |
{ |
| 1089 |
while (n) { |
| 1090 |
if (type(n) == local_par_node || type(n) == dir_node) { |
| 1091 |
n = vlink(n); |
| 1092 |
} else { |
| 1093 |
return 0; |
| 1094 |
} |
| 1095 |
} |
| 1096 |
return 1; |
| 1097 |
} |
| 1098 |
|
| 1099 |
void enter_display_math(void) |
| 1100 |
{ |
| 1101 |
/*tex new or partial |pre_display_size| */ |
| 1102 |
scaled w; |
| 1103 |
/*tex new |display_width| */ |
| 1104 |
scaled l; |
| 1105 |
/*tex new |display_indent| */ |
| 1106 |
scaled s; |
| 1107 |
pointer p; |
| 1108 |
/*tex scope of paragraph shape specification */ |
| 1109 |
int n; |
| 1110 |
/*tex |
| 1111 |
|
| 1112 |
`\.{\\noindent\$\$}' or `\.{\$\${ }\$\$}' or the 2nd of \.{\$\${ }\$\$} |
| 1113 |
\.{\$\${ }\$\$} |
| 1114 |
|
| 1115 |
*/ |
| 1116 |
if (head == tail) { |
| 1117 |
pop_nest(); |
| 1118 |
w = -max_dimen; |
| 1119 |
} else if ((vlink(head) == tail && |
| 1120 |
type(tail) == local_par_node && vlink(tail) == null) || |
| 1121 |
(math_empty_display_mode_par == 1 && only_dirs(vlink(head)))) { |
| 1122 |
/*tex |
| 1123 |
|
| 1124 |
We ignore |null| paragraphs, that is those that only have a local par node, |
| 1125 |
for example because |resume_after_display| inserts a |local_par_node|. |
| 1126 |
If |\mathdisplayemptymode=1| paragraphs caontaining only local par node |
| 1127 |
and possibly a few dir nodes are ignored as well. |
| 1128 |
|
| 1129 |
*/ |
| 1130 |
flush_node_list(vlink(head)); |
| 1131 |
pop_nest(); |
| 1132 |
w = -max_dimen; |
| 1133 |
} else { |
| 1134 |
line_break(true, math_shift_group); |
| 1135 |
w = actual_box_width(just_box, x_over_n(quad(get_cur_font()),1000) * math_pre_display_gap_factor_par); |
| 1136 |
} |
| 1137 |
/*tex |
| 1138 |
|
| 1139 |
Now we are in vertical mode, working on the list that will contain the |
| 1140 |
display. A displayed equation is considered to be three lines long, so we |
| 1141 |
calculate the length and offset of line number |prev_graf+2|. |
| 1142 |
|
| 1143 |
*/ |
| 1144 |
if (par_shape_par_ptr == null) { |
| 1145 |
if ((hang_indent_par != 0) && (((hang_after_par >= 0) && (prev_graf_par + 2 > hang_after_par)) || (prev_graf_par + 1 < -hang_after_par))) { |
| 1146 |
halfword used_hang_indent = swap_hang_indent(hang_indent_par); |
| 1147 |
l = hsize_par - abs(used_hang_indent); |
| 1148 |
if (used_hang_indent > 0) |
| 1149 |
s = used_hang_indent; |
| 1150 |
else |
| 1151 |
s = 0; |
| 1152 |
} else { |
| 1153 |
l = hsize_par; |
| 1154 |
s = 0; |
| 1155 |
} |
| 1156 |
} else { |
| 1157 |
n = vinfo(par_shape_par_ptr + 1); |
| 1158 |
if (prev_graf_par + 2 >= n) |
| 1159 |
p = par_shape_par_ptr + 2 * n + 1; |
| 1160 |
else |
| 1161 |
p = par_shape_par_ptr + 2 * (prev_graf_par + 2) + 1; |
| 1162 |
s = varmem[(p - 1)].cint; |
| 1163 |
l = varmem[p].cint; |
| 1164 |
s = swap_parshape_indent(s,l); |
| 1165 |
} |
| 1166 |
push_math(math_shift_group, display_style); |
| 1167 |
mode = mmode; |
| 1168 |
eq_word_define(int_base + cur_fam_code, -1); |
| 1169 |
eq_word_define(dimen_base + pre_display_size_code, w); |
| 1170 |
eq_word_define(dimen_base + display_width_code, l); |
| 1171 |
eq_word_define(dimen_base + display_indent_code, s); |
| 1172 |
eq_word_define(int_base + pre_display_direction_code, (math_and_text_reversed_p() ? -1 : 0)); |
| 1173 |
if (every_display_par != null) |
| 1174 |
begin_token_list(every_display_par, every_display_text); |
| 1175 |
if (nest_ptr == 1) { |
| 1176 |
checked_page_filter(before_display); |
| 1177 |
build_page(); |
| 1178 |
} |
| 1179 |
} |
| 1180 |
|
| 1181 |
/*tex |
| 1182 |
|
| 1183 |
The next routine parses all variations of a delimiter code. The |extcode| |
| 1184 |
tells what syntax form to use (\TeX, XeTeX, XeTeXnum, ...) , the |doclass| |
| 1185 |
tells whether or not read a math class also (for \.{\\delimiter} c.s.). (the |
| 1186 |
class is passed on for conversion to \.{\\mathchar}). |
| 1187 |
|
| 1188 |
*/ |
| 1189 |
|
| 1190 |
static delcodeval do_scan_extdef_del_code(int extcode, boolean doclass) |
| 1191 |
{ |
| 1192 |
const char *hlp[] = { |
| 1193 |
"I'm going to use 0 instead of that illegal code value.", |
| 1194 |
NULL |
| 1195 |
}; |
| 1196 |
delcodeval d; |
| 1197 |
int mcls = 0, msfam = 0, mschr = 0, mlfam = 0, mlchr = 0; |
| 1198 |
if (extcode == tex_mathcode) { |
| 1199 |
/*tex |
| 1200 |
|
| 1201 |
\.{\\delcode}, this is the easiest |
| 1202 |
|
| 1203 |
*/ |
| 1204 |
scan_int(); |
| 1205 |
/*tex "MFCCFCC or "FCCFCC */ |
| 1206 |
if (doclass) { |
| 1207 |
mcls = (cur_val / 0x1000000); |
| 1208 |
cur_val = (cur_val & 0xFFFFFF); |
| 1209 |
} |
| 1210 |
if (cur_val > 0xFFFFFF) { |
| 1211 |
tex_error("Invalid delimiter code", hlp); |
| 1212 |
cur_val = 0; |
| 1213 |
} |
| 1214 |
msfam = (cur_val / 0x100000); |
| 1215 |
mschr = (cur_val % 0x100000) / 0x1000; |
| 1216 |
mlfam = (cur_val & 0xFFF) / 0x100; |
| 1217 |
mlchr = (cur_val % 0x100); |
| 1218 |
} else if (extcode == umath_mathcode) { |
| 1219 |
/*tex |
| 1220 |
|
| 1221 |
\.{\\Udelcode}: <0-7>,<0-0xFF>,<0-0x10FFFF> or <0-0xFF>,<0-0x10FFFF> |
| 1222 |
|
| 1223 |
*/ |
| 1224 |
if (doclass) { |
| 1225 |
scan_int(); |
| 1226 |
mcls = cur_val; |
| 1227 |
} |
| 1228 |
scan_int(); |
| 1229 |
msfam = cur_val; |
| 1230 |
scan_char_num(); |
| 1231 |
mschr = cur_val; |
| 1232 |
if (msfam < 0 || msfam > 255) { |
| 1233 |
tex_error("Invalid delimiter code", hlp); |
| 1234 |
msfam = 0; |
| 1235 |
mschr = 0; |
| 1236 |
} |
| 1237 |
mlfam = 0; |
| 1238 |
mlchr = 0; |
| 1239 |
} else if (extcode == umathnum_mathcode) { |
| 1240 |
/*tex |
| 1241 |
|
| 1242 |
\.{\\Udelcodenum}:"FF<21bits>; the largest numeric value is $2^29-1$, |
| 1243 |
but the top of bit 21 can't be used as it contains invalid USV's. |
| 1244 |
|
| 1245 |
*/ |
| 1246 |
if (doclass) { /* such a primitive doesn't exist */ |
| 1247 |
confusion("umathnum_mathcode"); |
| 1248 |
} |
| 1249 |
scan_int(); |
| 1250 |
msfam = (cur_val / 0x200000); |
| 1251 |
mschr = cur_val & 0x1FFFFF; |
| 1252 |
if (msfam < 0 || msfam > 255 || mschr > 0x10FFFF) { |
| 1253 |
tex_error("Invalid delimiter code", hlp); |
| 1254 |
msfam = 0; |
| 1255 |
mschr = 0; |
| 1256 |
} |
| 1257 |
mlfam = 0; |
| 1258 |
mlchr = 0; |
| 1259 |
} else { |
| 1260 |
/*tex Something's gone wrong! */ |
| 1261 |
confusion("unknown_extcode"); |
| 1262 |
} |
| 1263 |
d.class_value = mcls; |
| 1264 |
d.small_family_value = msfam; |
| 1265 |
d.small_character_value = mschr; |
| 1266 |
d.large_family_value = mlfam; |
| 1267 |
d.large_character_value = mlchr; |
| 1268 |
return d; |
| 1269 |
} |
| 1270 |
|
| 1271 |
void scan_extdef_del_code(int level, int extcode) |
| 1272 |
{ |
| 1273 |
delcodeval d; |
| 1274 |
int p; |
| 1275 |
scan_char_num(); |
| 1276 |
p = cur_val; |
| 1277 |
scan_optional_equals(); |
| 1278 |
d = do_scan_extdef_del_code(extcode, false); |
| 1279 |
set_del_code(p, d.small_family_value, d.small_character_value, |
| 1280 |
d.large_family_value, d.large_character_value, (quarterword) (level)); |
| 1281 |
} |
| 1282 |
|
| 1283 |
mathcodeval scan_mathchar(int extcode) |
| 1284 |
{ |
| 1285 |
char errstr[255] = { 0 }; |
| 1286 |
const char *hlp[] = { |
| 1287 |
"I'm going to use 0 instead of that illegal code value.", |
| 1288 |
NULL |
| 1289 |
}; |
| 1290 |
mathcodeval d; |
| 1291 |
int mcls = 0, mfam = 0, mchr = 0; |
| 1292 |
if (extcode == tex_mathcode) { |
| 1293 |
/*tex \.{\\mathcode}: "TFCC */ |
| 1294 |
scan_int(); |
| 1295 |
if (cur_val > 0x8000) { |
| 1296 |
/*tex Needed for latex: fallback to umathnum_mathcode. */ |
| 1297 |
mfam = (cur_val / 0x200000) & 0x7FF; |
| 1298 |
mcls = mfam % 0x08; |
| 1299 |
mfam = mfam / 0x08; |
| 1300 |
mchr = cur_val & 0x1FFFFF; |
| 1301 |
if (mchr > 0x10FFFF) { |
| 1302 |
tex_error("Invalid math code during > 0x8000 mathcode fallback", hlp); |
| 1303 |
mcls = 0; |
| 1304 |
mfam = 0; |
| 1305 |
mchr = 0; |
| 1306 |
} |
| 1307 |
} else { |
| 1308 |
if (cur_val < 0) { |
| 1309 |
snprintf(errstr, 255, "Bad mathchar (%d)", (int)cur_val); |
| 1310 |
tex_error(errstr, hlp); |
| 1311 |
cur_val = 0; |
| 1312 |
} |
| 1313 |
mcls = (cur_val / 0x1000); |
| 1314 |
mfam = ((cur_val % 0x1000) / 0x100); |
| 1315 |
mchr = (cur_val % 0x100); |
| 1316 |
} |
| 1317 |
} else if (extcode == umath_mathcode) { |
| 1318 |
/*tex <0-0x7> <0-0xFF> <0-0x10FFFF> */ |
| 1319 |
scan_int(); |
| 1320 |
mcls = cur_val; |
| 1321 |
scan_int(); |
| 1322 |
mfam = cur_val; |
| 1323 |
scan_char_num(); |
| 1324 |
mchr = cur_val; |
| 1325 |
if (mcls < 0 || mcls > 7 || mfam > 255) { |
| 1326 |
tex_error("Invalid math code", hlp); |
| 1327 |
mchr = 0; |
| 1328 |
mfam = 0; |
| 1329 |
mcls = 0; |
| 1330 |
} |
| 1331 |
} else if (extcode == umathnum_mathcode) { |
| 1332 |
/*tex |
| 1333 |
|
| 1334 |
"FFT<21bits>: the largest numeric value is $2^32-1$, but the top of |
| 1335 |
bit 21 can't be used as it contains invalid USV's |
| 1336 |
|
| 1337 |
Note: |scan_int| won't accept families 128-255 because these use bit 32 |
| 1338 |
|
| 1339 |
*/ |
| 1340 |
scan_int(); |
| 1341 |
mfam = (cur_val / 0x200000) & 0x7FF; |
| 1342 |
mcls = mfam % 0x08; |
| 1343 |
mfam = mfam / 0x08; |
| 1344 |
mchr = cur_val & 0x1FFFFF; |
| 1345 |
if (mchr > 0x10FFFF) { |
| 1346 |
tex_error("Invalid math code", hlp); |
| 1347 |
mcls = 0; |
| 1348 |
mfam = 0; |
| 1349 |
mchr = 0; |
| 1350 |
} |
| 1351 |
} else { |
| 1352 |
/*tex Something's gone wrong. */ |
| 1353 |
confusion("unknown_extcode"); |
| 1354 |
} |
| 1355 |
d.class_value = mcls; |
| 1356 |
d.family_value = mfam; |
| 1357 |
d.character_value = mchr; |
| 1358 |
return d; |
| 1359 |
} |
| 1360 |
|
| 1361 |
void scan_extdef_math_code(int level, int extcode) |
| 1362 |
{ |
| 1363 |
mathcodeval d; |
| 1364 |
int p; |
| 1365 |
scan_char_num(); |
| 1366 |
p = cur_val; |
| 1367 |
scan_optional_equals(); |
| 1368 |
d = scan_mathchar(extcode); |
| 1369 |
set_math_code(p, d.class_value, d.family_value, d.character_value, (quarterword) (level)); |
| 1370 |
} |
| 1371 |
|
| 1372 |
/*tex This reads in a delcode when actually a mathcode is needed. */ |
| 1373 |
|
| 1374 |
mathcodeval scan_delimiter_as_mathchar(int extcode) |
| 1375 |
{ |
| 1376 |
delcodeval dval; |
| 1377 |
mathcodeval mval; |
| 1378 |
dval = do_scan_extdef_del_code(extcode, true); |
| 1379 |
mval.class_value = dval.class_value; |
| 1380 |
mval.family_value = dval.small_family_value; |
| 1381 |
mval.character_value = dval.small_character_value; |
| 1382 |
return mval; |
| 1383 |
} |
| 1384 |
|
| 1385 |
/*tex |
| 1386 |
|
| 1387 |
Recall that the |nucleus|, |subscr|, and |supscr| fields in a noad are broken |
| 1388 |
down into subfields called |type| and either |math_list| or |
| 1389 |
|(math_fam,math_character)|. The job of |scan_math| is to figure out what to |
| 1390 |
place in one of these principal fields; it looks at the subformula that comes |
| 1391 |
next in the input, and places an encoding of that subformula into a given |
| 1392 |
word of |mem|. |
| 1393 |
|
| 1394 |
*/ |
| 1395 |
|
| 1396 |
#define get_next_nb_nr() do { get_x_token(); } while (cur_cmd==spacer_cmd||cur_cmd==relax_cmd) |
| 1397 |
|
| 1398 |
int scan_math_style(pointer p, int mstyle) |
| 1399 |
{ |
| 1400 |
get_next_nb_nr(); |
| 1401 |
back_input(); |
| 1402 |
scan_left_brace(); |
| 1403 |
set_saved_record(0, saved_math, 0, p); |
| 1404 |
incr(save_ptr); |
| 1405 |
push_math(math_group, mstyle); |
| 1406 |
return 1; |
| 1407 |
} |
| 1408 |
|
| 1409 |
int scan_math(pointer p, int mstyle) |
| 1410 |
{ |
| 1411 |
mathcodeval mval = { 0, 0, 0 }; |
| 1412 |
assert(p != null); |
| 1413 |
RESTART: |
| 1414 |
get_next_nb_nr(); |
| 1415 |
RESWITCH: |
| 1416 |
switch (cur_cmd) { |
| 1417 |
case letter_cmd: |
| 1418 |
case other_char_cmd: |
| 1419 |
case char_given_cmd: |
| 1420 |
mval = get_math_code(cur_chr); |
| 1421 |
if (mval.class_value == 8) { |
| 1422 |
/*tex An active character that is an |outer_call| is allowed here. */ |
| 1423 |
cur_cs = active_to_cs(cur_chr, true); |
| 1424 |
cur_cmd = eq_type(cur_cs); |
| 1425 |
cur_chr = equiv(cur_cs); |
| 1426 |
x_token(); |
| 1427 |
back_input(); |
| 1428 |
goto RESTART; |
| 1429 |
} |
| 1430 |
break; |
| 1431 |
case char_num_cmd: |
| 1432 |
scan_char_num(); |
| 1433 |
cur_chr = cur_val; |
| 1434 |
cur_cmd = char_given_cmd; |
| 1435 |
goto RESWITCH; |
| 1436 |
break; |
| 1437 |
case math_char_num_cmd: |
| 1438 |
if (cur_chr == 0) |
| 1439 |
mval = scan_mathchar(tex_mathcode); |
| 1440 |
else if (cur_chr == 1) |
| 1441 |
mval = scan_mathchar(umath_mathcode); |
| 1442 |
else if (cur_chr == 2) |
| 1443 |
mval = scan_mathchar(umathnum_mathcode); |
| 1444 |
else |
| 1445 |
confusion("scan_math"); |
| 1446 |
break; |
| 1447 |
case math_given_cmd: |
| 1448 |
mval = mathchar_from_integer(cur_chr, tex_mathcode); |
| 1449 |
break; |
| 1450 |
case xmath_given_cmd: |
| 1451 |
mval = mathchar_from_integer(cur_chr, umath_mathcode); |
| 1452 |
break; |
| 1453 |
case delim_num_cmd: |
| 1454 |
if (cur_chr == 0) |
| 1455 |
mval = scan_delimiter_as_mathchar(tex_mathcode); |
| 1456 |
else if (cur_chr == 1) |
| 1457 |
mval = scan_delimiter_as_mathchar(umath_mathcode); |
| 1458 |
else |
| 1459 |
confusion("scan_math"); |
| 1460 |
break; |
| 1461 |
default: |
| 1462 |
/*tex |
| 1463 |
|
| 1464 |
The pointer |p| is placed on |save_stack| while a complex subformula |
| 1465 |
is being scanned. |
| 1466 |
|
| 1467 |
*/ |
| 1468 |
back_input(); |
| 1469 |
scan_left_brace(); |
| 1470 |
set_saved_record(0, saved_math, 0, p); |
| 1471 |
incr(save_ptr); |
| 1472 |
push_math(math_group, mstyle); |
| 1473 |
return 1; |
| 1474 |
} |
| 1475 |
type(p) = math_char_node; |
| 1476 |
math_character(p) = mval.character_value; |
| 1477 |
if ((mval.class_value == math_use_current_family_code) && cur_fam_par_in_range) |
| 1478 |
math_fam(p) = cur_fam_par; |
| 1479 |
else if ((mval.family_value == var_fam_par) && var_fam_par_in_range) |
| 1480 |
math_fam(p) = cur_fam_par; |
| 1481 |
else |
| 1482 |
math_fam(p) = mval.family_value; |
| 1483 |
return 0; |
| 1484 |
} |
| 1485 |
|
| 1486 |
/*tex |
| 1487 |
|
| 1488 |
The |set_math_char| procedure creates a new noad appropriate to a given math |
| 1489 |
code, and appends it to the current mlist. However, if the math code is |
| 1490 |
sufficiently large, the |cur_chr| is treated as an active character and |
| 1491 |
nothing is appended. |
| 1492 |
|
| 1493 |
*/ |
| 1494 |
|
| 1495 |
#define math_class_to_type(target,source) \ |
| 1496 |
switch (source) { \ |
| 1497 |
case 0: target = ord_noad_type; break; \ |
| 1498 |
case 1: target = op_noad_type_normal; break; \ |
| 1499 |
case 2: target = bin_noad_type; break; \ |
| 1500 |
case 3: target = rel_noad_type; break; \ |
| 1501 |
case 4: target = open_noad_type; break; \ |
| 1502 |
case 5: target = close_noad_type; break; \ |
| 1503 |
case 6: target = punct_noad_type; break; \ |
| 1504 |
} |
| 1505 |
|
| 1506 |
void set_math_char(mathcodeval mval) |
| 1507 |
{ |
| 1508 |
pointer p; |
| 1509 |
if (mval.class_value == 8) { |
| 1510 |
/*tex An active character that is an |outer_call| is allowed here */ |
| 1511 |
cur_cs = active_to_cs(cur_chr, true); |
| 1512 |
cur_cmd = eq_type(cur_cs); |
| 1513 |
cur_chr = equiv(cur_cs); |
| 1514 |
x_token(); |
| 1515 |
back_input(); |
| 1516 |
} else { |
| 1517 |
pointer q; |
| 1518 |
p = new_noad(); |
| 1519 |
q = new_node(math_char_node, 0); |
| 1520 |
nucleus(p) = q; |
| 1521 |
math_character(nucleus(p)) = mval.character_value; |
| 1522 |
math_fam(nucleus(p)) = mval.family_value; |
| 1523 |
if (mval.class_value == math_use_current_family_code) { |
| 1524 |
if (cur_fam_par_in_range) |
| 1525 |
math_fam(nucleus(p)) = cur_fam_par; |
| 1526 |
subtype(p) = ord_noad_type; |
| 1527 |
} else if ((mval.family_value == var_fam_par) && var_fam_par_in_range) { |
| 1528 |
if (cur_fam_par_in_range) |
| 1529 |
math_fam(nucleus(p)) = cur_fam_par; |
| 1530 |
} else { |
| 1531 |
math_class_to_type(subtype(p),mval.class_value); |
| 1532 |
} |
| 1533 |
tail_append(p); |
| 1534 |
} |
| 1535 |
} |
| 1536 |
|
| 1537 |
/*tex |
| 1538 |
|
| 1539 |
The |math_char_in_text| procedure creates a new node representing a math char |
| 1540 |
in text code, and appends it to the current list. However, if the math code |
| 1541 |
is sufficiently large, the |cur_chr| is treated as an active character and |
| 1542 |
nothing is appended. |
| 1543 |
|
| 1544 |
*/ |
| 1545 |
|
| 1546 |
void math_char_in_text(mathcodeval mval) |
| 1547 |
{ |
| 1548 |
pointer p; |
| 1549 |
if (mval.class_value == 8) { |
| 1550 |
/*tex An active character that is an |outer_call| is allowed here */ |
| 1551 |
cur_cs = active_to_cs(cur_chr, true); |
| 1552 |
cur_cmd = eq_type(cur_cs); |
| 1553 |
cur_chr = equiv(cur_cs); |
| 1554 |
x_token(); |
| 1555 |
back_input(); |
| 1556 |
} else { |
| 1557 |
p = new_char(fam_fnt(mval.family_value, text_size), mval.character_value); |
| 1558 |
tail_append(p); |
| 1559 |
} |
| 1560 |
} |
| 1561 |
|
| 1562 |
void math_math_comp(void) |
| 1563 |
{ |
| 1564 |
pointer q; |
| 1565 |
tail_append(new_noad()); |
| 1566 |
subtype(tail) = (quarterword) cur_chr; |
| 1567 |
q = new_node(math_char_node, 0); |
| 1568 |
nucleus(tail) = q; |
| 1569 |
if (cur_chr == over_noad_type) |
| 1570 |
(void) scan_math(nucleus(tail), cramped_style(m_style)); |
| 1571 |
else |
| 1572 |
(void) scan_math(nucleus(tail), m_style); |
| 1573 |
} |
| 1574 |
|
| 1575 |
void math_limit_switch(void) |
| 1576 |
{ |
| 1577 |
const char *hlp[] = { |
| 1578 |
"I'm ignoring this misplaced \\limits or \\nolimits command.", |
| 1579 |
NULL |
| 1580 |
}; |
| 1581 |
if (head != tail) { |
| 1582 |
if (type(tail) == simple_noad && |
| 1583 |
(subtype(tail) == op_noad_type_normal || |
| 1584 |
subtype(tail) == op_noad_type_limits || |
| 1585 |
subtype(tail) == op_noad_type_no_limits)) { |
| 1586 |
subtype(tail) = (quarterword) cur_chr; |
| 1587 |
return; |
| 1588 |
} |
| 1589 |
} |
| 1590 |
tex_error("Limit controls must follow a math operator", hlp); |
| 1591 |
} |
| 1592 |
|
| 1593 |
/*tex |
| 1594 |
|
| 1595 |
Delimiter fields of noads are filled in by the |scan_delimiter| routine. The |
| 1596 |
first parameter of this procedure is the |mem| address where the delimiter |
| 1597 |
is to be placed; the second tells if this delimiter follows \.{\\radical} or |
| 1598 |
not. |
| 1599 |
|
| 1600 |
*/ |
| 1601 |
|
| 1602 |
static void scan_delimiter(pointer p, int r) |
| 1603 |
{ |
| 1604 |
delcodeval dval = { 0, 0, 0, 0, 0 }; |
| 1605 |
if (r == tex_mathcode) { |
| 1606 |
/*tex \.{\\radical} */ |
| 1607 |
dval = do_scan_extdef_del_code(tex_mathcode, true); |
| 1608 |
} else if (r == umath_mathcode) { |
| 1609 |
/*tex \.{\\Uradical} */ |
| 1610 |
dval = do_scan_extdef_del_code(umath_mathcode, false); |
| 1611 |
} else if (r == no_mathcode) { |
| 1612 |
get_next_nb_nr(); |
| 1613 |
switch (cur_cmd) { |
| 1614 |
case letter_cmd: |
| 1615 |
case other_char_cmd: |
| 1616 |
dval = get_del_code(cur_chr); |
| 1617 |
break; |
| 1618 |
case delim_num_cmd: |
| 1619 |
if (cur_chr == 0) { |
| 1620 |
/*tex \.{\\delimiter} */ |
| 1621 |
dval = do_scan_extdef_del_code(tex_mathcode, true); |
| 1622 |
} else if (cur_chr == 1) { |
| 1623 |
/*tex \.{\\Udelimiter} */ |
| 1624 |
dval = do_scan_extdef_del_code(umath_mathcode, true); |
| 1625 |
} else { |
| 1626 |
confusion("scan_delimiter1"); |
| 1627 |
} |
| 1628 |
break; |
| 1629 |
default: |
| 1630 |
dval.small_family_value = -1; |
| 1631 |
break; |
| 1632 |
} |
| 1633 |
} else { |
| 1634 |
confusion("scan_delimiter2"); |
| 1635 |
} |
| 1636 |
if (p == null) |
| 1637 |
return; |
| 1638 |
if (dval.small_family_value < 0) { |
| 1639 |
const char *hlp[] = { |
| 1640 |
"I was expecting to see something like `(' or `\\{' or", |
| 1641 |
"`\\}' here. If you typed, e.g., `{' instead of `\\{', you", |
| 1642 |
"should probably delete the `{' by typing `1' now, so that", |
| 1643 |
"braces don't get unbalanced. Otherwise just proceed", |
| 1644 |
"Acceptable delimiters are characters whose \\delcode is", |
| 1645 |
"nonnegative, or you can use `\\delimiter <delimiter code>'.", |
| 1646 |
NULL |
| 1647 |
}; |
| 1648 |
back_error("Missing delimiter (. inserted)", hlp); |
| 1649 |
small_fam(p) = 0; |
| 1650 |
small_char(p) = 0; |
| 1651 |
large_fam(p) = 0; |
| 1652 |
large_char(p) = 0; |
| 1653 |
} else { |
| 1654 |
small_fam(p) = dval.small_family_value; |
| 1655 |
small_char(p) = dval.small_character_value; |
| 1656 |
large_fam(p) = dval.large_family_value; |
| 1657 |
large_char(p) = dval.large_character_value; |
| 1658 |
} |
| 1659 |
return; |
| 1660 |
} |
| 1661 |
|
| 1662 |
/*tex |
| 1663 |
Because \LATEX\ expect some different defaults, we now have |math_defaults_mode_par| controlling |
| 1664 |
the style. By using a variable we can remain downward compatible. |
| 1665 |
*/ |
| 1666 |
|
| 1667 |
void math_radical(void) |
| 1668 |
{ |
| 1669 |
halfword q; |
| 1670 |
int chr_code = cur_chr; |
| 1671 |
halfword options = 0; |
| 1672 |
halfword used_style = cramped_style(m_style); |
| 1673 |
tail_append(new_node(radical_noad, chr_code)); |
| 1674 |
q = new_node(delim_node, 0); |
| 1675 |
left_delimiter(tail) = q; |
| 1676 |
while (1) { |
| 1677 |
if (scan_keyword("width")) { |
| 1678 |
scan_dimen(false,false,false); |
| 1679 |
radicalwidth(tail) = cur_val ; |
| 1680 |
} else if (scan_keyword("left")) { |
| 1681 |
options = options | noad_option_left ; |
| 1682 |
} else if (scan_keyword("middle")) { |
| 1683 |
options = options | noad_option_middle ; |
| 1684 |
} else if (scan_keyword("right")) { |
| 1685 |
options = options | noad_option_right ; |
| 1686 |
} else { |
| 1687 |
break; |
| 1688 |
} |
| 1689 |
} |
| 1690 |
radicaloptions(tail) = options; |
| 1691 |
if (chr_code == 0) { |
| 1692 |
/*tex \.{\\radical} */ |
| 1693 |
scan_delimiter(left_delimiter(tail), tex_mathcode); |
| 1694 |
} else if (chr_code == 1) { |
| 1695 |
/*tex \.{\\Uradical} */ |
| 1696 |
scan_delimiter(left_delimiter(tail), umath_mathcode); |
| 1697 |
} else if (chr_code == 2) { |
| 1698 |
/*tex \.{\\Uroot} */ |
| 1699 |
scan_delimiter(left_delimiter(tail), umath_mathcode); |
| 1700 |
} else if (chr_code == 3) { |
| 1701 |
/*tex \.{\\Uunderdelimiter} */ |
| 1702 |
scan_delimiter(left_delimiter(tail), umath_mathcode); |
| 1703 |
if (math_defaults_mode_par > 0) { |
| 1704 |
used_style = sub_style(m_style); |
| 1705 |
} |
| 1706 |
} else if (chr_code == 4) { |
| 1707 |
/*tex \.{\\Uoverdelimiter} */ |
| 1708 |
scan_delimiter(left_delimiter(tail), umath_mathcode); |
| 1709 |
if (math_defaults_mode_par > 0) { |
| 1710 |
used_style = sup_style(m_style); |
| 1711 |
} |
| 1712 |
} else if (chr_code == 5) { |
| 1713 |
/*tex \.{\\Udelimiterunder} */ |
| 1714 |
scan_delimiter(left_delimiter(tail), umath_mathcode); |
| 1715 |
if (math_defaults_mode_par > 0) { |
| 1716 |
used_style = m_style; |
| 1717 |
} |
| 1718 |
} else if (chr_code == 6) { |
| 1719 |
/*tex \.{\\Udelimiterover} */ |
| 1720 |
scan_delimiter(left_delimiter(tail), umath_mathcode); |
| 1721 |
} else if (chr_code == 7) { |
| 1722 |
/*tex \.{\\Uhextensible} */ |
| 1723 |
scan_delimiter(left_delimiter(tail), umath_mathcode); |
| 1724 |
} else { |
| 1725 |
confusion("math_radical"); |
| 1726 |
} |
| 1727 |
if (chr_code == 7) { |
| 1728 |
/*tex type will change */ |
| 1729 |
q = new_node(sub_box_node, 0); |
| 1730 |
nucleus(tail) = q; |
| 1731 |
return; |
| 1732 |
} else if (chr_code == 2) { |
| 1733 |
/*tex |
| 1734 |
|
| 1735 |
The trick with the |vlink(q)| is used by |scan_math| to decide |
| 1736 |
whether it needs to go on. |
| 1737 |
|
| 1738 |
*/ |
| 1739 |
q = new_node(math_char_node, 0); |
| 1740 |
vlink(q) = tail; |
| 1741 |
degree(tail) = q; |
| 1742 |
if (!scan_math(degree(tail), sup_sup_style(m_style))) { |
| 1743 |
vlink(degree(tail)) = null; |
| 1744 |
q = new_node(math_char_node, 0); |
| 1745 |
nucleus(tail) = q; |
| 1746 |
(void) scan_math(nucleus(tail), used_style); |
| 1747 |
} |
| 1748 |
} else { |
| 1749 |
q = new_node(math_char_node, 0); |
| 1750 |
nucleus(tail) = q; |
| 1751 |
(void) scan_math(nucleus(tail), used_style); |
| 1752 |
} |
| 1753 |
} |
| 1754 |
|
| 1755 |
void math_ac(void) |
| 1756 |
{ |
| 1757 |
halfword q; |
| 1758 |
mathcodeval t = { 0, 0, 0 }; |
| 1759 |
mathcodeval b = { 0, 0, 0 }; |
| 1760 |
mathcodeval o = { 0, 0, 0 }; |
| 1761 |
if (cur_cmd == accent_cmd) { |
| 1762 |
const char *hlp[] = { |
| 1763 |
"I'm changing \\accent to \\mathaccent here; wish me luck.", |
| 1764 |
"(Accents are not the same in formulas as they are in text.)", |
| 1765 |
NULL |
| 1766 |
}; |
| 1767 |
tex_error("Please use \\mathaccent for accents in math mode", hlp); |
| 1768 |
} |
| 1769 |
tail_append(new_node(accent_noad, 0)); |
| 1770 |
if (cur_chr == 0) { |
| 1771 |
/*tex \.{\\mathaccent} */ |
| 1772 |
t = scan_mathchar(tex_mathcode); |
| 1773 |
} else if (cur_chr == 1) { |
| 1774 |
/*tex \.{\\Umathaccent} */ |
| 1775 |
if (scan_keyword("fixed")) { |
| 1776 |
/*tex top */ |
| 1777 |
subtype(tail) = 1; |
| 1778 |
t = scan_mathchar(umath_mathcode); |
| 1779 |
} else if (scan_keyword("both")) { |
| 1780 |
/*tex top bottom */ |
| 1781 |
if (scan_keyword("fixed")) { |
| 1782 |
subtype(tail) = 1; |
| 1783 |
} |
| 1784 |
t = scan_mathchar(umath_mathcode); |
| 1785 |
if (scan_keyword("fixed")) { |
| 1786 |
subtype(tail) += 2; |
| 1787 |
} |
| 1788 |
b = scan_mathchar(umath_mathcode); |
| 1789 |
} else if (scan_keyword("bottom")) { |
| 1790 |
/*tex bottom */ |
| 1791 |
if (scan_keyword("fixed")) { |
| 1792 |
subtype(tail) = 2; |
| 1793 |
} |
| 1794 |
b = scan_mathchar(umath_mathcode); |
| 1795 |
} else if (scan_keyword("top")) { |
| 1796 |
/*tex top */ |
| 1797 |
if (scan_keyword("fixed")) { |
| 1798 |
subtype(tail) = 1; |
| 1799 |
} |
| 1800 |
t = scan_mathchar(umath_mathcode); |
| 1801 |
} else if (scan_keyword("overlay")) { |
| 1802 |
/* overlay */ |
| 1803 |
if (scan_keyword("fixed")) { |
| 1804 |
subtype(tail) = 1; |
| 1805 |
} |
| 1806 |
o = scan_mathchar(umath_mathcode); |
| 1807 |
} else { |
| 1808 |
/*tex top */ |
| 1809 |
t = scan_mathchar(umath_mathcode); |
| 1810 |
} |
| 1811 |
if (scan_keyword("fraction")) { |
| 1812 |
scan_int(); |
| 1813 |
accentfraction(tail) = cur_val; |
| 1814 |
} |
| 1815 |
} else { |
| 1816 |
confusion("mathaccent"); |
| 1817 |
} |
| 1818 |
if (!(t.character_value == 0 && t.family_value == 0)) { |
| 1819 |
q = new_node(math_char_node, 0); |
| 1820 |
top_accent_chr(tail) = q; |
| 1821 |
math_character(top_accent_chr(tail)) = t.character_value; |
| 1822 |
if ((t.class_value == math_use_current_family_code) && cur_fam_par_in_range) |
| 1823 |
math_fam(top_accent_chr(tail)) = cur_fam_par; |
| 1824 |
else if ((t.family_value == var_fam_par) && var_fam_par_in_range) |
| 1825 |
math_fam(top_accent_chr(tail)) = cur_fam_par; |
| 1826 |
else |
| 1827 |
math_fam(top_accent_chr(tail)) = t.family_value; |
| 1828 |
} |
| 1829 |
if (!(b.character_value == 0 && b.family_value == 0)) { |
| 1830 |
q = new_node(math_char_node, 0); |
| 1831 |
bot_accent_chr(tail) = q; |
| 1832 |
math_character(bot_accent_chr(tail)) = b.character_value; |
| 1833 |
if ((b.class_value == math_use_current_family_code) && cur_fam_par_in_range) |
| 1834 |
math_fam(bot_accent_chr(tail)) = cur_fam_par; |
| 1835 |
else if ((b.family_value == var_fam_par) && var_fam_par_in_range) |
| 1836 |
math_fam(bot_accent_chr(tail)) = cur_fam_par; |
| 1837 |
else |
| 1838 |
math_fam(bot_accent_chr(tail)) = b.family_value; |
| 1839 |
} |
| 1840 |
if (!(o.character_value == 0 && o.family_value == 0)) { |
| 1841 |
q = new_node(math_char_node, 0); |
| 1842 |
overlay_accent_chr(tail) = q; |
| 1843 |
math_character(overlay_accent_chr(tail)) = o.character_value; |
| 1844 |
if ((o.class_value == math_use_current_family_code) && cur_fam_par_in_range) |
| 1845 |
math_fam(overlay_accent_chr(tail)) = cur_fam_par; |
| 1846 |
else if ((o.family_value == var_fam_par) && var_fam_par_in_range) |
| 1847 |
math_fam(overlay_accent_chr(tail)) = cur_fam_par; |
| 1848 |
else |
| 1849 |
math_fam(overlay_accent_chr(tail)) = o.family_value; |
| 1850 |
} |
| 1851 |
q = new_node(math_char_node, 0); |
| 1852 |
nucleus(tail) = q; |
| 1853 |
(void) scan_math(nucleus(tail), cramped_style(m_style)); |
| 1854 |
} |
| 1855 |
|
| 1856 |
pointer math_vcenter_group(pointer p) |
| 1857 |
{ |
| 1858 |
pointer q, r; |
| 1859 |
q = new_noad(); |
| 1860 |
subtype(q) = vcenter_noad_type; |
| 1861 |
r = new_node(sub_box_node, 0); |
| 1862 |
nucleus(q) = r; |
| 1863 |
math_list(nucleus(q)) = p; |
| 1864 |
return q; |
| 1865 |
} |
| 1866 |
|
| 1867 |
/*tex |
| 1868 |
|
| 1869 |
The routine that scans the four mlists of a \.{\\mathchoice} is very much |
| 1870 |
like the routine that builds discretionary nodes. |
| 1871 |
|
| 1872 |
*/ |
| 1873 |
|
| 1874 |
void append_choices(void) |
| 1875 |
{ |
| 1876 |
tail_append(new_choice()); |
| 1877 |
incr(save_ptr); |
| 1878 |
set_saved_record(-1, saved_choices, 0, 0); |
| 1879 |
push_math(math_choice_group, display_style); |
| 1880 |
scan_left_brace(); |
| 1881 |
} |
| 1882 |
|
| 1883 |
void build_choices(void) |
| 1884 |
{ |
| 1885 |
pointer p; |
| 1886 |
int prev_style; |
| 1887 |
prev_style = m_style; |
| 1888 |
unsave_math(); |
| 1889 |
p = fin_mlist(null); |
| 1890 |
assert(saved_type(-1) == saved_choices); |
| 1891 |
switch (saved_value(-1)) { |
| 1892 |
case 0: |
| 1893 |
display_mlist(tail) = p; |
| 1894 |
break; |
| 1895 |
case 1: |
| 1896 |
text_mlist(tail) = p; |
| 1897 |
break; |
| 1898 |
case 2: |
| 1899 |
script_mlist(tail) = p; |
| 1900 |
break; |
| 1901 |
case 3: |
| 1902 |
script_script_mlist(tail) = p; |
| 1903 |
decr(save_ptr); |
| 1904 |
return; |
| 1905 |
break; |
| 1906 |
} |
| 1907 |
set_saved_record(-1, saved_choices, 0, (saved_value(-1) + 1)); |
| 1908 |
push_math(math_choice_group, (prev_style + 2)); |
| 1909 |
scan_left_brace(); |
| 1910 |
} |
| 1911 |
|
| 1912 |
/*tex |
| 1913 |
|
| 1914 |
Subscripts and superscripts are attached to the previous nucleus by the |
| 1915 |
action procedure called |sub_sup|. |
| 1916 |
|
| 1917 |
*/ |
| 1918 |
|
| 1919 |
static void do_sub_sup(int no) |
| 1920 |
{ |
| 1921 |
pointer q; |
| 1922 |
if (tail == head || (!scripts_allowed(tail))) { |
| 1923 |
tail_append(new_noad()); |
| 1924 |
q = new_node(sub_mlist_node, 0); |
| 1925 |
nucleus(tail) = q; |
| 1926 |
} |
| 1927 |
if (cur_cmd == sup_mark_cmd || cur_chr == sup_mark_cmd) { |
| 1928 |
/*tex |super_sub_script| */ |
| 1929 |
if (supscr(tail) != null) { |
| 1930 |
const char *hlp[] = { |
| 1931 |
"I treat `x^1^2' essentially like `x^1{}^2'.", NULL |
| 1932 |
}; |
| 1933 |
tail_append(new_noad()); |
| 1934 |
q = new_node(sub_mlist_node, 0); |
| 1935 |
nucleus(tail) = q; |
| 1936 |
tex_error("Double superscript", hlp); |
| 1937 |
} |
| 1938 |
if (no) { |
| 1939 |
noadoptions(tail) = noadoptions(tail) | noad_option_no_super_script ; |
| 1940 |
} |
| 1941 |
q = new_node(math_char_node, 0); |
| 1942 |
supscr(tail) = q; |
| 1943 |
(void) scan_math(supscr(tail), sup_style(m_style)); |
| 1944 |
} else if (cur_cmd == sub_mark_cmd || cur_chr == sub_mark_cmd) { |
| 1945 |
if (subscr(tail) != null) { |
| 1946 |
const char *hlp[] = { |
| 1947 |
"I treat `x_1_2' essentially like `x_1{}_2'.", NULL |
| 1948 |
}; |
| 1949 |
tail_append(new_noad()); |
| 1950 |
q = new_node(sub_mlist_node, 0); |
| 1951 |
nucleus(tail) = q; |
| 1952 |
tex_error("Double subscript", hlp); |
| 1953 |
} |
| 1954 |
if (no) { |
| 1955 |
noadoptions(tail) = noadoptions(tail) | noad_option_no_sub_script ; |
| 1956 |
} |
| 1957 |
q = new_node(math_char_node, 0); |
| 1958 |
subscr(tail) = q; |
| 1959 |
(void) scan_math(subscr(tail), sub_style(m_style)); |
| 1960 |
} |
| 1961 |
} |
| 1962 |
|
| 1963 |
void sub_sup(void) |
| 1964 |
{ |
| 1965 |
do_sub_sup(0); |
| 1966 |
} |
| 1967 |
|
| 1968 |
void no_sub_sup(void) |
| 1969 |
{ |
| 1970 |
do_sub_sup(1); |
| 1971 |
} |
| 1972 |
|
| 1973 |
/*tex |
| 1974 |
|
| 1975 |
An operation like `\.{\\over}' causes the current mlist to go into a state of |
| 1976 |
suspended animation: |incompleat_noad| points to a |fraction_noad| that |
| 1977 |
contains the mlist-so-far as its numerator, while the denominator is yet to |
| 1978 |
come. Finally when the mlist is finished, the denominator will go into the |
| 1979 |
incompleat fraction noad, and that noad will become the whole formula, unless |
| 1980 |
it is surrounded by `\.{\\left}' and `\.{\\right}' delimiters. |
| 1981 |
|
| 1982 |
*/ |
| 1983 |
|
| 1984 |
void math_fraction(void) |
| 1985 |
{ |
| 1986 |
/*tex The type of generalized fraction we are scanning: */ |
| 1987 |
halfword c; |
| 1988 |
pointer q; |
| 1989 |
halfword options = 0; |
| 1990 |
halfword temp_value; |
| 1991 |
c = cur_chr; |
| 1992 |
if (incompleat_noad_par != null) { |
| 1993 |
const char *hlp[] = { |
| 1994 |
"I'm ignoring this fraction specification, since I don't", |
| 1995 |
"know whether a construction like `x \\over y \\over z'", |
| 1996 |
"means `{x \\over y} \\over z' or `x \\over {y \\over z}'.", |
| 1997 |
NULL |
| 1998 |
}; |
| 1999 |
if (c >= delimited_code) { |
| 2000 |
scan_delimiter(null, no_mathcode); |
| 2001 |
scan_delimiter(null, no_mathcode); |
| 2002 |
} |
| 2003 |
if ((c % delimited_code) == above_code) |
| 2004 |
scan_normal_dimen(); |
| 2005 |
tex_error("Ambiguous; you need another { and }", hlp); |
| 2006 |
} else { |
| 2007 |
incompleat_noad_par = new_node(fraction_noad, 0); |
| 2008 |
temp_value = new_node(sub_mlist_node, 0); |
| 2009 |
numerator(incompleat_noad_par) = temp_value; |
| 2010 |
math_list(numerator(incompleat_noad_par)) = vlink(head); |
| 2011 |
vlink(head) = null; |
| 2012 |
tail = head; |
| 2013 |
m_style = cramped_style(m_style); |
| 2014 |
|
| 2015 |
if ((c % delimited_code) == skewed_code) { |
| 2016 |
q = new_node(delim_node, 0); |
| 2017 |
middle_delimiter(incompleat_noad_par) = q; |
| 2018 |
scan_delimiter(middle_delimiter(incompleat_noad_par), no_mathcode); |
| 2019 |
} |
| 2020 |
if (c >= delimited_code) { |
| 2021 |
q = new_node(delim_node, 0); |
| 2022 |
left_delimiter(incompleat_noad_par) = q; |
| 2023 |
q = new_node(delim_node, 0); |
| 2024 |
right_delimiter(incompleat_noad_par) = q; |
| 2025 |
scan_delimiter(left_delimiter(incompleat_noad_par), no_mathcode); |
| 2026 |
scan_delimiter(right_delimiter(incompleat_noad_par), no_mathcode); |
| 2027 |
} |
| 2028 |
switch (c % delimited_code) { |
| 2029 |
case above_code: |
| 2030 |
while (1) { |
| 2031 |
if (scan_keyword("exact")) { |
| 2032 |
options = options | noad_option_exact ; |
| 2033 |
} else if (scan_keyword("norule")) { |
| 2034 |
options = options | noad_option_no_rule ; |
| 2035 |
} else { |
| 2036 |
break; |
| 2037 |
} |
| 2038 |
} |
| 2039 |
fractionoptions(incompleat_noad_par) = options; |
| 2040 |
scan_normal_dimen(); |
| 2041 |
thickness(incompleat_noad_par) = cur_val; |
| 2042 |
break; |
| 2043 |
case over_code: |
| 2044 |
thickness(incompleat_noad_par) = default_code; |
| 2045 |
break; |
| 2046 |
case atop_code: |
| 2047 |
thickness(incompleat_noad_par) = 0; |
| 2048 |
break; |
| 2049 |
case skewed_code: |
| 2050 |
while (1) { |
| 2051 |
if (scan_keyword("exact")) { |
| 2052 |
options = options | noad_option_exact ; |
| 2053 |
} else if (scan_keyword("noaxis")) { |
| 2054 |
options = options | noad_option_no_axis ; |
| 2055 |
} else { |
| 2056 |
break; |
| 2057 |
} |
| 2058 |
} |
| 2059 |
fractionoptions(incompleat_noad_par) = options; |
| 2060 |
thickness(incompleat_noad_par) = 0; |
| 2061 |
break; |
| 2062 |
} |
| 2063 |
} |
| 2064 |
} |
| 2065 |
|
| 2066 |
/*tex |
| 2067 |
|
| 2068 |
At the end of a math formula or subformula, the |fin_mlist| routine is called |
| 2069 |
upon to return a pointer to the newly completed mlist, and to pop the nest |
| 2070 |
back to the enclosing semantic level. The parameter to |fin_mlist|, if not |
| 2071 |
null, points to a |fence_noad| that ends the current mlist; this |fence_noad| |
| 2072 |
has not yet been appended. |
| 2073 |
|
| 2074 |
*/ |
| 2075 |
|
| 2076 |
pointer fin_mlist(pointer p) |
| 2077 |
{ |
| 2078 |
pointer q; |
| 2079 |
if (incompleat_noad_par != null) { |
| 2080 |
if (denominator(incompleat_noad_par) != null) { |
| 2081 |
type(denominator(incompleat_noad_par)) = sub_mlist_node; |
| 2082 |
} else { |
| 2083 |
q = new_node(sub_mlist_node, 0); |
| 2084 |
denominator(incompleat_noad_par) = q; |
| 2085 |
} |
| 2086 |
math_list(denominator(incompleat_noad_par)) = vlink(head); |
| 2087 |
if (p == null) { |
| 2088 |
q = incompleat_noad_par; |
| 2089 |
} else { |
| 2090 |
q = math_list(numerator(incompleat_noad_par)); |
| 2091 |
if ((type(q) != fence_noad) || (subtype(q) != left_noad_side) || (delim_par == null)) |
| 2092 |
confusion("right"); |
| 2093 |
math_list(numerator(incompleat_noad_par)) = vlink(delim_par); |
| 2094 |
vlink(delim_par) = incompleat_noad_par; |
| 2095 |
vlink(incompleat_noad_par) = p; |
| 2096 |
} |
| 2097 |
} else { |
| 2098 |
vlink(tail) = p; |
| 2099 |
q = vlink(head); |
| 2100 |
} |
| 2101 |
pop_nest(); |
| 2102 |
return q; |
| 2103 |
} |
| 2104 |
|
| 2105 |
/*tex |
| 2106 |
|
| 2107 |
Now at last we're ready to see what happens when a right brace occurs in a |
| 2108 |
math formula. Two special cases are simplified here: Braces are effectively |
| 2109 |
removed when they surround a single Ord without sub/superscripts, or when |
| 2110 |
they surround an accent that is the nucleus of an Ord atom. |
| 2111 |
|
| 2112 |
*/ |
| 2113 |
|
| 2114 |
void close_math_group(pointer p) |
| 2115 |
{ |
| 2116 |
int old_style = m_style; |
| 2117 |
unsave_math(); |
| 2118 |
decr(save_ptr); |
| 2119 |
assert(saved_type(0) == saved_math); |
| 2120 |
type(saved_value(0)) = sub_mlist_node; |
| 2121 |
p = fin_mlist(null); |
| 2122 |
math_list(saved_value(0)) = p; |
| 2123 |
if (p != null && vlink(p) == null) { |
| 2124 |
if (type(p) == simple_noad) { |
| 2125 |
if (subscr(p) == null && supscr(p) == null) { |
| 2126 |
/*tex |
| 2127 |
In traditional \TEX\ this only happens for ordinary noads. |
| 2128 |
*/ |
| 2129 |
// int flatten = subtype(p) == ord_noad_type; /*tex traditional \TEX. */ |
| 2130 |
int flatten = 0; |
| 2131 |
int modepar = math_flatten_mode_par; |
| 2132 |
switch (subtype(p)) { |
| 2133 |
case ord_noad_type : |
| 2134 |
flatten = (modepar & 1) == 1; /* the default */ |
| 2135 |
break; |
| 2136 |
case bin_noad_type : |
| 2137 |
flatten = (modepar & 2) == 2; |
| 2138 |
break; |
| 2139 |
case rel_noad_type : |
| 2140 |
flatten = (modepar & 4) == 4; |
| 2141 |
break; |
| 2142 |
case punct_noad_type : |
| 2143 |
flatten = (modepar & 8) == 8; |
| 2144 |
break; |
| 2145 |
case inner_noad_type : |
| 2146 |
flatten = (modepar & 16) == 16; |
| 2147 |
break; |
| 2148 |
default: |
| 2149 |
break; |
| 2150 |
} |
| 2151 |
if (flatten) { |
| 2152 |
type(saved_value(0)) = type(nucleus(p)); |
| 2153 |
if (type(nucleus(p)) == math_char_node) { |
| 2154 |
math_fam(saved_value(0)) = math_fam(nucleus(p)); |
| 2155 |
math_character(saved_value(0)) = |
| 2156 |
math_character(nucleus(p)); |
| 2157 |
} else { |
| 2158 |
math_list(saved_value(0)) = math_list(nucleus(p)); |
| 2159 |
math_list(nucleus(p)) = null; |
| 2160 |
} |
| 2161 |
delete_attribute_ref(node_attr(saved_value(0))); |
| 2162 |
node_attr(saved_value(0)) = node_attr(nucleus(p)); |
| 2163 |
node_attr(nucleus(p)) = null; |
| 2164 |
flush_node(p); |
| 2165 |
} |
| 2166 |
} |
| 2167 |
} else if (type(p) == accent_noad) { |
| 2168 |
if (saved_value(0) == nucleus(tail) && type(tail) == simple_noad && subtype(tail) == ord_noad_type) { |
| 2169 |
pointer q = head; |
| 2170 |
while (vlink(q) != tail) |
| 2171 |
q = vlink(q); |
| 2172 |
vlink(q) = p; |
| 2173 |
nucleus(tail) = null; |
| 2174 |
subscr(tail) = null; |
| 2175 |
supscr(tail) = null; |
| 2176 |
delete_attribute_ref(node_attr(p)); |
| 2177 |
node_attr(p) = node_attr(tail); |
| 2178 |
node_attr(tail) = null; |
| 2179 |
flush_node(tail); |
| 2180 |
tail = p; |
| 2181 |
} |
| 2182 |
} |
| 2183 |
} |
| 2184 |
if (vlink(saved_value(0)) > 0) { |
| 2185 |
pointer q; |
| 2186 |
q = new_node(math_char_node, 0); |
| 2187 |
nucleus(vlink(saved_value(0))) = q; |
| 2188 |
vlink(saved_value(0)) = null; |
| 2189 |
saved_value(0) = q; |
| 2190 |
(void) scan_math(saved_value(0), old_style); |
| 2191 |
/*tex restart */ |
| 2192 |
} |
| 2193 |
} |
| 2194 |
|
| 2195 |
/*tex |
| 2196 |
|
| 2197 |
We have dealt with all constructions of math mode except `\.{\\left}' and |
| 2198 |
`\.{\\right}', so the picture is completed by the following sections of the |
| 2199 |
program. The |middle| feature of eTeX allows one ore several \.{\\middle} |
| 2200 |
delimiters to appear between \.{\\left} and \.{\\right}. |
| 2201 |
|
| 2202 |
*/ |
| 2203 |
|
| 2204 |
void math_left_right(void) |
| 2205 |
{ |
| 2206 |
/*tex |left_noad_side| .. |right_noad_side| */ |
| 2207 |
halfword t; |
| 2208 |
/*tex new noad */ |
| 2209 |
pointer p; |
| 2210 |
/*tex resulting mlist */ |
| 2211 |
pointer q; |
| 2212 |
/*tex temporary */ |
| 2213 |
pointer r; |
| 2214 |
halfword ht = 0; |
| 2215 |
halfword dp = 0; |
| 2216 |
halfword options = 0; |
| 2217 |
halfword type = -1 ; |
| 2218 |
t = cur_chr; |
| 2219 |
if (t > 10) { |
| 2220 |
/*tex we have \Uleft \Uright \Umiddle */ |
| 2221 |
t = t - 10; |
| 2222 |
while (1) { |
| 2223 |
if (scan_keyword("height")) { |
| 2224 |
scan_dimen(false,false,false); |
| 2225 |
ht = cur_val ; |
| 2226 |
} else if (scan_keyword("depth")) { |
| 2227 |
scan_dimen(false,false,false); |
| 2228 |
dp = cur_val ; |
| 2229 |
} else if (scan_keyword("axis")) { |
| 2230 |
options = options | noad_option_axis ; |
| 2231 |
} else if (scan_keyword("noaxis")) { |
| 2232 |
options = options | noad_option_no_axis ; |
| 2233 |
} else if (scan_keyword("exact")) { |
| 2234 |
options = options | noad_option_exact ; |
| 2235 |
} else if (scan_keyword("class")) { |
| 2236 |
scan_int(); |
| 2237 |
math_class_to_type(type,cur_val); |
| 2238 |
} else { |
| 2239 |
break; |
| 2240 |
} |
| 2241 |
} |
| 2242 |
} |
| 2243 |
if ((t != no_noad_side) && (t != left_noad_side) && (cur_group != math_left_group)) { |
| 2244 |
if (cur_group == math_shift_group) { |
| 2245 |
scan_delimiter(null, no_mathcode); |
| 2246 |
if (t == middle_noad_side) { |
| 2247 |
const char *hlp[] = { |
| 2248 |
"I'm ignoring a \\middle that had no matching \\left.", |
| 2249 |
NULL |
| 2250 |
}; |
| 2251 |
tex_error("Extra \\middle", hlp); |
| 2252 |
} else { |
| 2253 |
const char *hlp[] = { |
| 2254 |
"I'm ignoring a \\right that had no matching \\left.", |
| 2255 |
NULL |
| 2256 |
}; |
| 2257 |
tex_error("Extra \\right", hlp); |
| 2258 |
} |
| 2259 |
} else { |
| 2260 |
off_save(); |
| 2261 |
} |
| 2262 |
} else { |
| 2263 |
p = new_noad(); |
| 2264 |
type(p) = fence_noad; |
| 2265 |
subtype(p) = (quarterword) t; |
| 2266 |
r = new_node(delim_node, 0); |
| 2267 |
delimiter(p) = r; |
| 2268 |
delimiterheight(p) = ht; |
| 2269 |
delimiterdepth(p) = dp; |
| 2270 |
delimiteroptions(p) = options; |
| 2271 |
delimiterclass(p) = type; |
| 2272 |
delimiteritalic(p) = 0; |
| 2273 |
delimitersamesize(p) = 0; |
| 2274 |
scan_delimiter(delimiter(p), no_mathcode); |
| 2275 |
if (t == no_noad_side) { |
| 2276 |
tail_append(new_noad()); |
| 2277 |
subtype(tail) = inner_noad_type; |
| 2278 |
r = new_node(sub_mlist_node, 0); |
| 2279 |
nucleus(tail) = r; |
| 2280 |
math_list(nucleus(tail)) = p; |
| 2281 |
return ; |
| 2282 |
} |
| 2283 |
if (t == left_noad_side) { |
| 2284 |
q = p; |
| 2285 |
} else { |
| 2286 |
q = fin_mlist(p); |
| 2287 |
unsave_math(); |
| 2288 |
} |
| 2289 |
if (t != right_noad_side) { |
| 2290 |
push_math(math_left_group, m_style); |
| 2291 |
vlink(head) = q; |
| 2292 |
tail = p; |
| 2293 |
delim_par = p; |
| 2294 |
} else { |
| 2295 |
tail_append(new_noad()); |
| 2296 |
subtype(tail) = inner_noad_type; |
| 2297 |
r = new_node(sub_mlist_node, 0); |
| 2298 |
nucleus(tail) = r; |
| 2299 |
math_list(nucleus(tail)) = q; |
| 2300 |
} |
| 2301 |
} |
| 2302 |
} |
| 2303 |
|
| 2304 |
/*tex |
| 2305 |
|
| 2306 |
\TeX\ gets to the following part of the program when the first `\.\$' ending |
| 2307 |
a display has been scanned. |
| 2308 |
|
| 2309 |
*/ |
| 2310 |
|
| 2311 |
static void check_second_math_shift(void) |
| 2312 |
{ |
| 2313 |
get_x_token(); |
| 2314 |
if (cur_cmd != math_shift_cmd) { |
| 2315 |
const char *hlp[] = { |
| 2316 |
"The `$' that I just saw supposedly matches a previous `$$'.", |
| 2317 |
"So I shall assume that you typed `$$' both times.", |
| 2318 |
NULL |
| 2319 |
}; |
| 2320 |
back_error("Display math should end with $$", hlp); |
| 2321 |
} |
| 2322 |
} |
| 2323 |
|
| 2324 |
static void check_display_math_end(void) |
| 2325 |
{ |
| 2326 |
if (cur_chr != cramped_display_style) { |
| 2327 |
const char *hlp[] = { |
| 2328 |
"I shall assume that you typed that.", |
| 2329 |
NULL |
| 2330 |
}; |
| 2331 |
tex_error("Display math should end with \\Ustopdisplaymath", hlp); |
| 2332 |
} |
| 2333 |
} |
| 2334 |
|
| 2335 |
static void check_inline_math_end(void) |
| 2336 |
{ |
| 2337 |
if (cur_chr != cramped_text_style) { |
| 2338 |
const char *hlp[] = { |
| 2339 |
"I shall assume that you typed that.", |
| 2340 |
NULL |
| 2341 |
}; |
| 2342 |
tex_error("Inline math should end with \\Ustopmath", hlp); |
| 2343 |
} |
| 2344 |
} |
| 2345 |
|
| 2346 |
static void resume_after_display(void) |
| 2347 |
{ |
| 2348 |
if (cur_group != math_shift_group) |
| 2349 |
confusion("display"); |
| 2350 |
unsave_math(); |
| 2351 |
prev_graf_par = prev_graf_par + 3; |
| 2352 |
push_nest(); |
| 2353 |
mode = hmode; |
| 2354 |
space_factor_par = 1000; |
| 2355 |
/*tex This needs to be intercepted in the display math start! */ |
| 2356 |
tail_append(make_local_par_node(penalty_par_code)); |
| 2357 |
get_x_token(); |
| 2358 |
if (cur_cmd != spacer_cmd) |
| 2359 |
back_input(); |
| 2360 |
if (nest_ptr == 1) { |
| 2361 |
normal_page_filter(after_display); |
| 2362 |
build_page(); |
| 2363 |
} |
| 2364 |
} |
| 2365 |
|
| 2366 |
/*tex |
| 2367 |
|
| 2368 |
The fussiest part of math mode processing occurs when a displayed formula is |
| 2369 |
being centered and placed with an optional equation number. |
| 2370 |
|
| 2371 |
At this time we are in vertical mode (or internal vertical mode). |
| 2372 |
|
| 2373 |
\starttabulate |
| 2374 |
\NC \type {p} \NC points to the mlist for the formula \NC \NR |
| 2375 |
\NC \type {a} \NC is either |null| or it points to a box containing the equation number \NC \NR |
| 2376 |
\NC \type {l} \NC is true if there was an \.{\\leqno}/ (so |a| is a horizontal box) \NC \NR |
| 2377 |
\stoptabulate |
| 2378 |
|
| 2379 |
*/ |
| 2380 |
|
| 2381 |
#define inject_display_skip_before(g) \ |
| 2382 |
if (g > 0) { \ |
| 2383 |
switch (display_skip_mode_par) { \ |
| 2384 |
case 0 : \ |
| 2385 |
/*tex normal tex | always */ \ |
| 2386 |
case 1 : \ |
| 2387 |
/*tex always */ \ |
| 2388 |
tail_append(new_param_glue(g)); \ |
| 2389 |
break; \ |
| 2390 |
case 2 : \ |
| 2391 |
/*tex non-zero */ \ |
| 2392 |
if (! glue_is_zero(glue_par(g))) \ |
| 2393 |
tail_append(new_param_glue(g)); \ |
| 2394 |
break; \ |
| 2395 |
case 3: \ |
| 2396 |
/*tex ignore */ \ |
| 2397 |
break; \ |
| 2398 |
default: \ |
| 2399 |
/*tex > 3 reserved for future use */ \ |
| 2400 |
tail_append(new_param_glue(g)); \ |
| 2401 |
break; \ |
| 2402 |
} \ |
| 2403 |
} |
| 2404 |
|
| 2405 |
#define inject_display_skip_after(g) \ |
| 2406 |
if (g > 0) { \ |
| 2407 |
switch (display_skip_mode_par) { \ |
| 2408 |
case 0 : \ |
| 2409 |
/*tex normal tex | always */ \ |
| 2410 |
case 1 : \ |
| 2411 |
/*tex always */ \ |
| 2412 |
tail_append(new_param_glue(g)); \ |
| 2413 |
break; \ |
| 2414 |
case 2 : \ |
| 2415 |
/*tex non-zero */ \ |
| 2416 |
if (! glue_is_zero(glue_par(g))) \ |
| 2417 |
tail_append(new_param_glue(g)); \ |
| 2418 |
break; \ |
| 2419 |
case 3: \ |
| 2420 |
/*tex ignore */ \ |
| 2421 |
break; \ |
| 2422 |
default: \ |
| 2423 |
/*tex > 3 reserved for future use */ \ |
| 2424 |
tail_append(new_param_glue(g)); \ |
| 2425 |
break; \ |
| 2426 |
} \ |
| 2427 |
} |
| 2428 |
|
| 2429 |
static void finish_displayed_math(boolean l, pointer eqno_box, pointer p) |
| 2430 |
{ |
| 2431 |
/*tex box containing the equation */ |
| 2432 |
pointer eq_box; |
| 2433 |
/*tex width of the equation */ |
| 2434 |
scaled eq_w; |
| 2435 |
/*tex width of the line */ |
| 2436 |
scaled line_w; |
| 2437 |
/*tex width of equation number */ |
| 2438 |
scaled eqno_w; |
| 2439 |
/*tex width of equation number plus space to separate from equation */ |
| 2440 |
scaled eqno_w2; |
| 2441 |
/*tex move the line right this much */ |
| 2442 |
scaled line_s; |
| 2443 |
/*tex displacement of equation in the line */ |
| 2444 |
scaled d; |
| 2445 |
/*tex glue parameter codes for before and after */ |
| 2446 |
small_number g1, g2; |
| 2447 |
/*tex kern nodes used to position the display */ |
| 2448 |
pointer r,s; |
| 2449 |
/*tex tail of adjustment list */ |
| 2450 |
pointer t; |
| 2451 |
/*tex tail of pre-adjustment list */ |
| 2452 |
pointer pre_t; |
| 2453 |
/*tex true if the math and surrounding text dirs are opposed */ |
| 2454 |
boolean swap_dir; |
| 2455 |
scaled eqno_width; |
| 2456 |
swap_dir = (pre_display_direction_par < 0 ? true : false ); |
| 2457 |
if (eqno_box != null && swap_dir) |
| 2458 |
l = !l; |
| 2459 |
adjust_tail = adjust_head; |
| 2460 |
pre_adjust_tail = pre_adjust_head; |
| 2461 |
eq_box = hpack(p, 0, additional, -1); |
| 2462 |
subtype(eq_box) = equation_list; |
| 2463 |
build_attribute_list(eq_box); |
| 2464 |
p = list_ptr(eq_box); |
| 2465 |
t = adjust_tail; |
| 2466 |
adjust_tail = null; |
| 2467 |
pre_t = pre_adjust_tail; |
| 2468 |
pre_adjust_tail = null; |
| 2469 |
eq_w = width(eq_box); |
| 2470 |
line_w = display_width_par; |
| 2471 |
line_s = display_indent_par; |
| 2472 |
if (eqno_box == null) { |
| 2473 |
eqno_w = 0; |
| 2474 |
eqno_width = 0; |
| 2475 |
eqno_w2 = 0; |
| 2476 |
} else { |
| 2477 |
eqno_w = width(eqno_box); |
| 2478 |
eqno_width = eqno_w; |
| 2479 |
eqno_w2 = eqno_w + round_xn_over_d(math_eqno_gap_step_par, get_math_quad_style(text_style), 1000); |
| 2480 |
subtype(eqno_box) = equation_number_list; |
| 2481 |
/*tex build_attribute_list(eqno_box); */ |
| 2482 |
} |
| 2483 |
if (eq_w + eqno_w2 > line_w) { |
| 2484 |
/*tex |
| 2485 |
|
| 2486 |
The user can force the equation number to go on a separate line by |
| 2487 |
causing its width to be zero. |
| 2488 |
|
| 2489 |
*/ |
| 2490 |
if ((eqno_w != 0) && ((eq_w - total_shrink[normal] + eqno_w2 <= line_w) |
| 2491 |
|| (total_shrink[sfi] != 0) || (total_shrink[fil] != 0) |
| 2492 |
|| (total_shrink[fill] != 0) || (total_shrink[filll] != 0))) { |
| 2493 |
list_ptr(eq_box) = null; |
| 2494 |
flush_node(eq_box); |
| 2495 |
eq_box = hpack(p, line_w - eqno_w2, exactly, -1); |
| 2496 |
subtype(eq_box) = equation_list; |
| 2497 |
build_attribute_list(eq_box); |
| 2498 |
} else { |
| 2499 |
eqno_w = 0; |
| 2500 |
if (eq_w > line_w) { |
| 2501 |
list_ptr(eq_box) = null; |
| 2502 |
flush_node(eq_box); |
| 2503 |
eq_box = hpack(p, line_w, exactly, -1); |
| 2504 |
subtype(eq_box) = equation_list; |
| 2505 |
build_attribute_list(eq_box); |
| 2506 |
} |
| 2507 |
} |
| 2508 |
eq_w = width(eq_box); |
| 2509 |
} |
| 2510 |
/*tex |
| 2511 |
|
| 2512 |
We try first to center the display without regard to the existence of the |
| 2513 |
equation number. If that would make it too close (where ``too close'' |
| 2514 |
means that the space between display and equation number is less than the |
| 2515 |
width of the equation number), we either center it in the remaining space |
| 2516 |
or move it as far from the equation number as possible. The latter |
| 2517 |
alternative is taken only if the display begins with glue, since we |
| 2518 |
assume that the user put glue there to control the spacing precisely. |
| 2519 |
|
| 2520 |
*/ |
| 2521 |
d = half(line_w - eq_w); |
| 2522 |
if ((eqno_w > 0) && (d < 2 * eqno_w)) { |
| 2523 |
/*tex too close */ |
| 2524 |
d = half(line_w - eq_w - eqno_w); |
| 2525 |
if (p != null) |
| 2526 |
if (!is_char_node(p)) |
| 2527 |
if (type(p) == glue_node) |
| 2528 |
d = 0; |
| 2529 |
} |
| 2530 |
tail_append(new_penalty(pre_display_penalty_par,before_display_penalty)); |
| 2531 |
|
| 2532 |
/* tex |
| 2533 |
By default the short skip detection is not adapted to r2l typesetting and that |
| 2534 |
hasn't been the case since the start of the project. Changing it could break |
| 2535 |
hacks that users came up with but when you set \.{\\matheqdirmode} to a positive |
| 2536 |
value direction will be taken into account. |
| 2537 |
*/ |
| 2538 |
if (math_eq_dir_mode_par<=0) { /* old behavior */ |
| 2539 |
if ((d + line_s <= pre_display_size_par) || l) { |
| 2540 |
/*tex not enough clearance */ |
| 2541 |
g1 = above_display_skip_code; |
| 2542 |
g2 = below_display_skip_code; |
| 2543 |
} else { |
| 2544 |
g1 = above_display_short_skip_code; |
| 2545 |
g2 = below_display_short_skip_code; |
| 2546 |
} |
| 2547 |
} else { |
| 2548 |
boolean math_and_par_reversed = math_and_text_reversed_p(); |
| 2549 |
if ((d + line_s <= pre_display_size_par) || (eqno_box != null |
| 2550 |
&& ((! math_and_par_reversed && l) |
| 2551 |
|| (math_and_par_reversed && ! l)))) { |
| 2552 |
/*tex not enough clearance */ |
| 2553 |
g1 = above_display_skip_code; |
| 2554 |
g2 = below_display_skip_code; |
| 2555 |
} else { |
| 2556 |
g1 = above_display_short_skip_code; |
| 2557 |
g2 = below_display_short_skip_code; |
| 2558 |
} |
| 2559 |
} |
| 2560 |
/*tex |
| 2561 |
|
| 2562 |
If the equation number is set on a line by itself, either before or after |
| 2563 |
the formula, we append an infinite penalty so that no page break will |
| 2564 |
separate the display from its number; and we use the same size and |
| 2565 |
displacement for all three potential lines of the display, even though |
| 2566 |
`\.{\\parshape}' may specify them differently. |
| 2567 |
|
| 2568 |
\.{\\leqno} on a forced single line due to |width=0|; it follows that |type(a)=hlist_node| |
| 2569 |
|
| 2570 |
*/ |
| 2571 |
if (eqno_box && l && (eqno_w == 0)) { |
| 2572 |
/* if (math_direction_par==dir_TLT) { */ |
| 2573 |
shift_amount(eqno_box) = 0; |
| 2574 |
/* } else { */ |
| 2575 |
/* } */ |
| 2576 |
append_to_vlist(eqno_box,lua_key_index(equation_number)); |
| 2577 |
tail_append(new_penalty(inf_penalty,equation_number_penalty)); |
| 2578 |
} else { |
| 2579 |
inject_display_skip_before(g1); |
| 2580 |
} |
| 2581 |
if (eqno_w != 0) { |
| 2582 |
r = new_kern(line_w - eq_w - eqno_w - d); |
| 2583 |
if (l) { |
| 2584 |
if (math_direction_par==dir_TLT) { |
| 2585 |
/*tex TRT + TLT + \eqno: (swap_dir=true, math_direction_par=TLT, l=true) */ |
| 2586 |
/*tex TLT + TLT + \leqno: (swap_dir=false, math_direction_par=TLT, l=true) */ |
| 2587 |
s = new_kern(width(r) + eqno_w); |
| 2588 |
} else { |
| 2589 |
/*tex TLT + TRT + \eqno: (swap_dir=true, math_direction_par=TRT, l=true) */ |
| 2590 |
/*tex TRT + TRT + \leqno: (swap_dir=false, math_direction_par=TRT, l=true) */ |
| 2591 |
s = new_kern(d); |
| 2592 |
} |
| 2593 |
try_couple_nodes(eqno_box,r); |
| 2594 |
try_couple_nodes(r,eq_box); |
| 2595 |
try_couple_nodes(eq_box,s); |
| 2596 |
eq_box = eqno_box; |
| 2597 |
} else { |
| 2598 |
if (math_direction_par==dir_TLT) { |
| 2599 |
/*tex TRT + TLT + \leqno: (swap_dir=true, math_direction_par=TLT, l=false) */ |
| 2600 |
/*tex TLT + TLT + \eqno: (swap_dir=false, math_direction_par=TLT, l=false) */ |
| 2601 |
s = new_kern(d); |
| 2602 |
} else { |
| 2603 |
/*tex TLT + TRT + \leqno: (swap_dir=true, math_direction_par=TRT, l=false) */ |
| 2604 |
/*tex TRT + TRT + \eqno: (swap_dir=false, math_direction_par=TRT, l=false) */ |
| 2605 |
s = new_kern(width(r) + eqno_w); |
| 2606 |
} |
| 2607 |
try_couple_nodes(s,eq_box); |
| 2608 |
try_couple_nodes(eq_box,r); |
| 2609 |
try_couple_nodes(r,eqno_box); |
| 2610 |
eq_box = s; |
| 2611 |
} |
| 2612 |
eq_box = hpack(eq_box, 0, additional, -1); |
| 2613 |
subtype(eq_box) = equation_list; /* new */ |
| 2614 |
build_attribute_list(eq_box); |
| 2615 |
shift_amount(eq_box) = line_s; |
| 2616 |
} else { |
| 2617 |
shift_amount(eq_box) = line_s + d; |
| 2618 |
} |
| 2619 |
/*tex check for prev: */ |
| 2620 |
append_to_vlist(eq_box,lua_key_index(equation)); |
| 2621 |
if ((eqno_box != null) && (eqno_w == 0) && !l) { |
| 2622 |
tail_append(new_penalty(inf_penalty,equation_number_penalty)); |
| 2623 |
/* if (math_direction_par==dir_TLT) { */ |
| 2624 |
shift_amount(eqno_box) = line_s + line_w - eqno_width ; |
| 2625 |
/* } else { */ |
| 2626 |
/* } */ |
| 2627 |
append_to_vlist(eqno_box,lua_key_index(equation_number)); |
| 2628 |
g2 = 0; |
| 2629 |
} |
| 2630 |
if (t != adjust_head) { |
| 2631 |
/*tex migrating material comes after equation number */ |
| 2632 |
vlink(tail) = vlink(adjust_head); |
| 2633 |
alink(adjust_tail) = alink(tail); |
| 2634 |
tail = t; |
| 2635 |
} |
| 2636 |
if (pre_t != pre_adjust_head) { |
| 2637 |
vlink(tail) = vlink(pre_adjust_head); |
| 2638 |
alink(pre_adjust_tail) = alink(tail); |
| 2639 |
tail = pre_t; |
| 2640 |
} |
| 2641 |
tail_append(new_penalty(post_display_penalty_par,after_display_penalty)); |
| 2642 |
inject_display_skip_after(g2); |
| 2643 |
resume_after_display(); |
| 2644 |
} |
| 2645 |
|
| 2646 |
void after_math(void) |
| 2647 |
{ |
| 2648 |
/*tex |mmode| or |-mmode| */ |
| 2649 |
int m; |
| 2650 |
/*tex the formula */ |
| 2651 |
pointer p; |
| 2652 |
/*tex box containing equation number */ |
| 2653 |
pointer a = null; |
| 2654 |
/*tex `\.{\\leqno}' instead of `\.{\\eqno}' */ |
| 2655 |
boolean l = false; |
| 2656 |
m = mode; |
| 2657 |
/*tex this pops the nest */ |
| 2658 |
p = fin_mlist(null); |
| 2659 |
if (cur_cmd == math_shift_cs_cmd && |
| 2660 |
(cur_chr == text_style || cur_chr == display_style)) { |
| 2661 |
you_cant(); |
| 2662 |
} |
| 2663 |
if (mode == -m) { |
| 2664 |
/*tex end of equation number */ |
| 2665 |
RECHECK: |
| 2666 |
if (cur_cmd == math_shift_cmd) { |
| 2667 |
check_second_math_shift(); |
| 2668 |
} else if (suppress_mathpar_error_par && cur_cmd == par_end_cmd) { |
| 2669 |
get_x_token(); |
| 2670 |
goto RECHECK; |
| 2671 |
} else { |
| 2672 |
check_display_math_end(); |
| 2673 |
} |
| 2674 |
run_mlist_to_hlist(p, false, text_style); |
| 2675 |
a = hpack(vlink(temp_head), 0, additional, -1); |
| 2676 |
build_attribute_list(a); |
| 2677 |
unsave_math(); |
| 2678 |
/*tex now |cur_group=math_shift_group| */ |
| 2679 |
decr(save_ptr); |
| 2680 |
assert(saved_type(0) == saved_eqno); |
| 2681 |
if (saved_value(0) == 1) |
| 2682 |
l = true; |
| 2683 |
m = mode; |
| 2684 |
p = fin_mlist(null); |
| 2685 |
|
| 2686 |
} |
| 2687 |
if (m < 0) { |
| 2688 |
/*tex |
| 2689 |
|
| 2690 |
The |unsave| is done after everything else here; hence an appearance |
| 2691 |
of `\.{\\mathsurround}' inside of `\.{\$...\$}' affects the spacing |
| 2692 |
at these particular \.\$'s. This is consistent with the conventions |
| 2693 |
of `\.{\$\$...\$\$}', since `\.{\\abovedisplayskip}' inside a display |
| 2694 |
affects the space above that display. |
| 2695 |
|
| 2696 |
*/ |
| 2697 |
if (cur_cmd == math_shift_cs_cmd) { |
| 2698 |
check_inline_math_end(); |
| 2699 |
} |
| 2700 |
tail_append(new_math(math_surround_par, before)); |
| 2701 |
/*tex begin mathskip code */ |
| 2702 |
switch (math_skip_mode) { |
| 2703 |
case 0 : |
| 2704 |
/*tex obey mathsurround when zero glue */ |
| 2705 |
if (! glue_is_zero(math_skip_par)) { |
| 2706 |
copy_glue_values(tail,math_skip_par); |
| 2707 |
surround(tail) = 0; |
| 2708 |
} |
| 2709 |
break ; |
| 2710 |
case 1 : |
| 2711 |
/*tex always left */ |
| 2712 |
case 3 : |
| 2713 |
/*tex always both */ |
| 2714 |
case 6 : |
| 2715 |
/*tex only when skip */ |
| 2716 |
copy_glue_values(tail,math_skip_par); |
| 2717 |
surround(tail) = 0; |
| 2718 |
break ; |
| 2719 |
case 2 : |
| 2720 |
/*tex only right */ |
| 2721 |
surround(tail) = 0; |
| 2722 |
break ; |
| 2723 |
case 4 : |
| 2724 |
/*tex ignore, obey marthsurround */ |
| 2725 |
break ; |
| 2726 |
case 5: |
| 2727 |
/*tex all spacing disabled */ |
| 2728 |
surround(tail) = 0; |
| 2729 |
break ; |
| 2730 |
} |
| 2731 |
/*tex end mathskip code */ |
| 2732 |
if (dir_math_save) { |
| 2733 |
tail_append(new_dir(math_direction_par)); |
| 2734 |
} |
| 2735 |
run_mlist_to_hlist(p, (mode > 0), text_style); |
| 2736 |
try_couple_nodes(tail,vlink(temp_head)); |
| 2737 |
|
| 2738 |
while (vlink(tail) != null) { |
| 2739 |
tail = vlink(tail); |
| 2740 |
} |
| 2741 |
if (dir_math_save) { |
| 2742 |
tail_append(new_dir(math_direction_par)); |
| 2743 |
subtype(tail) = cancel_dir; |
| 2744 |
} |
| 2745 |
dir_math_save = false; |
| 2746 |
tail_append(new_math(math_surround_par, after)); |
| 2747 |
/*tex begin mathskip code */ |
| 2748 |
switch (math_skip_mode) { |
| 2749 |
case 0 : |
| 2750 |
/*tex obey mathsurround when zero glue */ |
| 2751 |
if (! glue_is_zero(math_skip_par)) { |
| 2752 |
copy_glue_values(tail,math_skip_par); |
| 2753 |
surround(tail) = 0; |
| 2754 |
} |
| 2755 |
break ; |
| 2756 |
case 2 : |
| 2757 |
/*tex always right */ |
| 2758 |
case 3 : |
| 2759 |
/*tex always both */ |
| 2760 |
case 6 : |
| 2761 |
/*tex only when skip */ |
| 2762 |
copy_glue_values(tail,math_skip_par); |
| 2763 |
surround(tail) = 0; |
| 2764 |
break ; |
| 2765 |
case 1 : |
| 2766 |
/*tex only left */ |
| 2767 |
surround(tail) = 0; |
| 2768 |
break ; |
| 2769 |
case 4 : |
| 2770 |
/*tex ignore, obey marthsurround */ |
| 2771 |
break ; |
| 2772 |
case 5: |
| 2773 |
/*tex all spacing disabled */ |
| 2774 |
surround(tail) = 0; |
| 2775 |
break ; |
| 2776 |
} |
| 2777 |
/*tex end mathskip code */ |
| 2778 |
space_factor_par = 1000; |
| 2779 |
unsave_math(); |
| 2780 |
} else { |
| 2781 |
if (a == null) { |
| 2782 |
if (cur_cmd == math_shift_cmd) { |
| 2783 |
check_second_math_shift(); |
| 2784 |
} else { |
| 2785 |
check_display_math_end(); |
| 2786 |
} |
| 2787 |
} |
| 2788 |
run_mlist_to_hlist(p, false, display_style); |
| 2789 |
finish_displayed_math(l, a, vlink(temp_head)); |
| 2790 |
} |
| 2791 |
} |
| 2792 |
|
| 2793 |
/*tex |
| 2794 |
|
| 2795 |
When \.{\\halign} appears in a display, the alignment routines operate |
| 2796 |
essentially as they do in vertical mode. Then the following program is |
| 2797 |
activated, with |p| and |q| pointing to the beginning and end of the |
| 2798 |
resulting list, and with |aux_save| holding the |prev_depth| value. |
| 2799 |
|
| 2800 |
*/ |
| 2801 |
|
| 2802 |
void finish_display_alignment(pointer p, pointer q, halfword saved_prevdepth) |
| 2803 |
{ |
| 2804 |
do_assignments(); |
| 2805 |
RECHECK: |
| 2806 |
if (cur_cmd == math_shift_cmd) { |
| 2807 |
check_second_math_shift(); |
| 2808 |
} else if (suppress_mathpar_error_par && cur_cmd == par_end_cmd) { |
| 2809 |
get_x_token(); |
| 2810 |
goto RECHECK; |
| 2811 |
} else { |
| 2812 |
check_display_math_end(); |
| 2813 |
} |
| 2814 |
pop_nest(); |
| 2815 |
tail_append(new_penalty(pre_display_penalty_par,before_display_penalty)); |
| 2816 |
inject_display_skip_before(above_display_skip_code); |
| 2817 |
vlink(tail) = p; |
| 2818 |
if (p != null) |
| 2819 |
tail = q; |
| 2820 |
tail_append(new_penalty(post_display_penalty_par,after_display_penalty)); |
| 2821 |
inject_display_skip_after(below_display_skip_code); |
| 2822 |
cur_list.prev_depth_field = saved_prevdepth; |
| 2823 |
resume_after_display(); |
| 2824 |
} |
| 2825 |
|
| 2826 |
/*tex Interface to \.{\\Umath} and \.{\\mathstyle}: */ |
| 2827 |
|
| 2828 |
void setup_math_style(void) |
| 2829 |
{ |
| 2830 |
pointer q; |
| 2831 |
tail_append(new_noad()); |
| 2832 |
q = new_node(math_char_node, 0); |
| 2833 |
nucleus(tail) = q; |
| 2834 |
(void) scan_math_style(nucleus(tail), num_style(m_style)); |
| 2835 |
} |
| 2836 |
|
| 2837 |
void print_math_style(void) |
| 2838 |
{ |
| 2839 |
if (abs(mode) == mmode) |
| 2840 |
print_int(m_style); |
| 2841 |
else |
| 2842 |
print_int(-1); |
| 2843 |
} |