ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
* [NTG-context] Breakable tcolorbox in ConTeXt
@ 2024-08-11  9:24 Florent Michel
  2024-08-11 11:16 ` [NTG-context] " Hans Hagen via ntg-context
  0 siblings, 1 reply; 3+ messages in thread
From: Florent Michel @ 2024-08-11  9:24 UTC (permalink / raw)
  To: ntg-context


[-- Attachment #1.1: Type: text/plain, Size: 6245 bytes --]

Hi,

I am trying to reproduce the behaviour of the tcolorbox LaTeX package, more
specifically breakable boxes (as illustrated here:
https://tex.stackexchange.com/a/676607/123770). It works nicely out of the
box (pun intended) using `\definetextbackground` and drawing the frame with
MetaPost, using counters to keep track of whether the current text is at
the top, middle, or bottom of the box. However, I'm having issues when one
box ends and another starts on the same page (see more details below). From
what I understand, the issue is that counters seem to be updated on a
page-wide basis, so each MPgraphic ‘sees’ the values of counters at the end
of the current page, which may not be right if another box modifies them.
Would anyone know how to resolve this?

Based on two answer on TeX StackExchange (
https://tex.stackexchange.com/a/486124/123770 ,
https://tex.stackexchange.com/questions/377234/context-frame-problems/377261#377261),
the following code works well if there is no more than one box per page:

```
\definecounter[pageNumberTop]
\definecounter[pageNumberBottom]

\startuseMPgraphic{mp:axiomframe}
  begingroup;
    for i=1 upto nofmultipars :
      path p;
      p := ( llcorner multipars[i]
             -- lrcorner multipars[i]
             -- urcorner multipars[i]
             -- ulcorner multipars[i]
             -- cycle )
             enlarged (EmWidth,EmWidth) ;
      fill p withcolor boxfillcolor ;

      % if the current page is the first one covered by the box...
      if (\pagenumber == \rawcountervalue[pageNumberTop]) :
        % ... if it is also the last one, the box fits entirely on the
page; we thus draw the full frame
        if \rawcountervalue[pageNumberTop] ==
\rawcountervalue[pageNumberBottom] :
          draw p
            withpen pencircle scaled 2pt
            withcolor boxlinecolor ;

        % ... otherwise, don't draw the bottom line
        else :
          draw llcorner multipars[i] + (-EmWidth, -EmWidth)
               -- ulcorner multipars[i] + (-EmWidth, EmWidth)
               -- urcorner multipars[i] + (EmWidth, EmWidth)
               -- lrcorner multipars[i] + (EmWidth, -EmWidth)
            withpen pencircle scaled 2pt
            withcolor boxlinecolor ;
        fi ;
        picture pic;
        pic := textext.ulft("\tfd\symbol[info]");
        pic := pic shifted ulcorner multipars[i];
        fill bbox pic withcolor white;
        draw pic;

      % if the current page is the last one covered by the box, draw the
left, bottom, and right lines
      elseif \pagenumber == \rawcountervalue[pageNumberBottom] :
        draw ulcorner multipars[i] + (-EmWidth, EmWidth)
          -- llcorner multipars[i] + (-EmWidth, -EmWidth)
          -- lrcorner multipars[i] + (EmWidth, -EmWidth)
          -- urcorner multipars[i] + (EmWidth, EmWidth)
          withpen pencircle scaled 2pt
          withcolor boxlinecolor ;

      % if the current page is neither the first one nor the last one, only
draw the left and right lines
      else :
        draw llcorner multipars[i] - (EmWidth, EmWidth)
             -- ulcorner multipars[i] + (-EmWidth, EmWidth)
          withpen pencircle scaled 2pt
          withcolor boxlinecolor ;
        draw urcorner multipars[i] + (EmWidth, EmWidth)
             -- lrcorner multipars[i] + (EmWidth, -EmWidth)
          withpen pencircle scaled 2pt
          withcolor boxlinecolor ;
      fi ;
    endfor ;
  setbounds currentpicture to OverlayBox ;
  endgroup;
\stopuseMPgraphic

\definetextbackground
  [theoremFrame]
  [mp=mp:axiomframe,
   location=paragraph,
   backgroundcolor=green,
   framecolor=red,
   before={\setcounter[pageNumberTop][\pagenumber]},
   after={\setcounter[pageNumberBottom][\pagenumber]}]

\definestartstop
  [ColoredBox]
  [before={\blank[2em]\starttheoremFrame},
   after={\stoptheoremFrame\blank[2em]}]
```

For instance,

```
\startColoredBox
    \input knuth
    \input knuth
    \input knuth
    \input knuth
    \input knuth
    \input knuth
    \input knuth
    \input knuth
    \input knuth
    \input knuth
    \input knuth
\stopColoredBox
```
correctly gives a box split over multiple pages, with the top part of the
frame only drawn on the first page and the bottom part only drawn on the
last one.
It also works correctly when adding `\page` between boxes:

```
\starttext
\startColoredBox
    \input knuth
    \input knuth
    \input knuth
    \input knuth
    \input knuth
    \input knuth
    \input knuth
    \input knuth
    \input knuth
    \input knuth
    \input knuth
\stopColoredBox
\page
\startColoredBox
    \input knuth
    \input knuth
    \input knuth
    \input knuth
    \input knuth
    \input knuth
    \input knuth
    \input knuth
    \input knuth
    \input knuth
    \input knuth
\stopColoredBox
```

However,  the following gives an incorrect output:
```
\starttext
\startColoredBox
    \input knuth
    \input knuth
    \input knuth
    \input knuth
    \input knuth
    \input knuth
    \input knuth
    \input knuth
    \input knuth
    \input knuth
    \input knuth
\stopColoredBox
\startColoredBox
    \input knuth
    \input knuth
    \input knuth
    \input knuth
    \input knuth
    \input knuth
    \input knuth
    \input knuth
    \input knuth
    \input knuth
    \input knuth
\stopColoredBox
```
On page 3, both frames are fully drawn while the first box is a
continuation of the one on the previous page and the second one is the
start of a box continuing on the next page. I tried several
variations without success; my main issue is that (if I understand
correctly) the values of counters are the same for the two boxes on page 3,
so that at least one of them will be wrong.

If someone has any ideas on how to resolve this, I'd be really grateful!
Alternatively, if someone has a solution using Tikz, that would also work -
I'd ideally use MetaPost as the tight integration with ConTeXt seems (from
my short experience) to make many things easier; but any solution using
ConTeXt + a small number of other dependencies is fine.

Cheers,
Florent

[-- Attachment #1.2: Type: text/html, Size: 7248 bytes --]

[-- Attachment #2: Type: text/plain, Size: 511 bytes --]

___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / https://mailman.ntg.nl/mailman3/lists/ntg-context.ntg.nl
webpage  : https://www.pragma-ade.nl / https://context.aanhet.net (mirror)
archive  : https://github.com/contextgarden/context
wiki     : https://wiki.contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [NTG-context] Re: Breakable tcolorbox in ConTeXt
  2024-08-11  9:24 [NTG-context] Breakable tcolorbox in ConTeXt Florent Michel
@ 2024-08-11 11:16 ` Hans Hagen via ntg-context
  2024-08-11 12:35   ` Florent Michel
  0 siblings, 1 reply; 3+ messages in thread
From: Hans Hagen via ntg-context @ 2024-08-11 11:16 UTC (permalink / raw)
  To: ntg-context; +Cc: Hans Hagen

On 8/11/2024 11:24 AM, Florent Michel wrote:
> Hi,
> 
> I am trying to reproduce the behaviour of the tcolorbox LaTeX package, 
> more specifically breakable boxes (as illustrated here: 
> https://tex.stackexchange.com/a/676607/123770 
> <https://tex.stackexchange.com/a/676607/123770>). It works nicely out of 
> the box (pun intended) using `\definetextbackground` and drawing the 
> frame with MetaPost, using counters to keep track of whether the current 
> text is at the top, middle, or bottom of the box. However, I'm having 
> issues when one box ends and another starts on the same page (see more 
> details below). From what I understand, the issue is that counters seem 
> to be updated on a page-wide basis, so each MPgraphic ‘sees’ the values 
> of counters at the end of the current page, which may not be right if 
> another box modifies them. Would anyone know how to resolve this?
> 
> Based on two answer on TeX StackExchange 
> (https://tex.stackexchange.com/a/486124/123770 
> <https://tex.stackexchange.com/a/486124/123770> , 
> https://tex.stackexchange.com/questions/377234/context-frame-problems/377261#377261 <https://tex.stackexchange.com/questions/377234/context-frame-problems/377261#377261>), the following code works well if there is no more than one box per page:
> 
> ```

You're missing a feature:

this is the original trick:

if     multilocs[i] == 1 :  % begin
elseif multilocs[i] == 2 :  % between
elseif multilocs[i] == 3 :  % end
fi ;

and in lmtx we have

if      multikind[i] = "single" :
elseif  multikind[i] = "first"  :
elseif  multikind[i] = "middle" :
elseif  multikind[i] = "last"   :

Also:

      path p;
       p := ( llcorner multipars[i]
              -- lrcorner multipars[i]
              -- urcorner multipars[i]
              -- ulcorner multipars[i]
              -- cycle )
              enlarged (EmWidth,EmWidth) ;
       fill p withcolor boxfillcolor ;

can be

fill multipars[i] enlarged (EmWidth,EmWidth) ...

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 / https://mailman.ntg.nl/mailman3/lists/ntg-context.ntg.nl
webpage  : https://www.pragma-ade.nl / https://context.aanhet.net (mirror)
archive  : https://github.com/contextgarden/context
wiki     : https://wiki.contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [NTG-context] Re: Breakable tcolorbox in ConTeXt
  2024-08-11 11:16 ` [NTG-context] " Hans Hagen via ntg-context
@ 2024-08-11 12:35   ` Florent Michel
  0 siblings, 0 replies; 3+ messages in thread
From: Florent Michel @ 2024-08-11 12:35 UTC (permalink / raw)
  To: mailing list for ConTeXt users


[-- Attachment #1.1: Type: text/plain, Size: 3333 bytes --]

Thank you very much Hans for your clear and detailed reply!
I can confirm that this resolves my issue - and I really
like the flexibility that this syntax provides!

Best regards,
Florent

Le dim. 11 août 2024 à 12:18, Hans Hagen via ntg-context <ntg-context@ntg.nl>
a écrit :

> On 8/11/2024 11:24 AM, Florent Michel wrote:
> > Hi,
> >
> > I am trying to reproduce the behaviour of the tcolorbox LaTeX package,
> > more specifically breakable boxes (as illustrated here:
> > https://tex.stackexchange.com/a/676607/123770
> > <https://tex.stackexchange.com/a/676607/123770>). It works nicely out
> of
> > the box (pun intended) using `\definetextbackground` and drawing the
> > frame with MetaPost, using counters to keep track of whether the current
> > text is at the top, middle, or bottom of the box. However, I'm having
> > issues when one box ends and another starts on the same page (see more
> > details below). From what I understand, the issue is that counters seem
> > to be updated on a page-wide basis, so each MPgraphic ‘sees’ the values
> > of counters at the end of the current page, which may not be right if
> > another box modifies them. Would anyone know how to resolve this?
> >
> > Based on two answer on TeX StackExchange
> > (https://tex.stackexchange.com/a/486124/123770
> > <https://tex.stackexchange.com/a/486124/123770> ,
> >
> https://tex.stackexchange.com/questions/377234/context-frame-problems/377261#377261
> <
> https://tex.stackexchange.com/questions/377234/context-frame-problems/377261#377261>),
> the following code works well if there is no more than one box per page:
> >
> > ```
>
> You're missing a feature:
>
> this is the original trick:
>
> if     multilocs[i] == 1 :  % begin
> elseif multilocs[i] == 2 :  % between
> elseif multilocs[i] == 3 :  % end
> fi ;
>
> and in lmtx we have
>
> if      multikind[i] = "single" :
> elseif  multikind[i] = "first"  :
> elseif  multikind[i] = "middle" :
> elseif  multikind[i] = "last"   :
>
> Also:
>
>       path p;
>        p := ( llcorner multipars[i]
>               -- lrcorner multipars[i]
>               -- urcorner multipars[i]
>               -- ulcorner multipars[i]
>               -- cycle )
>               enlarged (EmWidth,EmWidth) ;
>        fill p withcolor boxfillcolor ;
>
> can be
>
> fill multipars[i] enlarged (EmWidth,EmWidth) ...
>
> 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 /
> https://mailman.ntg.nl/mailman3/lists/ntg-context.ntg.nl
> webpage  : https://www.pragma-ade.nl / https://context.aanhet.net (mirror)
> archive  : https://github.com/contextgarden/context
> wiki     : https://wiki.contextgarden.net
>
> ___________________________________________________________________________________
>

[-- Attachment #1.2: Type: text/html, Size: 5265 bytes --]

[-- Attachment #2: Type: text/plain, Size: 511 bytes --]

___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / https://mailman.ntg.nl/mailman3/lists/ntg-context.ntg.nl
webpage  : https://www.pragma-ade.nl / https://context.aanhet.net (mirror)
archive  : https://github.com/contextgarden/context
wiki     : https://wiki.contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2024-08-11 12:38 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-08-11  9:24 [NTG-context] Breakable tcolorbox in ConTeXt Florent Michel
2024-08-11 11:16 ` [NTG-context] " Hans Hagen via ntg-context
2024-08-11 12:35   ` Florent Michel

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).