I'm trying to make use of lualatex to automate generation of some part of a pdf. The directory tree for my minimal working example looks like the following:

│   lua_escaping_issue.tex
  • a_folder/first_file.tex contains the text "Hello"
  • a_folder/second_file contains the text "World"
  • a_folder/third_file contains the text "!. How are"
  • a_folder/fourth_file contains the text "you?"

Using lua, I'd like to generate the text "HelloWorld!.How areyou?". So I wrote lua_escaping_issue.tex which contains the following:



                require 'lfs'
                for file in lfs.dir("a\_folder") do
                    if file ~= "." and file ~= ".." then
                        tex.print("\\input\{" .. "a\_folder/" .. file .. "\}")


Unfortunately when I compile with latexmk -lualatex lua_escaping_issue.tex I get an error:

[\directlua]:1: invalid escape sequence near '"a\p'

I believe the idea of \directlua is that everything will be expanded, therefore commands susceptible to be recognized by LaTeX should be escaped. I've already done this, and also tested my file in pure lua and it seems to work. So I'm not sure what I'm doing wrong here.


1 Answer 1


You need to prevent \\ and ~ from expanding. prefixing them wih \ does not do that:


% no   require 'lfs'
        for file in lfs.dir("a_folder") do
           if file \string~= "." and file \string~= ".." then
              tex.sprint( "\string\\input{a_folder/" .. file .. "}" )

  • Thanks, that makes things a little clearer. I'm a bit surprised that requireing lfs is not needed. I thought that wasn't part of the standard library...
    – user32882
    Apr 15, 2022 at 9:54
  • @user32882 the standard Lua libs are not in luatex's default paths, but lfs is compiled in (see texdoc luatex) Apr 15, 2022 at 9:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .