5

How can I add comments to my directlua code?

{\catcode`\%=12
\directlua{
function boxed (s)
 ??? Add a comment ???
 if not(boxedflag) then
  i=string.find(s,'^!!!')
  if(i==1) then
    boxedflag=true
    print('A' ..string.gsub(s,'^!!! (%a*) "(.*)"', '\string\\mybox{%1}{%2}['))
    return string.gsub(s,'^!!! (%a*) "(.*)"', '\string\\mybox{%1}{%2}{')
  else
    return s
  end
else
  i=string.find(s,'^XXX')
  if i==1 then
   print('C' .. s)
   return string.gsub(s,'^XXX','')
  else
    print('B' .. s)
    boxedflag=false
    return '}' .. s
  end
 end
end}
}

I want to add a comments in the code of the function boxed. I tried -- comment and \% comment but both gave a error.

Any ideas how you can add comments in directlua functions?

  • 5
    Code in the argument to \directlua is read in with the normal TeX conventions and expansion. Better storing complex Lua code in a separate file and load it as a function. – egreg Jan 2 '17 at 11:47
  • 1
    The final } was not included in the code area. I suggest editing to fix that, in case anyone wants to try your code. – user103221 Jan 2 '17 at 15:06
9

Two suggestions:

  • As @egreg has already noted in a comment, you could store your Lua code in an external file (typically, but not necessarily, with extension .lua), and load the code into Lua(La)TeX via a \directlua{dofile(...)} (or similar) directive. All native Lua syntax, including -- to initiate comments, is allowed. If you need to output a \ (backslash) character inside a string, you'll need to input \\ since, as you probably know, \ all by itself is Lua's escape character.

  • For shorter pieces of Lua code, for which it's too much overhead to create external .lua files, you could load the luacode package and use either a luacode environment or a luacode* environment. In both environments, -- is treated as the start of a comment. The main difference between the luacode and luacode* environments is that TeX macros are allowed in the former but not in the latter.

The luacode package also provides the nifty helper macros \luastring, \luastringN, and \luastringO for passing strings that may contain TeX-special characters and/or TeX macros. The three macros differ as follows: the argument of \luastring is expanded (in the TeX sense of the word) before being passed to Lua, that of \luastringN is not expanded at all, and that of \luastringO is expanded only once (and of the first token only).

  • thanks! i really appreciate your constructive feedback on my luacode since i'm fairly new to latex and luatex. Thanks a lot Mico. – Fabio Schmid Jan 2 '17 at 23:13
  • Explanation: I use the luacode to parse a non latex syntax (Markdown Admonition Syntax) to Latex friendly styling. The print statements are just for testing purposes and debugging. I will remove them and improve the code according to your tipps. Thanks – Fabio Schmid Jan 2 '17 at 23:17
  • @FabioSchmid - I decided to delete my cooments about the Lua code after I realized that that the code came from somebody else's answer to an earlier query of yours. – Mico Jan 4 '17 at 13:28
0

This parsed. Whether it does what you want, is for you to decide:

\def\cmnt#1{} % ignores the argument
{\catcode`\%=12
\directlua{
function boxed (s)
\cmnt{Works for me.}
... the rest of your code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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