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'm using a process_input_buffer callback to look at lines read from LuaLaTeX input before passing them on to TeX, to be processed as »verbatim« text (code and command examples). There are some lines in the input that I don't want to pass to TeX at all. If I return »nil«, then TeX gets the original line (as the documentation says). If I return an empty string, an empty line results in the TeX output, which I don't want. How can I arrange for the callback to throw the line away altogether without passing anything on to TeX?

(One obvious approach would be to read all of the example into a buffer and then simply cut out the undesired stuff before passing the complete buffer on to TeX, en bloc. This doesn't work for me due to other considerations.)

The idea is that in an environment like

\begin{example}
  # foo
  bar
  baz
\end{example}

the »#« line should not be passed to TeX but the other lines should. (In the real world, the »#« line is somewhat more complicated but is being processed on the Lua side of things.)

(In the meantime, I found a workaround such that I'm basically inserting a control-B character in front of the »#« on the Lua side, and declaring ^^B to be the fancyvrb »commentchar«, which causes anything after a ^^B, including the newline, to be ignored. I'd still be interested to see whether there is a simpler method.)

share|improve this question
1  
Can you provide an example? –  Marco Daniel Dec 16 '11 at 12:17
add comment

2 Answers 2

You can return an empty string, and if you are in vertical mode, no line will be created.

Another approach is to set \endlinechar to -1 and return the empty string for "don't pass to TeX" or the buffer plus ^^M for the unfiltered line.

share|improve this answer
    
Yes, but I'm doing this while from the point of view of LaTeX I'm in a fancyvrb »Verbatim« environment, so I'm not in vertical mode. –  Anselm Lingnau Dec 16 '11 at 13:15
add comment

I don't know what you mean by buffer in LuaLaTeX. In ConTeXt, buffers are best way to pass data from TeX to lua and back. Below is an example (in ConTeXt) that removes lines starting with # and typesets the result of the environment with verbatim catcodes.

\def\startfoo
    {\dostartbuffer[foo][startfoo][stopfoo]}

\def\stopfoo
    {\ctxlua{thirddata.foo.clean_lines(buffers.getcontent('foo'))}}

\startluacode
  thirddata = thirddata or {}
  thirddata.foo = {}
  function thirddata.foo.clean_lines(content)
    local result = {}
    local lines = string.splitlines(content)
    for i = 1, #lines do
      -- Change the test with a more complicated test.
      if not string.find(lines[i], '^#') then
          table.insert(result, lines[i])
      end
    end
    result = table.concat(result, '\n')
    tex.sprint("\\starttyping ")
    tex.sprint(result)
    tex.sprint("\\stoptyping ")
  end
\stopluacode

\starttext
\startfoo
# foo
bar %verbatim # environment
bar
\stopfoo
\stoptext
share|improve this answer
3  
You're late! Usually we both post LuaTeX answers within 2 hours and I was waiting for you ;-) –  topskip Dec 17 '11 at 7:40
    
Thank you for all the ConTeXt examples but I said quite clearly I was using LaTeX :-) Anyway, I can see now that there is apparently no way to do this on the Lua side without any help from the TeX side, and the approach I outlined in my update works for me. –  Anselm Lingnau Dec 19 '11 at 8:58
add 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.