You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Support for show rules that match on their own output #229
Yeah, the styling system is a bit broken. That's one of the next things on my list. If you just use it within the output, it works fine but if you create a new element that matches again, it's broken.
Yeah, just trying around a bit :) What's maybe more important is that if the compiler crashes once, it stays broken until I reload the page. Not sure if that's the right behavior. I think it should restart the compiler after the next edit
I think we just need some way to "unset" the #show.
#show "Test": it => [
#show "Test": none
Test
]
Test
This works fine except that it makes the Test invisible.
Although the behavior would be a bit difficult to predict (should it unset one layer or all layers?) given how the changes are being piled up at the moment:
#show "Test": it => [a] + it
#show "Test": it => [b] + it
Test
Or maybe show rules should just apply to things that come after them. I don't know how show-rule application works in typst, but it seems that they are applied within their scope, but only to text that comes after them.
For example:
a — b — c
#[
a — b — c
#show "c": it => [fromC(a)]
#show "a": it => [fromA(#it)]
#show "b": it => [fromB(a)]
a — b — c
]
a — b — c
gives:
a — b — c
a — b — c // show rules are not applied because text comes before (?)
fromA(a) — fromB(fromA(a)) — fromC(fromA(a)) // rule "a" applied within rule "c" even though "c" comes before
a — b — c
If they would be applied only in whatever comes after them and excluding their own transformation function, all recursion stuff would be solved (because recursion is not possible anymore)... though on the other hand, that would invite behaviour where stuff looks different depending on the order of rules... though that is already the case...
I'm not sure how to solve this with show "Text": .... But from a quick look at the code it seems that functions have an internal name associated with them, so for show elem: f would this be enough?
If f is a lambda expression then compile/process that first so we get a function object which references the current version of elem.
Set the internal name of elem to something new (like "elem" + unique ID). Note that f necessarily references the version with the old name.
Now process show elem: f just like we do currently.
I'm not sure this was clearly mentioned in this thread, but in the mean time, in situations involving strings and regexes, you can use a show rule with a function and reuse the it parameter in the output. That won't cause recursion.
That doesn't work
#show"My Project": underline("My Project")
That works
#show"My Project": it=>underline(it)
Reacted by jassler, Martín Andrighetti, Andrew Voynov and John
I see fixing this issue is on the roadmap - is there any timeline for the fix? I'm asking because my company won't move from Latex to typst until this is resolved. In any case, thanks for your work!
I failed to make an alias show "cmd": it => raw(it) because it is content instead of string. Adding repr/str also doesn't work. Does anyone have a workaround for this?
@Andrew15-5 The mark you wanted can be implemented without any changes to typst through a zero-width no-break space. Hacky, causing double-raws like below if cmd is already in a codeblock and possibly detrimental to accessibility, but it works for me.
#letinsert-zwsp(it) = {
(it.first()
+"\u{FEFF}"+it.clusters().slice(1).join())
}
#show"cmd": it=>raw(insert-zwsp(it.text))
Use cmd for more information, as in `cmd lol`.
EDIT: Updated to use the non-breaking zero-width space U+FEFF instead of the breaking one (U+200B).
Very hacky indeed. Thank you. I already forgot about this character. I found out about it a year or two ago. I think that's a great universal workaround. Okular was able to find both cmd, so the ZWSP char does not affect word search.
That's the thing — I'm talking about marking each processed "content", meaning that if the top list element was processed, then mark it, then separately mark each nested list element.
We already do that, but it doesn't work anyway because, to use laurmaedje's example, with show "hey": box[hey], you get "hey" (unmarked) --> box("hey" (unmarked)) (marked) --> box(box("hey" (unmarked)) (marked)) (marked) -> etc.
@Andrew15-5 the problem I see with the word joiner is that potentially emojis are formed with that. Maybe that's the case with U+FEFF as well, but idk.
Anyway, maybe you want to check out the Discord server for quicker responses to your specific problem.
I agree, as this was one of my first thoughts when I read the article. But since we are (I'm assuming) specifically talking about text substitution show rules, it is safe to say that this is impossible to happen. After all, the main point is to simplify the source file and potentially remove all the emojis and stuff (it's especially noticeable in Neovim, since not all of them are shown as a single cluster) with another pack of show rules (show "green-heart": "💚").
With the latest changes, this doesn't crash anymore, instead giving a proper error message. Hence, I'm converting this from a bug to a feature request.
I have opened #3327 to fix this for text show rules. For other show rules, I think the current error is fine and should remain. I've explained my reasoning for that in the linked PR. The issue will be closed once the PR is merged.
Activity
laurmaedje commentedon Mar 23, 2023
Yeah, the styling system is a bit broken. That's one of the next things on my list. If you just use
itwithin the output, it works fine but if you create a new element that matches again, it's broken.SrTobi commentedon Mar 23, 2023
Yeah, just trying around a bit :) What's maybe more important is that if the compiler crashes once, it stays broken until I reload the page. Not sure if that's the right behavior. I think it should restart the compiler after the next edit
laurmaedje commentedon Mar 23, 2023
It should do that, strange ...
SrTobi commentedon Mar 23, 2023
Hmm ok, nvm... tried again and now it works...
user202729 commentedon Mar 26, 2023
I think we just need some way to "unset" the
#show.This works fine except that it makes the
Testinvisible.Although the behavior would be a bit difficult to predict (should it unset one layer or all layers?) given how the changes are being piled up at the moment:
will result in
baTest.SrTobi commentedon Mar 26, 2023
Or maybe show rules should just apply to things that come after them. I don't know how show-rule application works in typst, but it seems that they are applied within their scope, but only to text that comes after them.
For example:
gives:
If they would be applied only in whatever comes after them and excluding their own transformation function, all recursion stuff would be solved (because recursion is not possible anymore)... though on the other hand, that would invite behaviour where stuff looks different depending on the order of rules... though that is already the case...
SrTobi commentedon Mar 26, 2023
One would think that using
it => itinstead ofnonewould fix that... but that, in turn, crashes the compiler again 😆ndtodoroff commentedon Mar 27, 2023
I'm not sure how to solve this with
show "Text": .... But from a quick look at the code it seems that functions have an internal name associated with them, so forshow elem: fwould this be enough?fis a lambda expression then compile/process that first so we get a function object which references the current version ofelem.elemto something new (like "elem" + unique ID). Note thatfnecessarily references the version with the old name.show elem: fjust like we do currently.SUPERCILEX commentedon Apr 11, 2023
Slightly more complicated test case for the eventual PR:
edgarogh commentedon Aug 22, 2023
I'm not sure this was clearly mentioned in this thread, but in the mean time, in situations involving strings and regexes, you can use a show rule with a function and reuse the
itparameter in the output. That won't cause recursion.That doesn't work
That works
MultisampledNight commentedon Nov 16, 2023
Fwiw another case where it's not directly visible that the content is reproduced causing the stack overflow would be this:
XenHum commentedon Nov 19, 2023
I see fixing this issue is on the roadmap - is there any timeline for the fix? I'm asking because my company won't move from Latex to typst until this is resolved. In any case, thanks for your work!
4 remaining items
Andrew15-5 commentedon Nov 26, 2023
I failed to make an alias
show "cmd": it => raw(it)becauseitis content instead of string. Addingrepr/stralso doesn't work. Does anyone have a workaround for this?MultisampledNight commentedon Nov 26, 2023
@Andrew15-5 The mark you wanted can be implemented without any changes to typst through a zero-width no-break space. Hacky, causing double-raws like below if
cmdis already in a codeblock and possibly detrimental to accessibility, but it works for me.EDIT: Updated to use the non-breaking zero-width space
U+FEFFinstead of the breaking one (U+200B).Andrew15-5 commentedon Nov 26, 2023
Very hacky indeed. Thank you. I already forgot about this character. I found out about it a year or two ago. I think that's a great universal workaround. Okular was able to find both
cmd, so the ZWSP char does not affect word search.T0mstone commentedon Nov 26, 2023
We already do that, but it doesn't work anyway because, to use laurmaedje's example, with
show "hey": box[hey], you get"hey" (unmarked) --> box("hey" (unmarked)) (marked) --> box(box("hey" (unmarked)) (marked)) (marked) -> etc.Andrew15-5 commentedon Nov 26, 2023
A related problem about "optimization". Is there a way to make this work?
It looks much nicer than the repetitive (working) way:
And the benefit increases as the number of show rules grows.
Solution
P.S.
shelluses the invisible char insertion hack + adds raw shellscript wrapper.Andrew15-5 commentedon Nov 26, 2023
@MultisampledNight, note that BOM char is deprecated for this use case: https://en.wikipedia.org/wiki/Word_joiner. So you better use
"\u{2060}"WJ. And you forgot to rename the function toinsert-zwnbsp(but for WJ —insert-wj).MultisampledNight commentedon Nov 26, 2023
@Andrew15-5 the problem I see with the word joiner is that potentially emojis are formed with that. Maybe that's the case with U+FEFF as well, but idk.
Anyway, maybe you want to check out the Discord server for quicker responses to your specific problem.
Andrew15-5 commentedon Nov 26, 2023
I agree, as this was one of my first thoughts when I read the article. But since we are (I'm assuming) specifically talking about text substitution show rules, it is safe to say that this is impossible to happen. After all, the main point is to simplify the source file and potentially remove all the emojis and stuff (it's especially noticeable in Neovim, since not all of them are shown as a single cluster) with another pack of show rules (
show "green-heart": "💚").[-]"show"-recursion crashes compiler[/-][+]Support for show rules that match on their own output[/+]laurmaedje commentedon Dec 4, 2023
With the latest changes, this doesn't crash anymore, instead giving a proper error message. Hence, I'm converting this from a bug to a feature request.
laurmaedje commentedon Feb 2, 2024
I have opened #3327 to fix this for text show rules. For other show rules, I think the current error is fine and should remain. I've explained my reasoning for that in the linked PR. The issue will be closed once the PR is merged.