[luatex] nodes

Paul Isambert zappathustra at free.fr
Sun Dec 16 01:53:13 CET 2012


Herbert Voss <Herbert.Voss at fu-berlin.de> a écrit:
> Hi all
> 
> I do not really understand how node.insert_before works.
> In the following example I want to draw a red line before each
> new line (just for fun). As can be seen it works only for the
> beginning of the paragraph, not for the other lines in a paragraph.
> However, if I change it to node.insert_after it works for all
> lines. So what is wrong with the
> List,w=node.insert_before(START,List,w) ???
> 
> Herbert
> 
> 
> \directlua{
> function markList(List)
>   local startLine=true
>   local START=List
>   while List do
>    if List.id == 0 or List.id == 1 then
>      markList(List.head)
>    end
>    texio.write(tostring(List.id.." \string\\n"))
>    if List.id==37 and startLine then
>     texio.write("new line\string\\n")
>     startLine=false
>     w = node.new("whatsit","pdf_literal")
>     w.data = string.format("q 1 0 0 RG 1 w 0 -0.1 m 0 10 l S Q")
>     List,w=node.insert_before(START,List,w)
> %   List,w=node.insert_after(START,List,w)
>    end
>    List=List.next
>   end
>   return true
> end

You have to reassign the first return value of node.insert_before to the
head of the list, otherwise when the hbox is analyzed by TeX it still
sees the original head as the first node. A much simpler version of your
function would be:

    function markList(List)
      for n in node.traverse(List) do
        if n.id == 1 then
          markList(List.head)
        elseif n.id == 0 then
          local w = node.new("whatsit","pdf_literal")
          w.data = string.format("q 1 0 0 RG 1 w 0 -0.1 m 0 10 l S Q")
          n.head = node.insert_before(n.head, n.head, w)
        end
      end
      return true
    end

Best,
Paul



More information about the luatex mailing list