# Strange interaction between fontspec, lua and pgfplots

While trying to combine some Lua code with plots written in pgfplots something went completely wrong. I have no idea what causes the problem, as the error message is a bit opaque to me. Here is the example which compiles nicely in luatex without the fontspec package:

\documentclass[12pt]{article}
\usepackage{pgfplots}

\usepackage{fontspec}
\usepackage{luacode}
\usepackage{filecontents}

\begin{filecontents*}{test.txt}
some text
\end{filecontents*}

\begin{luacode*}
file = io.open("test.txt", "r")
return tex.print(text)
end
\end{luacode*}

\begin{document}

\begin{tikzpicture}
\begin{axis}
\end{axis}
\end{tikzpicture}

\end{document}


With fontspec enabled the first error messages of a total of 58 are:

! LuaTeX error ...)/MiKTeX 2.9/tex/luatex/luaotfload/otfl-font-def.lua:239: att
empt to call field 'suffix' (a nil value).
relax
l.26   \end{axis}

The lua interpreter ran into a problem, so the
remainder of this lua chunk will be ignored.

! Font \EU2/lmr/m/n/8=file:lmroman8-regular:script=latn;+trep;+tlig; at 8pt not
relax
l.26   \end{axis}

I wasn't able to read the size data for this font,
so I will ignore the font specification.


Somehow there is some negative interaction between the three components, Lua, pgfplots and fontspec. Individually they all work nicely. Some interaction was reported in this question: Bad interaction between fontspec and some lua code that performs string-related search and replace operations but this seems unrelated.

Any ideas what is going on here?

-

The problem has nothing to do with pgfplots: You get the same error if you simply try to change the font after your \directlua e.g. by issuing a \tiny.

The source of the problem is the name of your file handle. Probably luaotfload is using this name too. Every thing works if I change the name to e.g. fileA:

\documentclass[12pt]{article}

\usepackage{fontspec}
\usepackage{luacode}
\usepackage{filecontents}

\begin{filecontents*}{test.txt}
some text
\end{filecontents*}

\begin{luacode*}
fileA = io.open("test.txt", "r") --changed

Thank you! The lua scoping rules have bitten me here. Turns out that lua uses 'global by default' and indeed, otfl-font-def.lua uses a variable called file which is overwritten by the declaration in readtxt(). –  Alexander Apr 21 '13 at 12:34
Always use local variables in Lua code, so local file = .... and do not pollute the global namespace with macro names. In ConTeXt, the standard practice is to use thirddats = thirddata or {} and then function thirddata.readtxt, etc. –  Aditya Apr 21 '13 at 14:24