Skip to content

A blank line between two set text(lang: ..) rules changes the PDF's language #9050

Open

Description

@TylerVigario

Description

A blank line between two set text(lang: ..) rules changes the language the PDF declares, though the page is the same.

#set text(lang: "de")
#set text(lang: "en")
"Hallo"

declares en. With a blank line between the two rules, it declares de, though every word is still set in English: the quotes are “Hallo”, where German's are „Hallo“.

It turns up when a document sets its language at its start, as the accessibility guide says to, and then applies a template that sets one too:

#let template(body) = { set text(lang: "en"); body }
#set text(lang: "de")
#show: template
"Hallo"

declares en, and de with a blank line before #show.

Compiled, as adjacent.typ and template.typ, and apart.typ and template-apart.typ with the blank line:

for f in adjacent apart template template-apart; do
  typst compile $f.typ
  printf '%-15s %s  %s  %s\n' $f "$(pdftotext $f.pdf - | grep Hallo)" \
    "$(tr -d '\n' < $f.pdf | grep -a -o '/Type */Catalog[^>]*/Lang *([a-z]*)' | grep -o '/Lang.*')" \
    "$(grep -a -o '<dc:language><rdf:Bag><rdf:li>[a-z]*' $f.pdf | sed 's/.*<rdf:li>/dc:language /')"
done
adjacent        “Hallo”  /Lang(en)  dc:language en
apart           “Hallo”  /Lang(de)  dc:language de
template        “Hallo”  /Lang(en)  dc:language en
template-apart  “Hallo”  /Lang(de)  dc:language de

typst 0.14.0 prints the same.

What I can see of the cause: eval_markup styles the rest of the markup with each set rule, and styled_with_map merges the styles into the rest when the rest is a single StyledElem. Adjacent, the two rules become one style list, and the locale is read from that list, where the later rule wins:

seq.push(eval_markup(vm, exprs)?.styled_with_map(styles))

pub fn styled_with_map(mut self, styles: Styles) -> Self {
if styles.is_empty() {
return self;
}
if let Some(style_elem) = self.to_packed_mut::<StyledElem>() {
style_elem.styles.apply(styles);
self
} else {
StyledElem::new(self, styles).into()
}
}

} else if elem == TextElem::ELEM {
// Infer the document locale from the first toplevel set rule.
if let RealizationKind::Document { info } = &mut s.kind {
info.populate_locale(StyleChain::new(&local));
}

With a blank line, the rest starts with the paragraph break, so each rule keeps its own StyledElem and the first one's language is taken. On main, visit_styled passes populate_locale the local styles chained with the outer ones, and the merged list is still among them. I haven't built main.

Operating system

Linux

Typst version

  • I am using the latest version of Typst (0.15.1)

Activity

  1. added
    styling
    syntax
    on Nov 13, 2026
  2. ZYZ666-RGB commented on Nov 18, 2026

    @ZYZ666-RGB

    I'll start work on this now.

    The later set text(lang: ..) should win regardless of a blank line between the rules, including when a template applies a second language after the document sets one. I'll follow the analysis in the issue (eval_markup / styled_with_map merging), make the PDF /Lang and dc:language consistent with the typographic language, and add tests for the adjacent, blank-line, and template cases.

  3. laurmaedje commented on Nov 18, 2026

    @laurmaedje
    Member

    @ZYZ666-RGB Please keep in mind that we do not accept AI-generated contributions.

  4. ZYZ666-RGB commented on Nov 20, 2026

    @ZYZ666-RGB

    Thanks for the reminder. I won't open a PR on this issue.

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

    stylingsyntax

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions