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.
%%
A B
1.0 20
1.1 21
1.2 22
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 \par
tokens).
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 \par
s 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 tex.print
).
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.
\documentclass{scrartcl}
%\url{http://tex.stackexchange.com/q/97822/86}
\usepackage{luatextra}
\usepackage{filecontents}
\begin{filecontents}{testdata.dat}
A B
1.0 20
1.1 21
1.2 22
\end{filecontents}
\begin{luacode}
function readtxt()
file = io.open("testdata.dat", "r")
text = file:read("*all")
clean = string.gsub(text,'%%[^\string\n]*',"")
print(clean)
return tex.print(clean)
end
\end{luacode}
\begin{document}
Reading in text:
\directlua{readtxt()}
Finished reading in text.
\end{document}
Notice the \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.
This produces:
Reading in text: A B 1.0 20 1.1 21 1.2 22 Finished reading in text.
testdata.dat
. The filecontents package seems to add a few lines at the beginning. Have you tried to disable any further interpretation intex.print
? This can be achieved by usingtex.print(-2, text)
.filecontents*
environment (note the star!) and the result will be good.