summaryrefslogtreecommitdiff
path: root/macros/luatex/latex/luamathalign/luamathalign.dtx
diff options
context:
space:
mode:
Diffstat (limited to 'macros/luatex/latex/luamathalign/luamathalign.dtx')
-rw-r--r--macros/luatex/latex/luamathalign/luamathalign.dtx112
1 files changed, 80 insertions, 32 deletions
diff --git a/macros/luatex/latex/luamathalign/luamathalign.dtx b/macros/luatex/latex/luamathalign/luamathalign.dtx
index 401c8ef7ea..1a1ebc614e 100644
--- a/macros/luatex/latex/luamathalign/luamathalign.dtx
+++ b/macros/luatex/latex/luamathalign/luamathalign.dtx
@@ -57,7 +57,7 @@
%
% \GetFileInfo{luamathalign.dtx}
% \title{The \pkg{luamathalign} package\thanks{This document
-% corresponds to \pkg{luamathalign}~v0.1, dated~2022-04-18.}}
+% corresponds to \pkg{luamathalign}~v0.3, dated~2022-05-04.}}
% \author{Marcel Kr\"uger \\ \href{mailto:tex@2krueger.de}{tex@2krueger.de}}
%
% \maketitle
@@ -206,16 +206,24 @@
%<*lua>
% \fi
% \begin{macrocode}
+local properties = node.get_properties_table()
local luacmd = require'luamathalign-luacmd'
-local luaprop = require'luamathalign-luaprop'('mathalign')
local hlist = node.id'hlist'
local vlist = node.id'vlist'
local whatsit = node.id'whatsit'
-local kern = node.id'kern'
+local glue = node.id'glue'
local user_defined = node.subtype'user_defined'
local whatsit_id = luatexbase.new_whatsit'mathalign'
local node_cmd = token.command_id'node'
local ampersand = token.new(38, 4)
+
+local mmode do
+ for k,v in next, tex.getmodevalues() do
+ if v == 'math' then mmode = k end
+ end
+ assert(mmode)
+end
+
-- We might want to add y later
local function is_marked(mark, list)
for n in node.traverse(list) do
@@ -349,8 +357,8 @@ local isolate do
x = x + w
list.head, last = node.remove(list.head, n)
if x ~= offset then
- local k = node.new(kern)
- k.kern, offset = x - offset, x
+ local k = node.new(glue)
+ k.width, offset = x - offset, x
newhead, newtail = node.insert_after(newhead, newtail, k)
end
newhead, newtail = node.insert_after(newhead, newtail, n)
@@ -397,8 +405,8 @@ local isolate do
local after
list.head, after = node.remove(list.head, n)
if 0 ~= offset then
- local k = node.new(kern)
- k.kern, offset = -offset, 0
+ local k = node.new(glue)
+ k.width, offset = -offset, 0
newhead, newtail = node.insert_after(newhead, newtail, k)
end
newhead, newtail = node.insert_after(newhead, newtail, n)
@@ -449,8 +457,8 @@ local isolate do
x = x + w
head, last = node.remove(head, n)
if x ~= offset then
- local k = node.new(kern)
- k.kern, offset = x - offset, x
+ local k = node.new(glue)
+ k.width, offset = x - offset, x
newhead, newtail = node.insert_after(newhead, newtail, k)
end
newhead, newtail = node.insert_after(newhead, newtail, n)
@@ -463,25 +471,51 @@ local isolate do
end
end
-local mark, afterkern
+local function find_mmode_boundary()
+ for i=tex.nest.ptr,0,-1 do
+ local nest = tex.nest[i]
+ if nest.mode ~= mmode and nest.mode ~= -mmode then
+ return nest, i
+ end
+ end
+end
+
luatexbase.add_to_callback('post_mlist_to_hlist_filter', function(n)
- if mark then
+ local nest = find_mmode_boundary()
+ local props = properties[nest.head]
+ local alignment = props and props.luamathalign_alignment
+ if alignment then
+ props.luamathalign_alignment = nil
local x
- n, x = measure(mark, n)
- local k = node.new'kern'
+ n, x = measure(alignment.mark, n)
+ local k = node.new'glue'
local off = x - n.width
- k.kern, afterkern.kern = off, -off
+ k.width, alignment.afterkern.width = off, -off
node.insert_after(n.head, nil, k)
n.width = x
- mark, afterkern = nil, nil
end
return n
end, 'luamathalign')
+% \end{macrocode}
+% The glue node is referred to as a kern for historical reasons. A glue node is
+% used since this interacts better with lua-ul.
+% \begin{macrocode}
local function get_kerntoken(newmark)
- assert(not mark)
- mark, afterkern = newmark, node.new'kern'
- return token.new(node.direct.todirect(afterkern), node_cmd)
+ local nest = find_mmode_boundary()
+ local props = properties[nest.head]
+ if not props then
+ props = {}
+ properties[nest.head] = props
+ end
+ if props.luamathalign_alignment then
+ tex.error('Multiple alignment classes trying to control the same cell')
+ return token.new(0, 0)
+ else
+ local afterkern = node.new'glue'
+ props.luamathalign_alignment = {mark = newmark, afterkern = afterkern}
+ return token.new(node.direct.todirect(afterkern), node_cmd)
+ end
end
local function insert_whatsit(mark)
@@ -494,10 +528,11 @@ luacmd("SetAlignmentPoint", function()
if mark < 0 then
for i=tex.nest.ptr,0,-1 do
local t = tex.nest[i].head
- if luaprop.query(t) ~= nil then
+ local props = properties[t]
+ if props and props.luamathalign_context ~= nil then
mark = mark + 1
if mark == 0 then
- luaprop.set(t, true)
+ props.luamathalign_context = true
return insert_whatsit(-i)
end
end
@@ -520,22 +555,34 @@ luacmd("ExecuteAlignment", function()
end, "protected")
luacmd("LuaMathAlign@begin", function()
- local nest = tex.nest.top
- luaprop.set(nest.head, false)
+ local t = tex.nest.top.head
+ local props = properties[t]
+ if not props then
+ props = {}
+ properties[t] = props
+ end
+ props.luamathalign_context = false
end, "protected")
luacmd("LuaMathAlign@end@early", function()
local t = tex.nest.top.head
- if luaprop.set(t, nil) == true then
- handle_whatsit(-tex.nest.ptr)
+ local props = properties[t]
+ if props then
+ if props.luamathalign_context == true then
+ handle_whatsit(-tex.nest.ptr)
+ end
+ props.luamathalign_context = nil
end
end, "protected")
local delayed
luacmd("LuaMathAlign@end", function()
- local nest = tex.nest.top
- local t = nest.head
- if luaprop.set(t, nil) == true then
- assert(not delayed)
- delayed = {get_kerntoken(-tex.nest.ptr), ampersand}
+ local t = tex.nest.top.head
+ local props = properties[t]
+ if props then
+ if props.luamathalign_context == true then
+ assert(not delayed)
+ delayed = {get_kerntoken(-tex.nest.ptr), ampersand}
+ end
+ props.luamathalign_context = nil
end
end, "protected")
luatexbase.add_to_callback("hpack_filter", function(head, groupcode)
@@ -552,7 +599,7 @@ end, "luamathalign.delayed")
luacmd("LuaMathAlign@IsolateAlignmentPoints", function()
local main = token.scan_int()
if not token.scan_keyword 'into' then
- tex.error'Expected "into"'
+ tex.error'Expected "into"'
end
local marks = token.scan_int()
local head, newhead = isolate(tex.box[main])
@@ -570,11 +617,12 @@ end, "protected")
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage
{luamathalign}
- [2022-04-18 v0.1 additional math alignment tricks using Lua]
+ [2022-05-04 v0.3 additional math alignment tricks using Lua]
% \fi
% The actual \LaTeX\ package just loads the Lua module and patches \pkg{amsmath}:
% \begin{macrocode}
-% \RequirePackage{scrlfile}
+\RequirePackage{iftex}
+\RequireLuaTeX
\directlua{require'luamathalign'}
\IfPackageLoadedTF{amsmath}{%
\@firstofone