2

lua supports multiline strings.

Mathematica graphics

I need to send such string from LUA back to Latex. I first verified the code works using lua standalone engine

Mathematica graphics

but lualatex does not like something. Here is the MWE

\documentclass[11pt]{article}
\IfFileExists{luatex85.sty}
{\usepackage{luatex85}}{}

\usepackage{amsmath}
\usepackage{luacode}

\begin{luacode}

  function foo(arg)
  local x = [[\\begin{align*}
              x &=y\\\\
              z &=r\\\\
             \\end{align*}
            ]]
  tex.print(x)
  end
\end{luacode}
\begin{document}

\directlua{foo()}
\end{document}

Error is

(/usr/local/texlive/2016/texmf-dist/tex/luatex/ctablestack/ctablestack.sty)))
No file foo7.aux.

! LaTeX Error: There's no line here to end.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.22 \directlua{foo()}

? 

It works when printing it on one line as a long string:

\begin{luacode}    
      function foo(arg)
      local x = "\\begin{align*} x &=y\\\\ z &=r\\\\ \\end{align*}"
      tex.sprint(x)
      end
\end{luacode}

It also works if I print each line one by one

\begin{luacode}    
      function foo(arg)      
      tex.print("\\begin{align*}")
      tex.print("x &=y\\\\")
      tex.print("z &=r\\\\")
      tex.print("\\end{align*}")
      end
\end{luacode}

It is easier if one can use the multiline [[ ]] syntax.

Question is: Is it possible to send multiline string from Lua to Latex using [[ .. ]] syntax or must be print each line at a time?

TL 2016

  • Extract from Lua Wiki: "Escape sequences are not recognized when using double brackets". – Paul Gaborit Jun 19 '16 at 5:57
4

Some remarks:

The following code:

\documentclass[11pt,twocolumn]{article}
\usepackage{amsmath}
\usepackage{luacode}
% \usepackage{fontspec}
\pagestyle{empty}
\begin{luacode*}
  require("lualibs") -- for 'string.split'
  function foo(arg)
  local a = "\\begin{align*} x &=y\\\\ z &=r\\\\ \\end{align*}\\par"
  local b = [[\begin{align*} x &=y\\ z &=r\\ \end{align*}\par]]
  local c = [[\begin{align*}
                x &=y \\
                z &=r \\
              \end{align*}\par]]
  print()
  print(a)
  tex.print(a)
  print(b)
  tex.print(b)
  print(c)
  tex.print(c)
  tex.print(string.split(c,"\n"))
  end
\end{luacode*}

\begin{document}
\directlua{foo()}
\end{document}

produces the following log:

\begin{align*} x &=y\\ z &=r\\ \end{align*}\par
\begin{align*} x &=y\\ z &=r\\ \end{align*}\par
\begin{align*}
x &=y \\
z &=r \\
\end{align*}\par

and the following result:

enter image description here

PS: uncomment \usepackage{fontspec} and the Omega disappears...

  • @Nasser Just an advice: don't mix Lua code and TeX code.... – Paul Gaborit Jun 19 '16 at 9:46
1

Success ! I have found a work around, which works just as well, even though it uses different syntax than [[ ]] but it allows me to put the code on separate lines for ease of writing, which was the main reason I wanted to use [[ ]] Here it is

\documentclass[11pt]{article}
\IfFileExists{luatex85.sty}
{\usepackage{luatex85}}{}

\usepackage{amsmath}
\usepackage{luacode}    
\begin{luacode}

  function foo(arg)
  local x = "\\begin{align*}"..
            "x &=y\\\\"..
            "z &=r\\\\"..
            "\\end{align*}"
  tex.print(x)
  end
\end{luacode}
\begin{document}
\directlua{foo()}
\end{document}

Now lualatex is happy, and so Am I, and the output is

Mathematica graphics

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.