# [luatex] Turning a string into a list of nodes.

Taco Hoekwater taco at elvenkind.com
Fri Nov 19 10:33:12 CET 2010

On 11/18/2010 09:52 PM, Paul Isambert wrote:
> Hello all,
>
> I've been trying to devise a Lua function which, when fed a string, returns a
> list of nodes. The application I have in mind is line numbering, counting lines
> in the post_linebreak_filter and adding an \hbox with the number to lines that
> are a multiple of a given number. Everything should be done in Lua, and thus the
> counter must be converted to a list of nodes. I.e. the following should be
> possible:
>
> mylist = string_tolist(tostring(linecounter))

This case is fairly straightforward, you could even prepare the 10
needed nodes in advance and then use node.copy() to chain them.

> And here come the questions: Have I missed a definitely simpler way to do what I
> want?

No, there is not. The simple reason for that is that it is an
exceedingly tricky function to write in the general case: the
luatex main control function has numerous side-effects and global
state variables at the moment, making it unsuitable for general use
on 'tex input', while on the other hand it seemed a shame to write a
'copy' of main control just for this.

The 'pure ascii input' is simple enough to do in pure lua, as seen from

>    for item in node.traverse(head) do
>      if item.next then
>        -- If the second argument isn't there, LuaTeX crashes.
>        node.kerning(item, item.next)
>      end
>    end

This is overdoing it, there is no need for a loop here. In fact,
the reason for it crashing in the lacking .next case is probably
because the list is getting modified inside of the loop.

You should be able to simply run