如何为某个单词的所有实例设置样式?

如何为某个单词的所有实例设置样式?

在我的文档中,我想将每个字符实例的颜色更改*为红色。

如何在不干扰文档的其他样式的情况下为单个单词或字符的每个实例设置样式?

答案1

如果按照该示例操作*,则可以执行以下操作:

\documentclass{article}
\usepackage{luacode}
\begin{document}

\begin{luacode}
dofile("star.lua")
\end{luacode}

Hello * cruel * world

\end{document}

如下star.lua

local GLYF = node.id("glyph")
local HLIST = node.id("hlist")
local VLIST = node.id("vlist")

local colstackstart, colstackstop
colstackstart = node.new("whatsit","pdf_colorstack")
colstackstop  = node.new("whatsit","pdf_colorstack")

colstackstart.data = "1 0 0 rg "
colstackstart.stack = 0
colstackstop.data = ""
colstackstop.stack = 0

-- one could look for the availability of the field 'cmd' instead
if status.luatex_version < 79 then
    colstackstart.cmd = 1
    colstackstop.cmd = 2
else
    colstackstart.command = 1
    colstackstop.command = 2
end



function star( head )
    local curhead, orighead = head, head
    while head do
        -- recurse into the hbox or vbox, if there are any
        if head.id == HLIST or head.id == VLIST then star(head.list)
        -- the * is ascii 42, I hope
        elseif head.id == GLYF and head.char == 42 then
            curhead = node.insert_before(curhead,head,node.copy(colstackstart))
            curhead = node.insert_after(curhead,head,node.copy(colstackstop))
        end
        head = head.next
    end
    return orighead
end



luatexbase.add_to_callback('pre_linebreak_filter', star, 'colorstar')

这会在数字 42 的字符每次出现之前和之后插入一个 colorstack whatsit 节点。

红星

相关内容