我正在尝试使用 luatex 回调标记“单词”(包括标点符号)的结尾。(最后我想在这个位置注入空格字符,但示例使用了可视化规则)。
使用基于包\countwords
中的一些代码chickenize
,它工作得很好。我只在数学方面有问题:我想避免在那里得到分数。尝试使用属性集来做到这一点\everymath
不起作用,如下面的代码所示:它还会影响其他地方,例如内部使用数学模式的表格。
有人知道如何从标记函数中排除“真实”数学吗?
\documentclass{article}
\usepackage{luacode,amsmath}
\newattribute\mathattr
\begin{luacode}
local MATHATTR = luatexbase.registernumber ("mathattr")
local nodetraverseid = node.traverse_id
local nodegetattribute = node.get_attribute
local GLUE = node.id("glue")
local GLYPH = node.id("glyph")
local function insertmark (head,current)
local pdfstring = node.new("whatsit","pdf_literal")
pdfstring.data =
string.format("q 1 0 0 RG 1 0 0 rg 0.4 w 0 %g m 0 %g l S Q",-3,10)
head = node.insert_after(head,current,pdfstring)
return head
end
local markwords = function(head)
for glyph in nodetraverseid(GLYPH,head) do
if glyph.next and (glyph.next.id == GLUE)
and not nodegetattribute(glyph,MATHATTR)
then
insertmark(head,glyph)
end
end
return head
end
luatexbase.add_to_callback("pre_linebreak_filter",markwords,"markwords")
luatexbase.add_to_callback("hpack_filter",markwords,"markwords")
\end{luacode}
\usepackage{lipsum}
\begin{document}
\begin{tabular}{l}
xxx xxx
\end{tabular}
$a=b \int f(x) =0\quad \text{abc abc abc abc} $
\everymath{\setattribute\mathattr{1}}
\begin{tabular}{l}
xxx xxx
\end{tabular}
$a=b \int f(x) =0\quad \text{abc abc abc abc} $
\bigskip
VA VA
\lipsum[1]
\end{document}
答案1
数学内容被节点包围math
,子类型 0 标记数学的开始,子类型 1 标记数学的结束:
\documentclass{article}
\usepackage{luacode,amsmath}
\newattribute\mathattr
\begin{luacode}
local MATHATTR = luatexbase.registernumber ("mathattr")
local nodetraverseid = node.traverse_id
local nodetraverse = node.traverse
local nodegetattribute = node.get_attribute
local GLUE = node.id("glue")
local GLYPH = node.id("glyph")
local MATH = node.id("math")
local HLIST = node.id("hlist")
local VLIST = node.id("vlist")
local function insertmark (head,current)
local pdfstring = node.new("whatsit","pdf_literal")
pdfstring.data =
string.format("q 1 0 0 RG 1 0 0 rg 0.4 w 0 %g m 0 %g l S Q",-3,10)
head = node.insert_after(head,current,pdfstring)
return head
end
local function markwords(head)
local inside_math = false
for n in nodetraverse(head) do
local id = n.id
if id == GLYPH then
local glyph = n
if glyph.next and (glyph.next.id == GLUE)
and not (inside_math or nodegetattribute(glyph,MATHATTR))
then
insertmark(head,glyph)
end
elseif id == MATH then
inside_math = (n.subtype == 0)
end
end
return head
end
luatexbase.add_to_callback("pre_linebreak_filter",markwords,"markwords")
luatexbase.add_to_callback("hpack_filter",markwords,"markwords")
\end{luacode}
\usepackage{lipsum}
\begin{document}
\begin{tabular}{l}
xxx xxx
\end{tabular}
$a=b \int f(x) =0\quad \text{abc abc abc abc} $
\everymath{\setattribute\mathattr{1}}
\begin{tabular}{l}
xxx xxx
\end{tabular}
$a=b \int f(x) =0\quad \text{abc abc abc abc} $
\bigskip
VA VA
\lipsum[1]
\end{document}
与您的屏幕截图相反,问题中的代码没有将 bar 放在\text{abc abc abc abc}
段落中最后一个单词的后面和末尾: