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 include a gcd function using lua in a sty file. Here is the code of euclide.sty :

\RequirePackage{ifluatex,luacode,luatextra}
\ifluatex
    \begin{luacode}
        function euclide(a,b)
            if a < b then a,b = b,a end
            r = a-math.floor(a/b)*b
            if r == 0 then tex.print(b) else euclide(b,r) end
        end
    \end{luacode}
    \newcommand{\euclide}[2]{\luaexec{euclide(#1,#2)}}
\fi

Here is the texfile code :

\documentclass[10pt,oneside]{article}
\usepackage{euclide}

\begin{document}
\euclide{435}{630}
\end{document}

The lualatex compilation give me errors :

Runaway argument?
! File ended while scanning use of \luacode@grab@lines.
<inserted text>
\par
l.4 

I suspect you have forgotten a `}', causing me
to read past where you wanted me to stop.
I'll try to recover; but if the error is serious,
you'd better type `E' or `X' now and fix your file.


! LaTeX Error: Missing \begin{document}.

Can somebody tell me what's wrong in my code ? (this code works fine in the main tex file, but not in sty file) Thanks, MB.

share|improve this question
    
This is 'works for me': could you perhaps add \listfile so we can see your file versions? –  Joseph Wright Jul 3 at 12:40
    
Just because I had tab character before \end{luacode}. Thanks all. –  MB78 Jul 3 at 12:52

1 Answer 1

up vote 4 down vote accepted

I can reproduce the problem, if the white space before \end{luacode} is a tabulator character. Environment luacode is a special environment, because it changes category codes to read its contents in a more verbatim way. Therefore the end detection is not done by the usual macro expansion, but by searching for the string \end{luacode} at the start of a new line (spaces are ignored with catcode 10 here).

Fix: Replace tabulators by spaces (depending on the editor, there might be an option for this.)

BTW: It is a little inconsistent: First packages are loaded that require LuaTeX (luacode and luaextra), then the code is wrapped in \ifluatex. If the package works with and without LuaTeX:

\RequirePackage{ifluatex}
\ifluatex
  \RequirePackage{luacode,luaextra}
  \begin{luacode}
  ...
  \end{luacode}
  ...
\else
  ...
\fi

Or test for LuaTeX and throw an error, if LuaTeX is required:

\RequirePackage{ifluatex}
\ifluatex
\else
  \PackageError{euclide}{LuaTeX is required}\@ehd
  \newcommand*{\euclide}[2]{%
    % dummy definition
  }%
  \expandafter\endinput % abort early
\fi

\RequirePackage{luacode,luaextra}
\begin{luacode}
...
\end{luacode}
share|improve this answer

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.