Unable to fold back unfold inside macro

Hi!

Following suggestions from previous post, I am trying to write a macro that is essentially unfold + a title:


Which should be default to unfold mode when it is created (so that typing is convenient), but default to fold when the file is reopened (to avoid spoilers).
I attempted with this piece of code:
image
However, with this code the unfold is no longer foldable back … What am I missing? Thanks :slight_smile:

Hi,
I thought that the fold/unfold behaviour might follow the idea of the “new list” behaviour which @mgubi showed in Creating a new list does not seem to work and in a few following posts.

After looking a bit in the Scheme files of TeXmacs, with the help of ack, and a bit of experimenting, I wrote this (copying the macros from fold/unfold and adapting the arguments)

<assign|folded-sol|<\macro|y>
  <\render-folded-std|<unfold-button|<resize|<with|mode|math|<op|\<circ\>>>|||<maximum|1r|1.5fn>|>|<with|font-series|bold|solution>>>
    <with|font-series|bold|solution>

    <hidden|<arg|y>>
  </render-folded-std>
</macro>>

<assign|unfolded-sol|<\macro|y>
  <\render-folded-std|<fold-button|<resize|<with|mode|math|\<bullet\>>|||<maximum|1r|1.5fn>|>|<with|font-series|bold|solution>>>
    <with|font-series|bold|solution>

    <arg|y>
  </render-folded-std>
</macro>>

which together with

(define-fold folded-sol unfolded-sol)

executed in a Scheme session, gave the title and the fold-unfold behaviour. I expect the Scheme command to work the same when placed in the personal initialization file.
Maybe it is even possible to do it more simply by writing the macro as you did and writing a second unfolded macro, but I did not yet try.

1 Like

This also works:

<assign|u-sol|<macro|body|<unfolded|<with|font-series|bold|solution>|<arg|body>>>>

<assign|f-sol|<macro|body|<folded|<with|font-series|bold|solution>|<arg|body>>>>

and in Scheme (again tried in a session only)

(define-fold f-sol u-sol)

Use Paste from -> TeXmacs to paste the macros into your preamble.

2 Likes

Thanks I tried the scheme part with Meta+X and it worked as expected. Is there any way to embed a scheme code into style file?

Maybe a short way is through the extern tag. Please try the following in the style file:

<extern|(lambda () (define-fold f-sol u-sol))>

Otherwise through the use-module macro, you would have to work a bit to place then the Scheme commands in the file you refer to with use-module; yet another way in your my-init-texmacs.scm in the user settings (.texmacs in Unix-like systems) directory.

Finally

I think that a Scheme session is more convenient (can’t so all that Meta+X can do though in my understanding).

Hm. I tried the extern one, but I don’t think it’s working … I would ideally rather not place in my-init-texmacs.scm because that would kinda lose the encapsulation of the macro inside style file (? idk)

For me the extern macro is working, but I placed it in the preamble, so let me think.

In addition to this, running from the terminal I have seen that my code (both versions) gives the following error when unfolding (but not when folding):

Backtrace:
In unknown file:
   ?: 0* [#<procedure {#f} ()>]
   ?: 1* [mouse-unfold <tree <with|font-series|bold|solution>>]
In /home/giovanni/Downloads/TeXmacs/TeXmacs-2.1.1-x86_64-pc-linux-gnu/TeXmacs/progs/utils/edit/variants.scm:
 223: 2  (if (tree->path t) (begin (tree-go-to t #) (if # #)))
In /home/giovanni/Downloads/TeXmacs/TeXmacs-2.1.1-x86_64-pc-linux-gnu/TeXmacs/progs/dynamic/fold-edit.scm:
   ...
 143: 3  [players-set-elapsed {#f} 0.0]

/home/giovanni/Downloads/TeXmacs/TeXmacs-2.1.1-x86_64-pc-linux-gnu/TeXmacs/progs/dynamic/fold-edit.scm:143:7: In procedure players-set-elapsed in expression (players-set-elapsed (tree-ref t i) 0.0):
/home/giovanni/Downloads/TeXmacs/TeXmacs-2.1.1-x86_64-pc-linux-gnu/TeXmacs/progs/dynamic/fold-edit.scm:143:7: Wrong type argument in position 1: #f

I did not yet have the time to look at the error. Maybe someone else knows what happens?

I have sent a message on the error to the TeXmacs-dev list, if I get any hint I will report it here.

I am also going to try and improve my answer to how to include the Scheme code into the style file - till soon.

Looking at std-fold.ts, it seems that fold macros take two arguments, one for the “folded” and one the “unfolded” contnent.

With the help of @jeroen above, I have fixed the code, adding to the macros an argument which I do not use (I do not know how to rewrite the macros and Scheme code to make the folding-unfolding work with one argument only).

I am not able to fit both the macros and the Scheme code in the style file, so I had to call the Scheme code in the style file with the use-module macro

I placed the following in a style file (in the styles directory of my personal initialization directory) which I called fold-unfold.ts

<use-package|generic>

<use-module|(styles fold-unfold)>

<assign|u-sol|<macro|dummy|body|<unfolded|<with|font-series|bold|solution>|<arg|body>>>>

<assign|f-sol|<macro|dummy|body|<folded|<with|font-series|bold|solution>|<arg|body>>>>

then in my personal initialization directory I used a directory progs/styles which I had already placed there for other Scheme files and added in there a file called fold-unfold.scm with the following content

(texmacs-module (styles fold-unfold))

(define-fold f-sol u-sol)

By setting as style file in a document fold-unfold (since the style file is in the styles directory, TeXmacs lists it in the menu) I have the macros available which fold and unfold.

Then you can adapt to your wishes.

I think that with styles this is the maximum “self-containment” that you can achieve.

2 Likes