3
\documentclass{article}
\usepackage{luacode,xkeyval}
\makeatletter
\define@key{someop}{a}{\def\mop@one{#1}}%
\setkeys{someop}{a=0}%
\newcommand{\luaTest}[2][]{{%
\setkeys{someop}{#1}%
\directlua{%
local mystr = [[
test
]]
tex.print(tostring(#2).." "..\mop@one)
}%
}%
}%

\makeatother
\begin{document}
\luaTest{'First'}

\begin{center}
\luaTest{'First'}  
\end{center}

\end{document}

The above code works as expected and produces the following expected output.

First 0
           First 0

However the following code does not work.

\documentclass{article}
\usepackage{luacode,xkeyval}
\makeatletter
\define@key{someop}{a}{\def\mop@one{#1}}%
\setkeys{someop}{a=0}%
\newcommand{\luaTest}[2][]{{%
\setkeys{someop}{#1}%
\directlua{%
local mystr = [[
test
]]

tex.print(tostring(#2).." "..\mop@one)
}%
}%
}%

\makeatother
\begin{document}
\luaTest{'First'}

\begin{center}
\luaTest{'First'}  
\end{center}

\end{document}

The only difference is one blank line before tex.print. It throws the following error.

 unexpected symbol near '\'.
\\luaTest ...print(tostring(#2).." "..\mop@one ) }
                                                  }

The command still works but not inside center environment. If I remove \begin{center} ...\end{center}, the document gets compiled. What is the issue? Could someone point out? Though I can get issue solved by removing blank line, I want to understand the issue.

0

1 Answer 1

6

empty lines inside \directlua matter. The parsing will insert a \par token. It errors in your example because inside a list (and center is a list) \par has a special meaning, but that doesn't mean that it does nothing outside the list. So avoid empty lines unless you know really what it will do.

As an example

\documentclass{article}

\begin{document}

{
 \makeatletter
 \@setpar{tex.print("blub")}
 \directlua{%
 tex.print(tostring(1))

 tex.print(tostring(2))
 }
}

\end{document}

will give

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .