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 used the following in the past and it worked:

\documentclass{minimal}
\usepackage{luacode,luatextra}
\begin{document}

\begin{luacode*}
MyVal=123
tex.print("\\def\\MyVal{"..MyVal.."}")
\end{luacode*}

MyVal=\MyVal
\end{document}

and it worked fine.

Today I had to use the same source file and now this bit of code no longer defines a macro that can be called in lualatex, i.e. I now get Undefined command message.

I read the following: Which Lua environment should I use with LuaTeX (LuaLaTeX)? and Create macros inside Lua block

I tried luacode sans and with '*' as well as tex.sprint.

I attempted tex.tprint, but I will have to delve in the documentation about that.

My questions then are:

  1. Has anything changed in the last two months?
  2. How would one do the equivalent of the following in lualatex:

    \def{Myval}{100}
    

I also added \noexpand and changed \\ to \, to no avail.

share|improve this question
add comment

1 Answer

up vote 7 down vote accepted

The luacode* environment forms a group, in common with other LaTeX environments. Thus if you want to use this approach and have the value 'escape' then you will need to use \gdef

\documentclass{article}
\usepackage{luacode,luatextra}
\begin{document}

\begin{luacode*}
MyVal=123
tex.print("\\gdef\\MyVal{"..MyVal.."}")
\end{luacode*}

MyVal=\MyVal
\end{document}

As observed in Which Lua environment should I use with LuaTeX (LuaLaTeX)?, the best plan is to use a separate file and load it without grouping, etc.

\RequirePackage{filecontents}
\begin{filecontents*}{\jobname.lua}
MyVal=123
tex.print("\\def\\MyVal{"..MyVal.."}")
\end{filecontents*}

\documentclass{article}
\begin{document}

\directlua{require("\jobname.lua")}

MyVal=\MyVal
\end{document}
share|improve this answer
    
It works and I found another bug that crept in; its sorted out. –  Louis Aug 19 '13 at 22:08
    
As a ConTeXt user who regularly uses lua to define macros, I find it strange that the luacode* environment creates a group. Writing lua code in a separate file is fine, but sometimes having all related macros in one file is easier to maintain. –  Aditya Aug 19 '13 at 23:43
1  
@Aditya LaTeX's \begin...\end always creates a group (except \begin{document}, which deliberately undoes this). Normally if you want to avoid it you use \foo...\endfoo instead, although this is not officially supported. It's of course possible to undo the group creation: it might well be sensible here but that is for the author of luacode* to decide and document! –  Joseph Wright Aug 20 '13 at 5:53
1  
(\document starts with \endgroup and \enddocument contains a \begingroup to 'neutralise' the matching grouping primitives in \begin and \end.) –  Joseph Wright Aug 20 '13 at 5:55
1  
@Aditya I do see the point about inline code for short sections (we have some in expl3). –  Joseph Wright Aug 20 '13 at 5:56
show 1 more 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.