[luatex] creating a "verbatim"-like catcode table from Lua

Eduardo Ochs eduardoochs at gmail.com
Sat Mar 7 01:48:43 CET 2015


Hi list,

this code uses token.getnext() to "eat" the CR immediately after the
\directlua{...} and the lines that say "one", "two two", and "three
three three" before returning control to LaTeX's parser:

  cd /tmp/
  cat > test.tex <<'%%%'
  \documentclass{book}
  \begin{document}
  \directlua{
  makecrendofline = function () tex.catcode[13] = 5  end
  makecrother     = function () tex.catcode[13] = 12 end
  nextchar = function () return token.get_next()[2] end
  eatline  = function () repeat until nextchar() == 13 end
  makecrother()
  for i=1,4 do eatline() end
  makecrendofline()
  }
  one
  two two
  three three three
  four four four four
  five five five five five
  \end{document}
  %%%
  lualatex test.tex
  xpdf test.pdf

We can use that idea, combined with something like this,

  texfile = readfile(tex.jobname..".tex")
  lines   = splitlines(texfile)
  for i=0,10 do dosomethingwith(lines[tex.inputlineno + 1]) end

to make Lua interpret the lines after a certain point, and then make
TeX's parser skip them.

The catcode trick that I used above is quite fragile, though - it will
not notice CRs in comments, for example. It would be better to use a
pre-prepared catcode table like the one that "verbatim" uses, instead
of just changing the catcodes of CR, %, and a few other chars.

How do I construct a "verbatim"-ish catcode table? How do I save it
and activate it? Is there a way to do that from pure Lua? How do I
check, for a given number n, if the catcode table "n" exists? I saw in
the reference manual that there are primitives \initcatcodetable and
\savecatcodetable... so there is a way to do that mixing TeX and Lua.

  Thanks in advance! Pointers appreciated!...
  Cheers,
    Eduardo Ochs
    eduardoochs at gmail.com
    http://angg.twu.net/


More information about the luatex mailing list