Repository navigation
Conversation
|
Without actually reviewing the code, was the edge case of a block equation being in the middle of a paragraph handled? A block equation might end a paragraph, but the text block following a block equation might also be a part of the same paragraph. Does the single proposed new field allow differentiating between these 2 cases, or is this simply handled by the usual empty lines in markup mode? What if such a paragraph is created in code mode? A block equation starting a paragraph is never appropriate. That case does not need to be considered as a valid structure. |
I wouldn't make such categorical statements. There's a lot of diversity out there. Edit: A simple example would be |
|
Here's a summary of the behaviour, using its hypothetical HTML equivalent to make things clear. Note that having an equation on its own in a paragraph is not covered by this. It's just a single block then.
As for creating paragraphs in code, I think it should work the same as whatever works currently. (I need to check, as I'm not entirely sure how the paragraph grouping works in code mode...) |
In most cases that may be true, but I think it’s important to leave the choice to the user. |
But you then go on to re-read your notes and fix your language, especially if you intend to send them for somebody else to read, right? I would not expect that personal quick notes necessarily conform to good writing practices, so what Typst does to the tag structure there does not really matter. But if I was your supervisor or a reviewer at a journal and you handed me your thesis or an article with an equation starting a paragraph, it would be something you would have to fix in the next revision. A better argument for allowing an equation to start a paragraph would be that the accessible PDF standards allow it (do they?), and therefore we should not apply additional restrictions. |
Yeah, this is semantically the correct thing to do. A paragraph by definition implies prose, possibly combined with other content. It would be misleading for screen readers if something that is not written discourse (which an equation by itself cannot be) would be marked as such. Actually, because of the last point, even 2 equations side by side should really not generate a paragraph in my humble opinion. They would generate 2 separate block equations. Unless of course there is a "proper paragraph" attached to them. |
@SeSodesa I agree that in most cases it doesn’t make sense to start a paragraph with an equation. However, that doesn’t mean this should be enforced by the typesetting system itself. Otherwise, we could just as well argue that integral signs should be disallowed in inline math because they’re often considered poor style. Correct me if I'm wrong, but this seems to be not merely a question of “good” style, but also one about (future) functionality. A typesetting system should not hard-code a specific editorial philosophy or writing convention. Moreover, as @Enivex pointed out, there are legitimate cases where an equation can start a paragraph. Style requirements can still be enforced by supervisors or journals where needed. But the tool itself shouldn’t artificially prohibit structurally valid constructs. I actually encountered this issue while experimenting with the interaction between text and display equations. In LaTeX, display equations are placed tightly relative to surrounding text when no blank lines are present: I imagine that the variable under discussion could be used to implement a similar spacing mechanism in Typst, making the spacing more responsive to the semantics of the surrounding content. If a paragraph begins with a display equation and is followed directly by text (without a blank line), no additional paragraph spacing ( |
|
I just did some further experimenting and it seems my previous comment oversimplifies the exact mechanism in LaTeX. There is some other small space inserted in some cases, but to be honest I don't understand the exact rules applied here. Here is the experiment: \documentclass[12pt]{article}
\begin{document}
\pagestyle{empty}
\setlength{\parindent}{0pt}
\setlength{\parskip}{0pt plus 0pt minus 0pt}
\setlength{\abovedisplayskip}{0pt plus 0pt minus 0pt}
\setlength{\abovedisplayshortskip}{0pt plus 0pt minus 0pt}
\setlength{\belowdisplayskip}{0pt plus 0pt minus 0pt}
\setlength{\belowdisplayshortskip}{0pt plus 0pt minus 0pt}
\setlength{\topsep}{0pt plus 0pt minus 0pt}
\setlength{\partopsep}{0pt plus 0pt minus 0pt}
\setlength{\baselineskip}{0pt plus 0pt minus 0pt}
% ---------------------------------
\setlength{\parskip}{20pt plus 0pt minus 0pt}
A long long long long long long long long long line
\[
E=mc^2
\]
\vspace{1em}
\textbf{is (as expected) \emph{not} the same as}
\vspace{1em}
A long long long long long long long long long line
\[
E=mc^2
\]
\setlength{\parskip}{0pt plus 0pt minus 0pt}
\vspace{4em}
-----------------------------------
\vspace{4em}
\setlength{\baselineskip}{12pt plus 0pt minus 0pt}
A long long long long long long long long long line
\[
E=mc^2
\]
\vspace{1em}
\textbf{is also \emph{not exactly} the same as}
\vspace{1em}
A long long long long long long long long long line
\[
E=mc^2
\]
\vspace{4em}
-----------------------------------
\vspace{4em}
\setlength{\baselineskip}{11pt plus 0pt minus 0pt}
A long long long long long long long long long line
\[
E=mc^2
\]
\vspace{1em}
\textbf{now it \emph{is} the same as}
\vspace{1em}
A long long long long long long long long long line
\[
E=mc^2
\]
\end{document}In any case, the general idea still holds: the spacing around display equations in LaTeX depends on the surrounding structure (paragraph boundaries, line lengths, spacing, etc.), which allows the layout to adapt to context. A similar mechanism in Typst - where the spacing of a display equation depends on whether it appears within a paragraph, at a paragraph boundary, or as a standalone block - could make equation spacing more semantically appropriate. Additionally, if the preceding line is short relative to the width of the equation, the spacing above the display could be reduced further, similar to how TeX uses |
There was a problem hiding this comment.
Whether this is actually a bug now though is unclear (at least to me). The nested tight list is now part of a single paragraph along with the rest of the list item's body. So should paragraph leading be used or not (i.e. the set list spacing) before the first list item?
| Item::Block(elem, styles) => { | ||
| // We need owned values as the `ParChild::Block` created | ||
| // will outlive inline layout. | ||
| block_elem = Some((*elem).clone()); | ||
| block_styles = Some(styles.to_map()); | ||
| block_idx = Some(idx); | ||
| } |
There was a problem hiding this comment.
Will StyleChain::to_map() have a big performance impact?
We need to get an owned version of the StyleChain onto the ParChild::Block enum variant
There was a problem hiding this comment.
It's not that big of a deal. Potentially though, it could use .suffix and chain that map with the shared paragraph-wide styles. Similarly to what's done in fn repack in typst-realize. That'd be a little more efficient and also conceptually cleaner.
| // TODO: collect frames for widow/orphan prevention calculation. Should | ||
| // frames only (not blocks) participate in widow/orphan prevention? | ||
| // Regardless, the code here is a little weird. | ||
| let frames: Vec<_> = children | ||
| .iter() | ||
| .filter_map(|c| match c { | ||
| ParChild::Frame(f) => Some(f), | ||
| ParChild::Block { .. } => None, | ||
| }) | ||
| .collect(); |
There was a problem hiding this comment.
This is very wrong currently (but does appear to be benign).
|
Most of the changes regarding PDF tags are fine. The paragraphs missing from some list bodies aren't an issue. What I'd consider a regression is the nesting of lists inside the list body ( This only seems to happen if there are list items that are tightly spaced and then a widely spaced item after that: - a
- Indented
- b // Parbreak forces the list to be wide.Here's the debug output of my main=== frames ===
[crates/typst-pdf/src/tags/tree/build.rs:201:9] &page.frame = Frame [
Start("item", Location(17212)),
Start("list", Location(37669)),
Start("pdf-marker-tag", Location(59320)),
Text("•"),
End(Location(59320)),
Group Frame [
Start("pdf-marker-tag", Location(58334)),
Start("par", Location(29122)),
Text("a"),
End(Location(29122)),
Start("item", Location(54946)),
Start("list", Location(63932)),
Group Frame [
Start("pdf-marker-tag", Location(20721)),
Text("‣"),
End(Location(20721)),
Start("pdf-marker-tag", Location(32160)),
Text("Indented"),
End(Location(32160)),
],
End(Location(63932)),
End(Location(54946)),
End(Location(58334)),
],
Group Frame [
Start("pdf-marker-tag", Location(3627)),
Text("•"),
End(Location(3627)),
Start("pdf-marker-tag", Location(63598)),
Start("par", Location(27817)),
Text("b"),
End(Location(27817)),
End(Location(63598)),
],
End(Location(37669)),
End(Location(17212)),
Start("item", Location(51167)),
End(Location(51167)),
]
=== tags ===
START 0 item
START 1 list
START 2 pdf-marker-tag ListItemLabel
text
END 2 pdf-marker-tag
> group
START 3 pdf-marker-tag ListItemBody
START 4 par
text
END 4 par
START 5 item
START 6 list
> group
START 7 pdf-marker-tag ListItemLabel
text
END 7 pdf-marker-tag
START 8 pdf-marker-tag ListItemBody
text
END 8 pdf-marker-tag
< group
END 6 list
END 5 item
END 3 pdf-marker-tag
< group
> group
START 9 pdf-marker-tag ListItemLabel
text
END 9 pdf-marker-tag
START 10 pdf-marker-tag ListItemBody
START 11 par
text
END 11 par
END 10 pdf-marker-tag
< group
END 1 list
END 0 item
START 12 item
END 12 item
=== tree ===
group Root Id::<Group>(0)
group List Id::<Group>(1)
group Standard Id::<Group>(11)
group ListItemLabel Id::<Group>(2)
leaf
group ListItemBody Id::<Group>(3)
group Par Id::<Group>(4) (weak)
leaf
group List Id::<Group>(5)
group Standard Id::<Group>(12)
group ListItemLabel Id::<Group>(6)
leaf
group ListItemBody Id::<Group>(7)
leaf
group Standard Id::<Group>(13)
group ListItemLabel Id::<Group>(8)
leaf
group ListItemBody Id::<Group>(9)
group Par Id::<Group>(10) (weak)
leafPR=== frames ===
[crates/typst-pdf/src/tags/tree/build.rs:201:9] &page.frame = Frame [
Start("item", Location(16966)),
Start("list", Location(27103)),
Start("pdf-marker-tag", Location(24678)),
Text("•"),
End(Location(24678)),
Group Frame [
Start("pdf-marker-tag", Location(40426)),
Start("par", Location(45515)),
Text("a"),
Start("item", Location(39568)),
Start("list", Location(13436)),
Group Frame [
Start("pdf-marker-tag", Location(21467)),
Text("‣"),
End(Location(21467)),
Start("pdf-marker-tag", Location(11517)),
Text("Indented"),
End(Location(11517)),
End(Location(13436)),
End(Location(39568)),
],
End(Location(45515)),
End(Location(40426)),
],
Group Frame [
Start("pdf-marker-tag", Location(19885)),
Text("•"),
End(Location(19885)),
Start("pdf-marker-tag", Location(16079)),
Start("par", Location(26183)),
Text("b"),
End(Location(26183)),
End(Location(16079)),
],
End(Location(27103)),
End(Location(16966)),
Start("item", Location(4100)),
End(Location(4100)),
]
=== tags ===
START 0 item
START 1 list
START 2 pdf-marker-tag ListItemLabel
text
END 2 pdf-marker-tag
> group
START 3 pdf-marker-tag ListItemBody
START 4 par
text
START 5 item
START 6 list
> group
START 7 pdf-marker-tag ListItemLabel
text
END 7 pdf-marker-tag
START 8 pdf-marker-tag ListItemBody
text
END 8 pdf-marker-tag
END 6 list
END 5 item
< group
END 4 par
END 3 pdf-marker-tag
< group
> group
START 9 pdf-marker-tag ListItemLabel
text
END 9 pdf-marker-tag
START 10 pdf-marker-tag ListItemBody
START 11 par
text
END 11 par
END 10 pdf-marker-tag
< group
END 1 list
END 0 item
START 12 item
END 12 item
=== tree ===
group Root Id::<Group>(0)
group List Id::<Group>(1)
group Standard Id::<Group>(11)
group ListItemLabel Id::<Group>(2)
leaf
group ListItemBody Id::<Group>(3)
group Par Id::<Group>(4) (weak)
leaf
group List Id::<Group>(5)
group Standard Id::<Group>(12)
group ListItemLabel Id::<Group>(6)
leaf
group ListItemBody Id::<Group>(7)
leaf
group Standard Id::<Group>(13)
group ListItemLabel Id::<Group>(8)
leaf
group ListItemBody Id::<Group>(9)
group Par Id::<Group>(10) (weak)
leaf |
|
Another issue to mention: lists aren't valid inside paragraph tags in HTML... |
|
What are the consequences of invalid tag structure in this case? Do browsers just not render the document? Do they force a paragraph break at the list start boundary as a means of recovery? Although I guess none of this matters if we aim to be standards-compliant as much as possible. Maybe we should be the ones to just emit a paragraph end tag before lists in the case of HTML. |
laurmaedje
left a comment
There was a problem hiding this comment.
I've pondered and discussed this with @reknih some more and I think I'm overall happy with the inlinable blocks concept. I would still keep it internal for now, but I think I can see it being exposed, too. My thoughts on the open design questions and the existing implementation are below.
These thoughts built up over a few hours each yesterday and today, so they are fairly fresh and not necessarily to be considered final. Consider them more an invitation for further discussion.
Design
Mixed flows (re. spacing and PDF tagging for tight lists)
On the regression of issue-6242-tight-list-attach-spacing: I think it is closely related to the issue with PDF tagging that @saecki raised. We could try to work around both issues, but the root cause is that the A text, and the B and C list items form a paragraph in:
- A
- B
- C
- CI think that's semantically wrong. The semantics are also wrong on main, where just "A" is a paragraph. I think really there ought to be no paragraph here.
To fix this, I think we need to take the work I started in #5746 even further. This PR added support for non-par inline layout. What we need here is non-par mixed layout, where the inline parts of it go through non-par inline layout and the rest uses normal flow layout.
This non-par mixed layout would really be a refactored extended version of the existing non-par inline layout and would be triggered by fully inline or inlinable content in containers (dropping the change to is_fully_inline_or_neutral in the process). Proper block-level content (non-inlinable) would still force adjacent inline content into a paragraph.
Subtly, but importantly, I think the block parts of such a mixed layout would be treated as normal blocks in this flow and not as inlined blocks in a non-par inline layout. As a consequence they would get normal block treatment and respect block.spacing (unlike inlined blocks, which currently don't respect this property and I believe rightly so since paragraphs use leading).
In this setup, perhaps we could use normal block.above spacing instead of the special v attach spacing for tight lists? The existing special attach v spacing was primarily needed due to special interactions with parbreaks which would be superseded by the inlinable blocks work.
Two tight lists vs one wide list
On the issue brought up about this being one wide list instead of two paragraphs, each with a tight list:
Blah paragraph:
- A
- C
New paragraph?With this PR it gets a little worse, but the issue already exists on main (#3062) with cases like this:
- A
- B
- A
- BYou would expect this to be two tight lists, but it's one wide list. I could potentially see adjusting the rules so that it only becomes a wide list if there are parbreaks between all items and around the full list1. That should fix both situations. (I think?)
I also recall discussing this with @isuffix, though I don't recall whether we reached the same conclusion. One thing to note is that this kind of change is definitely a candidate for very silent breakage... so, if we do this, perhaps also a candidate for upcoming migration tooling? Overall, I would say, it's probably best to deal with this issue separately from this PR. I don't think the PR worsens the situation by a lot as I imagine paragraphs starting with a list, following a paragraph ending with one, to be rather rare.
Paragraphs only consisting of inlinable blocks
My gut feeling is that it makes more sense to only create a paragraph if there is something properly inline in play. Multiple inlinable blocks following each other feel like they should remain normal blocks to me. If you really want them in a paragraph, you could wrap them in a #par(..) call.
Generally, I would probably make the inlinable property all about grouping. All it does would be to define that the block can group into a paragraph if adjacent to inline-level material. The paragraph itself would generally gain the ability to hold blocks, i.e. if I explicitly put a block into a #par(..) call, it becomes irrelevant whether it's inlinable. It's just in there. (Whereas today, it warns with "block may not occur inside of a paragraph and was ignored".) Potentially, the paragraph could also gain the ability to hold further things like place in the future. (This would help with #3967.)
Lists in paragraphs in HTML
As mentioned above, the content model of <p> forbids <ul> and <ol> elements. I dug a little deeper into why this is the case. The WhatWG mailing list has an interesting discussion from 2011 about this.
Ian Hickson (who seems to have done a lot of work on HTML5) doesn't really think the construct makes sense at all. While I disagree, at least he proposes a DOM structure to express the situation, which I think we should adopt. The proposed structure of a paragraph containing inlinable blocks would simply be a <div> containing multiple <p>s with the block content in between. I would propose adding a class (or maybe a role?) to that <div>.
Alternatives I see include (1) not writing anything extra or (2) writing a class on the "continuation" paragraphs. Non-alternatives include just ignoring the spec. Even if we wanted to do that (which we don't), it wouldn't work because the HTML parser will auto-close <p> tags when it sees a list, so the DOM would just be messed up.
Implementation
I think the implementation is a good start, but I would propose one major change and a few smaller ones. The outcomes of the design thoughts above might of course further affect implementation (in particular, the mixed flow stuff).
Inline layout
Currently, blocks are dragged all the way through inline layout. This necessitates subtle changes to inline collection, line layout, line breaking, finalization, and tag handling.
I would propose an alternative direction. Instead of treating blocks as opaque items in inline layout that are just extracted again in the end, we could already perform the splitting during collection and collect multiple sub-inline-layouts that are individually prepared, linebreaked, and finalized.
A few comments I had written up on the implementation that would be superseded by this high-level proposal.
-
The code currently crashes on
Consider~$ x + y $because icu4x does not emit a breakpoint due to the nbsp. -
The linebreaking change could use a comment. Also, can we use
\nbefore it instead? We already use it after it? -
The code in
fn commitlooks a bit unclean... it's purpose is to get the tags and the lone block out of theLinestruct. And the sorting is also an artifact of having BiDi reordering happen which is kind of pointless in the first place.
Another thing is that end tags are currently in the wrong position (at (0,0) in the frame).2 Also, end tags for breakable blocks are pushed into the first frame, when they should be put after the last frame (if spilled). Both of these could likely be solved by emitting tags as separate ParChild::Tag around the block and turning these into Child::Tag in the flow. I didn't test whether there are any issues with this, though, so take this with a grain of salt.
On the widow/orphan prevention: Probably, blocks should also participate in it? (I mean, ideally on a line-by-line basis for equations, but I'd consider that out of scope.) The code duplication for widow/orphan prevention would also be good to avoid.
Realization
I think it'd be nice to have a bit less special casing and instead introduce a new grouping effect. We could call it Tentative, but maybe there is also a semantically clearer name. The semantics would be that the tentative elements would be finished with the group if the group contains at least one trigger element, but if the group only ends up with tentative and inner elements, it is dissolved without finishing (the decision on "Paragraphs only consisting of inlinable blocks" is relevant here). We'd need to make sure to drop inner elements between tentative ones if there ends up being no trigger element or else we could end up with SpaceElems in flow layout.
The style handling code in finish_interrupted feels like a bit of hack, but I suppose it would be okay. The interruption mechanism is already a bit ad-hoc and special cases AlignElem, so this extra special case does not change things a lot. But perhaps, it should be made a bit more targeted. I think it's okay to ignore both par and align styles, but I would make the exception specific to par grouping.
Testing
One thing that I think would be good to test is the within selector on par and list for an inlined list.
Docs
We'll need to update the relevant explanations in the docs and probably adjust various docs examples (e.g. that for block.spacing)
Footnotes
| regions.expand.y &= self.alone; | ||
| // For inline blocks, use the line width as the width constraint. | ||
| if let Some((line_width, _, _)) = self.inline { | ||
| regions.size.x = line_width; |
There was a problem hiding this comment.
This seems to affect no test. What's the idea here?
| let align = styles.resolve(AlignElem::alignment); | ||
| let locator = self.locator.next(&elem.span()); | ||
|
|
||
| if breakable { |
There was a problem hiding this comment.
This does not have the fr.is_some() check that the normal block branch has. Is there a reason for this?
Closes #3206.
This PR adds a new internal field
inlinabletoblock(), which allows blocks that are next to paragraph-level content (and other inlinable blocks) to become part of the paragraph. Block equations and tight lists/enums now have this property.The field is being kept internal for now as the design is still undecided and subject to change (if it is to be exposed at all).
The implementation is almost complete and is working, but there are some things that need discussion still - to be updated soon™.
More details
inlinablefield is internal.inlinableblock together with paragraph-level content joins the paragraph.inlinableblock stays block-level, whereas multipleinlinableblocks together become a paragraph.tightListElems andEnumElems now constructBlockElems withinlinable: true.blockEquationElemconstructs aBlockElemwithinlinable: true.Todos
issue-6242-tight-list-attach-spacingtest regressionneedcalculation intypst_layout::flow::distribute::Collector::par_children