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.

I'm trying to build some sort of an interface to a lua matrix module. I've navigated a few issues and arrived at something that I felt like I could probably modify to work. Anticipating the need for expl3 functions as some point I wrapped the macro in \ExplSyntaxOn and \ExplSyntaxOff expecting a total catcode disaster. Rather I got an error relating to 'global' and 'nil value'. I am very much fumbling my way through this so, I don't have the tools to understand why what I did was bad. In the code below, the wrapper function spits out a matrix, however, when uncommenting an \ExplSyntaxOn/Off pair the error mentioned above occurs. If needed, the matrix.lua file can be found here:

https://raw.github.com/davidm/lua-matrix/master/lua/matrix.lua

\documentclass{article}
\usepackage{xparse}
\usepackage{luacode}
\usepackage{fontspec}

%\ExplSyntaxOn
\directlua{dofile("matrix.lua")}
%\ExplSyntaxOn
\NewDocumentCommand{\matrixop}{}
{
    \luaexec{
        local matrix = require 'matrix'
        m1 = matrix{{8,4,1},{6,8,3}}
        m2 = matrix{{-8,1,3},{5,2,1}}
        m3 = matrix.add(m1,m2)
        a=matrix.latex(m3,c)
        tex.print(a)
    }
}
%\ExplSyntaxOff
\begin{document}

\matrixop{}

\end{document}
share|improve this question
    
Just to clarify, I know that the macro doesn't do anything at the moment. I just gradually add things until it breaks, then fix and repeat. –  Scott H. Sep 20 '12 at 5:34
add comment

1 Answer

up vote 6 down vote accepted

Inside the expl3 code environment, spaces are ignored. That will cause a problem if you directly embed Lua code, unless your replace the required spaces by ~ or reset the catcode of the space character. However, Lua code within a file is fine, as it's never tokenized by TeX:

\documentclass{article}
\usepackage{xparse}
\usepackage{luacode}
\usepackage{fontspec}

\ExplSyntaxOn
\directlua{dofile("matrix.lua")}
\ExplSyntaxOff
\NewDocumentCommand{\matrixop}{}
  {%
    \luaexec{%
        local matrix = require 'matrix'
        m1 = matrix{{8,4,1},{6,8,3}}
        m2 = matrix{{-8,1,3},{5,2,1}}
        m3 = matrix.add(m1,m2)
        a=matrix.latex(m3,c)
        tex.print(a)
    }%
  }
\begin{document}

\matrixop{}

\end{document}

Both expl3 and Lua 'best practice' guidelines aim to separate out implementations from point of use. For Lua code, that means putting the Lua in a separate file, and acting it using dofile or the function name only. In the expl3 model, a document command such as \matrixop should not include the 'payload', but should simple hand off the appropriate internal command, which should then again do good separation of material. I'd therefore suggest that rather than messing about with ~ or catcodes, you should put your Lua into a separate file, and access it inside expl3 blocks using the dofile approach.

share|improve this answer
    
Your \matrixop command adds spurious spaces. –  Bruno Le Floch Sep 22 '12 at 14:34
    
@BrunoLeFloch Fixed –  Joseph Wright Sep 22 '12 at 14:36
add comment

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.