| 1 |
% \iffalse meta-comment |
| 2 |
% |
| 3 |
%% Copyright (C) 2019--2022 by Marcel Krueger |
| 4 |
%% |
| 5 |
%% This file may be distributed and/or modified under the |
| 6 |
%% conditions of the LaTeX Project Public License, either |
| 7 |
%% version 1.3c of this license or (at your option) any later |
| 8 |
%% version. The latest version of this license is in: |
| 9 |
%% |
| 10 |
%% http://www.latex-project.org/lppl.txt |
| 11 |
%% |
| 12 |
%% and version 1.3 or later is part of all distributions of |
| 13 |
%% LaTeX version 2005/12/01 or later. |
| 14 |
% |
| 15 |
%<*batch> |
| 16 |
%<*gobble> |
| 17 |
\ifx\jobname\relax\let\documentclass\undefined\fi |
| 18 |
\ifx\documentclass\undefined |
| 19 |
\csname fi\endcsname |
| 20 |
%</gobble> |
| 21 |
\input l3docstrip.tex |
| 22 |
\keepsilent |
| 23 |
\preamble |
| 24 |
\endpreamble |
| 25 |
\generate{\file{luamathalign.sty}{\from{luamathalign.dtx}{package}}} |
| 26 |
\begingroup |
| 27 |
\def\MetaPrefix{--} |
| 28 |
\preamble |
| 29 |
\endpreamble |
| 30 |
\postamble |
| 31 |
\endpostamble |
| 32 |
\generate{\file{luamathalign.lua}{\from{luamathalign.dtx}{lua}}} |
| 33 |
\endgroup |
| 34 |
\endbatchfile |
| 35 |
%</batch> |
| 36 |
%<*gobble> |
| 37 |
\fi |
| 38 |
\expandafter\ifx\csname @currname\endcsname\empty |
| 39 |
\csname fi\endcsname |
| 40 |
%</gobble> |
| 41 |
%<*driver> |
| 42 |
\documentclass[full]{l3doc} |
| 43 |
\usepackage{luamathalign} |
| 44 |
\usepackage{csquotes} |
| 45 |
\MakeShortVerb{\|} |
| 46 |
\setlength\IndexMin{100pt} |
| 47 |
\begin{document} |
| 48 |
\DocInput{luamathalign.dtx} |
| 49 |
\PrintIndex |
| 50 |
\PrintChanges |
| 51 |
\end{document} |
| 52 |
%</driver> |
| 53 |
%<*gobble> |
| 54 |
\fi |
| 55 |
%</gobble> |
| 56 |
% \fi |
| 57 |
% |
| 58 |
% \GetFileInfo{luamathalign.dtx} |
| 59 |
% \title{The \pkg{luamathalign} package\thanks{This document |
| 60 |
% corresponds to \pkg{luamathalign}~v0.3, dated~2022-05-04.}} |
| 61 |
% \author{Marcel Kr\"uger \\ \href{mailto:[email protected]}{[email protected]}} |
| 62 |
% |
| 63 |
% \maketitle |
| 64 |
% |
| 65 |
% \begin{documentation} |
| 66 |
% \section{The problem} |
| 67 |
% In most cases, |amsmath| makes it simple to align multiple equations in a |
| 68 |
% |align| environment. But sometimes, special requirements come up. |
| 69 |
% |
| 70 |
% Maybe one of your alignment points is in an exponent, or in a radical? |
| 71 |
% The first attempts for such alignments often fail. For example, assume that |
| 72 |
% you want to align the following radicals like this (at the $x^3$ term): |
| 73 |
% \begin{align*} |
| 74 |
% \sqrt{1-3x+3x^2+(\AlignHere x-1)^3}\\ |
| 75 |
% =\sqrt{1-3x+3x^2+{\AlignHere x}^3-3x^2+3x-1}\\ |
| 76 |
% =\sqrt{\AlignHere x^3} |
| 77 |
% \end{align*} |
| 78 |
% \enquote{Just adding \texttt{\&} at the alignment points} doesn't work: |
| 79 |
% \begin{verbatim} |
| 80 |
% \begin{align*} |
| 81 |
% \sqrt{1-3x+3x^2+(&x-1)^3}\\ |
| 82 |
% =\sqrt{1-3x+3x^2+&x^3-3x^2+3x-1}\\ |
| 83 |
% =\sqrt{&x^3} |
| 84 |
% \end{align*} |
| 85 |
% \end{verbatim} |
| 86 |
% fails with |
| 87 |
% \begin{verbatim} |
| 88 |
% ! Missing } inserted. |
| 89 |
% <inserted text> |
| 90 |
% } |
| 91 |
% l.73 \end{align*} |
| 92 |
% \end{verbatim} |
| 93 |
% |
| 94 |
% Another problem are nested alignments. Take this sample from \href{https://tex.stackexchange.com/questions/68547/alignment-across-nested-aligned-environments}{anonymous on \TeX\ -- \LaTeX\ StackExchange}: We want alignment like |
| 95 |
% \begin{align*} |
| 96 |
% aaaa &= 1 &&\text{for $X$} \\ |
| 97 |
% bbbb &= 1 &&\text{for $Y$} \\ |
| 98 |
% \left. \begin{aligned} |
| 99 |
% c \SetAlignmentPoint1 &= 1 \\ |
| 100 |
% d &= 12 |
| 101 |
% \end{aligned} \right\}\ExecuteAlignment1 &&\text{for $Z$} |
| 102 |
% \end{align*} |
| 103 |
% but in |
| 104 |
% \begin{verbatim} |
| 105 |
% \begin{align*} |
| 106 |
% aaaa &= 1 &&\text{for $X$} \\ |
| 107 |
% bbbb &= 1 &&\text{for $Y$} \\ |
| 108 |
% \left. \begin{aligned} |
| 109 |
% c &= 1 \\ |
| 110 |
% d &= 12 |
| 111 |
% \end{aligned} \right\}&&&\text{for $Z$} |
| 112 |
% \end{align*} |
| 113 |
% \end{verbatim} |
| 114 |
% there is not obvious way to align the equal signs in the nested |aligned| with the outer signs. |
| 115 |
% \section{The solution} |
| 116 |
% \pkg{luamathalign} provides solutions for both problems under Lua\LaTeX: |
| 117 |
% \begin{function}{\AlignHere} |
| 118 |
% The most important new macro is \cmd\AlignHere: |
| 119 |
% It generates an alignment point like \texttt{\&}, |
| 120 |
% but it can be used almost everywhere. |
| 121 |
% |
| 122 |
% So problems like our first example can be implemented by just using \cmd\AlignHere\ instead of \texttt{\&}: |
| 123 |
% \begin{verbatim} |
| 124 |
% \begin{align*} |
| 125 |
% \sqrt{1-3x+3x^2+(\AlignHere x-1)^3}\\ |
| 126 |
% =\sqrt{1-3x+3x^2+{\AlignHere x}^3-3x^2+3x-1}\\ |
| 127 |
% =\sqrt{\AlignHere x^3} |
| 128 |
% \end{align*} |
| 129 |
% \end{verbatim} |
| 130 |
% \begin{align*} |
| 131 |
% \sqrt{1-3x+3x^2+(\AlignHere x-1)^3}\\ |
| 132 |
% =\sqrt{1-3x+3x^2+{\AlignHere x}^3-3x^2+3x-1}\\ |
| 133 |
% =\sqrt{\AlignHere x^3} |
| 134 |
% \end{align*} |
| 135 |
% \end{function} |
| 136 |
% Sadly, this doesn't really help with the nested alignment problem: |
| 137 |
% Even if we use \cmd\AlignHere\ in the \env{aligned} environment, the alignment points |
| 138 |
% would be inserted in the inner and not in the outer alignment. |
| 139 |
% For such cases, there is a variant which allows to specify at which level the alignment should happen: |
| 140 |
% \begin{function}{\SetAlignmentPoint,\ExecuteAlignment} |
| 141 |
% The primary command for this is \cmd\SetAlignmentPoint\meta{number}. When called with a negative number it specifies the nesting level. |
| 142 |
% For example when \meta{number} is -1 it is the same as \cmd\AlignHere, while for -2 it is aligning one level higher and so on. |
| 143 |
% |
| 144 |
% For example, our nested alignment above wanted to align the inner \env{aligned} and the outer \env{align*} at the same point, |
| 145 |
% so |\SetAlignmentPoint-2| is used directly next to a inner alignment point (here |&|, \cmd\AlignHere\ would work too). |
| 146 |
% Then the \cmd\ExecuteAlignment\ has to appear in the context of the outer \env{align*}, so it can be written e.g. directly before the next |&| of the outer \env{align*}: |
| 147 |
% \begin{verbatim} |
| 148 |
% \begin{align*} |
| 149 |
% aaaa &= 1 &&\text{for $X$} \\ |
| 150 |
% bbbb &= 1 &&\text{for $Y$} \\ |
| 151 |
% \left. \begin{aligned} |
| 152 |
% c \SetAlignmentPoint-2 &= 1 \\ |
| 153 |
% d &= 12 |
| 154 |
% \end{aligned} \right\}&&\text{for $Z$} |
| 155 |
% \end{align*} |
| 156 |
% \end{verbatim} |
| 157 |
% \begin{align*} |
| 158 |
% aaaa &= 1 &&\text{for $X$} \\ |
| 159 |
% bbbb &= 1 &&\text{for $Y$} \\ |
| 160 |
% \left. \begin{aligned} |
| 161 |
% c \SetAlignmentPoint-2 &= 1 \\ |
| 162 |
% d &= 12 |
| 163 |
% \end{aligned} \right\}&&\text{for $Z$} |
| 164 |
% \end{align*} |
| 165 |
% |
| 166 |
% If you do not want to keep track of the right nesting level you can explicitly mark a level and refer to it. |
| 167 |
% To do so, use a non-negative \meta{number}. |
| 168 |
% When \cmd\SetAlignmentPoint\ is used with a non-negative \meta{number} then \cmd\ExecuteAlignment\meta{number} |
| 169 |
% must be executed afterwards with the same \meta{number} at a point where adding a |&| would add a valid alignment point at the right level. |
| 170 |
% |
| 171 |
% Our example above could therefore also be written as |
| 172 |
% \begin{verbatim} |
| 173 |
% \begin{align*} |
| 174 |
% aaaa &= 1 &&\text{for $X$} \\ |
| 175 |
% bbbb &= 1 &&\text{for $Y$} \\ |
| 176 |
% \left. \begin{aligned} |
| 177 |
% c \SetAlignmentPoint0 &= 1 \\ |
| 178 |
% d &= 12 |
| 179 |
% \end{aligned} \right\}\ExecuteAlignment0 &&\text{for $Z$} |
| 180 |
% \end{align*} |
| 181 |
% \end{verbatim} |
| 182 |
% \begin{align*} |
| 183 |
% aaaa &= 1 &&\text{for $X$} \\ |
| 184 |
% bbbb &= 1 &&\text{for $Y$} \\ |
| 185 |
% \left. \begin{aligned} |
| 186 |
% c \SetAlignmentPoint0 &= 1 \\ |
| 187 |
% d &= 12 |
| 188 |
% \end{aligned} \right\}\ExecuteAlignment0 &&\text{for $Z$} |
| 189 |
% \end{align*} |
| 190 |
% |
| 191 |
% This variant is also useful when working with custom alignment environment not prepared to work with luamathalign. |
| 192 |
% By default \cmd\SetAlignmentPoint\meta{number} with negative numbers (and therefore also \cmd\AlignHere) only work with |
| 193 |
% amsmath's \texttt{\{align\}}, \texttt{\{aligned\}} and their variants. |
| 194 |
% If you have another environment which also follows similar alignment rules then you can either restrict yourself to |
| 195 |
% non-negative \meta{number}s in combination with \cmd\ExecuteAlignment\ or patch these environments similar to what luamathalign does for amsmath. |
| 196 |
% \end{function} |
| 197 |
% \end{documentation} |
| 198 |
% \begin{implementation} |
| 199 |
% \section{The implementation} |
| 200 |
% \subsection{Lua} |
| 201 |
% \iffalse |
| 202 |
%<*gobble> |
| 203 |
\RequirePackage{docstrip-luacode} |
| 204 |
\begin{docstrip-luacode}{luamathalign} |
| 205 |
%</gobble> |
| 206 |
%<*lua> |
| 207 |
% \fi |
| 208 |
% \begin{macrocode} |
| 209 |
local properties = node.get_properties_table() |
| 210 |
local luacmd = require'luamathalign-luacmd' |
| 211 |
local hlist = node.id'hlist' |
| 212 |
local vlist = node.id'vlist' |
| 213 |
local whatsit = node.id'whatsit' |
| 214 |
local glue = node.id'glue' |
| 215 |
local user_defined = node.subtype'user_defined' |
| 216 |
local whatsit_id = luatexbase.new_whatsit'mathalign' |
| 217 |
local node_cmd = token.command_id'node' |
| 218 |
local ampersand = token.new(38, 4) |
| 219 |
|
| 220 |
local mmode do |
| 221 |
for k,v in next, tex.getmodevalues() do |
| 222 |
if v == 'math' then mmode = k end |
| 223 |
end |
| 224 |
assert(mmode) |
| 225 |
end |
| 226 |
|
| 227 |
-- We might want to add y later |
| 228 |
local function is_marked(mark, list) |
| 229 |
for n in node.traverse(list) do |
| 230 |
local id = n.id |
| 231 |
if id == hlist or id == vlist then |
| 232 |
if is_marked(mark, n.head) then return true end |
| 233 |
elseif id == whatsit and n.subtype == user_defined |
| 234 |
and n.user_id == whatsit_id and n.value == mark then |
| 235 |
return true |
| 236 |
end |
| 237 |
end |
| 238 |
return false |
| 239 |
end |
| 240 |
local function assert_unmarked(mark, list, ...) |
| 241 |
local marked = is_marked(mark, list) |
| 242 |
if marked then |
| 243 |
tex.error("Multiple alignment marks", "I found multiple alignment marks \z |
| 244 |
of type " .. mark .. " in an alignment where I already had an \z |
| 245 |
alignment mark of that type. You should look at both of them and \z |
| 246 |
decide which one is right. I will continue with the first one for now.") |
| 247 |
end |
| 248 |
return ... |
| 249 |
end |
| 250 |
local measure do |
| 251 |
local vmeasure |
| 252 |
local function hmeasure(mark, list) |
| 253 |
local x, last = 0, list.head |
| 254 |
for n in node.traverse(last) do |
| 255 |
local id = n.id |
| 256 |
if id == hlist then |
| 257 |
local w, h, d = node.rangedimensions(list, last, n) |
| 258 |
x, last = x + w, n |
| 259 |
local dx = hmeasure(mark, n) |
| 260 |
if dx then return assert_unmarked(mark, n.next, dx + x) end |
| 261 |
elseif id == vlist then |
| 262 |
local w, h, d = node.rangedimensions(list, last, n) |
| 263 |
x, last = x + w, n |
| 264 |
local dx = vmeasure(mark, n) |
| 265 |
if dx then return assert_unmarked(mark, n.next, dx + x) end |
| 266 |
elseif id == whatsit and n.subtype == user_defined |
| 267 |
and n.user_id == whatsit_id and n.value == mark then |
| 268 |
local w, h, d = node.rangedimensions(list, last, n) |
| 269 |
local after |
| 270 |
list.head, after = node.remove(list.head, n) |
| 271 |
return assert_unmarked(mark, after, x + w) |
| 272 |
end |
| 273 |
end |
| 274 |
end |
| 275 |
function vmeasure(mark, list) |
| 276 |
for n in node.traverse(list.head) do |
| 277 |
local id = n.id |
| 278 |
if id == hlist then |
| 279 |
local dx = hmeasure(mark, n) |
| 280 |
if dx then return assert_unmarked(mark, n.next, dx + n.shift) end |
| 281 |
elseif id == vlist then |
| 282 |
local dx = vmeasure(mark, n) |
| 283 |
if dx then return assert_unmarked(mark, n.next, dx + n.shift) end |
| 284 |
elseif id == whatsit and n.subtype == user_defined |
| 285 |
and n.user_id == whatsit_id and n.value == mark then |
| 286 |
local after |
| 287 |
list.head, after = node.remove(list.head, n) |
| 288 |
return assert_unmarked(mark, after, 0) |
| 289 |
end |
| 290 |
end |
| 291 |
end |
| 292 |
function measure(mark, head) |
| 293 |
local x, last = 0, head |
| 294 |
for n in node.traverse(last) do |
| 295 |
local id = n.id |
| 296 |
if id == hlist then |
| 297 |
local w, h, d = node.dimensions(last, n) |
| 298 |
x, last = x + w, n |
| 299 |
local dx = hmeasure(mark, n) |
| 300 |
if dx then return assert_unmarked(mark, n.next, head, dx + x) end |
| 301 |
elseif id == vlist then |
| 302 |
local w, h, d = node.dimensions(last, n) |
| 303 |
x, last = x + w, n |
| 304 |
local dx = vmeasure(mark, n) |
| 305 |
if dx then return assert_unmarked(mark, n.next, head, dx + x) end |
| 306 |
elseif id == whatsit and n.subtype == user_defined |
| 307 |
and n.user_id == whatsit_id and n.value == mark then |
| 308 |
local w, h, d = node.dimensions(last, n) |
| 309 |
local after |
| 310 |
head, after = node.remove(head, n) |
| 311 |
return assert_unmarked(mark, after, head, x + w) |
| 312 |
end |
| 313 |
end |
| 314 |
return head |
| 315 |
end |
| 316 |
end |
| 317 |
|
| 318 |
local isolate do |
| 319 |
local visolate |
| 320 |
local function hisolate(list, offset) |
| 321 |
local x, last = 0, list.head |
| 322 |
local newhead, newtail = nil, nil |
| 323 |
local n = last |
| 324 |
while n do |
| 325 |
local id = n.id |
| 326 |
if id == hlist then |
| 327 |
local w, h, d = node.rangedimensions(list, last, n) |
| 328 |
x, last = x + w, n |
| 329 |
local inner_head, inner_tail, new_offset = hisolate(n, offset - x) |
| 330 |
if inner_head then |
| 331 |
if newhead then |
| 332 |
newtail.next, inner_head.prev = inner_head, newtail |
| 333 |
else |
| 334 |
newhead = inner_head |
| 335 |
end |
| 336 |
newtail = inner_tail |
| 337 |
offset = x + new_offset |
| 338 |
end |
| 339 |
n = n.next |
| 340 |
elseif id == vlist then |
| 341 |
local w, h, d = node.rangedimensions(list, last, n) |
| 342 |
x, last = x + w, n |
| 343 |
local inner_head, inner_tail, new_offset = visolate(n, offset - x) |
| 344 |
if inner_head then |
| 345 |
if newhead then |
| 346 |
newtail.next, inner_head.prev = inner_head, newtail |
| 347 |
else |
| 348 |
newhead = inner_head |
| 349 |
end |
| 350 |
newtail = inner_tail |
| 351 |
offset = x + new_offset |
| 352 |
end |
| 353 |
n = n.next |
| 354 |
elseif id == whatsit and n.subtype == user_defined |
| 355 |
and n.user_id == whatsit_id then |
| 356 |
local w, h, d = node.rangedimensions(list, last, n) |
| 357 |
x = x + w |
| 358 |
list.head, last = node.remove(list.head, n) |
| 359 |
if x ~= offset then |
| 360 |
local k = node.new(glue) |
| 361 |
k.width, offset = x - offset, x |
| 362 |
newhead, newtail = node.insert_after(newhead, newtail, k) |
| 363 |
end |
| 364 |
newhead, newtail = node.insert_after(newhead, newtail, n) |
| 365 |
n = last |
| 366 |
else |
| 367 |
n = n.next |
| 368 |
end |
| 369 |
end |
| 370 |
return newhead, newtail, offset |
| 371 |
end |
| 372 |
function visolate(list, offset) |
| 373 |
local newhead, newtail = nil, nil |
| 374 |
local n = list.head |
| 375 |
while n do |
| 376 |
local id = n.id |
| 377 |
if id == hlist then |
| 378 |
if dx then return assert_unmarked(mark, n.next, dx + n.shift) end |
| 379 |
local inner_head, inner_tail, new_offset = hisolate(n, offset) |
| 380 |
if inner_head then |
| 381 |
if newhead then |
| 382 |
newtail.next, inner_head.prev = inner_head, newtail |
| 383 |
else |
| 384 |
newhead = inner_head |
| 385 |
end |
| 386 |
newtail = inner_tail |
| 387 |
offset = new_offset |
| 388 |
end |
| 389 |
n = n.next |
| 390 |
elseif id == vlist then |
| 391 |
if dx then return assert_unmarked(mark, n.next, dx + n.shift) end |
| 392 |
local inner_head, inner_tail, new_offset = visolate(n, offset) |
| 393 |
if inner_head then |
| 394 |
if newhead then |
| 395 |
newtail.next, inner_head.prev = inner_head, newtail |
| 396 |
else |
| 397 |
newhead = inner_head |
| 398 |
end |
| 399 |
newtail = inner_tail |
| 400 |
offset = new_offset |
| 401 |
end |
| 402 |
n = n.next |
| 403 |
elseif id == whatsit and n.subtype == user_defined |
| 404 |
and n.user_id == whatsit_id then |
| 405 |
local after |
| 406 |
list.head, after = node.remove(list.head, n) |
| 407 |
if 0 ~= offset then |
| 408 |
local k = node.new(glue) |
| 409 |
k.width, offset = -offset, 0 |
| 410 |
newhead, newtail = node.insert_after(newhead, newtail, k) |
| 411 |
end |
| 412 |
newhead, newtail = node.insert_after(newhead, newtail, n) |
| 413 |
n = last |
| 414 |
else |
| 415 |
n = n.next |
| 416 |
end |
| 417 |
end |
| 418 |
return newhead, newtail, offset |
| 419 |
end |
| 420 |
function isolate(head) |
| 421 |
local x, last = 0, head |
| 422 |
local newhead, newtail, offset = nil, nil, 0 |
| 423 |
local n = last |
| 424 |
while n do |
| 425 |
local id = n.id |
| 426 |
if id == hlist then |
| 427 |
local w, h, d = node.dimensions(last, n) |
| 428 |
x, last = x + w, n |
| 429 |
local inner_head, inner_tail, new_offset = hisolate(n, offset - x) |
| 430 |
if inner_head then |
| 431 |
if newhead then |
| 432 |
newtail.next, inner_head.prev = inner_head, newtail |
| 433 |
else |
| 434 |
newhead = inner_head |
| 435 |
end |
| 436 |
newtail = inner_tail |
| 437 |
offset = x + new_offset |
| 438 |
end |
| 439 |
n = n.next |
| 440 |
elseif id == vlist then |
| 441 |
local w, h, d = node.dimensions(last, n) |
| 442 |
x, last = x + w, n |
| 443 |
local inner_head, inner_tail, new_offset = visolate(n, offset - x) |
| 444 |
if inner_head then |
| 445 |
if newhead then |
| 446 |
newtail.next, inner_head.prev = inner_head, newtail |
| 447 |
else |
| 448 |
newhead = inner_head |
| 449 |
end |
| 450 |
newtail = inner_tail |
| 451 |
offset = x + new_offset |
| 452 |
end |
| 453 |
n = n.next |
| 454 |
elseif id == whatsit and n.subtype == user_defined |
| 455 |
and n.user_id == whatsit_id then |
| 456 |
local w, h, d = node.dimensions(last, n) |
| 457 |
x = x + w |
| 458 |
head, last = node.remove(head, n) |
| 459 |
if x ~= offset then |
| 460 |
local k = node.new(glue) |
| 461 |
k.width, offset = x - offset, x |
| 462 |
newhead, newtail = node.insert_after(newhead, newtail, k) |
| 463 |
end |
| 464 |
newhead, newtail = node.insert_after(newhead, newtail, n) |
| 465 |
n = last |
| 466 |
else |
| 467 |
n = n.next |
| 468 |
end |
| 469 |
end |
| 470 |
return head, newhead |
| 471 |
end |
| 472 |
end |
| 473 |
|
| 474 |
local function find_mmode_boundary() |
| 475 |
for i=tex.nest.ptr,0,-1 do |
| 476 |
local nest = tex.nest[i] |
| 477 |
if nest.mode ~= mmode and nest.mode ~= -mmode then |
| 478 |
return nest, i |
| 479 |
end |
| 480 |
end |
| 481 |
end |
| 482 |
|
| 483 |
luatexbase.add_to_callback('post_mlist_to_hlist_filter', function(n) |
| 484 |
local nest = find_mmode_boundary() |
| 485 |
local props = properties[nest.head] |
| 486 |
local alignment = props and props.luamathalign_alignment |
| 487 |
if alignment then |
| 488 |
props.luamathalign_alignment = nil |
| 489 |
local x |
| 490 |
n, x = measure(alignment.mark, n) |
| 491 |
local k = node.new'glue' |
| 492 |
local off = x - n.width |
| 493 |
k.width, alignment.afterkern.width = off, -off |
| 494 |
node.insert_after(n.head, nil, k) |
| 495 |
n.width = x |
| 496 |
end |
| 497 |
return n |
| 498 |
end, 'luamathalign') |
| 499 |
|
| 500 |
% \end{macrocode} |
| 501 |
% The glue node is referred to as a kern for historical reasons. A glue node is |
| 502 |
% used since this interacts better with lua-ul. |
| 503 |
% \begin{macrocode} |
| 504 |
local function get_kerntoken(newmark) |
| 505 |
local nest = find_mmode_boundary() |
| 506 |
local props = properties[nest.head] |
| 507 |
if not props then |
| 508 |
props = {} |
| 509 |
properties[nest.head] = props |
| 510 |
end |
| 511 |
if props.luamathalign_alignment then |
| 512 |
tex.error('Multiple alignment classes trying to control the same cell') |
| 513 |
return token.new(0, 0) |
| 514 |
else |
| 515 |
local afterkern = node.new'glue' |
| 516 |
props.luamathalign_alignment = {mark = newmark, afterkern = afterkern} |
| 517 |
return token.new(node.direct.todirect(afterkern), node_cmd) |
| 518 |
end |
| 519 |
end |
| 520 |
|
| 521 |
local function insert_whatsit(mark) |
| 522 |
local n = node.new(whatsit, user_defined) |
| 523 |
n.user_id, n.type, n.value = whatsit_id, string.byte'd', mark |
| 524 |
node.write(n) |
| 525 |
end |
| 526 |
luacmd("SetAlignmentPoint", function() |
| 527 |
local mark = token.scan_int() |
| 528 |
if mark < 0 then |
| 529 |
for i=tex.nest.ptr,0,-1 do |
| 530 |
local t = tex.nest[i].head |
| 531 |
local props = properties[t] |
| 532 |
if props and props.luamathalign_context ~= nil then |
| 533 |
mark = mark + 1 |
| 534 |
if mark == 0 then |
| 535 |
props.luamathalign_context = true |
| 536 |
return insert_whatsit(-i) |
| 537 |
end |
| 538 |
end |
| 539 |
end |
| 540 |
tex.error('No compatible alignment environment found', |
| 541 |
'This either means that \\SetAlignmentPoint was used outside\n\z |
| 542 |
of an alignment or the used alignment is not setup for use with\n\z |
| 543 |
luamathalign. In the latter case you might want to look at\n\z |
| 544 |
non-negative alignment marks.') |
| 545 |
else |
| 546 |
return insert_whatsit(mark) |
| 547 |
end |
| 548 |
end, "protected") |
| 549 |
|
| 550 |
function handle_whatsit(mark) |
| 551 |
token.put_next(ampersand, get_kerntoken(mark)) |
| 552 |
end |
| 553 |
luacmd("ExecuteAlignment", function() |
| 554 |
return handle_whatsit(token.scan_int()) |
| 555 |
end, "protected") |
| 556 |
|
| 557 |
luacmd("LuaMathAlign@begin", function() |
| 558 |
local t = tex.nest.top.head |
| 559 |
local props = properties[t] |
| 560 |
if not props then |
| 561 |
props = {} |
| 562 |
properties[t] = props |
| 563 |
end |
| 564 |
props.luamathalign_context = false |
| 565 |
end, "protected") |
| 566 |
luacmd("LuaMathAlign@end@early", function() |
| 567 |
local t = tex.nest.top.head |
| 568 |
local props = properties[t] |
| 569 |
if props then |
| 570 |
if props.luamathalign_context == true then |
| 571 |
handle_whatsit(-tex.nest.ptr) |
| 572 |
end |
| 573 |
props.luamathalign_context = nil |
| 574 |
end |
| 575 |
end, "protected") |
| 576 |
local delayed |
| 577 |
luacmd("LuaMathAlign@end", function() |
| 578 |
local t = tex.nest.top.head |
| 579 |
local props = properties[t] |
| 580 |
if props then |
| 581 |
if props.luamathalign_context == true then |
| 582 |
assert(not delayed) |
| 583 |
delayed = {get_kerntoken(-tex.nest.ptr), ampersand} |
| 584 |
end |
| 585 |
props.luamathalign_context = nil |
| 586 |
end |
| 587 |
end, "protected") |
| 588 |
luatexbase.add_to_callback("hpack_filter", function(head, groupcode) |
| 589 |
if delayed and groupcode == "align_set" then |
| 590 |
-- HACK: token.put_next puts the tokens into the input stream after the cell |
| 591 |
-- is fully read, before the next starts. This will act as if the content was |
| 592 |
-- written as the first element of the next field. |
| 593 |
token.put_next(delayed) |
| 594 |
delayed = nil |
| 595 |
end |
| 596 |
return true |
| 597 |
end, "luamathalign.delayed") |
| 598 |
|
| 599 |
luacmd("LuaMathAlign@IsolateAlignmentPoints", function() |
| 600 |
local main = token.scan_int() |
| 601 |
if not token.scan_keyword 'into' then |
| 602 |
tex.error'Expected "into"' |
| 603 |
end |
| 604 |
local marks = token.scan_int() |
| 605 |
local head, newhead = isolate(tex.box[main]) |
| 606 |
tex.box[marks] = node.direct.tonode(node.direct.hpack( |
| 607 |
newhead and node.direct.todirect(newhead) or 0)) |
| 608 |
end, "protected") |
| 609 |
% \end{macrocode} |
| 610 |
% \subsection{LaTeX} |
| 611 |
% \iffalse |
| 612 |
%</lua> |
| 613 |
%<*gobble> |
| 614 |
\end{docstrip-luacode} |
| 615 |
%</gobble> |
| 616 |
%<*package> |
| 617 |
\NeedsTeXFormat{LaTeX2e} |
| 618 |
\ProvidesPackage |
| 619 |
{luamathalign} |
| 620 |
[2022-05-04 v0.3 additional math alignment tricks using Lua] |
| 621 |
% \fi |
| 622 |
% The actual \LaTeX\ package just loads the Lua module and patches \pkg{amsmath}: |
| 623 |
% \begin{macrocode} |
| 624 |
\RequirePackage{iftex} |
| 625 |
\RequireLuaTeX |
| 626 |
\directlua{require'luamathalign'} |
| 627 |
\IfPackageLoadedTF{amsmath}{% |
| 628 |
\@firstofone |
| 629 |
}{% |
| 630 |
\AddToHook{package/amsmath/after} |
| 631 |
} |
| 632 |
{% |
| 633 |
\def\align@preamble{% |
| 634 |
&\hfil |
| 635 |
\strut@ |
| 636 |
\setboxz@h{\@lign$\m@th\displaystyle{% |
| 637 |
\LuaMathAlign@begin##\LuaMathAlign@end}$}% |
| 638 |
\ifmeasuring@\savefieldlength@\fi |
| 639 |
\set@field |
| 640 |
\tabskip\z@skip |
| 641 |
&\setboxz@h{\@lign$\m@th\displaystyle{{}##}$}% |
| 642 |
\ifmeasuring@\savefieldlength@\fi |
| 643 |
\set@field |
| 644 |
\hfil |
| 645 |
\tabskip\alignsep@ |
| 646 |
} |
| 647 |
\renewcommand{\start@aligned}[2]{% |
| 648 |
\RIfM@\else |
| 649 |
\nonmatherr@{\begin{\@currenvir}}% |
| 650 |
\fi |
| 651 |
\savecolumn@ % Assumption: called inside a group |
| 652 |
\alignedspace@left |
| 653 |
\if #1t\vtop \else \if#1b \vbox \else \vcenter \fi \fi \bgroup |
| 654 |
\maxfields@#2\relax |
| 655 |
\ifnum\maxfields@>\m@ne |
| 656 |
\multiply\maxfields@\tw@ |
| 657 |
\let\math@cr@@@\math@cr@@@alignedat |
| 658 |
\alignsep@\z@skip |
| 659 |
\else |
| 660 |
\let\math@cr@@@\math@cr@@@aligned |
| 661 |
\alignsep@\minalignsep |
| 662 |
\fi |
| 663 |
\Let@ \chardef\dspbrk@context\@ne |
| 664 |
\default@tag |
| 665 |
\spread@equation % no-op if already called |
| 666 |
\global\column@\z@ |
| 667 |
\ialign\bgroup |
| 668 |
&\column@plus |
| 669 |
\hfil |
| 670 |
\strut@ |
| 671 |
$\m@th\displaystyle{\LuaMathAlign@begin##\LuaMathAlign@end}$% |
| 672 |
\tabskip\z@skip |
| 673 |
&\column@plus |
| 674 |
$\m@th\displaystyle{{}##}$% |
| 675 |
\hfil |
| 676 |
\tabskip\alignsep@ |
| 677 |
\crcr |
| 678 |
} |
| 679 |
\edef\math@cr@@@alignedat{\LuaMathAlign@end@early |
| 680 |
\unexpanded\expandafter{\math@cr@@@alignedat}} |
| 681 |
\edef\math@cr{\LuaMathAlign@end@early |
| 682 |
\unexpanded\expandafter{\math@cr}} |
| 683 |
\edef\endaligned{\LuaMathAlign@end@early |
| 684 |
\unexpanded\expandafter{\endaligned}} |
| 685 |
} |
| 686 |
\protected\def\AlignHere{\SetAlignmentPoint\m@ne} |
| 687 |
\begingroup |
| 688 |
\def\patch@finph@nt\setbox\tw@\null{% |
| 689 |
\LuaMathAlign@IsolateAlignmentPoints\z@ into \tw@ |
| 690 |
}% |
| 691 |
\expanded{\endgroup% |
| 692 |
\protected\def\noexpand\finph@nt{% |
| 693 |
\unexpanded\expandafter\expandafter\expandafter{% |
| 694 |
\expandafter\patch@finph@nt\finph@nt |
| 695 |
}% |
| 696 |
}} |
| 697 |
\ExplSyntaxOff |
| 698 |
% \end{macrocode} |
| 699 |
% \iffalse |
| 700 |
%</package> |
| 701 |
% \fi |
| 702 |
% \end{implementation} |
| 703 |
\endinput |