3

I have got a strange behaviour. In a lua program, I use CRs to make it more readable. When I execute my lua program with ZeroBrane and copy the result in LuaLaTeX, it works well. When I use \luaexec{my program} directly in LuaLaTeX, it doesn't work. When I get rid of all the CRs within the tkz commands, then it works.

Is there a substitue of CRs to have the change of line available in the lua program and yet the program can be used directly within LuaLaTeX ?

\documentclass{article} \usepackage{tikz} \usepackage{luacode} \begin{luacode*} function MNotWE() local res = [[ \begin{tikzpicture}[fill=blue!20] \path (.2,.8) node {Hello} (.2,.4) node {World}; \end{tikzpicture} ]] return res end function MWE() local res = [[ \begin{tikzpicture}[fill=blue!20] \path (.2,.8) node {Hello} (.2,.4) node {World}; \end{tikzpicture}]] return res end \end{luacode*} \begin{document} \directlua{tex.print(MWE())} %\directlua{tex.print(MNotWE())} % <-- UNCOMMENT TO SEE THE PROBLEM. \end{document} 

First program doesn't work whereas the second does.

3
  • @DavidCarlisle Can you delete your comment because I have added a MWE LaTeX code? Commented Mar 20, 2021 at 12:47
  • @projetmbc Thanks for the edit of MWE() etc. The lua program is external and I use luaexec but that doesn't interfere here and you allow people to experiment the effect. Thanks. Commented Mar 20, 2021 at 17:33
  • @user1771398 You should accept the answer of Marcel Krüger. Commented Mar 21, 2021 at 10:50

2 Answers 2

3

You need to call the function, so MNWE() not MNWE but also when read as TeX newlines are converted to spaces but that stage is skipped here however you can force the space interpretation via

\documentclass{article} \usepackage{tikz} \begin{document} \directlua{require "\jobname.lua"} {\catcode10=10 \directlua{tex.print(MNWE())}} \end{document} 

enter image description here

1
  • clear explanation of the effect, easy correction - Thanks ! Commented Mar 20, 2021 at 17:32
2

Printing newlines using tex.print does not, from TeX's point of view, start new lines but only adds literal line feed tokens. You can avoid that by splitting the line before feeding it to TeX.

This can be done for example using LPEG:

local nl = lpeg.P'\n' -- A newline is a single NL byte. local line = lpeg.C((1-nl)^0) -- A line is a sequence of zero -- or more characters which are not -- newlines ((1-nl)^0) and we use -- lpeg.C to capture the lines -- (aka. return them as results later) -- In total we want zero or more lines followed by newlines and then one -- final line which is not followed by a newline: mfk_splitlines_pattern = (line*nl)^0*line 
\documentclass{article} \usepackage{tikz} \usepackage{luacode} \begin{luacode*} function MNotWE() local res = [[ \begin{tikzpicture}[fill=blue!20] \path (.2,.8) node {Hello} (.2,.4) node {World}; \end{tikzpicture} ]] return res end function MWE() local res = [[ \begin{tikzpicture}[fill=blue!20] \path (.2,.8) node {Hello} (.2,.4) node {World}; \end{tikzpicture}]] return res end local nl = lpeg.P'\n' local line = lpeg.C((1-nl)^0) mfk_splitlines_pattern = (line*nl)^0*line \end{luacode*} \begin{document} \directlua{tex.print(MWE())} \directlua{tex.print(mfk_splitlines_pattern:match(MNotWE()))} \end{document} 
0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.