Skip to content

Wrong leading formatting in Paragraph #159

Closed

Description

@vaavaav

E.g., #par(leading: 20.4pt)[#lorem(20)] is producing
image

But it should approximately produce this:
image

My guess is that something is not scaled properly, or the second line needs to subtract the baseline size to the distance of the previous line.

Activity

  1. laurmaedje commented on Mar 27, 2023

    @laurmaedje
    Member

    Leading doesn't work that way in Typst. It's the space between the line edges and not between the baselines. Just like the amount of lead you would put between the lines. I'm not saying this is necessarily the best way to do it or the end of the story for Typst, just that it's that way right now.

  2. vaavaav commented on Mar 27, 2023

    @vaavaav
    Author

    Hmmm... But do you know how to obtain Typst lead from Latex lead? (From the leading package)

    This is important to me because I'm recreating my college's template for master dissertation and they have specific leading and i would also need 1.5 spacing...

    In latex i would achieve this with \leading{20.4pt} (but it produces different results on Typst).

    For the 1.5 spacing i can do this with \linespread{1.5}\selectfont

  3. Enivex commented on Mar 27, 2023

    @Enivex
    Collaborator

    I think the simplest solution would be to typeset the same text in whatever tool your numbers are from, and then play around with leading in typst (ideally in em and not pt so it's correct if you change font size) until it's close enough to your liking.

  4. reknih commented on Apr 4, 2023

    @reknih
    Member

    I used the measure tool in Adobe Acrobat to create our templates!

  5. added
    layout
    on Apr 4, 2023
  6. Andrew15-5 commented on Jun 18, 2023

    @Andrew15-5
    Contributor

    I used the measure tool in Adobe Acrobat to create our templates!

    But why do we have to suffer by using this "not [...] necessarily the best way", when there are at least 2 issues (#159, #1221) that are saying this way is not the best and there is a better way. (And I'm not including my issue, because I didn't open it and instead writing here.)

    I was so frustrated that I "must" have a 1.5 line spacing, but when I used \fontsize{14}{21} the person who was validating the document said that it's nothing like 1.5 line spacing. The root of the problem lies in the proprietary software called Word by Microsoft. With digital ruler (flameshot and okular without text antialiasing) I found out that "1.5" in Word is actually ~1.92. Which is basically 2.0 instead of 1.5! I was so shocked and angry.

    I found out that font height is basically the vertically longest glyph in the font — most of the times () are probably the tallest glyphs. And so the line spacing=1 is when the () from one line barely touches () from an adjacent line. I just checked — space between 2 adjacent baselines is roughly the same as space from the lowest point of () to the lowest point of the next ().

    When I used \fontsize{14}{21} (and similar) in reality the leading was in the range of 1.55–1.65 which can be considered "1.5 line spacing" as 1, 1.5 and 2 can be defined as groups in which each exact line spacing fall. In other words, leading 1.4–1.6 falls in "1.5" group, 1.9–2.1 — "2" group etc. This means that I was actually right and the aforementioned validator was wrong. If only proprietary software (Google Docs & Microsoft Office) was explicit about what this "1.5" actually means! Or just simply stick to the truth and use an actual "1.5" instead of "1.92".

    Anyway, setting block(spacing) or par(leading) to 0pt leads to this:

    image

    Parenthesis are overlapped, but the rest of the glyphs are not. So, when leading is 0em, then the text lines could collide into each other — which is bad (this should never happen). Wait, but leading is space between baselines. In Typst case, it is the space between "the baseline of the top line" and "not the highest point of the bottom line" (kind of in the middle). Which only works for limited amount of glyphs (again, not including () and probably some other glyphs). And the "only works for the limited amount of glyphs" is the main problem. If Typst were to use "the better way", then it would work for any glyphs in the selected font. And the fact that leading in par() (and block(spacing)) is actually (leading – 1 baseline) makes it more confusing.

    I'm practically begging to differ, i.e., change the definition of leading that Typst is following. For example, when I #set par(leading: 1.5em) it would be the same as \fontsize{14}{21}, but more precise (as I mentioned this macro makes leading about 1.55–1.65 instead of 1.5, but you can make it exactly 1.5 with different macros). And when, for example, I #set block(spacing: 1em) it would show parenthesis on adjacent lines barely touching each other.

    This would make Typst more compatible with LaTeX and will remove all the "measuring" to get the ideal leading. When requirement says "set leading to 1.5" or "2", then Typst will actually do exactly that and everyone will be happy.

    P.S. I didn't test leading in LibreOffice (and other FOSS). Sorry about switching between "line spacing" and "leading" a lot: when I was using "line spacing" I was referring to either quote from document requirements or just as a "widespread term" which is used in many software programs, and when I was using "leading" I was being more "correct" as leading is the original and more precise term.

  7. Enivex commented on Jun 18, 2023

    @Enivex
    Collaborator

    saying this way is not the best and there is a better way.

    They're just two different competing definitions of leading. The one typst uses is unarguably the oldest, original definition. Leading refers to the lead strips put between consecutive lines in manual typesetting, i.e. spacing between lines. Unfortunately with the advent of computer typesetting, some tools ended up using a different definition.

  8. Andrew15-5 commented on Jun 18, 2023

    @Andrew15-5
    Contributor

    But what about tall glyphs? If 0em means 0 space between glyphs (in practice this is what it looks like), then what about parenthesis and other overlapping glyphs such as p and h?

    image

    Is it really OK to leave this with no changes?

    And LaTeX compatibility — why break the compatibility so bad? There is a guide for LaTeX users and a special section with settings that mimics the "LaTeX look":

    ...
    #set par(leading: 0.55em, ...)
    ...

    This maybe will mimic the default LaTeX look, but you can't mimic the \fontsize{10}{15} with #set par(leading: 15pt/1.5em) or #set par(leading: 5pt/0.5em) (including/excluding 1 baselineskip). Everyone just defaults to using measuring tools to get leading just right.

    Typst is still in beta, and breaking changes are a normal thing at this stage. I think before stable release (i.e., in the near future) is the perfect time to switch the definition of leading to more understandable and widespread (and it's not a wrong definition too).

  9. Enivex commented on Jun 18, 2023

    @Enivex
    Collaborator

    But what about tall glyphs? If 0em means 0 space between glyphs (in practice this is what it looks like), then what about parenthesis and other overlapping glyphs such as p and h?

    image

    Is it really OK to leave this with no changes?

    This has nothing to do with the definition of leading. It's the top-edge and bottom-edge options of https://typst.app/docs/reference/text/text/

    And LaTeX compatibility — why break the compatibility so bad?

    Typst does not claim to be compatible with LaTeX.

    Why did LaTeX break compatibility with physical typesetting so bad?

    There is a guide for LaTeX users and a special section with settings that mimics the "LaTeX look":

    ...
    #set par(leading: 0.55em, ...)
    ...

    This maybe will mimic the default LaTeX look, but you can't mimic the \fontsize{10}{15} with #set par(leading: 15pt/1.5em) or #set par(leading: 5pt/0.5em) (including/excluding 1 baselineskip). Everyone just defaults to using measuring tools to get leading just right.

    Only because you're starting with measurements using one definition, and want to apply them directly. You would have the same issue going the other way.

    Typst is still in beta, and breaking changes are a normal thing at this stage. I think before stable release (i.e., in the near future) is the perfect time to switch the definition of leading to more understandable and widespread (and it's not a wrong definition too).

    That is subjective. I find the definition that typst uses more understandable. It's also the original definition.

  10. Andrew15-5 commented on Jun 18, 2023

    @Andrew15-5
    Contributor

    Only because you're starting with measurements using one definition, and want to apply them directly. You would have the same issue going the other way.

    Well, yes, but I was speaking like I'm a LaTeX user who wants to "directly" port his leading settings to Typst (which isn't so far from the truth), and it wouldn't work because of the reason you said. I of course understand that leading settings in these programs are based on the different truths definitions.

  11. Andrew15-5 commented on Jun 27, 2023

    @Andrew15-5
    Contributor

    I checked and for me 0.25em "Typst leading" is "close enough" for x1 baseline a.k.a. 1.0 line spacing (sometimes also called leading) (tested for 10pt and 30pt, also for different fonts). This means that 0.75em is approximately 1.5 line spacing and, therefore, 1.25em is ~2.0 line spacing (well, more like 2.1).

    Basically:

    #let leading = 1.5em // Your line spacing (1, 1.5, 2, etc.)
    #let leading = leading - 0.75em // "Normalization"
    // #set block(spacing: leading) // This is for an old version of Typst (<=0.11.1).
    #set par(leading: leading, spacing: leading)

    With leading: 0.25em parentheses do overlap a little (in some fonts more, in others less). For 0.75em and 1.25em the error only increases (~1.56 instead of 1.5, ~2.1 instead of 2.0).

    If someone need more precise line spacing, then good luck with ruler measurements and pixel peeping.

  12. balthild commented on Oct 16, 2023

    @balthild

    Just want to mention: after setting par.leading to zero, the distance between baselines actually equals to text.top-edge. You could experiment it to see this behavior:

    #set text(size: 64pt, font: ("Linux Libertine", "Noto Serif CJK SC"))
    
    #set par(leading: 0pt)
    Text1\ Text1\ 中文
    
    #set text(top-edge: 32pt)
    Text2\ Text2\ 中文
    
    #set text(top-edge: 0cm)
    Text3\ Text3\ 中文
    
    #set par(leading: 1em)
    Text4\ Text4\ 中文

    image

    If you want to get the behavior like LaTeX, you could do set text(top-edge: 0cm). However, this may lead to unexpected results as some default styles rely on something similar to the default par.leading behavior.

    I recommend setting the text.top-edge to a certain em length that matches your font, and then you could calculate a proper par.leading that results in a certain distance of baselines. For example, I have set text(top-edge: 0.85em) because 0.85em could just matches the top edge of the CJK characters in my font; then, I have set par(leading: 0.9em), and the actual distance of baselines will be 0.85em + 0.9em = 1.75em.

    Update

    I did some hack and found that similar spacing behavior could be achieved better with measure. However, it is only suitable for non-CJK documents.

    image

    // Assume that the font's cap-height are consistent
    // over different font sizes
    #let leading(line-height) = {
      let cap-height = measure("X").height
      let cap-em = cap-height / 1em.to-absolute()
      line-height - cap-em * 1em
    }
    
    #let paper(body) = {
      set text(
        lang: "en",
        region: "US",
        font: "TeX Gyre Termes",
        size: rem,
      )
    
      context {
        let lineskip = leading(1.75em)
      
        set par(
          justify: true,
          leading: lineskip,
          spacing: lineskip,
          first-line-indent: 2em,
        )
      
        body
      }
    }
    #show: paper
    
    #context leading(1em)
    
    #let lh = 1.75em
    #stack(dir: ltr,
      stack(dir: ttb,
        rect(height: lh, fill: aqua),
        rect(height: lh, fill: green),
        rect(height: lh, fill: red),
        rect(height: lh, fill: aqua),
        rect(height: lh, fill: green),
        rect(height: lh, fill: red),
      ),
      stack(dir: ttb,
        context rect(height: leading(lh), fill: red),
        lorem(100),
      ),
    )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    layout

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions