Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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*}
  function readtxt()
    file = io.open("test.txt", "r")
    text = file:read("*all")
    return tex.print(text) 
  end
\end{luacode*}

\begin{document}
\directlua{readtxt()} 

\begin{tikzpicture}
  \begin{axis}
    \addplot[] coordinates {(0.0, 0.0) (1,1)};
  \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).
<to be read again> 
                   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
 loadable: metric data not found or bad.
<to be read again> 
                   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?

share|improve this question

1 Answer 1

up vote 6 down vote accepted

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*}
  function readtxt()
    fileA = io.open("test.txt", "r") --changed
    text = fileA:read("*all")
    return tex.print(text)
  end
\end{luacode*}

\begin{document}
\directlua{readtxt()}

\tiny abc
\end{document} 
share|improve this answer
    
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
1  
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.