TransWikia.com

ConTeXt: How to append content to already used buffer

TeX - LaTeX Asked on September 2, 2021

I am learning ConTeXt. One of the reasons that brought me in is the idea of buffers. So far, I was using datatool package to obtain similar functionalities (with the overhead cost involved).

I wonder, if there is an command, or option, that would allow to append content to already used buffer other than saving the buffer with savebuffer as .tex file and then input it again.

I hope I am stating the problem clearly. Hopefully MWE will illustrate that too:

MWE:

starttext

definebuffer[MyBuffer]
startbuffer[MyBuffer]
First part of buffer content.
stopbuffer

savebuffer[MyBuffer]
[mybuffer.tex]

Used here:

getbuffer[MyBuffer]

blank

startbuffer[MyBuffer]
input jobname-mybuffer.tex

Adding more:

The additional stuff.
stopbuffer

Now all together:

getbuffer[MyBuffer]

stoptext

If there is not, I would like to know if it is possible to have luacode that would deal with those saved buffers (deal=remove them) to not clutter project directory.

Thank you in advance for any help with this.

One Answer

When you label a buffer, it is already accessible to getbuffer[label]. definebuffer has other purposes. To answer your questions I suggest two ways:

A nice approach from the mailing list

The following example does a better handling of buffers.append. I've just adapted it from the mailing list. It defines startappendbuffer and stopappendbuffer so it's more comfortable to use:

startluacode
    userdata = userdata or { }
    function userdata.addTempToBuffer(buffername)
        buffers.append(
            buffername,
            'n' .. buffers.getcontent('APPENDTEMP')
        )
    end
stopluacode

defstartappendbuffer[#1]%
{defstopappendbuffer%
      {ctxlua{userdata.addTempToBuffer('#1')}}
   dostartbuffer[APPENDTEMP][startappendbuffer][stopappendbuffer]}

starttext

startbuffer[x]
Hola
stopbuffer

startlines
getbuffer[x]
stoplines

startappendbuffer[x]
input knuth
stopappendbuffer

startlines
getbuffer[x]
stoplines

stoptext

A naïve approach: wrap buffers.append

There's already a Lua function to append content to a buffer, i.e. buffers.append(buffer, text) (see buffers in LuaTeX). So we're going to wrap it using ctxlua, which serves to call Lua functions in TeX:

%#1 is the name of the buffer
%#2 is the content to append
unprotect
defappendtobuffer#1#2{ctxlua{buffers.append("#1", !!bs#2!!es)}}
protect
starttext 

startbuffer[lol]
dorecurse{6}{hello! }
stopbuffer
getbuffer[lol]
appendtobuffer{lol}{startformula e^{ipi}+1=0stopformula input knuth}
getbuffer[lol]
stoptext

Notice the use of !!bs and !!es so your content is passed as a string to Lua. I'd use it if I want to add simpler, shorter snippets of text because, as Henri Menke points out (thanks! ?), it doesn't handle line breaks and expands its content, which is troublesome in special cases, among other unexpected effects.

Correct answer by user220367 on September 2, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP