The file being written by
filecontents looks like this:
%% LaTeX2e file `testdata.dat'
%% generated by the `filecontents' environment
%% from source `luareadfile' on 2013/02/12.
When lua reads that in to the string, it does no processing. If TeX were to read that in, it would ignore the comment lines since everything from a
% to a newline gets eaten up. Moreover, TeX would translate the newlines to space characters (and translate any double newlines to
The file then gets inserted into the TeX stream via
tex.print. This, it would appear, is inserted as if the input parser has already done its job. So TeX assumes that double newlines have already been replaced by
\pars and treats a newline character as simply a space token. However, it still sees the
% character and is prepared to deal with it by ignoring anything after it up to a newline. But there are no newlines, so TeX keeps discarding stuff until it is stopped (at the end of the
I don't know if it is possible to fix this "properly" by getting the new lines to be interpreted correctly, but if you have control over the file contents (as you appear to here) you could get lua to strip out the comments first using a simple pattern match before presenting the data to TeX.
file = io.open("testdata.dat", "r")
text = file:read("*all")
clean = string.gsub(text,'%%[^\string\n]*',"")
Reading in text:
Finished reading in text.
\string! That's because your lua code is embedded in your document. It's usually a good idea to avoid that by having the lua code in a separate file.
Reading in text: A B 1.0 20 1.1 21 1.2 22 Finished reading in text.