Hi! I'm the author of a PGF/TikZ-based LaTeX package Forest for drawing (linguistic) trees. I have received several requests to support ConTexT and I'm trying to do that now. Forest uses a lot of \boxes. Actually, the number of boxes used cannot be known in advance, as it depends on the document. Here's why: Forest's main job is to place the nodes of a tree so that they don't overlap and to do that, it needs to first typeset the individual nodes and measure them. The typeset nodes are kept around and later used when typesetting the entire tree. There are three reasons for keeping the typeset nodes, and not typesetting them again as part of the entire tree. First (at least historically): performance. Second: there is no guarantee that running the same code twice will result in the same box. Third: changing this now would break compatibility. The typeset nodes are kept in "local" boxes. They are created using \locbox, which is defined either by plain eTeX format or package elocalloc.sty for LaTeX >2015. (Prior to LaTeX 2015, \locbox was defined in etex.sty.) elocalloc.sty's \locbox is somewhat broken, but still works, mainly because it seems no other package is using local allocation. ;-) Using normal, "global" boxes, created by \newbox, would be bad. If a \newbox was simply executed for each new node, the number of used boxes would remain increased even after Forest was done with a tree, so in a document with lots of big trees, the upper limit of 32000+ could actually be hit. An alternative approach would be to track and reuse boxes which were created by \newbox by are not used anymore. But implementing such a tracking mechanism seems quite complicated, and besides, it would not be perfect: after exiting the TeX group where the tree is drawn, the allocated boxes would persist. (So, from the outside perspective, Forest's box usage in the entire document would equal the box usage of the largest tree.) The approach taken in the implementation of \locbox seems quite appealing in its simplicity: The local boxes (counts etc) are allocated "from the top" (starting at 32767 and going down) and all the \chardefs and allocation register changes are local (i.e. not \global). A bit of care to prevent clashes with global allocation "from the bottom", and that's it. I've studied the allocation mechanism of ConTeXt --- but note that I'm new to ConTeXt! --- and it seems that I'd know how to implement \loc<thing>s using "from the top" approach, but there are two, or rather three, problems. (I took a look at both MKII and MKIV, but the changes seemed mostly cosmetic, so I concentrated on MKIV.) 1. I'm not sure how happy ConTeXt were if I changed the value of \c_syst_max_allocated_register. The name seems to imply it should be constant. 2. Simply decreasing \c_syst_max_allocated_register would while doing a \locbox would also decrease the upper limit for, say, \loccount, as they use the same upper limit, i.e. \c_syst_max_allocated_register. A proper way would be to declare \c_syst_max_allocated_box_register etc. Again, I have no idea how such a change would impact other parts of the system. (Btw, elocalloc.sty's implementation, unlike the plain eTeX one, suffers from precisely this problem.) 3. It seems that, unlike in the messy LaTeX world, ConTeXt people like to do things The Right Way. ;-) As making a change like this in a module is surely not The Right Way, I hope that ConTeXt developers can help me out here, either by implementing the local allocation (I'm happy to assist in any way) or by maybe suggesting an alternative approach. (Btw, if it helps, I guess a MKIV-only solution would be acceptable to most users.) Thanks in advance, Sašo Živanović
On Tue, 27 Dec 2016, Sašo Živanović wrote:
I'm the author of a PGF/TikZ-based LaTeX package Forest for drawing (linguistic) trees. I have received several requests to support ConTexT and I'm trying to do that now.
Thank you for trying to port your code to ConTeXt. Although I don't foresee that I will use the forest package, I do use pgfplots quite a lot and appreciate the extra effort put in by the package authors to support two different macro packages.
Forest uses a lot of \boxes. Actually, the number of boxes used cannot be known in advance, as it depends on the document. Here's why: Forest's main job is to place the nodes of a tree so that they don't overlap and to do that, it needs to first typeset the individual nodes and measure them. The typeset nodes are kept around and later used when typesetting the entire tree.
I don't have an answer to your questions, but I am replying partly to bump this thread again.
The typeset nodes are kept in "local" boxes. They are created using \locbox, which is defined either by plain eTeX format or package elocalloc.sty for LaTeX >2015. (Prior to LaTeX 2015, \locbox was defined in etex.sty.) elocalloc.sty's \locbox is somewhat broken, but still works, mainly because it seems no other package is using local allocation. ;-)
Let me see if I understand this correctly. You are asking how to define a macro, \locbox, such that the following code should not change the value of \c_syst_last_allocated_box. \bgroup \locbox\BoxOne \locbox\BoxTwo \BoxOne\hbox{Box One} \BoxTow\hbox{Box Two} % Do some measurement based on the size of the boxes \egroup The fact that a LaTeX package implements this behaviour by allocating local box numbers from the end seems to be an implementation detail. I don't know the best way to implement such a macro in ConTeXt, but I want to make sure that the user-level behavior that you want is properly understood. Given the details in your question, one can easily lose the forest for the trees (pun intended :-) Aditya
On 23. 01. 2017 09:26, Aditya Mahajan wrote:
I don't have an answer to your questions, but I am replying partly to bump this thread again. Thanks!
The typeset nodes are kept in "local" boxes. They are created using \locbox, which is defined either by plain eTeX format or package elocalloc.sty for LaTeX >2015. (Prior to LaTeX 2015, \locbox was defined in etex.sty.) elocalloc.sty's \locbox is somewhat broken, but still works, mainly because it seems no other package is using local allocation. ;-)
Let me see if I understand this correctly.
You are asking how to define a macro, \locbox, such that the following code should not change the value of \c_syst_last_allocated_box.
\bgroup \locbox\BoxOne \locbox\BoxTwo
\BoxOne\hbox{Box One} \BoxTow\hbox{Box Two} You probably meant \setbox\BoxOne\hbox{Box One} % Do some measurement based on the size of the boxes \egroup Essentially, yes.
The intro section to package localloc (afaik, historically the first package to implement local register allocation) might shed some light on the issue as well. http://ctan.org/pkg/localloc
The fact that a LaTeX package implements this behaviour by allocating local box numbers from the end seems to be an implementation detail. True, although I'm not aware of any other possibility to implement this.
I don't know the best way to implement such a macro in ConTeXt, but I want to make sure that the user-level behavior that you want is properly understood. Given the details in your question, one can easily lose the forest for the trees (pun intended :-) Damn. Precisely what the users say about my documentation! ;-)
Best, Sašo
Aditya
On 1/23/2017 9:26 AM, Aditya Mahajan wrote:
On Tue, 27 Dec 2016, Sašo Živanović wrote:
I'm the author of a PGF/TikZ-based LaTeX package Forest for drawing (linguistic) trees. I have received several requests to support ConTexT and I'm trying to do that now.
Thank you for trying to port your code to ConTeXt. Although I don't foresee that I will use the forest package, I do use pgfplots quite a lot and appreciate the extra effort put in by the package authors to support two different macro packages.
Forest uses a lot of \boxes. Actually, the number of boxes used cannot be known in advance, as it depends on the document. Here's why: Forest's main job is to place the nodes of a tree so that they don't overlap and to do that, it needs to first typeset the individual nodes and measure them. The typeset nodes are kept around and later used when typesetting the entire tree.
I don't have an answer to your questions, but I am replying partly to bump this thread again.
The typeset nodes are kept in "local" boxes. They are created using \locbox, which is defined either by plain eTeX format or package elocalloc.sty for LaTeX >2015. (Prior to LaTeX 2015, \locbox was defined in etex.sty.) elocalloc.sty's \locbox is somewhat broken, but still works, mainly because it seems no other package is using local allocation. ;-)
Let me see if I understand this correctly.
You are asking how to define a macro, \locbox, such that the following code should not change the value of \c_syst_last_allocated_box.
\bgroup \locbox\BoxOne \locbox\BoxTwo
\BoxOne\hbox{Box One} \BoxTow\hbox{Box Two} % Do some measurement based on the size of the boxes \egroup
The fact that a LaTeX package implements this behaviour by allocating local box numbers from the end seems to be an implementation detail.
I don't know the best way to implement such a macro in ConTeXt, but I want to make sure that the user-level behavior that you want is properly understood. Given the details in your question, one can easily lose the forest for the trees (pun intended :-)
\unprotect \installcorenamespace{localbox} \unexpanded\def\newlocalbox#1% {\expandafter\let\expandafter#1\csname\??localbox\string#1\endcsname \ifx#1\relax \syst_aux_new_localbox#1% \fi} \def\syst_aux_new_localbox#1% {\expandafter\newbox\csname\??localbox\string#1\endcsname \newlocalbox#1} \protect \starttext \newlocalbox\BoxOne \newlocalbox\BoxTwo \setbox\BoxOne\hbox{Box One} \setbox\BoxTwo\hbox{Box Two} [\box\BoxTwo] [\box\BoxOne] \let\locbox\newlocalbox \stoptext ----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl -----------------------------------------------------------------
I'm afraid this doesn't do the trick: \c_syst_last_allocated_box is definitely incremented. Let me try explaining again. This is essentially what I do: { \newbox\TempBox \setbox\TempBox\hbox{Box One} \let\BoxOne\TempBox \global\let\TempBox\relax \newbox\TempBox \setbox\TempBox\hbox{Box Two} \let\BoxTwo\TempBox \global\let\TempBox\relax \the\numexpr\BoxOne: [\box\BoxOne] \the\numexpr\BoxTwo: [\box\BoxTwo] } But I want the number of available box registers at entering and exiting the group to be the same, and substituting \newbox for \locbox is what does the trick in my package. So I can run the following a million times: { \newlocalbox\TempBox \setbox\TempBox\hbox{Box One} \let\BoxOne\TempBox \global\let\TempBox\relax \newlocalbox\TempBox \setbox\TempBox\hbox{Box Two} \let\BoxTwo\TempBox \global\let\TempBox\relax \the\numexpr\BoxOne: [\box\BoxOne] \the\numexpr\BoxTwo: [\box\BoxTwo] } Each of the million "\the\numexpr\BoxOne" should produce the same number, whereas in the \newbox-based example these numbers increment. Best, Sašo On 24. 01. 2017 09:52, Hans Hagen wrote:
You are asking how to define a macro, \locbox, such that the following code should not change the value of \c_syst_last_allocated_box.
\bgroup \locbox\BoxOne \locbox\BoxTwo
\BoxOne\hbox{Box One} \BoxTow\hbox{Box Two} % Do some measurement based on the size of the boxes \egroup
The fact that a LaTeX package implements this behaviour by allocating local box numbers from the end seems to be an implementation detail.
I don't know the best way to implement such a macro in ConTeXt, but I want to make sure that the user-level behavior that you want is properly understood. Given the details in your question, one can easily lose the forest for the trees (pun intended :-)
\unprotect
\installcorenamespace{localbox}
\unexpanded\def\newlocalbox#1% {\expandafter\let\expandafter#1\csname\??localbox\string#1\endcsname \ifx#1\relax \syst_aux_new_localbox#1% \fi}
\def\syst_aux_new_localbox#1% {\expandafter\newbox\csname\??localbox\string#1\endcsname \newlocalbox#1}
\protect
\starttext
\newlocalbox\BoxOne \newlocalbox\BoxTwo
\setbox\BoxOne\hbox{Box One} \setbox\BoxTwo\hbox{Box Two}
[\box\BoxTwo] [\box\BoxOne]
\let\locbox\newlocalbox
\stoptext
----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl ----------------------------------------------------------------- _______________________________________________ dev-context mailing list dev-context@ntg.nl https://mailman.ntg.nl/mailman/listinfo/dev-context
participants (3)
-
Aditya Mahajan
-
Hans Hagen
-
Sašo Živanović