How to check if buffer or block has content
Hi, How can I check if a buffer or block has content (or is empty) before a \useblocks or \getbuffer statement? I would like to typeset a headline only if the buffer/block has been used in a particular section and want to avoid headlines with empty sections. Thanks! Benjamin
On Mon, 2 Aug 2021, Benjamin Buchmuller via ntg-context wrote:
Hi,
How can I check if a buffer or block has content (or is empty) before a \useblocks or \getbuffer statement?
I would like to typeset a headline only if the buffer/block has been used in a particular section and want to avoid headlines with empty sections.
There may be more efficient ways to do it (since buffers are stored at the lua end), but you can always do \doiftextelse(\getbuffer[...]} to test it buffer creates any output. Aditya
On 8/2/2021 4:43 PM, Aditya Mahajan via ntg-context wrote:
On Mon, 2 Aug 2021, Benjamin Buchmuller via ntg-context wrote:
Hi,
How can I check if a buffer or block has content (or is empty) before a \useblocks or \getbuffer statement?
I would like to typeset a headline only if the buffer/block has been used in a particular section and want to avoid headlines with empty sections.
There may be more efficient ways to do it (since buffers are stored at the lua end), but you can always do \doiftextelse(\getbuffer[...]} to test it buffer creates any output. indeed you know there is a way ...
\starttext \startbuffer[test] test \stopbuffer \doifelsebuffer{test} {YES} {NOP} \permanent\protected\def\doifelsebuffercontent#1% {\doifelse{\luaexpr{(string.strip(buffers.getcontent("#1")) ~= "" and "yes")}}{yes}} \startbuffer[test] \stopbuffer \doifelsebuffercontent{test} {YES} {NOP} \startbuffer[test] \stopbuffer \doifelsebuffercontent{test} {YES} {NOP} \stoptext if we can agree on a proper name then i can add a (more efficient) \doifelse... Hans ----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl -----------------------------------------------------------------
And in pathological cases where even this does not work (as I encountered a few days ago): \setbox\scratchbox=\vboxx{content}\ifdim\wd\scratchbox>\zeropoint .. \else .. \fi dr. Hans van der Meer
On 2 Aug 2021, at 17:17, Hans Hagen via ntg-context
wrote: On 8/2/2021 4:43 PM, Aditya Mahajan via ntg-context wrote:
On Mon, 2 Aug 2021, Benjamin Buchmuller via ntg-context wrote:
Hi,
How can I check if a buffer or block has content (or is empty) before a \useblocks or \getbuffer statement?
I would like to typeset a headline only if the buffer/block has been used in a particular section and want to avoid headlines with empty sections. There may be more efficient ways to do it (since buffers are stored at the lua end), but you can always do \doiftextelse(\getbuffer[...]} to test it buffer creates any output. indeed you know there is a way ...
\starttext
\startbuffer[test] test \stopbuffer
\doifelsebuffer{test} {YES} {NOP}
\permanent\protected\def\doifelsebuffercontent#1% {\doifelse{\luaexpr{(string.strip(buffers.getcontent("#1")) ~= "" and "yes")}}{yes}}
\startbuffer[test] \stopbuffer
\doifelsebuffercontent{test} {YES} {NOP}
\startbuffer[test]
\stopbuffer
\doifelsebuffercontent{test} {YES} {NOP}
\stoptext
if we can agree on a proper name then i can add a (more efficient) \doifelse...
Hans
----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl ----------------------------------------------------------------- ___________________________________________________________________________________ If your question is of interest to others as well, please add an entry to the Wiki!
maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context webpage : http://www.pragma-ade.nl / http://context.aanhet.net archive : https://bitbucket.org/phg/context-mirror/commits/ wiki : http://contextgarden.net ___________________________________________________________________________________
Hans van der Meer via ntg-context schrieb am 02.08.2021 um 18:21:
And in pathological cases where even this does not work (as I encountered a few days ago): \setbox\scratchbox=\vboxx{content}\ifdim\wd\scratchbox>\zeropoint .. \else .. \fi
This is how \doiftext works but it uses a \hbox. Wolfgang
Hans Hagen via ntg-context schrieb am 02.08.2021 um 17:17:
On 8/2/2021 4:43 PM, Aditya Mahajan via ntg-context wrote:
On Mon, 2 Aug 2021, Benjamin Buchmuller via ntg-context wrote:
Hi,
How can I check if a buffer or block has content (or is empty) before a \useblocks or \getbuffer statement?
I would like to typeset a headline only if the buffer/block has been used in a particular section and want to avoid headlines with empty sections.
There may be more efficient ways to do it (since buffers are stored at the lua end), but you can always do \doiftextelse(\getbuffer[...]} to test it buffer creates any output. indeed you know there is a way ...
\starttext
\startbuffer[test] test \stopbuffer
\doifelsebuffer{test} {YES} {NOP}
\permanent\protected\def\doifelsebuffercontent#1% {\doifelse{\luaexpr{(string.strip(buffers.getcontent("#1")) ~= "" and "yes")}}{yes}}
\startbuffer[test] \stopbuffer
\doifelsebuffercontent{test} {YES} {NOP}
\startbuffer[test]
\stopbuffer
\doifelsebuffercontent{test} {YES} {NOP}
\stoptext
if we can agree on a proper name then i can add a (more efficient) \doifelse...
To follow the existing commands we can use \doifelsebufferempty \doifelsebuffersomething \doifelsebuffernothing or (we already have \xmldoif... and \btxdoif...) \bufferdoifelseempty \bufferdoifelsesomething \bufferdoifelsenothing Wolfgang
On 8/2/2021 6:31 PM, Wolfgang Schuster via ntg-context wrote:
\doifelsebufferempty
i go for that one (mkiv and lmtx0 ; btw, \doifelsebuffer is expandable but the empty test not) Hans ----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl -----------------------------------------------------------------
participants (5)
-
Aditya Mahajan
-
Benjamin Buchmuller
-
Hans Hagen
-
Hans van der Meer
-
Wolfgang Schuster