5

In this question / answer : one pass verbatim latex material to lua, but latex displays this material by him self. If one suppress the two lines :

local buf_without_end = mybuf:gsub("\\end{foobar}\n","")
print(string.format("Lua: %q", buf_without_end))

the material doesn't disapear. I'd like to modify this material using lua code and display it after.

\documentclass{beamer}
\usepackage{luacode,tikz}

\directlua{dofile("verb.lua")}

\newenvironment{LuaVerb}{%
    \directlua{start_recording()}}{%
    \directlua{stop_recording()}
}

\begin{document}

\begin{frame}

% pass the material to lua
\begin{LuaVerb}
    hello word
\end{LuaVerb}

% use of this material, but with or witout this line
% the material is displayed

%\directlua{print_recording()}

\end{frame}

\end{document}

verb.lua

tp = tex.print

mybuf = ""

function readbuf( buf )
    mybuf = mybuf .. buf .. "\n" 
end

function start_recording()
    luatexbase.add_to_callback('process_input_buffer', readbuf, 'readbuf')
end

function stop_recording()
    luatexbase.remove_from_callback('process_input_buffer', 'readbuf')
end

function print_recording()
    local buf_without_end = mybuf:gsub("\\end{LuaVerb}\n","")
    print(string.format("Lua: %q", buf_without_end))
    mybuf = ""
end
4

If you don't want to pass lines to LaTeX, you have to return empty string from the callback, otherwise callback pass the line if return value is nil. Because you don't return any value, nil is used implicitly. And because LaTeX doesn't process input anymore, you have to parse input for \end{LuaVerb} from Lua side, otherwise you would grab all lines until the end:

local end_verb = '%s*\\end{LuaVerb}'

function readbuf( buf )
    if buf:match(end_verb) then 
        return buf 
    end
    mybuf = mybuf .. buf .. "\n" 
    return ""
end

Now there is some strange issue with beamer, which is maybe caused by how frames are processed, because no output is printed with print_recording(). But when I changed document class to article, I got:

(/opt/texlive/2013/texmf-dist/tex/luatex/luatexbase/mcb.lua)))) (./jej.aux)Lua: 
"    hello word\
"
  • It works quite fine. I use Texlive 2014 and both beamer and article output Hello world on the console output, but the text displayed by lua tex.print(mybuf) is followed an Omega ??? – Tarass Oct 16 '14 at 18:45
  • @Tarass I am still on TL 2013, so maybe this issue is fixed in recent beamer. Do you mean that omega character is added after Hello world? – michal.h21 Oct 16 '14 at 18:50
  • Yes it comes from the \n. but it wanishes with loading fontspec. It's normal I think. Thank you very much. – Tarass Oct 16 '14 at 18:57
  • Still a problem, it works with beamer outside a frame, but in a frame the LuaVerb text is not hidden. I had to store the latex material in LuaVerb outside the frame and then I can use it in the frame. – Tarass Oct 17 '14 at 7:05
  • @Tarass I think that beamer process frame in a special way, because of overlays and similar fetures. the solution could be to use buffer reader to read whole document and parse the document for \begin/\end{LuaVerb}` to start/stop processing – michal.h21 Oct 17 '14 at 7:43

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.